async-validate
Advanced tools
Comparing version 0.7.10 to 0.7.11
@@ -1,22 +0,27 @@ | ||
var Schema = require('../../') | ||
// assign a function to a rule | ||
var Schema = require('../..') | ||
, descriptor = { | ||
id: { | ||
foo: 'bar', | ||
validator: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
id: { | ||
expected: 'foo', | ||
validator: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
} | ||
cb(); | ||
} | ||
} | ||
} | ||
} | ||
, source = {} | ||
, source = {id: 'qux'} | ||
, schema; | ||
Schema.plugin([ | ||
require('../../plugin/object'), | ||
require('../../plugin/string'), | ||
require('../../plugin/util') | ||
]); | ||
require('../../plugin/all'); | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) {}); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); |
@@ -1,18 +0,14 @@ | ||
var Schema = require('../../') | ||
// validate a field as whitespace | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
, source = {name: ' '} | ||
, schema; | ||
Schema.plugin([ | ||
require('../../plugin/object'), | ||
require('../../plugin/string'), | ||
require('../../plugin/util') | ||
]); | ||
require('../../plugin/all'); | ||
var schema = new Schema(descriptor); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
// error on name field | ||
console.dir(res.errors); | ||
}); |
@@ -28,3 +28,3 @@ var iterator = require('./iterator') | ||
// wrap shorthand declaration without `fields` | ||
if(!rules.fields && !opts._deep) { | ||
if(!rules.fields && !opts._deep && (!rules.type && !rules.pattern)) { | ||
rules = {fields: rules}; | ||
@@ -64,3 +64,3 @@ } | ||
if(!source && !options._deep) { | ||
if(source === undefined && !options._deep) { | ||
throw new Error('Cannot validate with no source.'); | ||
@@ -100,6 +100,2 @@ }else if(typeof cb !== 'function') { | ||
if(!rule.type && !options._deep) { | ||
rule.type = typeof(source); | ||
} | ||
// infer rule type | ||
@@ -117,2 +113,6 @@ if(rule.type === undefined | ||
if(!rule.type && !options._deep) { | ||
rule.type = typeof(source); | ||
} | ||
if(typeof rule === 'function') { | ||
@@ -119,0 +119,0 @@ rule.validator = rule; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.7.10", | ||
"version": "0.7.11", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -23,2 +23,3 @@ "license": "MIT", | ||
"devDependencies": { | ||
"async": "~1.4.2", | ||
"browserify": "~11.1.0", | ||
@@ -73,4 +74,9 @@ "chai": "~3.2.0", | ||
{ | ||
"inc": "api.md" | ||
}, | ||
{ | ||
"bin": "node doc/example.js" | ||
}, | ||
{ | ||
"inc": [ | ||
"api.md", | ||
"developer.md", | ||
@@ -77,0 +83,0 @@ "license.md" |
@@ -29,3 +29,3 @@ /** | ||
this.main.date = function date(cb) { | ||
var validate = this.rule.required | ||
var validate = this.isRoot() || this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.field) | ||
@@ -32,0 +32,0 @@ && this.source[this.field]); |
214
README.md
@@ -58,2 +58,10 @@ Table of Contents | ||
* [diff](#diff) | ||
* [Examples](#examples) | ||
* [assigned-rule](#assigned-rule) | ||
* [deep](#deep) | ||
* [inline-rule](#inline-rule) | ||
* [message](#message) | ||
* [required](#required-1) | ||
* [source-root](#source-root) | ||
* [whitespace](#whitespace) | ||
* [Developer](#developer) | ||
@@ -759,2 +767,208 @@ * [Test](#test) | ||
### Examples | ||
#### assigned-rule | ||
* [doc/example/assigned-rule](https://github.com/freeformsystems/async-validate/blob/master/doc/example/assigned-rule.js). | ||
```javascript | ||
// assign a function to a rule | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
id: { | ||
expected: 'foo', | ||
validator: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
} | ||
cb(); | ||
} | ||
} | ||
} | ||
, source = {id: 'qux'} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: id expects foo, got qux] field: 'id', reason: { id: 'unexpected-id' } } ] | ||
``` | ||
#### deep | ||
* [doc/example/deep](https://github.com/freeformsystems/async-validate/blob/master/doc/example/deep.js). | ||
```javascript | ||
// validate properties of a nested object | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
} | ||
, source = {address: {name: '1024c', street: 'Mock St', city: 'Mock City'}} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: zip is required] field: 'zip', reason: { id: 'required' } } ] | ||
``` | ||
#### inline-rule | ||
* [doc/example/inline-rule](https://github.com/freeformsystems/async-validate/blob/master/doc/example/inline-rule.js). | ||
```javascript | ||
// assign a function as a rule | ||
var Schema = require('async-validate') | ||
, reserved = ['foo'] | ||
, descriptor = { | ||
id: function(cb) { | ||
if(~reserved.indexOf(this.value)) { | ||
this.raise('%s is a reserved id', this.value); | ||
} | ||
cb(); | ||
} | ||
} | ||
, source = {id: 'foo'} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: foo is a reserved id] field: 'id' } ] | ||
``` | ||
#### message | ||
* [doc/example/message](https://github.com/freeformsystems/async-validate/blob/master/doc/example/message.js). | ||
```javascript | ||
// override error message | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'name must be specified' | ||
} | ||
} | ||
, source = {} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: name must be specified] field: 'name', reason: { id: 'required' } } ] | ||
``` | ||
#### required | ||
* [doc/example/required](https://github.com/freeformsystems/async-validate/blob/master/doc/example/required.js). | ||
```javascript | ||
// validate a field as required | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
name: {type: 'string', required: true} | ||
} | ||
, source = {} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: name is required] field: 'name', reason: { id: 'required' } } ] | ||
``` | ||
#### source-root | ||
* [doc/example/source-root](https://github.com/freeformsystems/async-validate/blob/master/doc/example/source-root.js). | ||
```javascript | ||
// validate the type of the source object | ||
var Schema = require('async-validate') | ||
, descriptor = {type: 'object'} | ||
, source = 'foo' | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: source is not an object] field: 'source', reason: { id: 'type' } } ] | ||
``` | ||
#### whitespace | ||
* [doc/example/whitespace](https://github.com/freeformsystems/async-validate/blob/master/doc/example/whitespace.js). | ||
```javascript | ||
// validate a field as whitespace | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
, source = {name: ' '} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: name cannot be empty] field: 'name', reason: { id: 'whitespace' } } ] | ||
``` | ||
## Developer | ||
@@ -761,0 +975,0 @@ |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
157937
90
3400
1040
6
1
1