Skip to content

Commit

Permalink
Add Format validator and prevent 'match is not a function' error.
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed May 18, 2015
1 parent 21af355 commit d815795
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/mixins/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default Ember.Mixin.create({
inclusionMessage: 'is not included in the list',
numericalityMessage: 'is not a number',
mailMessage: 'is not a valid email',
formatMessage: 'is invalid',

validationErrors: {},
isValidNow: true,
Expand Down Expand Up @@ -46,9 +47,18 @@ export default Ember.Mixin.create({
errors[property].push([this.presenceMessage]);
}
},
_validateFormat: function(property, validation) {
var errors = this.get('validationErrors'),
withRegexp = validation.format.with;
if (!this.get(property) || String(this.get(property)).match(withRegexp) === null){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
this.set('isValidNow',false);
errors[property].push([this.formatMessage]);
}
},
_validateEmail: function(property, validation) {
var errors = this.get('validationErrors');
if (!this.get(property) || this.get(property).match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) === null){
if (!this.get(property) || String(this.get(property)).match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) === null){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
this.set('isValidNow',false);
errors[property].push([this.mailMessage]);
Expand Down
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 @@ -5,6 +5,7 @@ export default DS.Model.extend(Validator,{
name: DS.attr('string'),
email: DS.attr('string'),

legacyCode: DS.attr('string'),
lotteryNumber: DS.attr('number'),

otherFakes: DS.hasMany('other-model'),
Expand All @@ -18,6 +19,9 @@ export default DS.Model.extend(Validator,{
presence: true,
email: true
},
legacyCode:{
format: {with: /^[a-zA-Z]+$/}
},
lotteryNumber: {
numericality: true
},
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/mixins/validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('ValidatorMixin', function() {
expect(model.get('errors').errorsFor('name').mapBy('message')[0][0]).to.equal(model.presenceMessage);
});

it('validates the format of the attributes set on `validations.format`', function() {
var model = this.subject({legacyCode: 3123123});
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('legacyCode').mapBy('message')[0][0]).to.equal(model.formatMessage);
});

it('validates the email format of the attributes set on `validations.email`', function() {
var model = this.subject({email:'adsfasdf$'});
expect(model.validate()).to.equal(false);
Expand Down Expand Up @@ -71,7 +77,7 @@ describe('ValidatorMixin', function() {
describe('when data is corrected after validation', function() {

it('it clean the erros', function() {
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124});
var model = this.subject({email:'adsfasdf$',name:'Jose Rene',lotteryNumber:124,legacyCode:'abc'});
Ember.run(function() {
expect(model.validate()).to.equal(false);
model.set('email','rene@higuita.com');
Expand Down

0 comments on commit d815795

Please sign in to comment.