From daf26b614bb8bcdcac6647de52cf26dfdcd56d66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Sat, 21 Apr 2018 18:21:25 -0300 Subject: [PATCH] Allows array of validators without them being wrapped in objects #332 --- src/backbone-validation.js | 15 +++++---- test/customValidators.js | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/backbone-validation.js b/src/backbone-validation.js index 16981911..2ae2c6b9 100644 --- a/src/backbone-validation.js +++ b/src/backbone-validation.js @@ -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]; @@ -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], diff --git a/test/customValidators.js b/test/customValidators.js index c8687d79..ed848112 100644 --- a/test/customValidators.js +++ b/test/customValidators.js @@ -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;