async-validate
Advanced tools
Comparing version
@@ -6,7 +6,14 @@ ### API | ||
```javascript | ||
function Schema(descriptor, [opts]) | ||
function Schema(rules, [opts]) | ||
``` | ||
Encapsulates the rules associated with a descriptor and the logic for performing validation. | ||
Encapsulates the rules associated with a schema and the logic for performing validation. | ||
* `rules`: The schema rules. | ||
* `opts`: Configuration options. | ||
Options: | ||
* `messages`: An alternative messages object for the schema. | ||
##### messages | ||
@@ -23,3 +30,3 @@ | ||
```javascript | ||
function validate(source, [options], cb) | ||
function validate(source, [opts], cb) | ||
``` | ||
@@ -30,3 +37,3 @@ | ||
* `source`: The object to validate. | ||
* `options`: An object describing processing options for the validation. | ||
* `opts`: Map of processing options for the validation. | ||
* `cb`: Callback function to invoke when validation completes. | ||
@@ -39,2 +46,3 @@ | ||
* `bail`: Shorthand for `single` and `first`. | ||
* `messages`: Overrides the schema messages. | ||
* `parallel`: A boolean indicating that the validation should be executed in parallel. | ||
@@ -41,0 +49,0 @@ * `field`: Field name for the source object, default is `source` when not specified. |
@@ -38,3 +38,3 @@ ## Guide | ||
Muliple objects containing an `address` field that needs the same validation rules: | ||
Muliple objects containing an `address` field that need the same validation rules: | ||
@@ -160,4 +160,4 @@ ```javascript | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({address: {}}, function(err, res) { | ||
// res.errors contains errors for name, street, city, zip | ||
@@ -164,0 +164,0 @@ }); |
@@ -11,3 +11,3 @@ ### Messages | ||
You may also use a function for the rule message, it is invoked in the scope of the [validator](#validator) and passed the original message and replacement parameters: | ||
You may also use a function for the rule message, it is invoked in the scope of the [Rule](#rule) and passed the original message and replacement parameters: | ||
@@ -14,0 +14,0 @@ ```javascript |
@@ -672,3 +672,3 @@ Table of Contents | ||
``` | ||
[ { [Error: email: could not resolve dns for domain 1442381544255.com] field: 'email' } ] | ||
[ { [Error: email: could not resolve dns for domain 1442381759600.com] field: 'email' } ] | ||
``` | ||
@@ -675,0 +675,0 @@ |
@@ -13,3 +13,2 @@ var iterator = require('./iterator') | ||
opts = opts || {}; | ||
this.messages(opts.messages || require('../messages')); | ||
@@ -24,2 +23,4 @@ if(rules === undefined) { | ||
this.messages(opts.messages || require('../messages')); | ||
// do not clone rules when deep is set | ||
@@ -55,13 +56,13 @@ // initial clone is a deep clone | ||
* @param source The object to validate. | ||
* @param options Validation options. | ||
* @param opts Validation options. | ||
* @param cb Callback to invoke when validation is complete. | ||
*/ | ||
function validate(source, options, cb) { | ||
if(typeof options === 'function') { | ||
cb = options; | ||
options = null; | ||
function validate(source, opts, cb) { | ||
if(typeof opts === 'function') { | ||
cb = opts; | ||
opts = null; | ||
} | ||
options = options || {}; | ||
opts = opts || {}; | ||
if(source === undefined && !options._deep) { | ||
if(source === undefined && !opts._deep) { | ||
throw new Error('Cannot validate with no source.'); | ||
@@ -76,14 +77,12 @@ }else if(typeof cb !== 'function') { | ||
, series = [] | ||
, state = options.state || {} | ||
, state = opts.state || {} | ||
// iterator function series/parallel | ||
, func = options.parallel ? iterator.map : iterator.mapSeries | ||
, messages; | ||
, func = opts.parallel ? iterator.map : iterator.mapSeries | ||
// configure messages to use defaults where necessary | ||
, messages = opts.messages || this.messages(); | ||
if(options.bail) { | ||
options.first = options.single = true; | ||
if(opts.bail) { | ||
opts.first = opts.single = true; | ||
} | ||
// configure messages to use defaults where necessary | ||
messages = options.messages || this.messages(); | ||
options.messages = messages; | ||
series = Array.isArray(this.rules) ? this.rules : [this.rules]; | ||
@@ -93,3 +92,3 @@ series = series.map(function(rule) { | ||
rule.field = options.field || 'source'; | ||
rule.field = opts.field || 'source'; | ||
@@ -99,4 +98,4 @@ // handle transformation | ||
value = source = rule.transform(value); | ||
if(options._parent && rule.field) { | ||
options._parent[rule.field] = value; | ||
if(opts._parent && rule.field) { | ||
opts._parent[rule.field] = value; | ||
} | ||
@@ -111,3 +110,3 @@ } | ||
if(!rule.type && !options._deep) { | ||
if(!rule.type && !opts._deep) { | ||
rule.type = typeof(source); | ||
@@ -124,3 +123,3 @@ } | ||
rule.value = value; | ||
rule.source = options._source || source; | ||
rule.source = opts._source || source; | ||
@@ -151,5 +150,5 @@ if(typeof rule.test !== 'function') { | ||
// next transient variables | ||
if(options.vars) { | ||
for(k in options.vars) { | ||
vars[k] = options.vars[k]; | ||
if(opts.vars) { | ||
for(k in opts.vars) { | ||
vars[k] = opts.vars[k]; | ||
} | ||
@@ -165,3 +164,3 @@ } | ||
vars.state = state; | ||
vars.messages = options.messages; | ||
vars.messages = messages; | ||
@@ -190,7 +189,8 @@ validator = Rule(vars); | ||
var keys = options.keys || Object.keys(rule.fields); | ||
var keys = opts.keys || Object.keys(rule.fields); | ||
func(keys, function iterator(key, cb) { | ||
var opts = clone(options) | ||
// nested options for property iteration | ||
var options = clone(opts) | ||
, descriptor = rule.fields[key] | ||
@@ -201,3 +201,3 @@ , value = rule.value[key] | ||
// state object is by pointer | ||
opts.state = state; | ||
options.state = state; | ||
@@ -237,5 +237,3 @@ if(descriptor.type === 'array' | ||
var tmp = Rule({field: key, rule: descriptor, errors: errors}); | ||
tmp.raise( | ||
tmp.reasons.required, | ||
options.messages.required, key); | ||
tmp.raise(tmp.reasons.required, messages.required, key); | ||
return cb(); | ||
@@ -247,11 +245,11 @@ } | ||
opts._parent = rule.value; | ||
opts._deep = true; | ||
options._parent = rule.value; | ||
options._deep = true; | ||
// important to maintain original source for isRoot() | ||
opts._source = source; | ||
options._source = source; | ||
opts.field = key; | ||
options.field = key; | ||
schema.validate(value, opts, function(err, res) { | ||
schema.validate(value, options, function(err, res) { | ||
if(res && res.errors.length) { | ||
@@ -264,4 +262,4 @@ errors = errors.concat(res.errors); | ||
// bail on first error | ||
if(options.first && errors && errors.length) { | ||
return complete(err, errors, options, cb); | ||
if(opts.first && errors && errors.length) { | ||
return complete(err, errors, opts, cb); | ||
} | ||
@@ -277,3 +275,3 @@ callback(err, errors); | ||
}, function(err, results) { | ||
complete(err, results, options, cb); | ||
complete(err, results, opts, cb); | ||
}); | ||
@@ -290,3 +288,3 @@ } | ||
*/ | ||
function complete(err, results, options, callback) { | ||
function complete(err, results, opts, callback) { | ||
var i | ||
@@ -302,3 +300,3 @@ , field | ||
if(errors.length) { | ||
if(options.single) { | ||
if(opts.single) { | ||
errors = errors.slice(0,1); | ||
@@ -305,0 +303,0 @@ } |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.9.4", | ||
"version": "0.9.6", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -151,3 +151,3 @@ Table of Contents | ||
Muliple objects containing an `address` field that needs the same validation rules: | ||
Muliple objects containing an `address` field that need the same validation rules: | ||
@@ -273,4 +273,4 @@ ```javascript | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({address: {}}, function(err, res) { | ||
// res.errors contains errors for name, street, city, zip | ||
@@ -581,3 +581,3 @@ }); | ||
You may also use a function for the rule message, it is invoked in the scope of the [validator](#validator) and passed the original message and replacement parameters: | ||
You may also use a function for the rule message, it is invoked in the scope of the [Rule](#rule) and passed the original message and replacement parameters: | ||
@@ -656,7 +656,14 @@ ```javascript | ||
```javascript | ||
function Schema(descriptor, [opts]) | ||
function Schema(rules, [opts]) | ||
``` | ||
Encapsulates the rules associated with a descriptor and the logic for performing validation. | ||
Encapsulates the rules associated with a schema and the logic for performing validation. | ||
* `rules`: The schema rules. | ||
* `opts`: Configuration options. | ||
Options: | ||
* `messages`: An alternative messages object for the schema. | ||
##### messages | ||
@@ -673,3 +680,3 @@ | ||
```javascript | ||
function validate(source, [options], cb) | ||
function validate(source, [opts], cb) | ||
``` | ||
@@ -680,3 +687,3 @@ | ||
* `source`: The object to validate. | ||
* `options`: An object describing processing options for the validation. | ||
* `opts`: Map of processing options for the validation. | ||
* `cb`: Callback function to invoke when validation completes. | ||
@@ -689,2 +696,3 @@ | ||
* `bail`: Shorthand for `single` and `first`. | ||
* `messages`: Overrides the schema messages. | ||
* `parallel`: A boolean indicating that the validation should be executed in parallel. | ||
@@ -691,0 +699,0 @@ * `field`: Field name for the source object, default is `source` when not specified. |
Sorry, the diff of this file is not supported yet
183048
0.1%871
0.93%3757
-0.08%