Skip to content

Commit

Permalink
Close #41
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Sep 17, 2016
1 parent 034d9b2 commit 8d2acf3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
23 changes: 21 additions & 2 deletions addon/mixins/model-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,15 @@ export default Ember.Mixin.create({
if (propertyValue.getTime() >= new Date(validation.date.before).getTime()) {
this.set('isValidNow', false);
let context = {date: new Date(validation.date.before)};
validation.date.interpolatedValue = validation.date.before;
this._addToErrors(property, validation.date, this._formatMessage(Messages.dateBeforeMessage, context));
}
}
if (validation.date.hasOwnProperty('after') && validation.date.after) {
if (propertyValue.getTime() <= new Date(validation.date.after).getTime()) {
this.set('isValidNow', false);
let context = {date: new Date(validation.date.after)};
validation.date.interpolatedValue = validation.date.after;
this._addToErrors(property, validation.date, this._formatMessage(Messages.dateAfterMessage, context));
}
}
Expand Down Expand Up @@ -217,34 +219,39 @@ export default Ember.Mixin.create({
if(propertyValue <= validation.numericality.greaterThan){
this.set('isValidNow',false);
let context = {count: validation.numericality.greaterThan};
validation.numericality.interpolatedValue = validation.numericality.greaterThan;
this._addToErrors(property, validation.numericality, this._formatMessage(Messages.numericalityGreaterThanMessage, context));
}
}
if(validation.numericality.hasOwnProperty('greaterThanOrEqualTo') && this._isNumber(validation.numericality.greaterThanOrEqualTo)){
if(propertyValue < validation.numericality.greaterThanOrEqualTo){
this.set('isValidNow',false);
let context = {count: validation.numericality.greaterThanOrEqualTo};
validation.numericality.interpolatedValue = validation.numericality.greaterThanOrEqualTo;
this._addToErrors(property, validation.numericality, this._formatMessage(Messages.numericalityGreaterThanOrEqualToMessage, context));
}
}
if(validation.numericality.hasOwnProperty('equalTo') && this._isNumber(validation.numericality.equalTo)){
if(propertyValue !== validation.numericality.equalTo){
this.set('isValidNow',false);
let context = {count: validation.numericality.equalTo};
validation.numericality.interpolatedValue = validation.numericality.equalTo;
this._addToErrors(property, validation.numericality, this._formatMessage(Messages.numericalityEqualToMessage, context));
}
}
if(validation.numericality.hasOwnProperty('lessThan') && this._isNumber(validation.numericality.lessThan)){
if(propertyValue >= validation.numericality.lessThan){
this.set('isValidNow',false);
let context = {count: validation.numericality.lessThan};
validation.numericality.interpolatedValue = validation.numericality.lessThan;
this._addToErrors(property, validation.numericality, this._formatMessage(Messages.numericalityLessThanMessage, context));
}
}
if(validation.numericality.hasOwnProperty('lessThanOrEqualTo') && this._isNumber(validation.numericality.lessThanOrEqualTo)){
if(propertyValue > validation.numericality.lessThanOrEqualTo){
this.set('isValidNow',false);
let context = {count: validation.numericality.lessThanOrEqualTo};
validation.numericality.interpolatedValue = validation.numericality.lessThanOrEqualTo;
this._addToErrors(property, validation.numericality, this._formatMessage(Messages.numericalityLessThanOrEqualToMessage, context));
}
}
Expand All @@ -271,7 +278,11 @@ export default Ember.Mixin.create({
matchingValue = this.get(matching);
if (propertyValue !== matchingValue) {
this.set('isValidNow',false);
let context = {match: this._unCamelCase(matching)};
let matchingUnCamelCase = this._unCamelCase(matching);
let context = {match: matchingUnCamelCase};
if(Ember.typeOf(validation.match) === 'object'){
validation.match.interpolatedValue = matchingUnCamelCase;
}
this._addToErrors(property, validation.match, this._formatMessage(Messages.matchMessage, context));
}
},
Expand All @@ -298,6 +309,7 @@ export default Ember.Mixin.create({
if(stringLength !== validation.length.is){
this.set('isValidNow',false);
let context = {count: validation.length.is};
validation.length.interpolatedValue = validation.length.is;
this._addToErrors(property, validation.length, this._formatMessage(Messages.wrongLengthMessage, context));
}
},
Expand All @@ -319,10 +331,16 @@ export default Ember.Mixin.create({
if(stringLength < minimum){
this.set('isValidNow',false);
let context = {count: minimum};
if(Ember.typeOf(validation.length.minimum) === 'object'){
validation.length.minimum.interpolatedValue = minimum;
}
this._addToErrors(property, validation.length.minimum, this._formatMessage(Messages.tooShortMessage, context));
}else if (stringLength > maximum) {
this.set('isValidNow',false);
let context = {count: maximum};
if(Ember.typeOf(validation.length.maximum) === 'object'){
validation.length.maximum.interpolatedValue = maximum;
}
this._addToErrors(property, validation.length.maximum, this._formatMessage(Messages.tooLongMessage, context));
}
},
Expand Down Expand Up @@ -397,7 +415,8 @@ export default Ember.Mixin.create({
let msg = validationObj.message.call(property, this.get(property), this);
return this._isString( msg ) ? msg : defaultMessage;
}else{
return validationObj.message;
let context = {value: validationObj.interpolatedValue};
return this._formatMessage(validationObj.message, context);
}
}else{
return defaultMessage;
Expand Down
9 changes: 9 additions & 0 deletions tests/dummy/app/models/fake-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default Model.extend(Validator,{
nsaNumber: attr('number', {defaultValue: 1234}),
chuncaluchoNumber: attr('number', {defaultValue: 1234567891}),
theMinimunmTwoNumber: attr('number', {defaultValue: 3223}),
theMinimunmInterpolatedTenNumber: attr('number', {defaultValue: 3223222222}),
hugeName: attr('string', {defaultValue: 12345}),
postalCodeUS: attr('string', {defaultValue: '09011'}),
postalCodeUK: attr('string', {defaultValue: 'KY16 8BP'}),
Expand Down Expand Up @@ -94,6 +95,14 @@ export default Model.extend(Validator,{
}
}
},
theMinimunmInterpolatedTenNumber:{
length: {
minimum: {
value: 10,
message: 'eeeche {value}'
}
}
},
hugeName:{
length: {
minimum: 3,
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/mixins/model-validator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ describe('ModelValidatorMixin', function() {
expect(model.get('errors').errorsFor('images').length).to.equal(0);
});
});
describe('message with interpolated values', function() {
it('interpolates the value whitn the message', function() {
var model = this.subject({theMinimunmInterpolatedTenNumber:'1'});
Ember.run(function() {
expect(model.validate({only:['theMinimunmInterpolatedTenNumber']})).to.equal(false);
expect(model.get('errors').errorsFor('theMinimunmInterpolatedTenNumber').mapBy('message')[0][0]).to.equal('eeeche 10');
});
});

});
describe('Presence validator', function() {
it('validates the presence of the attributes set on `validations.presence`', function() {
var model = this.subject(),
Expand Down

0 comments on commit 8d2acf3

Please sign in to comment.