async-validate
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -124,24 +124,43 @@ var util = require('util'); | ||
} | ||
validator = function(callback) { | ||
var data = arguments.callee.data; | ||
var cb = function(errors) { | ||
//delete data.rule.validator; | ||
if(options.first) { | ||
return complete(errors); | ||
} | ||
callback(null, errors); | ||
series.push({rule: rule, value: value, source: source}); | ||
} | ||
} | ||
async.mapSeries( series, function(data, callback) { | ||
var rule = data.rule; | ||
var deep = (rule.type == 'object' || rule.type == 'array') | ||
&& typeof(rule.fields) == 'object'; | ||
deep = deep && (rule.required || (!rule.required && data.value)); | ||
var cb = function(errors) { | ||
if(options.first) { | ||
return complete(errors); | ||
} | ||
if(!deep) { | ||
callback(null, errors); | ||
}else{ | ||
errors = errors || []; | ||
// if rule is required but the target object | ||
// does not exist fail at the rule level and don't | ||
// go deeper | ||
if(rule.required && !data.value) { | ||
return callback(null, [ | ||
options.error( | ||
rule, util.format(options.messages.required, rule.field)) | ||
]); | ||
} | ||
//console.log("calling validator with messages %s", messages); | ||
data.rule.validator( | ||
data.rule, data.value, cb, data.source, options); | ||
var schema = new Schema(data.rule.fields); | ||
schema.messages(options.messages); | ||
if(data.rule.options) { | ||
data.rule.options.messages = options.messages; | ||
data.rule.options.exception = options.exception; | ||
data.rule.options.error = options.error; | ||
} | ||
schema.validate( | ||
data.value, data.rule.options || options, function(errs, fields) { | ||
callback(null, errs && errs.length ? errors.concat(errs) : errs); | ||
}); | ||
} | ||
validator.data = {rule: rule, value: value, source: source}; | ||
series.push(validator); | ||
if((rule.type == 'object' || rule.type == 'array') | ||
&& typeof(rule.fields) == 'object') { | ||
this.getDeepValidator(series, rule, value, options); | ||
} | ||
} | ||
} | ||
async.series(series, function(err, results) { | ||
rule.validator( | ||
rule, data.value, cb, data.source, options); | ||
}, function(err, results) { | ||
complete(results); | ||
@@ -188,30 +207,2 @@ }); | ||
/** | ||
* Handles creating deep validation rules. | ||
* | ||
* @param series The array of asynchronous functions. | ||
* @param rule The validation rule. | ||
* @param value The value on the source object. | ||
* @param options The options passed to validate the object. | ||
* | ||
* @api private | ||
*/ | ||
Schema.prototype.getDeepValidator = function(series, rule, value, options) { | ||
if(value === undefined) { | ||
return false; | ||
} | ||
var messages = this.messages(); | ||
var deep = function(callback) { | ||
var data = arguments.callee.data; | ||
var schema = new Schema(data.rule.fields); | ||
schema.messages(messages); | ||
schema.validate( | ||
data.value, data.rule.options || data.options, function(errors, fields) { | ||
callback(null, errors); | ||
}); | ||
} | ||
deep.data = {rule: rule, value: value, options: options}; | ||
series.push(deep); | ||
} | ||
/** | ||
* Register a validator function for a type. | ||
@@ -218,0 +209,0 @@ * |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -159,5 +159,5 @@ var util = require('util'); | ||
validator.validate({name: "user"}, function(errors, fields) { | ||
console.log("after revalidation %j", errors); | ||
//assert.isNull(errors); | ||
//assert.isNull(fields); | ||
//console.log("after revalidation %j", errors); | ||
assert.isNull(errors); | ||
assert.isNull(fields); | ||
}); | ||
@@ -164,0 +164,0 @@ }); |
Sorry, the diff of this file is not supported yet
74019
1703