Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Amanda v0.2.1 #3

Merged
merged 1 commit into from Nov 24, 2011
Jump to file or symbol
Failed to load files and symbols.
+40 −32
Split
View
@@ -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 {
View
@@ -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();
}
};
View
@@ -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);
}
@@ -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',