async-validate
Advanced tools
Comparing version 0.9.3 to 0.9.4
@@ -23,2 +23,34 @@ ## Guide | ||
#### Composite Definition | ||
To share common fields across different schemas move them to a module and require them. | ||
Module to represent an `address` field: | ||
```javascript | ||
module.exports = { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
``` | ||
Muliple objects containing an `address` field that needs the same validation rules: | ||
```javascript | ||
var address = require('./address') | ||
, user = { | ||
type: 'object', | ||
address: address | ||
} | ||
, invoice = { | ||
type: 'object', | ||
bill: { | ||
type: 'object', | ||
address: address | ||
} | ||
} | ||
``` | ||
### Rules | ||
@@ -51,3 +83,3 @@ | ||
Assigned to the `validator` field so that you may pass data from the rule to the function: | ||
Assigned to the `test` field so that you may pass data from the rule to the function: | ||
@@ -282,4 +314,3 @@ ```javascript | ||
this.raise( | ||
this.reason( | ||
'id', {description: 'Field value failed pattern match'}), | ||
this.reason('id', {level: 'warn'}), | ||
'%s is not a valid id', | ||
@@ -292,3 +323,3 @@ this.field); | ||
Adding a reason allows associating an identifier with an error and optional meta data about the error. | ||
Adding a reason allows associating an identifier with an error and optional meta data about the error which can be useful if you need to associate a *severity* with errors to distinguish between error types. | ||
@@ -295,0 +326,0 @@ To signal that an internal processing error has occured pass an Error to the callback, for example: |
@@ -14,3 +14,5 @@ Table of Contents | ||
* [max](#max) | ||
* [message-clone](#message-clone) | ||
* [message-function](#message-function) | ||
* [message-override](#message-override) | ||
* [message](#message) | ||
@@ -294,2 +296,35 @@ * [min](#min) | ||
#### message-clone | ||
* [doc/example/message-clone](https://github.com/freeformsystems/async-validate/blob/master/doc/example/message-clone.js). | ||
```javascript | ||
// clone default messages | ||
var Schema = require('async-validate') | ||
, messages = Schema.clone(require('async-validate/messages')) | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
, source = {} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
// change message in place | ||
messages.required = '%s is a required field'; | ||
// pass messages as constructor option | ||
schema = new Schema(descriptor, {messages: messages}); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: name is a required field] field: 'name', reason: { id: 'required' } } ] | ||
``` | ||
#### message-function | ||
@@ -327,2 +362,34 @@ | ||
#### message-override | ||
* [doc/example/message-override](https://github.com/freeformsystems/async-validate/blob/master/doc/example/message-override.js). | ||
```javascript | ||
// override default error message | ||
var Schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
, source = {} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
// change default message in place | ||
messages.required = '%s is a required field'; | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: name is a required field] field: 'name', reason: { id: 'required' } } ] | ||
``` | ||
#### message | ||
@@ -608,3 +675,3 @@ | ||
``` | ||
[ { [Error: email: could not resolve dns for domain 1442378343796.com] field: 'email' } ] | ||
[ { [Error: email: could not resolve dns for domain 1442381544255.com] field: 'email' } ] | ||
``` | ||
@@ -611,0 +678,0 @@ |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -12,2 +12,3 @@ Table of Contents | ||
* [Function Definition](#function-definition) | ||
* [Composite Definition](#composite-definition) | ||
* [Rules](#rules) | ||
@@ -136,2 +137,34 @@ * [Inline Rule](#inline-rule) | ||
#### Composite Definition | ||
To share common fields across different schemas move them to a module and require them. | ||
Module to represent an `address` field: | ||
```javascript | ||
module.exports = { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
``` | ||
Muliple objects containing an `address` field that needs the same validation rules: | ||
```javascript | ||
var address = require('./address') | ||
, user = { | ||
type: 'object', | ||
address: address | ||
} | ||
, invoice = { | ||
type: 'object', | ||
bill: { | ||
type: 'object', | ||
address: address | ||
} | ||
} | ||
``` | ||
### Rules | ||
@@ -164,3 +197,3 @@ | ||
Assigned to the `validator` field so that you may pass data from the rule to the function: | ||
Assigned to the `test` field so that you may pass data from the rule to the function: | ||
@@ -395,4 +428,3 @@ ```javascript | ||
this.raise( | ||
this.reason( | ||
'id', {description: 'Field value failed pattern match'}), | ||
this.reason('id', {level: 'warn'}), | ||
'%s is not a valid id', | ||
@@ -405,3 +437,3 @@ this.field); | ||
Adding a reason allows associating an identifier with an error and optional meta data about the error. | ||
Adding a reason allows associating an identifier with an error and optional meta data about the error which can be useful if you need to associate a *severity* with errors to distinguish between error types. | ||
@@ -408,0 +440,0 @@ To signal that an internal processing error has occured pass an Error to the callback, for example: |
182874
107
3760
863