async-validate
Advanced tools
Comparing version 0.7.6 to 0.7.7
@@ -38,2 +38,3 @@ ### API | ||
* `field`: Field name for the source object, default is `source` when not specified. | ||
* `vars`: Object map of variables to assign to each rule. | ||
@@ -62,3 +63,3 @@ ##### Schema.plugin | ||
Represents the reason for a validation error, may be created using `getReason()`. | ||
Represents the reason for a validation error, may be created using `reason()`. | ||
@@ -79,3 +80,2 @@ You must supply a reason `id`; if `opts` are passed they are assigned as properties of the reason instance. When `toString()` is called on a `Reason` instance the `id` is returned. | ||
* `source`: The source object passed to `validate()`. | ||
* `options`: The options passed to `validate()`. | ||
* `messages`: Reference to the schema messages. | ||
@@ -93,6 +93,6 @@ * `errors`: Array of errors for the field validation. | ||
##### getReason | ||
##### reason | ||
```javascript | ||
function getReason(id, [opts]) | ||
function reason(id, [opts]) | ||
``` | ||
@@ -110,3 +110,3 @@ | ||
The first argument may optionally be a `Reason` instance returned by `getReason()` allowing a user to associate an identifier with the validation error and optional additional information. A validation error generated with a `Reason` has a `reason` field referencing the supplied reason. | ||
The first argument may optionally be a `Reason` instance returned by `reason()` allowing a user to associate an identifier with the validation error and optional additional information. A validation error generated with a `Reason` has a `reason` field referencing the supplied reason. | ||
@@ -113,0 +113,0 @@ When replacement parameters are supplied the behaviour is identical to `util.format`. |
@@ -193,7 +193,8 @@ ## Guide | ||
function id(cb) { | ||
var reason; | ||
if(!/^[a-z0-9-]+$/i.test(this.value)) { | ||
reason = this.getReason( | ||
'id', {description: 'Field value failed pattern match'}); | ||
this.raise(reason, '%s is not a valid id', this.field); | ||
this.raise( | ||
this.reason( | ||
'id', {description: 'Field value failed pattern match'}), | ||
'%s is not a valid id', | ||
this.field); | ||
} | ||
@@ -382,1 +383,15 @@ cb(); | ||
You may wish to sanitize user input instead of testing for whitespace, see [transform](#transform) for an example that would allow you to strip whitespace. | ||
### Validation | ||
#### Variables | ||
Sometimes it is useful to pass existing data into all rule functions as transient data so that your rule functions may reference existing code for performing async operations. A common use case would be using a model class to query a database and then validate on the returned data. | ||
To do this you may use the `vars` processing option when calling [validate](#validate). | ||
The value should be an Object; each property of the `vars` object is passed into the [Rule](#rule) scope so that they are available via `this`. | ||
Be aware that if you use a built in field (see [Rule](#rule)) it will be overwritten. | ||
See the [vars test fixture](/test/spec/vars.js) for an example. |
@@ -77,3 +77,3 @@ var plugin = require('zephyr') | ||
*/ | ||
function getReason(id, opts) { | ||
function reason(id, opts) { | ||
return new Reason(id, opts); | ||
@@ -135,3 +135,3 @@ } | ||
Rule.prototype.getReason = getReason; | ||
Rule.prototype.reason = reason; | ||
Rule.prototype.error = error; | ||
@@ -138,0 +138,0 @@ Rule.prototype.raise = raise; |
@@ -73,3 +73,4 @@ var iterator = require('./iterator') | ||
, series = [] | ||
, func | ||
// iterator function series/parallel | ||
, func = options.parallel ? iterator.map : iterator.mapSeries | ||
, messages; | ||
@@ -81,5 +82,2 @@ | ||
// iterator function series/parallel | ||
func = options.parallel ? iterator.map : iterator.mapSeries; | ||
// configure messages to use defaults where necessary | ||
@@ -139,12 +137,16 @@ messages = options.messages || this.messages(); | ||
var validator = Rule({ | ||
rule: rule, | ||
field: rule.field, | ||
value: rule.value, | ||
source: rule.source, | ||
errors: [], | ||
messages: options.messages | ||
}); | ||
var vars = options.vars || {} | ||
, validator | ||
, isDeep; | ||
var isDeep = (rule.type === 'object' || rule.type === 'array') | ||
vars.rule = rule; | ||
vars.field = rule.field; | ||
vars.value = rule.value; | ||
vars.source = rule.source; | ||
vars.errors = []; | ||
vars.messages = options.messages; | ||
validator = Rule(vars); | ||
isDeep = (rule.type === 'object' || rule.type === 'array') | ||
&& typeof(rule.fields) === 'object'; | ||
@@ -151,0 +153,0 @@ isDeep = isDeep && (rule.required || (!rule.required && rule.value)); |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.7.6", | ||
"version": "0.7.7", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -35,2 +35,4 @@ Table of Contents | ||
* [Whitespace](#whitespace) | ||
* [Validation](#validation) | ||
* [Variables](#variables) | ||
* [Messages](#messages) | ||
@@ -47,3 +49,3 @@ * [Transform](#transform) | ||
* [isRoot](#isroot) | ||
* [getReason](#getreason) | ||
* [reason](#reason) | ||
* [raise](#raise) | ||
@@ -298,7 +300,8 @@ * [format](#format) | ||
function id(cb) { | ||
var reason; | ||
if(!/^[a-z0-9-]+$/i.test(this.value)) { | ||
reason = this.getReason( | ||
'id', {description: 'Field value failed pattern match'}); | ||
this.raise(reason, '%s is not a valid id', this.field); | ||
this.raise( | ||
this.reason( | ||
'id', {description: 'Field value failed pattern match'}), | ||
'%s is not a valid id', | ||
this.field); | ||
} | ||
@@ -488,2 +491,16 @@ cb(); | ||
### Validation | ||
#### Variables | ||
Sometimes it is useful to pass existing data into all rule functions as transient data so that your rule functions may reference existing code for performing async operations. A common use case would be using a model class to query a database and then validate on the returned data. | ||
To do this you may use the `vars` processing option when calling [validate](#validate). | ||
The value should be an Object; each property of the `vars` object is passed into the [Rule](#rule) scope so that they are available via `this`. | ||
Be aware that if you use a built in field (see [Rule](#rule)) it will be overwritten. | ||
See the [vars test fixture](https://github.com/freeformsystems/async-validate/blob/master/test/spec/vars.js) for an example. | ||
### Messages | ||
@@ -605,2 +622,3 @@ | ||
* `field`: Field name for the source object, default is `source` when not specified. | ||
* `vars`: Object map of variables to assign to each rule. | ||
@@ -629,3 +647,3 @@ ##### Schema.plugin | ||
Represents the reason for a validation error, may be created using `getReason()`. | ||
Represents the reason for a validation error, may be created using `reason()`. | ||
@@ -646,3 +664,2 @@ You must supply a reason `id`; if `opts` are passed they are assigned as properties of the reason instance. When `toString()` is called on a `Reason` instance the `id` is returned. | ||
* `source`: The source object passed to `validate()`. | ||
* `options`: The options passed to `validate()`. | ||
* `messages`: Reference to the schema messages. | ||
@@ -660,6 +677,6 @@ * `errors`: Array of errors for the field validation. | ||
##### getReason | ||
##### reason | ||
```javascript | ||
function getReason(id, [opts]) | ||
function reason(id, [opts]) | ||
``` | ||
@@ -677,3 +694,3 @@ | ||
The first argument may optionally be a `Reason` instance returned by `getReason()` allowing a user to associate an identifier with the validation error and optional additional information. A validation error generated with a `Reason` has a `reason` field referencing the supplied reason. | ||
The first argument may optionally be a `Reason` instance returned by `reason()` allowing a user to associate an identifier with the validation error and optional additional information. A validation error generated with a `Reason` has a `reason` field referencing the supplied reason. | ||
@@ -680,0 +697,0 @@ When replacement parameters are supplied the behaviour is identical to `util.format`. |
@@ -25,3 +25,3 @@ var expect = require('chai').expect | ||
var v = Validator.Type({}); | ||
expect(v.getReason('mock-reason', {foo: 'bar'})).to.be.an('object'); | ||
expect(v.reason('mock-reason', {foo: 'bar'})).to.be.an('object'); | ||
done(); | ||
@@ -28,0 +28,0 @@ }); |
137530
82
2966
808