Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amanda v0.2.1 #3

Merged
merged 1 commit into from
Nov 24, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
58 changes: 32 additions & 26 deletions src/amanda.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 = {
Expand All @@ -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);

Expand All @@ -507,7 +513,7 @@
* }
*/
} else {
return (types[options](value)) ? callback() : callback(true);
return (types[validator](propertyValue)) ? callback() : callback(true);
}

};
Expand Down Expand Up @@ -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);

Expand All @@ -586,7 +592,7 @@
* }
*/
} else {
return (formats[options](value)) ? callback() : callback(true);
return (formats[validator](propertyValue)) ? callback() : callback(true);
}

};
Expand All @@ -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
Expand All @@ -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();
}

};
Expand Down
6 changes: 3 additions & 3 deletions tests/addValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/validators/required/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down