Skip to content

Commit daf26b6

Browse files
author
Luiz Américo
committed
Allows array of validators without them being wrapped in objects thedersen#332
1 parent c5e52cf commit daf26b6

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/backbone-validation.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ Backbone.Validation = (function(_){
122122
var getValidators = function(model, attr) {
123123
var attrValidationSet = model.validation ? _.result(model, 'validation')[attr] || {} : {};
124124

125-
// If the validator is a function or a string, wrap it in a function validator
126-
if (_.isFunction(attrValidationSet) || _.isString(attrValidationSet)) {
127-
attrValidationSet = {
128-
fn: attrValidationSet
129-
};
130-
}
131-
132125
// Stick the validator object into an array
133126
if(!_.isArray(attrValidationSet)) {
134127
attrValidationSet = [attrValidationSet];
@@ -138,6 +131,14 @@ Backbone.Validation = (function(_){
138131
// with a validation method to call, the value to validate against
139132
// and the specified error message, if any
140133
return _.reduce(attrValidationSet, function(memo, attrValidation) {
134+
135+
// If the validator is a function or a string, wrap it in a function validator
136+
if (_.isFunction(attrValidation) || _.isString(attrValidation)) {
137+
attrValidation = {
138+
fn: attrValidation
139+
};
140+
}
141+
141142
_.each(_.without(_.keys(attrValidation), 'msg'), function(validator) {
142143
memo.push({
143144
fn: defaultValidators[validator],

test/customValidators.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,73 @@ module.exports = {
3636
}
3737
},
3838

39+
'Defining a custom validator as a string': {
40+
beforeEach: function() {
41+
var Model = Backbone.Model.extend({
42+
validation: {
43+
age: 'validateAge'
44+
},
45+
46+
validateAge(value, attr, computedState) {
47+
if (value != 1) return 'Age invalid'
48+
}
49+
});
50+
this.model = new Model();
51+
_.extend(this.model, Backbone.Validation.mixin);
52+
this.validateAgeSpy = sinon.spy(this.model, 'validateAge');
53+
},
54+
55+
'should execute corresponding method in model': function() {
56+
assert(this.model.set({
57+
age: 1
58+
}, { validate: true }));
59+
sinon.assert.calledOnce(this.validateAgeSpy);
60+
assert(this.model.set({
61+
age: '1'
62+
}, { validate: true }));
63+
sinon.assert.calledTwice(this.validateAgeSpy);
64+
refute(this.model.set({
65+
age: 2
66+
}, { validate: true }));
67+
sinon.assert.calledThrice(this.validateAgeSpy);
68+
}
69+
},
70+
71+
'Defining a custom validator as a string array': {
72+
beforeEach: function() {
73+
var Model = Backbone.Model.extend({
74+
validation: {
75+
age: ['validateAge', 'validateNumber']
76+
},
77+
78+
validateAge(value, attr, computedState) {
79+
if (value != 1) return 'Age invalid'
80+
},
81+
82+
validateNumber(value, attr, computedState) {
83+
if (typeof value !== 'number') return 'Not a number'
84+
}
85+
});
86+
this.model = new Model();
87+
_.extend(this.model, Backbone.Validation.mixin);
88+
this.validateAgeSpy = sinon.spy(this.model, 'validateAge');
89+
this.validateNumberSpy = sinon.spy(this.model, 'validateNumber');
90+
},
91+
92+
'should use corresponding methods in model': function() {
93+
assert(this.model.set({
94+
age: 1
95+
}, { validate: true }));
96+
sinon.assert.calledOnce(this.validateAgeSpy);
97+
sinon.assert.calledOnce(this.validateNumberSpy);
98+
refute(this.model.set({
99+
age: '1'
100+
}, { validate: true }));
101+
sinon.assert.calledTwice(this.validateAgeSpy);
102+
sinon.assert.calledTwice(this.validateNumberSpy);
103+
}
104+
},
105+
39106
'Overriding built-in validator in Backbone.Validation': {
40107
beforeEach: function () {
41108
this.builtinMin = Backbone.Validation.validators.min;

0 commit comments

Comments
 (0)