async-validate
Advanced tools
Comparing version
## Guide | ||
### Rules | ||
A rule is a function that performs the validation of a value, the [plugin rule](#plugin-rule) method of declaring rule functions is preferred as it is the most modular and allow for easily reusing code. | ||
Rule functions may be declared in one of the following ways. | ||
#### Inline Rule | ||
The rule function is assigned directly to the field: | ||
```javascript | ||
var descriptor = { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
``` | ||
#### Assigned Rule | ||
Assigned to the `validator` field so that you may pass data from the rule to the function: | ||
```javascript | ||
var descriptor = { | ||
id: { | ||
foo: 'bar', | ||
validator: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
} | ||
``` | ||
#### Plugin Rule | ||
Or as a plugin module that assigns the rule type as a static plugin method: | ||
```javascript | ||
function plugin() { | ||
this.main.id = function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
var Schema = require('async-validate'); | ||
Schema.plugin([plugin]); | ||
var descriptor = { | ||
id: {type: 'id'} | ||
} | ||
``` | ||
### Plugins | ||
@@ -33,11 +88,2 @@ | ||
```javascript | ||
var descriptor = { | ||
name: function(cb) { | ||
// if has error condition call this.raise() | ||
cb(this.errors); | ||
} | ||
} | ||
``` | ||
#### Type | ||
@@ -140,3 +186,3 @@ | ||
// and add a validation error to the errors array if it does | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -143,0 +189,0 @@ ] |
@@ -258,3 +258,5 @@ var iterator = require('./iterator') | ||
function onValidate(errors) { | ||
function onValidate() { | ||
var errors = validator.errors; | ||
// bail on first error | ||
@@ -261,0 +263,0 @@ if(options.first && errors && errors.length) { |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.4.9", | ||
"version": "0.4.10", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -12,3 +12,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -12,3 +12,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -36,3 +36,3 @@ var moment = require('moment'); | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -39,0 +39,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -14,0 +14,0 @@ |
@@ -12,3 +12,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -12,3 +12,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -14,0 +14,0 @@ |
@@ -12,3 +12,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -31,4 +31,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -35,0 +34,0 @@ |
@@ -13,3 +13,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -16,0 +16,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -14,0 +14,0 @@ |
@@ -17,3 +17,3 @@ /** | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -20,0 +20,0 @@ |
@@ -8,2 +8,6 @@ Table of Contents | ||
* [Guide](#guide) | ||
* [Rules](#rules) | ||
* [Inline Rule](#inline-rule) | ||
* [Assigned Rule](#assigned-rule) | ||
* [Plugin Rule](#plugin-rule) | ||
* [Plugins](#plugins) | ||
@@ -28,3 +32,3 @@ * [Descriptor](#descriptor) | ||
* [Options](#options) | ||
* [Rules](#rules) | ||
* [Rules](#rules-1) | ||
* [Scope](#scope) | ||
@@ -78,2 +82,57 @@ * [Developer](#developer) | ||
### Rules | ||
A rule is a function that performs the validation of a value, the [plugin rule](#plugin-rule) method of declaring rule functions is preferred as it is the most modular and allow for easily reusing code. | ||
Rule functions may be declared in one of the following ways. | ||
#### Inline Rule | ||
The rule function is assigned directly to the field: | ||
```javascript | ||
var descriptor = { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
``` | ||
#### Assigned Rule | ||
Assigned to the `validator` field so that you may pass data from the rule to the function: | ||
```javascript | ||
var descriptor = { | ||
id: { | ||
foo: 'bar', | ||
validator: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
} | ||
``` | ||
#### Plugin Rule | ||
Or as a plugin module that assigns the rule type as a static plugin method: | ||
```javascript | ||
function plugin() { | ||
this.main.id = function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
var Schema = require('async-validate'); | ||
Schema.plugin([plugin]); | ||
var descriptor = { | ||
id: {type: 'id'} | ||
} | ||
``` | ||
### Plugins | ||
@@ -109,11 +168,2 @@ | ||
```javascript | ||
var descriptor = { | ||
name: function(cb) { | ||
// if has error condition call this.raise() | ||
cb(this.errors); | ||
} | ||
} | ||
``` | ||
#### Type | ||
@@ -216,3 +266,3 @@ | ||
// and add a validation error to the errors array if it does | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -219,0 +269,0 @@ ] |
@@ -6,3 +6,3 @@ function id(cb) { | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -9,0 +9,0 @@ |
@@ -64,3 +64,3 @@ var expect = require('chai').expect | ||
this.raise("%s is a required field", this.field); | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -67,0 +67,0 @@ } |
@@ -50,3 +50,3 @@ var util = require('util'); | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -53,0 +53,0 @@ ] |
@@ -36,3 +36,3 @@ var expect = require('chai').expect | ||
it("should error on validate with no source", function(done) { | ||
var validator = new schema({name: function(cb){cb(this.errors)}}); | ||
var validator = new schema({name: function(cb){cb()}}); | ||
function fn() { | ||
@@ -47,3 +47,3 @@ validator.validate(); | ||
it("should error on validate with no callback", function(done) { | ||
var validator = new schema({name: function(cb){cb(this.errors)}}); | ||
var validator = new schema({name: function(cb){cb()}}); | ||
function fn() { | ||
@@ -50,0 +50,0 @@ validator.validate({}); |
@@ -27,3 +27,3 @@ var util = require('util'); | ||
} | ||
cb(this.errors); | ||
cb(); | ||
} | ||
@@ -30,0 +30,0 @@ } |
120715
1.66%2757
0.04%558
9.84%