async-validate
Advanced tools
Comparing version 0.4.12 to 0.4.13
@@ -27,10 +27,10 @@ ### API | ||
* `source`: The object to validate (required). | ||
* `options`: An object describing processing options for the validation (optional). | ||
* `cb`: Callback function to invoke when validation completes (required). | ||
* `source`: The object to validate. | ||
* `options`: An object describing processing options for the validation. | ||
* `cb`: Callback function to invoke when validation completes. | ||
Options: | ||
* `first`: Invoke `callback` when the first validation rule generates an error, no more validation rules are processed. If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option. | ||
* `single`: Only ever return a single error. Typically used in conjunction with `first` when a validation rule could generate multiple errors. | ||
* `first`: Invoke callback when the first validation rule generates an error, no more validation rules are processed. | ||
* `single`: Only ever return a single error, typically used in conjunction with `first` when a validation rule could generate multiple errors. | ||
* `keys`: Specifies the keys on the source object to be validated. Use this option to validate fields in a determinate order or to validate a subset of the rules assigned to a schema. | ||
@@ -75,3 +75,3 @@ * `parallel`: A boolean indicating that the validation should be executed in parallel. | ||
* `rule`: The validation rule in the source descriptor. | ||
* `rule`: The validation rule in the schema descriptor. | ||
* `value`: The value of the source object property being validated. | ||
@@ -81,3 +81,3 @@ * `field`: The name of the field being validated. | ||
* `options`: The options passed to `validate()`. | ||
* `messages`: Reference to the messages assigned to `options`. | ||
* `messages`: Reference to the schema messages. | ||
* `errors`: Array of errors for the field validation. | ||
@@ -84,0 +84,0 @@ * `reasons`: Map of default error reasons. |
@@ -9,3 +9,3 @@ ## Guide | ||
Rules are functions that perform validation of a value. They are invoked in the scope of a validator ([file](/lib/validator.js), [api docs](#validator)). | ||
Rules are functions that perform validation of a value, they are invoked in the scope of a validator ([file](/lib/validator.js), [api docs](#validator)). | ||
@@ -48,3 +48,3 @@ A rule function can access all relevant properties and methods using `this` and should [raise](#raise) an error if `this.value` fails a validation test, see [errors](#errors). | ||
Plugin function that assigns the rule function as a static method. | ||
Plugin that assigns the rule function as a static method. | ||
@@ -75,2 +75,85 @@ Create a plugin module: | ||
#### Multiple Rules | ||
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example: | ||
```javascript | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
function(cb) { | ||
// test if email address (this.value) already exists | ||
// in a database and call this.raise() if it does | ||
cb(); | ||
} | ||
] | ||
} | ||
``` | ||
#### Deep Rules | ||
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule. | ||
```javascript | ||
var descriptor = { | ||
name: {type: "string", required: true}, | ||
address: { | ||
type: "object", | ||
required: true, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// errors for name, street, city, zip | ||
}); | ||
``` | ||
Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against. | ||
Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method. | ||
```javascript | ||
var descriptor = { | ||
name: {type: "string", required: true}, | ||
address: { | ||
type: "object", | ||
required: true, | ||
options: {single: true, first: true}, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// now only errors for name and street | ||
}); | ||
``` | ||
The parent rule is also validated so if you have a set of rules such as: | ||
```javascript | ||
var descriptor = { | ||
roles: { | ||
type: "array", | ||
required: true, | ||
len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
} | ||
``` | ||
And supply a source object of `{roles: ["admin", "user"]}` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2. | ||
### Errors | ||
@@ -133,3 +216,3 @@ | ||
#### Type Constants | ||
#### Type Identifier | ||
@@ -154,2 +237,10 @@ The `type` rule property indicates the type of validator to use, a type corresponds to a plugin function and the plugin should have been loaded. | ||
#### Additional | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
#### Fields | ||
Rules of the `object` and `array` type may declare a `fields` object which declares a nested descriptor, see [deep rules](#deep-rules). | ||
#### Message | ||
@@ -165,3 +256,3 @@ | ||
The `pattern` rule property indicates a regular expression that the value must match to pass validation. | ||
The `pattern` rule property is a regular expression that the value must match to pass validation. | ||
@@ -226,78 +317,1 @@ #### Range | ||
#### Multiple Rules | ||
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example: | ||
```javascript | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
function(cb) { | ||
// test if email address already exists in a database | ||
// and add a validation error to the errors array if it does | ||
cb(); | ||
} | ||
] | ||
} | ||
``` | ||
#### Deep Rules | ||
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule. | ||
```javascript | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
}, | ||
name: {type: "string", required: true} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({ address: {} }, function(errors, fields) { | ||
// errors for street, city, zip and name | ||
}); | ||
``` | ||
Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against. | ||
Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method. | ||
```javascript | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, options: {single: true, first: true}, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
}, | ||
name: {type: "string", required: true} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({ address: {} }, function(errors, fields) { | ||
// now only errors for street and name | ||
}); | ||
``` | ||
The parent rule is also validated so if you have a set of rules such as: | ||
```javascript | ||
var descriptor = { | ||
roles: { | ||
type: "array", required: true, len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
} | ||
``` | ||
And supply a source object of `{roles: ["admin", "user"]}` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2. |
@@ -11,3 +11,3 @@ ### Messages | ||
You may also use a function for the rule message, it is invoked in the scope of the rule 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 [validator](#validator) and passed the original message and replacement parameters: | ||
@@ -29,5 +29,8 @@ ```javascript | ||
```javascript | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages'); | ||
messages.required = "%s is a required field"; // change the message | ||
var Schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, schema; | ||
messages.required = "%s is a required field"; | ||
schema = new Schema(descriptor, {messages: messages}); | ||
``` | ||
@@ -40,21 +43,8 @@ | ||
```javascript | ||
var schema = require('async-validate'); | ||
var es = require('messages-es'); | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); | ||
var Schema = require('async-validate') | ||
, messages = require('messages-es') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, schema = new Schema(descriptor, {messages: messages}); | ||
``` | ||
Or you could clone a default messages instance and then assign language specific messages to the schema using the `messages` method. | ||
```javascript | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
var es = schema.clone(messages); | ||
es.required = "%s es un campo obligatorio"; // change the message | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); // ensure this schema uses the altered messages | ||
``` | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the validation function. | ||
If you are defining your own rule functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the function. |
@@ -7,79 +7,11 @@ var iterator = require('./iterator') | ||
/** | ||
* Clone helper function. | ||
*/ | ||
function clone(source, target) { | ||
var k | ||
, v; | ||
function isComplex(obj) { | ||
return Array.isArray(obj) | ||
|| (obj && typeof obj === 'object') && !(obj instanceof RegExp); | ||
} | ||
// simple source object | ||
if(!isComplex(source)) { | ||
return source; | ||
} | ||
target = target || (Array.isArray(source) ? [] : {}); | ||
for(k in source) { | ||
v = source[k]; | ||
if(isComplex(v)) { | ||
target[k] = Array.isArray(v) ? [] : {}; | ||
clone(v, target[k]); | ||
}else{ | ||
target[k] = v; | ||
} | ||
} | ||
return target; | ||
} | ||
/** | ||
* @private | ||
*/ | ||
function defineProp(target, prop, value) { | ||
Object.defineProperty(target, prop, { | ||
value: value, | ||
enumerable: false | ||
}) | ||
} | ||
/** | ||
* Encapsulates a validation schema. | ||
* | ||
* @param descriptor Validation rules for this schema. | ||
* @param rules Validation rules for this schema. | ||
* @param opts Options for the schema. | ||
*/ | ||
function Schema(descriptor, opts) { | ||
function Schema(rules, opts) { | ||
opts = opts || {}; | ||
this.messages(opts.messages || require('../messages')); | ||
this.define(descriptor); | ||
} | ||
/** | ||
* Get or set the messages used for this schema. | ||
* | ||
* @param messages The validation messages. | ||
* | ||
* @return The validation messages. | ||
*/ | ||
function messages(messages) { | ||
if(messages !== undefined) { | ||
this._messages = messages; | ||
} | ||
return this._messages; | ||
} | ||
/** | ||
* Define rules on this schema. | ||
* | ||
* If rules are already defined new rules will overwrite or add to the | ||
* previously defined rules. | ||
* | ||
* @param rules The schema rules. | ||
*/ | ||
function define(rules) { | ||
if(!rules) { | ||
@@ -106,2 +38,16 @@ throw new Error('Cannot configure a schema with no rules'); | ||
/** | ||
* Get or set the messages used for this schema. | ||
* | ||
* @param messages The validation messages. | ||
* | ||
* @return The validation messages. | ||
*/ | ||
function messages(messages) { | ||
if(messages !== undefined) { | ||
this._messages = messages; | ||
} | ||
return this._messages; | ||
} | ||
/** | ||
* Collates the errors arrays and maps field names to errors | ||
@@ -191,4 +137,4 @@ * specific to the field. | ||
rule.field = options.field || 'source'; | ||
rule.type = this.getType(rule); | ||
rule.validator = this.getValidationMethod(rule); | ||
rule.type = getType(rule); | ||
rule.validator = getValidationMethod(rule); | ||
rule.keys = this.keys; | ||
@@ -199,3 +145,7 @@ rule.value = source; | ||
} | ||
defineProp(options, '_root', true); | ||
Object.defineProperty(options, '_root', { | ||
value: true, | ||
enumerable: false | ||
}) | ||
} | ||
@@ -224,4 +174,4 @@ | ||
rule.field = z; | ||
rule.type = this.getType(rule); | ||
rule.validator = this.getValidationMethod(rule); | ||
rule.type = getType(rule); | ||
rule.validator = getValidationMethod(rule); | ||
rule.value = value; | ||
@@ -338,5 +288,4 @@ rule.source = source; | ||
// validator plugin functions are static methods | ||
var validators = Validator; | ||
if(typeof(rule.validator) !== 'function' | ||
&& (!rule.type || !validators.hasOwnProperty(rule.type))) { | ||
&& (!rule.type || !Validator.hasOwnProperty(rule.type))) { | ||
throw new Error(format('Unknown rule type %s', rule.type)); | ||
@@ -353,18 +302,44 @@ } | ||
function getValidationMethod(rule) { | ||
// validator plugin functions are static methods | ||
var validators = Validator; | ||
if(typeof rule.validator === 'function') { | ||
return rule.validator; | ||
} | ||
return validators[rule.type]; | ||
// validator plugin functions are static methods | ||
return Validator[rule.type]; | ||
} | ||
/** | ||
* Clone helper function. | ||
*/ | ||
function clone(source, target) { | ||
var k | ||
, v; | ||
function isComplex(obj) { | ||
return Array.isArray(obj) | ||
|| (obj && typeof obj === 'object') && !(obj instanceof RegExp); | ||
} | ||
// simple source object | ||
if(!isComplex(source)) { | ||
return source; | ||
} | ||
target = target || (Array.isArray(source) ? [] : {}); | ||
for(k in source) { | ||
v = source[k]; | ||
if(isComplex(v)) { | ||
target[k] = Array.isArray(v) ? [] : {}; | ||
clone(v, target[k]); | ||
}else{ | ||
target[k] = v; | ||
} | ||
} | ||
return target; | ||
} | ||
Schema.prototype.messages = messages; | ||
Schema.prototype.validate = validate; | ||
// private | ||
Schema.prototype.define = define; | ||
Schema.prototype.getType = getType; | ||
Schema.prototype.getValidationMethod = getValidationMethod; | ||
// static | ||
@@ -371,0 +346,0 @@ Schema.clone = clone; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.4.12", | ||
"version": "0.4.13", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
233
README.md
@@ -12,6 +12,10 @@ Table of Contents | ||
* [Plugin Rule](#plugin-rule) | ||
* [Multiple Rules](#multiple-rules) | ||
* [Deep Rules](#deep-rules) | ||
* [Errors](#errors) | ||
* [Plugins](#plugins) | ||
* [Descriptor](#descriptor) | ||
* [Type Constants](#type-constants) | ||
* [Type Identifier](#type-identifier) | ||
* [Additional](#additional) | ||
* [Fields](#fields) | ||
* [Message](#message) | ||
@@ -26,4 +30,2 @@ * [Required](#required) | ||
* [Whitespace](#whitespace) | ||
* [Multiple Rules](#multiple-rules) | ||
* [Deep Rules](#deep-rules) | ||
* [Messages](#messages) | ||
@@ -101,3 +103,3 @@ * [Transform](#transform) | ||
Rules are functions that perform validation of a value. They are invoked in the scope of a validator ([file](https://github.com/freeformsystems/async-validate/blob/master/lib/validator.js), [api docs](#validator)). | ||
Rules are functions that perform validation of a value, they are invoked in the scope of a validator ([file](https://github.com/freeformsystems/async-validate/blob/master/lib/validator.js), [api docs](#validator)). | ||
@@ -140,3 +142,3 @@ A rule function can access all relevant properties and methods using `this` and should [raise](#raise) an error if `this.value` fails a validation test, see [errors](#errors). | ||
Plugin function that assigns the rule function as a static method. | ||
Plugin that assigns the rule function as a static method. | ||
@@ -167,2 +169,85 @@ Create a plugin module: | ||
#### Multiple Rules | ||
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example: | ||
```javascript | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
function(cb) { | ||
// test if email address (this.value) already exists | ||
// in a database and call this.raise() if it does | ||
cb(); | ||
} | ||
] | ||
} | ||
``` | ||
#### Deep Rules | ||
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule. | ||
```javascript | ||
var descriptor = { | ||
name: {type: "string", required: true}, | ||
address: { | ||
type: "object", | ||
required: true, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// errors for name, street, city, zip | ||
}); | ||
``` | ||
Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against. | ||
Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method. | ||
```javascript | ||
var descriptor = { | ||
name: {type: "string", required: true}, | ||
address: { | ||
type: "object", | ||
required: true, | ||
options: {single: true, first: true}, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// now only errors for name and street | ||
}); | ||
``` | ||
The parent rule is also validated so if you have a set of rules such as: | ||
```javascript | ||
var descriptor = { | ||
roles: { | ||
type: "array", | ||
required: true, | ||
len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
} | ||
``` | ||
And supply a source object of `{roles: ["admin", "user"]}` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2. | ||
### Errors | ||
@@ -225,3 +310,3 @@ | ||
#### Type Constants | ||
#### Type Identifier | ||
@@ -246,2 +331,10 @@ The `type` rule property indicates the type of validator to use, a type corresponds to a plugin function and the plugin should have been loaded. | ||
#### Additional | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
#### Fields | ||
Rules of the `object` and `array` type may declare a `fields` object which declares a nested descriptor, see [deep rules](#deep-rules). | ||
#### Message | ||
@@ -257,3 +350,3 @@ | ||
The `pattern` rule property indicates a regular expression that the value must match to pass validation. | ||
The `pattern` rule property is a regular expression that the value must match to pass validation. | ||
@@ -318,80 +411,2 @@ #### Range | ||
#### Multiple Rules | ||
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example: | ||
```javascript | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
function(cb) { | ||
// test if email address already exists in a database | ||
// and add a validation error to the errors array if it does | ||
cb(); | ||
} | ||
] | ||
} | ||
``` | ||
#### Deep Rules | ||
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule. | ||
```javascript | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
}, | ||
name: {type: "string", required: true} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({ address: {} }, function(errors, fields) { | ||
// errors for street, city, zip and name | ||
}); | ||
``` | ||
Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against. | ||
Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method. | ||
```javascript | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, options: {single: true, first: true}, | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "invalid zip"} | ||
} | ||
}, | ||
name: {type: "string", required: true} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({ address: {} }, function(errors, fields) { | ||
// now only errors for street and name | ||
}); | ||
``` | ||
The parent rule is also validated so if you have a set of rules such as: | ||
```javascript | ||
var descriptor = { | ||
roles: { | ||
type: "array", required: true, len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
} | ||
``` | ||
And supply a source object of `{roles: ["admin", "user"]}` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2. | ||
### Messages | ||
@@ -407,3 +422,3 @@ | ||
You may also use a function for the rule message, it is invoked in the scope of the rule 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 [validator](#validator) and passed the original message and replacement parameters: | ||
@@ -425,5 +440,8 @@ ```javascript | ||
```javascript | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages'); | ||
messages.required = "%s is a required field"; // change the message | ||
var Schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, schema; | ||
messages.required = "%s is a required field"; | ||
schema = new Schema(descriptor, {messages: messages}); | ||
``` | ||
@@ -436,23 +454,10 @@ | ||
```javascript | ||
var schema = require('async-validate'); | ||
var es = require('messages-es'); | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); | ||
var Schema = require('async-validate') | ||
, messages = require('messages-es') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, schema = new Schema(descriptor, {messages: messages}); | ||
``` | ||
Or you could clone a default messages instance and then assign language specific messages to the schema using the `messages` method. | ||
If you are defining your own rule functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the function. | ||
```javascript | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
var es = schema.clone(messages); | ||
es.required = "%s es un campo obligatorio"; // change the message | ||
var descriptor = {name:{type: "string", required: true}}; | ||
var validator = new schema(descriptor); | ||
validator.messages(es); // ensure this schema uses the altered messages | ||
``` | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the validation function. | ||
### Transform | ||
@@ -522,10 +527,10 @@ | ||
* `source`: The object to validate (required). | ||
* `options`: An object describing processing options for the validation (optional). | ||
* `cb`: Callback function to invoke when validation completes (required). | ||
* `source`: The object to validate. | ||
* `options`: An object describing processing options for the validation. | ||
* `cb`: Callback function to invoke when validation completes. | ||
Options: | ||
* `first`: Invoke `callback` when the first validation rule generates an error, no more validation rules are processed. If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option. | ||
* `single`: Only ever return a single error. Typically used in conjunction with `first` when a validation rule could generate multiple errors. | ||
* `first`: Invoke callback when the first validation rule generates an error, no more validation rules are processed. | ||
* `single`: Only ever return a single error, typically used in conjunction with `first` when a validation rule could generate multiple errors. | ||
* `keys`: Specifies the keys on the source object to be validated. Use this option to validate fields in a determinate order or to validate a subset of the rules assigned to a schema. | ||
@@ -570,3 +575,3 @@ * `parallel`: A boolean indicating that the validation should be executed in parallel. | ||
* `rule`: The validation rule in the source descriptor. | ||
* `rule`: The validation rule in the schema descriptor. | ||
* `value`: The value of the source object property being validated. | ||
@@ -576,3 +581,3 @@ * `field`: The name of the field being validated. | ||
* `options`: The options passed to `validate()`. | ||
* `messages`: Reference to the messages assigned to `options`. | ||
* `messages`: Reference to the schema messages. | ||
* `errors`: Array of errors for the field validation. | ||
@@ -579,0 +584,0 @@ * `reasons`: Map of default error reasons. |
@@ -1,4 +0,3 @@ | ||
var util = require('util'); | ||
var assert = require('chai').assert; | ||
var schema = require('../../index'); | ||
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
@@ -19,4 +18,4 @@ describe("async-validate:", function() { | ||
validator.validate({id: "-hyphen"}, function(errors, fields) { | ||
assert.equal(errors.length, 1); | ||
assert.equal(errors[0].message, "id is not a valid identifier"); | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("id is not a valid identifier"); | ||
done(); | ||
@@ -32,4 +31,3 @@ }); | ||
validator.validate({id: "my-valid-id"}, function(errors, fields) { | ||
assert.isNull(errors); | ||
assert.isNull(fields); | ||
expect(errors).to.eql(null); | ||
done(); | ||
@@ -36,0 +34,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
717
126156
2714