Skip to content

Commit

Permalink
Fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Oct 13, 2015
1 parent fa51a25 commit 805356a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
14 changes: 13 additions & 1 deletion addon/mixins/model-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export default Ember.Mixin.create({
}
},
_validatePresence: function(property, validation) {
if (Ember.isBlank(this.get(property))){
var propertyValue = this.get(property);
// If the property is an async relationship.
if(this._modelRelations() && !Ember.isBlank(this._modelRelations()[property])){
propertyValue = this.get(`${property}.content`);
}
if(Ember.isBlank(propertyValue)){
this.set('isValidNow',false);
this._addToErrors(property, validation.presence, Messages.presenceMessage);
}
Expand Down Expand Up @@ -352,5 +357,12 @@ export default Ember.Mixin.create({
},
_isString: function(str){
return Ember.isEqual(Ember.typeOf(str), 'string');
},
_modelRelations: function() {
if(this.get('_relationships')){
return this.get('_relationships');
}else{
return this.get('_internalModel._relationships.initializedRelationships');
}
}
});
3 changes: 3 additions & 0 deletions tests/dummy/app/models/async-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import DS from 'ember-data';

export default DS.Model.extend();
4 changes: 4 additions & 0 deletions tests/dummy/app/models/fake-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ export default DS.Model.extend(Validator,{
myBlog: DS.attr('string', {defaultValue: 'http://esbanarango.com'}),
otherFakes: DS.hasMany('other-model'),
otherFake: DS.belongsTo('other-model'),
asyncModel: DS.belongsTo('async-model',{async: true}),
thing: DS.attr(''),
otherCustomValidation: DS.attr('number', { defaultValue: 12345 }),
otherCustomValidationBadMessageFunction: DS.attr('number', { defaultValue: 12345 }),

validations: {
asyncModel: {
presence: true
},
name: {
presence: { errorAs:'profile.name' },
inclusion: { in: ['Jose Rene', 'Aristi Gol', 'Armani'], message: 'Solo verde a morir' }
Expand Down
54 changes: 34 additions & 20 deletions tests/unit/mixins/model-validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('ModelValidatorMixin', function() {
describeModel('fake-model','Fake model with simple validations',
{
// Specify the other units that are required for this test.
needs: ['model:other-model']
needs: ['model:other-model', 'model:async-model']
},
function() {
// Replace this with your real tests.
Expand All @@ -30,27 +30,35 @@ describe('ModelValidatorMixin', function() {
expect(model).to.be.ok;
});


it('skips other validations when optional field is blank', function(){
var model = this.subject({anOptionalNumber: null});
model.validate();
expect(model.get('errors').errorsFor('anOptionalNumber').length).to.equal(0);
});
it('runs remaining validations when optional field is not blank', function(){
var model = this.subject({anOptionalNumber: 'abc'});
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('anOptionalNumber').mapBy('message')[0][0]).to.equal(Messages.numericalityMessage);
expect(model.get('errors').errorsFor('anOptionalNumber').mapBy('message')[1][0]).to.equal(Messages.numericalityOnlyIntegerMessage);
describe('allowBlank option', function() {
it('skips other validations when optional field is blank', function(){
var model = this.subject({anOptionalNumber: null});
model.validate();
expect(model.get('errors').errorsFor('anOptionalNumber').length).to.equal(0);
});
it('runs remaining validations when optional field is not blank', function(){
var model = this.subject({anOptionalNumber: 'abc'});
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('anOptionalNumber').mapBy('message')[0][0]).to.equal(Messages.numericalityMessage);
expect(model.get('errors').errorsFor('anOptionalNumber').mapBy('message')[1][0]).to.equal(Messages.numericalityOnlyIntegerMessage);
});
});
it('validates the presence of the attributes set on `validations.presence`', function() {
var model = this.subject(),

it('validates the presence of the attributes set on `validations.presence`', function() {
var model = this.subject(),
errorAs = model.validations.name.presence.errorAs;
delete model.validations.name.presence.errorAs;
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('email').mapBy('message')[0][0]).to.equal(Messages.presenceMessage);
expect(model.get('errors').errorsFor('name').mapBy('message')[0][0]).to.equal(Messages.presenceMessage);
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('email').mapBy('message')[0][0]).to.equal(Messages.presenceMessage);
expect(model.get('errors').errorsFor('name').mapBy('message')[0][0]).to.equal(Messages.presenceMessage);
model.validations.name.presence['errorAs'] = errorAs;
});
});

it('validates the presence of the attributes set on `validations.presence` even whe is a relation', function() {
var model = this.subject();
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('asyncModel').mapBy('message')[0][0]).to.equal(Messages.presenceMessage);
});

it('validates the format of the attributes set on `validations.format`', function() {
var model = this.subject({legacyCode: 3123123});
Expand Down Expand Up @@ -492,12 +500,17 @@ describe('ModelValidatorMixin', function() {
describe('when data is corrected after validation', function() {

it('it clean the errors', function() {
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124,alibabaNumber:33,legacyCode:'abc', acceptConditions: 1, password: 'k$1hkjGd', favoriteColor: 'FFFFFF', socialSecurity: 12312});
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124,alibabaNumber:33,legacyCode:'abc', acceptConditions: 1, password: 'k$1hkjGd', favoriteColor: 'FFFFFF', socialSecurity: 12312}),
store = model.get('store');

Ember.run(function() {
expect(model.validate()).to.equal(false);
var asyncModel = store.createRecord('async-model');
model.set('password','k$1hkjGd');
model.set('passwordConfirmation','k$1hkjGd');
model.set('email','[email protected]');
model.set('asyncModel',asyncModel);

expect(model.validate()).to.equal(true);

});
Expand Down Expand Up @@ -528,8 +541,9 @@ describe('ModelValidatorMixin', function() {
Ember.run(function() {
model.set('password','k$1hkjGd');
model.set('passwordConfirmation','k$1hkjGd');
model.set('email','[email protected]');
expect(model.validate()).to.equal(false);
expect(model.validate({except:['email']})).to.equal(true);
expect(model.validate({except:['asyncModel']})).to.equal(true);
});
});

Expand Down

0 comments on commit 805356a

Please sign in to comment.