Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Add Ember Suave and fix all errors
Browse files Browse the repository at this point in the history
  • Loading branch information
martndemus committed Aug 12, 2016
1 parent 4f9d8a4 commit 922304d
Show file tree
Hide file tree
Showing 39 changed files with 720 additions and 745 deletions.
3 changes: 3 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "ember-suave"
}
14 changes: 9 additions & 5 deletions addon/errors.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Ember from 'ember';

var get = Ember.get;
var set = Ember.set;
const {
A: emberArray,
Object: EmberObject,
get,
set
} = Ember;

export default Ember.Object.extend({
unknownProperty: function(property) {
set(this, property, Ember.A());
export default EmberObject.extend({
unknownProperty(property) {
set(this, property, emberArray());
return get(this, property);
}
});
2 changes: 1 addition & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import Mixin from 'ember-validations/mixin';

export default Mixin;
export function validator(callback) {
return { callback: callback };
return { callback };
}
59 changes: 32 additions & 27 deletions addon/messages.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import Ember from 'ember';

const { I18n } = Ember;

export default {
render: function(attribute, context) {
if (Ember.I18n) {
return Ember.I18n.t('errors.' + attribute, context);
render(attribute, context) {
if (I18n) {
return I18n.t(`errors.${attribute}`, context);
} else {
var regex = new RegExp("{{(.*?)}}"),
attributeName = "";
let regex = new RegExp('{{(.*?)}}');
let attributeName = '';

if (regex.test(this.defaults[attribute])) {
attributeName = regex.exec(this.defaults[attribute])[1];
}

return this.defaults[attribute].replace(regex, context[attributeName]);
}
},

defaults: {
inclusion: "is not included in the list",
exclusion: "is reserved",
invalid: "is invalid",
confirmation: "doesn't match {{attribute}}",
accepted: "must be accepted",
empty: "can't be empty",
blank: "can't be blank",
present: "must be blank",
tooLong: "is too long (maximum is {{count}} characters)",
tooShort: "is too short (minimum is {{count}} characters)",
wrongLength: "is the wrong length (should be {{count}} characters)",
notANumber: "is not a number",
notAnInteger: "must be an integer",
greaterThan: "must be greater than {{count}}",
greaterThanOrEqualTo: "must be greater than or equal to {{count}}",
equalTo: "must be equal to {{count}}",
lessThan: "must be less than {{count}}",
lessThanOrEqualTo: "must be less than or equal to {{count}}",
otherThan: "must be other than {{count}}",
odd: "must be odd",
even: "must be even",
url: "is not a valid URL"
inclusion: 'is not included in the list',
exclusion: 'is reserved',
invalid: 'is invalid',
confirmation: 'doesn\'t match {{attribute}}',
accepted: 'must be accepted',
empty: 'can\'t be empty',
blank: 'can\'t be blank',
present: 'must be blank',
tooLong: 'is too long (maximum is {{count}} characters)',
tooShort: 'is too short (minimum is {{count}} characters)',
wrongLength: 'is the wrong length (should be {{count}} characters)',
notANumber: 'is not a number',
notAnInteger: 'must be an integer',
greaterThan: 'must be greater than {{count}}',
greaterThanOrEqualTo: 'must be greater than or equal to {{count}}',
equalTo: 'must be equal to {{count}}',
lessThan: 'must be less than {{count}}',
lessThanOrEqualTo: 'must be less than or equal to {{count}}',
otherThan: 'must be other than {{count}}',
odd: 'must be odd',
even: 'must be even',
url: 'is not a valid URL'
}
};
172 changes: 106 additions & 66 deletions addon/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,50 @@ import Errors from 'ember-validations/errors';
import Base from 'ember-validations/validators/base';
import getOwner from 'ember-getowner-polyfill';

var get = Ember.get;
var set = Ember.set;

var setValidityMixin = Ember.Mixin.create({
isValid: Ember.computed('[email protected]', function() {
var compactValidators = get(this, 'validators').compact();
var filteredValidators = compactValidators.filter(function(validator) {
return !get(validator, 'isValid');
});
const {
A: emberArray,
ArrayProxy,
Mixin,
RSVP: { all, reject },
computed,
computed: { alias, not },
get,
inject: { service },
isArray,
isNone,
isPresent,
set,
warn
} = Ember;

const setValidityMixin = Mixin.create({
isValid: computed('[email protected]', function() {
let compactValidators = get(this, 'validators').compact();
let filteredValidators = compactValidators.filter((validator) => !get(validator, 'isValid'));

return get(filteredValidators, 'length') === 0;
}),
isInvalid: Ember.computed.not('isValid')

isInvalid: not('isValid')
});

var pushValidatableObject = function(model, property) {
var content = get(model, property);
const pushValidatableObject = function(model, property) {
let content = get(model, property);

model.removeObserver(property, pushValidatableObject);
if (Ember.isArray(content)) {
model.validators.pushObject(ArrayValidatorProxy.create({model: model, property: property, contentBinding: 'model.' + property}));

if (isArray(content)) {
model.validators.pushObject(ArrayValidatorProxy.create({ model, property, contentBinding: `model.${property}` }));
} else {
model.validators.pushObject(content);
}
};

var lookupValidator = function(validatorName) {
var owner = getOwner(this);
var service = owner.lookup('service:validations');
var validators = [];
var cache;
const lookupValidator = function(validatorName) {
let owner = getOwner(this);
let service = get(this, 'validationService');
let validators = [];
let cache;

if (service) {
cache = get(service, 'cache');
Expand All @@ -44,19 +57,19 @@ var lookupValidator = function(validatorName) {
if (cache[validatorName]) {
validators = validators.concat(cache[validatorName]);
} else {
var local = owner.resolveRegistration('validator:local/' + validatorName);
var remote = owner.resolveRegistration('validator:remote/' + validatorName);
let local = owner.resolveRegistration(`validator:local/${validatorName}`);
let remote = owner.resolveRegistration(`validator:remote/${validatorName}`);

if (local || remote) {
validators = validators.concat([local, remote]);
} else {
var base = owner.resolveRegistration('validator:'+validatorName);
let base = owner.resolveRegistration(`validator:${validatorName}`);

if (base) {
validators = validators.concat([base]);
} else {
local = owner.resolveRegistration('ember-validations@validator:local/'+validatorName);
remote = owner.resolveRegistration('ember-validations@validator:remote/'+validatorName);
local = owner.resolveRegistration(`ember-validations@validator:local/${validatorName}`);
remote = owner.resolveRegistration(`ember-validations@validator:remote/${validatorName}`);

if (local || remote) {
validators = validators.concat([local, remote]);
Expand All @@ -67,46 +80,64 @@ var lookupValidator = function(validatorName) {
cache[validatorName] = validators;
}

Ember.warn('Could not find the "'+validatorName+'" validator.', !Ember.isEmpty(validators), {id: 'ember-validations.faild-to-find-validator'});
warn(`Could not find the "${validatorName}" validator.`, isPresent(validators), {
id: 'ember-validations.faild-to-find-validator'
});

return validators;
};

var ArrayValidatorProxy = Ember.ArrayProxy.extend(setValidityMixin, {
validate: function() {
const ArrayValidatorProxy = ArrayProxy.extend(setValidityMixin, {
init() {
this._validate();
},

validate() {
return this._validate();
},
_validate: Ember.on('init', function() {
var promises = get(this, 'content').invoke('_validate').without(undefined);
return Ember.RSVP.all(promises);
}),
validators: Ember.computed.alias('content')

_validate() {
let promises = get(this, 'content').invoke('_validate').without(undefined);
return all(promises);
},

validators: alias('content')
});

export default Ember.Mixin.create(setValidityMixin, {
init: function() {
this._super();
export default Mixin.create(setValidityMixin, {
validationService: service('validations'),

init() {
this._super(...arguments);
this.errors = Errors.create();
this.dependentValidationKeys = {};
this.validators = Ember.A();
this.validators = emberArray();

if (get(this, 'validations') === undefined) {
this.validations = {};
}

this.buildValidators();
this.validators.forEach(function(validator) {

this.validators.forEach((validator) => {
validator.addObserver('errors.[]', this, function(sender) {
var errors = Ember.A();
this.validators.forEach(function(validator) {
let errors = emberArray();

this.validators.forEach((validator) => {
if (validator.property === sender.property) {
errors.addObjects(validator.errors);
}
}, this);
set(this, 'errors.' + sender.property, errors);
});

set(this, `errors.${sender.property}`, errors);
});
}, this);
});

this._validate();
},
buildValidators: function() {
var property;

buildValidators() {
let property;

for (property in this.validations) {
if (this.validations[property].constructor === Object) {
Expand All @@ -116,57 +147,66 @@ export default Ember.Mixin.create(setValidityMixin, {
}
}
},
buildRuleValidator: function(property) {
var pushValidator = function(validator) {

buildRuleValidator(property) {
let pushValidator = (validator, validatorName) => {
if (validator) {
this.validators.pushObject(validator.create({model: this, property: property, options: this.validations[property][validatorName]}));
this.validators.pushObject(validator.create({ model: this, property, options: this.validations[property][validatorName] }));
}
};

if (this.validations[property].callback) {
this.validations[property] = { inline: this.validations[property] };
}

var createInlineClass = function(callback) {
let createInlineClass = (callback) => {
return Base.extend({
call: function() {
var errorMessage = this.callback.call(this);
call() {
let errorMessage = this.callback.call(this);

if (errorMessage) {
this.errors.pushObject(errorMessage);
}
},
callback: callback

callback
});
};

for (var validatorName in this.validations[property]) {
Object.keys(this.validations[property]).forEach((validatorName) => {
if (validatorName === 'inline') {
pushValidator.call(this, createInlineClass(this.validations[property][validatorName].callback));
let validator = createInlineClass(this.validations[property][validatorName].callback);
pushValidator(validator, validatorName);
} else if (this.validations[property].hasOwnProperty(validatorName)) {
lookupValidator.call(this, validatorName).forEach(pushValidator, this);
lookupValidator.call(this, validatorName).forEach((validator) => {
return pushValidator.call(this, validator, validatorName);
});
}
}
});
},
buildObjectValidator: function(property) {
if (Ember.isNone(get(this, property))) {

buildObjectValidator(property) {
if (isNone(get(this, property))) {
this.addObserver(property, this, pushValidatableObject);
} else {
pushValidatableObject(this, property);
}
},
validate: function() {
var self = this;
return this._validate().then(function(vals) {
var errors = get(self, 'errors');

validate() {
return this._validate().then((vals) => {
let errors = get(this, 'errors');

if (vals.indexOf(false) > -1) {
return Ember.RSVP.reject(errors);
return reject(errors);
}

return errors;
});
},
_validate: Ember.on('init', function() {
var promises = this.validators.invoke('_validate').without(undefined);
return Ember.RSVP.all(promises);
})

_validate() {
let promises = this.validators.invoke('_validate').without(undefined);
return all(promises);
}
});
4 changes: 3 additions & 1 deletion addon/patterns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ember from 'ember';

export default Ember.Namespace.create({
const { Namespace } = Ember;

export default Namespace.create({
numericality: /^(-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d*)?$/,
blank: /^\s*$/
});
Loading

0 comments on commit 922304d

Please sign in to comment.