Skip to content

Commit

Permalink
Adding subdomain validator
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed May 21, 2015
1 parent f648201 commit 6f79991
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/mixins/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default Ember.Mixin.create({
mailMessage: 'is not a valid email',
formatMessage: 'is invalid',
colorMessage: 'must be a valid CSS hex color code',
subdomainMessage: 'must be a valid CSS hex color code',

validationErrors: {},
isValidNow: true,
Expand Down Expand Up @@ -76,8 +77,20 @@ export default Ember.Mixin.create({
},
_validateColor: function(property, validation) {
var errors = this.get('validationErrors'),
propertyValue = this.get(property),
message = this._getCustomMessage(validation.color, this.colorMessage);
if (!this.get(property) || String(this.get(property)).match(/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i) === null){
if (!propertyValue || String(propertyValue).match(/([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i) === null){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
this.set('isValidNow',false);
errors[property].push([message]);
}
},
_validateSubdomain: function(property, validation) {
var errors = this.get('validationErrors'),
message = this._getCustomMessage(validation.subdomain, this.subdomainMessage),
propertyValue = this.get(property),
reserved = validation.subdomain.reserved || [];
if (!propertyValue || String(propertyValue).match(/^[a-z\d]+([-_][a-z\d]+)*$/i) === null || reserved.indexOf(propertyValue) !== -1){
if (!Ember.isArray(errors[property])) {errors[property] = [];}
this.set('isValidNow',false);
errors[property].push([message]);
Expand Down
5 changes: 5 additions & 0 deletions tests/dummy/app/models/fake-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default DS.Model.extend(Validator,{
lotteryNumber: DS.attr('number'),
alibabaNumber: DS.attr('number'),

mySubdomain: DS.attr('number'),

otherFakes: DS.hasMany('other-model'),

validations: {
Expand All @@ -33,6 +35,9 @@ export default DS.Model.extend(Validator,{
presence: true,
email: true
},
mySubdomain:{
subdomain:{ reserved:['admin','blog'], message: 'this subdomain is super invalid' }
},
mainstreamCode: {
format: { with: /^[a-zA-Z]+$/, message: 'nu nu, that\'s not the format' }
},
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/mixins/validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ describe('ValidatorMixin', function() {
expect(model.get('errors').errorsFor('lotteryNumber').mapBy('message')[0][0]).to.equal(model.numericalityMessage);
});

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

it('validates the inclusion of the attributes set on `validations.inclusion`', function() {
var model = this.subject({name:'adsfasdf$'}),
message = model.validations.name.inclusion.message;
Expand Down Expand Up @@ -121,6 +130,22 @@ describe('ValidatorMixin', function() {
});
});

it('validates the subdomain format of the attributes set on `validations.subdomain` and use the correct message', function() {
var model = this.subject({mySubdomain:'with space'});
Ember.run(function() {
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('mySubdomain').mapBy('message')[0][0]).to.equal(model.validations.mySubdomain.subdomain.message);
});
});

it('validates the subdomain reserved words of the attributes set on `validations.subdomain` and use the correct message', function() {
var model = this.subject({mySubdomain:'admin'});
Ember.run(function() {
expect(model.validate()).to.equal(false);
expect(model.get('errors').errorsFor('mySubdomain').mapBy('message')[0][0]).to.equal(model.validations.mySubdomain.subdomain.message);
});
});

it('validates the format of the attributes set on `validations.format` and use the correct message', function() {
var model = this.subject({mainstreamCode: 3123123});
Ember.run(function() {
Expand Down Expand Up @@ -165,6 +190,7 @@ describe('ValidatorMixin', function() {
model.set('bussinessEmail','[email protected]');
model.set('mainstreamCode','hiphopBachatudo');
model.set('favoritColor','423abb');
model.set('mySubdomain','fake_subdomain');
expect(model.validate()).to.equal(true);
});
});
Expand Down

0 comments on commit 6f79991

Please sign in to comment.