Skip to content

Commit

Permalink
Allows array of validators without them being wrapped in objects thed…
Browse files Browse the repository at this point in the history
  • Loading branch information
Luiz Américo committed Apr 21, 2018
1 parent c5e52cf commit daf26b6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/backbone-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ Backbone.Validation = (function(_){
var getValidators = function(model, attr) {
var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {};

// If the validator is a function or a string, wrap it in a function validator
if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
attrValidationSet = {
fn: attrValidationSet
};
}

// Stick the validator object into an array
if(!_.isArray(attrValidationSet)) {
attrValidationSet = [attrValidationSet];
Expand All @@ -138,6 +131,14 @@ Backbone.Validation = (function(_){
// with a validation method to call, the value to validate against
// and the specified error message, if any
return _.reduce(attrValidationSet, function(memo, attrValidation) {

// If the validator is a function or a string, wrap it in a function validator
if (_.isFunction(attrValidation) || _.isString(attrValidation)) {
attrValidation = {
fn: attrValidation
};
}

_.each(_.without(_.keys(attrValidation), 'msg'), function(validator) {
memo.push({
fn: defaultValidators[validator],
Expand Down
67 changes: 67 additions & 0 deletions test/customValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,73 @@ module.exports = {
}
},

'Defining a custom validator as a string': {
beforeEach: function() {
var Model = Backbone.Model.extend({
validation: {
age: 'validateAge'
},

validateAge(value, attr, computedState) {
if (value != 1) return 'Age invalid'
}
});
this.model = new Model();
_.extend(this.model, Backbone.Validation.mixin);
this.validateAgeSpy = sinon.spy(this.model, 'validateAge');
},

'should execute corresponding method in model': function() {
assert(this.model.set({
age: 1
}, { validate: true }));
sinon.assert.calledOnce(this.validateAgeSpy);
assert(this.model.set({
age: '1'
}, { validate: true }));
sinon.assert.calledTwice(this.validateAgeSpy);
refute(this.model.set({
age: 2
}, { validate: true }));
sinon.assert.calledThrice(this.validateAgeSpy);
}
},

'Defining a custom validator as a string array': {
beforeEach: function() {
var Model = Backbone.Model.extend({
validation: {
age: ['validateAge', 'validateNumber']
},

validateAge(value, attr, computedState) {
if (value != 1) return 'Age invalid'
},

validateNumber(value, attr, computedState) {
if (typeof value !== 'number') return 'Not a number'
}
});
this.model = new Model();
_.extend(this.model, Backbone.Validation.mixin);
this.validateAgeSpy = sinon.spy(this.model, 'validateAge');
this.validateNumberSpy = sinon.spy(this.model, 'validateNumber');
},

'should use corresponding methods in model': function() {
assert(this.model.set({
age: 1
}, { validate: true }));
sinon.assert.calledOnce(this.validateAgeSpy);
sinon.assert.calledOnce(this.validateNumberSpy);
refute(this.model.set({
age: '1'
}, { validate: true }));
sinon.assert.calledTwice(this.validateAgeSpy);
sinon.assert.calledTwice(this.validateNumberSpy);
}
},

'Overriding built-in validator in Backbone.Validation': {
beforeEach: function () {
this.builtinMin = Backbone.Validation.validators.min;
Expand Down

0 comments on commit daf26b6

Please sign in to comment.