async-validate
Advanced tools
Comparing version 0.12.2 to 0.12.3
@@ -142,2 +142,3 @@ ### API | ||
* `key`: Unique key for the error, eg: `address.name`. | ||
* `field`: The name of the property that failed validation. | ||
@@ -144,0 +145,0 @@ * `value`: The value of the property. |
@@ -219,3 +219,3 @@ ## Guide | ||
* `type <string|function>`: Type identifier or constructor function. | ||
* `type <string|function|array>`: Type identifier, constructor function or list of types. | ||
@@ -222,0 +222,0 @@ The `type` property indicates the type of rule to use, a type corresponds to a plugin function and the plugin should have been loaded. |
@@ -22,4 +22,4 @@ var Schema = require('async-validate') | ||
// validation failed, res.errors is an array of all errors | ||
// res.fields is a map keyed by field name with an array of | ||
// errors per field | ||
// res.fields is a map keyed by field unique id (eg: `address.name`) | ||
// assigned an array of errors per field | ||
return console.dir(res.errors) | ||
@@ -26,0 +26,0 @@ } |
202
EXAMPLE.md
@@ -87,4 +87,20 @@ Table of Contents | ||
``` | ||
[ { [Error: extraneous fields (id, name) found in root] field: 'root', reason: { id: 'additional' } }, | ||
{ [Error: extraneous fields (name) found in address] field: 'address', reason: { id: 'additional' } } ] | ||
[ { [Error: extraneous fields (id, name) found in root] | ||
field: 'root', | ||
value: { id: 'unknown-field', name: 'unknown-field', address: [Object] }, | ||
parent: { id: 'unknown-field', name: 'unknown-field', address: [Object] }, | ||
names: [], | ||
key: 'root', | ||
reason: { id: 'additional' } }, | ||
{ [Error: extraneous fields (name) found in address] | ||
field: 'address', | ||
value: | ||
{ name: 'unknown-field', | ||
street: 'Mock St', | ||
city: 'Mock City', | ||
zip: '12345678' }, | ||
parent: { id: 'unknown-field', name: 'unknown-field', address: [Object] }, | ||
names: [ 'address' ], | ||
key: 'address', | ||
reason: { id: 'additional' } } ] | ||
``` | ||
@@ -130,3 +146,9 @@ | ||
``` | ||
[ { [Error: id expects foo, got qux] field: 'id', reason: { id: 'unexpected-id' } } ] | ||
[ { [Error: id expects foo, got qux] | ||
field: 'id', | ||
value: 'qux', | ||
parent: { id: 'qux' }, | ||
names: [ 'id' ], | ||
key: 'id', | ||
reason: { id: 'unexpected-id' } } ] | ||
``` | ||
@@ -168,3 +190,9 @@ | ||
``` | ||
[ { [Error: street is required] field: 'street', reason: { id: 'required' } } ] | ||
[ { [Error: street is required] | ||
field: 'street', | ||
value: undefined, | ||
parent: undefined, | ||
names: [ 'address' ], | ||
key: 'address.street', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -205,3 +233,9 @@ | ||
``` | ||
[ { [Error: zip is required] field: 'zip', reason: { id: 'required' } } ] | ||
[ { [Error: zip is required] | ||
field: 'zip', | ||
value: undefined, | ||
parent: undefined, | ||
names: [ 'address' ], | ||
key: 'address.zip', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -240,3 +274,8 @@ | ||
``` | ||
[ { [Error: foo is a reserved id] field: 'id' } ] | ||
[ { [Error: foo is a reserved id] | ||
field: 'id', | ||
value: 'foo', | ||
parent: { id: 'foo' }, | ||
names: [ 'id' ], | ||
key: 'id' } ] | ||
``` | ||
@@ -270,3 +309,9 @@ | ||
``` | ||
[ { [Error: comp is not an instance of Component] field: 'comp', reason: { id: 'instanceof' } } ] | ||
[ { [Error: comp is not an instance of Component] | ||
field: 'comp', | ||
value: {}, | ||
parent: { comp: {} }, | ||
names: [ 'comp' ], | ||
key: 'comp', | ||
reason: { id: 'instanceof' } } ] | ||
``` | ||
@@ -299,3 +344,9 @@ | ||
``` | ||
[ { [Error: func must have exactly 1 arguments] field: 'func', reason: { id: 'length' } } ] | ||
[ { [Error: func must have exactly 1 arguments] | ||
field: 'func', | ||
value: [Function: noop], | ||
parent: { func: [Function: noop] }, | ||
names: [ 'func' ], | ||
key: 'func', | ||
reason: { id: 'length' } } ] | ||
``` | ||
@@ -332,3 +383,9 @@ | ||
``` | ||
[ { [Error: address3 is not a string] field: 'address3', reason: { id: 'type' } } ] | ||
[ { [Error: address3 is not a string] | ||
field: 'address3', | ||
value: false, | ||
parent: { address1: 'foo', address2: 'bar', address3: false }, | ||
names: [ 'address3' ], | ||
key: 'address3', | ||
reason: { id: 'type' } } ] | ||
``` | ||
@@ -366,3 +423,9 @@ | ||
``` | ||
[ { [Error: func cannot have more than 1 arguments] field: 'func', reason: { id: 'max' } } ] | ||
[ { [Error: func cannot have more than 1 arguments] | ||
field: 'func', | ||
value: [Function: noop], | ||
parent: { func: [Function: noop] }, | ||
names: [ 'func' ], | ||
key: 'func', | ||
reason: { id: 'max' } } ] | ||
``` | ||
@@ -403,3 +466,9 @@ | ||
``` | ||
[ { [Error: name is a required field] field: 'name', reason: { id: 'required' } } ] | ||
[ { [Error: name is a required field] | ||
field: 'name', | ||
value: undefined, | ||
parent: undefined, | ||
names: [], | ||
key: 'name', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -439,3 +508,9 @@ | ||
``` | ||
[ { [Error: name must be specified (field: name)] field: 'name', reason: { id: 'required' } } ] | ||
[ { [Error: name must be specified (field: name)] | ||
field: 'name', | ||
value: undefined, | ||
parent: undefined, | ||
names: [], | ||
key: 'name', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -475,3 +550,9 @@ | ||
``` | ||
[ { [Error: name is a required field] field: 'name', reason: { id: 'required' } } ] | ||
[ { [Error: name is a required field] | ||
field: 'name', | ||
value: undefined, | ||
parent: undefined, | ||
names: [], | ||
key: 'name', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -508,3 +589,9 @@ | ||
``` | ||
[ { [Error: name must be specified] field: 'name', reason: { id: 'required' } } ] | ||
[ { [Error: name must be specified] | ||
field: 'name', | ||
value: undefined, | ||
parent: undefined, | ||
names: [], | ||
key: 'name', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -537,3 +624,9 @@ | ||
``` | ||
[ { [Error: func must have at least 1 arguments] field: 'func', reason: { id: 'min' } } ] | ||
[ { [Error: func must have at least 1 arguments] | ||
field: 'func', | ||
value: [Function: noop], | ||
parent: { func: [Function: noop] }, | ||
names: [ 'func' ], | ||
key: 'func', | ||
reason: { id: 'min' } } ] | ||
``` | ||
@@ -577,3 +670,9 @@ | ||
``` | ||
[ { [Error: id foo does not exist] field: 'id', reason: { id: 'missing-id' } } ] | ||
[ { [Error: id foo does not exist] | ||
field: 'id', | ||
value: 'foo', | ||
parent: { id: 'foo' }, | ||
names: [ 'id' ], | ||
key: 'id', | ||
reason: { id: 'missing-id' } } ] | ||
``` | ||
@@ -606,3 +705,9 @@ | ||
``` | ||
[ { [Error: flag is not one of the allowed types boolean, Boolean] field: 'flag', reason: { id: 'type' } } ] | ||
[ { [Error: flag is not one of the allowed types boolean, Boolean] | ||
field: 'flag', | ||
value: 'foo', | ||
parent: { flag: 'foo' }, | ||
names: [ 'flag' ], | ||
key: 'flag', | ||
reason: { id: 'type' } } ] | ||
``` | ||
@@ -635,3 +740,9 @@ | ||
``` | ||
[ { [Error: name value -name does not match pattern /^[a-z0-9]+$/i] field: 'name', reason: { id: 'pattern' } } ] | ||
[ { [Error: name value -name does not match pattern /^[a-z0-9]+$/i] | ||
field: 'name', | ||
value: '-name', | ||
parent: { name: '-name' }, | ||
names: [ 'name' ], | ||
key: 'name', | ||
reason: { id: 'pattern' } } ] | ||
``` | ||
@@ -713,3 +824,9 @@ | ||
``` | ||
[ { [Error: invalid id -foo] field: 'id', reason: { id: 'id' } } ] | ||
[ { [Error: invalid id -foo] | ||
field: 'id', | ||
value: '-foo', | ||
parent: { id: '-foo' }, | ||
names: [ 'id' ], | ||
key: 'id', | ||
reason: { id: 'id' } } ] | ||
``` | ||
@@ -748,3 +865,9 @@ | ||
``` | ||
[ { [Error: func must have arguments length between 1 and 2] field: 'func', reason: { id: 'max' } } ] | ||
[ { [Error: func must have arguments length between 1 and 2] | ||
field: 'func', | ||
value: [Function: noop], | ||
parent: { func: [Function: noop] }, | ||
names: [ 'func' ], | ||
key: 'func', | ||
reason: { id: 'max' } } ] | ||
``` | ||
@@ -777,3 +900,9 @@ | ||
``` | ||
[ { [Error: name is required] field: 'name', reason: { id: 'required' } } ] | ||
[ { [Error: name is required] | ||
field: 'name', | ||
value: undefined, | ||
parent: undefined, | ||
names: [], | ||
key: 'name', | ||
reason: { id: 'required' } } ] | ||
``` | ||
@@ -801,3 +930,9 @@ | ||
``` | ||
[ { [Error: source is not an object] field: 'source', reason: { id: 'type' } } ] | ||
[ { [Error: source is not an object] | ||
field: 'source', | ||
value: 'foo', | ||
parent: 'foo', | ||
names: [], | ||
key: 'source', | ||
reason: { id: 'type' } } ] | ||
``` | ||
@@ -859,3 +994,8 @@ | ||
``` | ||
[ { [Error: email: could not resolve dns for domain 1442566773189.com] field: 'email' } ] | ||
[ { [Error: email: could not resolve dns for domain 1442714024700.com] | ||
field: 'email', | ||
value: 'foo@1442714024700.com', | ||
parent: { email: 'foo@1442714024700.com' }, | ||
names: [ 'email' ], | ||
key: 'email' } ] | ||
``` | ||
@@ -888,3 +1028,9 @@ | ||
``` | ||
[ { [Error: flag is not a boolean] field: 'flag', reason: { id: 'type' } } ] | ||
[ { [Error: flag is not a boolean] | ||
field: 'flag', | ||
value: 'foo', | ||
parent: { flag: 'foo' }, | ||
names: [ 'flag' ], | ||
key: 'flag', | ||
reason: { id: 'type' } } ] | ||
``` | ||
@@ -917,3 +1063,9 @@ | ||
``` | ||
[ { [Error: name cannot be empty] field: 'name', reason: { id: 'whitespace' } } ] | ||
[ { [Error: name cannot be empty] | ||
field: 'name', | ||
value: ' ', | ||
parent: { name: ' ' }, | ||
names: [ 'name' ], | ||
key: 'name', | ||
reason: { id: 'whitespace' } } ] | ||
``` | ||
@@ -920,0 +1072,0 @@ |
@@ -82,3 +82,13 @@ var plugin = require('zephyr') | ||
err.parent = this.parent; | ||
err.names = this.names; | ||
err.key = this.key || err.field; | ||
//console.dir('raising error with names: ' + this.names); | ||
if(!this.key && this.names && this.names.length) { | ||
err.key = this.names.join('.'); | ||
} | ||
//console.dir(err.key); | ||
if(reason) { | ||
@@ -85,0 +95,0 @@ err.reason = reason; |
@@ -56,7 +56,2 @@ var iterator = require('./iterator') | ||
this.messages(opts.messages || require('../messages')); | ||
// do not clone rules when deep is set | ||
// initial clone is a deep clone | ||
rules = opts._deep ? rules : clone(rules); | ||
this.rules = rules; | ||
@@ -130,2 +125,4 @@ } | ||
this.rules = opts._deep ? this.rules : clone(this.rules); | ||
series = Array.isArray(this.rules) ? this.rules : [this.rules]; | ||
@@ -174,2 +171,8 @@ | ||
rule.names = opts._names || []; | ||
if(rule.field && opts._deep) { | ||
rule.names = rule.names.concat(rule.field); | ||
} | ||
rule.parent = opts.parent || source; | ||
@@ -218,2 +221,3 @@ rule.value = value; | ||
vars.source = rule.source; | ||
vars.names = rule.names; | ||
vars.errors = []; | ||
@@ -289,2 +293,4 @@ vars.state = state; | ||
rule: descriptor, | ||
names: rule.names, | ||
key: rule.names.concat(key).join('.'), | ||
errors: scope.errors | ||
@@ -296,13 +302,11 @@ }); | ||
schema = new Schema( | ||
descriptor, {messages: options.messages, _deep: true}); | ||
schema = new Schema(descriptor, {messages: options.messages}); | ||
options.field = key; | ||
options.parent = rule.value; | ||
options._deep = true; | ||
// important to maintain original source for isRoot() | ||
options._source = source; | ||
options._names = rule.names; | ||
options._deep = true; | ||
options.field = key; | ||
schema.validate(value, options, function(err, res) { | ||
@@ -355,3 +359,3 @@ if(res && res.errors.length) { | ||
for(i = 0;i < errors.length;i++) { | ||
field = errors[i].field; | ||
field = errors[i].key; | ||
fields[field] = fields[field] || []; | ||
@@ -358,0 +362,0 @@ fields[field].push(errors[i]); |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -116,4 +116,4 @@ Table of Contents | ||
// validation failed, res.errors is an array of all errors | ||
// res.fields is a map keyed by field name with an array of | ||
// errors per field | ||
// res.fields is a map keyed by field unique id (eg: `address.name`) | ||
// assigned an array of errors per field | ||
return console.dir(res.errors) | ||
@@ -343,3 +343,3 @@ } | ||
* `type <string|function>`: Type identifier or constructor function. | ||
* `type <string|function|array>`: Type identifier, constructor function or list of types. | ||
@@ -914,2 +914,3 @@ The `type` property indicates the type of rule to use, a type corresponds to a plugin function and the plugin should have been loaded. | ||
* `key`: Unique key for the error, eg: `address.name`. | ||
* `field`: The name of the property that failed validation. | ||
@@ -916,0 +917,0 @@ * `value`: The value of the property. |
@@ -19,3 +19,3 @@ // schema for a schema definition | ||
type: { | ||
type: ['string', 'function'], | ||
type: ['string', 'function', 'array'], | ||
required: true | ||
@@ -22,0 +22,0 @@ }, |
211201
188
4455
1021