Skip to content

Commit

Permalink
Fix #4
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Jun 1, 2015
1 parent a1320ab commit 5525b4c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
18 changes: 15 additions & 3 deletions addon/mixins/model-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ export default Ember.Mixin.create({
validationErrors: {},
isValidNow: true,

validate: function() {
validate: function(options) {
var store = this.get('store'),
errors = null,
validations = this.get('validations');

if (options == null) {
options = {};
}

// Clean all the current errors
this.get('errors').clear();
this.set('validationErrors',{});
Expand All @@ -20,8 +24,10 @@ export default Ember.Mixin.create({
// Call validators defined on each property
for (var property in validations) {
for (var validation in validations[property]) {
var validationName = (validation.charAt(0).toUpperCase() + validation.slice(1));
this[`_validate${validationName}`](property, validations[property]);
if (this._exceptOrOnly(property,options)) {
var validationName = (validation.charAt(0).toUpperCase() + validation.slice(1));
this[`_validate${validationName}`](property, validations[property]);
}
}
}

Expand Down Expand Up @@ -115,6 +121,12 @@ export default Ember.Mixin.create({
},

/**** Helpder methods ****/
_exceptOrOnly: function(property, options) {
var validateThis = true;
if(options.hasOwnProperty('except') && options.except.indexOf(property) !== -1){ validateThis = false; }
if(options.hasOwnProperty('only') && options.only.indexOf(property) === -1){ validateThis = false; }
return validateThis;
},
_getCustomMessage: function(validationObj,defaultMessage) {
if (this._isThisAnObject(validationObj) && validationObj.hasOwnProperty('message')) {
return validationObj.message;
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/mixins/model-validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,36 @@ describe('ModelValidatorMixin', function() {

});


describe('when except is passed to `validate`', function() {

it('it validates all the attributes except the ones specifed', function() {
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124,alibabaNumber:33,legacyCode:'abc'});
Ember.run(function() {
expect(model.validate()).to.equal(false);
model.set('bussinessEmail','donJoseRene@higuita.com');
model.set('mainstreamCode','hiphopBachatudo');
model.set('favoritColor','423abb');
model.set('mySubdomain','fake_subdomain');
expect(model.validate({except:['email']})).to.equal(true);
});
});

});

describe('when only is passed to `validate`', function() {

it('it validates only the attributes specifed', function() {
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124,alibabaNumber:33,legacyCode:'abc'});
Ember.run(function() {
expect(model.validate()).to.equal(false);
model.set('email','rene@higuita.com');
expect(model.validate({only:['email']})).to.equal(true);
});
});

});

}
);

Expand Down

0 comments on commit 5525b4c

Please sign in to comment.