From ac05c1d8612145d9cd6a60ecd9e60407b956dfe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frantis=CC=8Cek=20Ha=CC=81ba?= Date: Thu, 24 Nov 2011 18:53:48 +0100 Subject: [PATCH] =?UTF-8?q?Implemented=20new=20=E2=80=98addValidator?= =?UTF-8?q?=E2=80=99=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A validation function now gets five arguments - ‘property’, ‘propertyValue’, ‘validator’, ‘propertyValidators’ and ‘callback’. --- README.md | 7 ++-- src/amanda.js | 58 +++++++++++++++------------- tests/addValidator.js | 6 +-- tests/validators/required/objects.js | 1 + 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index d5c30da..691cfdf 100644 --- a/README.md +++ b/README.md @@ -161,12 +161,13 @@ This method allows you to add a custom validator. **Example** ```javascript -var evenValidator = function(value, options, callback) { + +var evenValidator = function(property, propertyValue, validator, propertyValidators, callback) { // If ‘even: true’ - if (options) { + if (validator) { - if (typeof value === 'number' && (value % 2) === 0) { + if (typeof propertyValue === 'number' && (propertyValue % 2) === 0) { // No problem, the number is event callback(); } else { diff --git a/src/amanda.js b/src/amanda.js index 0e163e4..1e75c0f 100644 --- a/src/amanda.js +++ b/src/amanda.js @@ -241,7 +241,7 @@ */ var iterator = function(validatorName, validatorFn, callback) { if (propertyValidators[validatorName]) { - validatorFn(propertyValue, propertyValidators[validatorName], function(error) { + validatorFn(property, propertyValue, propertyValidators[validatorName], propertyValidators, function(error) { if (error) { self.Errors.addError({ @@ -456,15 +456,21 @@ * Validators */ var validators = { - - 'required': function(value, options, callback) { - if (options && !value) { + + /** + * Required + */ + 'required': function(property, propertyValue, validator, propertyValidators, callback) { + if (validator && !propertyValue) { return callback(true); } else { return callback(); } }, + /** + * Type + */ 'type': (function() { var types = { @@ -488,16 +494,16 @@ }; }); - return function(value, options, callback) { + return function(property, propertyValue, validator, propertyValidators, callback) { /** * { * type: ['string', 'number'] * } */ - if (Object.prototype.toString.call(options) === '[object Array]') { + if (Object.prototype.toString.call(validator) === '[object Array]') { var noError = options.some(function(type) { - return types[type](value); + return types[type](propertyValue); }); return (noError) ? callback() : callback(true); @@ -507,7 +513,7 @@ * } */ } else { - return (types[options](value)) ? callback() : callback(true); + return (types[validator](propertyValue)) ? callback() : callback(true); } }; @@ -567,16 +573,16 @@ } }; - return function(value, options, callback) { + return function(property, propertyValue, validator, propertyValidators, callback) { /** * { * type: ['string', 'number'] * } */ - if (Object.prototype.toString.call(options) === '[object Array]') { + if (Object.prototype.toString.call(validator) === '[object Array]') { var noError = options.some(function(format) { - return formats[format](value); + return formats[format](propertyValue); }); return (noError) ? callback() : callback(true); @@ -586,7 +592,7 @@ * } */ } else { - return (formats[options](value)) ? callback() : callback(true); + return (formats[validator](propertyValue)) ? callback() : callback(true); } }; @@ -596,18 +602,18 @@ /** * Length */ - 'length': function(value, options, callback) { + 'length': function(property, propertyValue, validator, propertyValidators, callback) { // Check the length only if the type of ‘paramValue’ is string - if (typeof value === 'string') { + if (typeof propertyValue === 'string') { // If the length is specified as an array (for instance ‘[2, 45]’) - if (Array.isArray(options) && (value.length < options[0] || value.length > options[1])) { + if (Array.isArray(validator) && (propertyValue.length < validator[0] || propertyValue.length > validator[1])) { return callback(true); // If the length is specified as a string (for instance ‘2’) - } else if (typeof options === 'number' && value.length !== options) { + } else if (typeof validator === 'number' && propertyValue.length !== validator) { return callback(true); // If the length is specified in a different way @@ -624,36 +630,36 @@ /** * Enum */ - 'enum': function(value, options, callback) { - return (options.indexOf(value) === -1) ? callback(true) : callback(); + 'enum': function(property, propertyValue, validator, propertyValidators, callback) { + return (validator.indexOf(propertyValue) === -1) ? callback(true) : callback(); }, /** * Except */ - 'except': function(value, options, callback) { - return (options.indexOf(value) !== -1) ? callback(true) : callback(); + 'except': function(property, propertyValue, validator, propertyValidators, callback) { + return (validator.indexOf(propertyValue) !== -1) ? callback(true) : callback(); }, /** * Min */ - 'min': function(value, options, callback) { - return (typeof value !== 'number' || value < options) ? callback(true) : callback(); + 'min': function(property, propertyValue, validator, propertyValidators, callback) { + return (typeof propertyValue !== 'number' || propertyValue < validator) ? callback(true) : callback(); }, /** * Max */ - 'max': function(value, options, callback) { - return (typeof value !== 'number' || value > options) ? callback(true) : callback(); + 'max': function(property, propertyValue, validator, propertyValidators, callback) { + return (typeof propertyValue !== 'number' || propertyValue > validator) ? callback(true) : callback(); }, /** * Pattern */ - 'pattern': function(value, options, callback) { - return (typeof value === 'string' && !value.match(options)) ? callback(true) : callback(); + 'pattern': function(property, propertyValue, validator, propertyValidators, callback) { + return (typeof propertyValue === 'string' && !propertyValue.match(validator)) ? callback(true) : callback(); } }; diff --git a/tests/addValidator.js b/tests/addValidator.js index 997ac83..1284668 100644 --- a/tests/addValidator.js +++ b/tests/addValidator.js @@ -9,7 +9,7 @@ exports['Test #1'] = function(test) { /** * AddValidator */ - amanda.addValidator('unique', function(value, options, callback) { + amanda.addValidator('unique', function(property, propertyValue, validator, propertyValidators, callback) { var takenUsernames = [ 'Baggz', @@ -23,8 +23,8 @@ exports['Test #1'] = function(test) { * unique: true * } */ - if (options && takenUsernames.indexOf(value) !== -1) { - return callback('Oops! This username - ' + value + ' - is taken.'); + if (validator && takenUsernames.indexOf(propertyValue) !== -1) { + return callback('Oops! This username - ' + propertyValue + ' - is taken.'); } else { return callback(null); } diff --git a/tests/validators/required/objects.js b/tests/validators/required/objects.js index c4c357f..485bdb8 100644 --- a/tests/validators/required/objects.js +++ b/tests/validators/required/objects.js @@ -47,6 +47,7 @@ exports['Test #2'] = function(test) { amanda.validate(data, schema, { singleError: false }, function(error) { delete error[0].message; + delete error[1].message; test.deepEqual(error[0], { property: 'user.name',