async-validate
Advanced tools
Comparing version 0.10.2 to 0.11.2
@@ -15,3 +15,8 @@ // generate errors when additional fields are present | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
zip: { | ||
type: 'string', | ||
required: true, | ||
len: 8, | ||
message: 'Invalid zip' | ||
} | ||
} | ||
@@ -28,3 +33,3 @@ } | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
@@ -31,0 +36,0 @@ } |
// assign a function to a rule | ||
var Schema = require('../..') | ||
, descriptor = { | ||
id: { | ||
expected: 'foo', | ||
test: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
fields: { | ||
id: { | ||
expected: 'foo', | ||
test: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
} | ||
cb(); | ||
} | ||
cb(); | ||
} | ||
@@ -17,0 +19,0 @@ } |
@@ -5,9 +5,11 @@ // bail on first error encountered | ||
, 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} | ||
fields: { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -14,0 +16,0 @@ } |
// validate properties of a nested object | ||
var Schema = require('../..') | ||
, 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} | ||
fields: { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -12,0 +14,0 @@ } |
@@ -5,7 +5,9 @@ // assign a function as a rule | ||
, descriptor = { | ||
id: function(cb) { | ||
if(~reserved.indexOf(this.value)) { | ||
this.raise('%s is a reserved id', this.value); | ||
fields: { | ||
id: function(cb) { | ||
if(~reserved.indexOf(this.value)) { | ||
this.raise('%s is a reserved id', this.value); | ||
} | ||
cb(); | ||
} | ||
cb(); | ||
} | ||
@@ -12,0 +14,0 @@ } |
@@ -5,3 +5,5 @@ // validate a field is an instanceof a function | ||
, descriptor = { | ||
comp: {type: Component, required: true} | ||
fields: { | ||
comp: {type: Component, required: true} | ||
} | ||
} | ||
@@ -8,0 +10,0 @@ , source = {comp: {}} |
// validate a field length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
func: {type: 'function', required: true, len: 1} | ||
fields: { | ||
func: {type: 'function', required: true, len: 1} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {func: function noop(){}} |
@@ -6,5 +6,7 @@ // validate all fields of an object | ||
required: true, | ||
all: { | ||
match: /./, | ||
type: 'string' | ||
fields: { | ||
all: { | ||
match: /./, | ||
type: 'string' | ||
} | ||
} | ||
@@ -11,0 +13,0 @@ } |
// validate a field has a maximum length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
func: {type: 'function', required: true, max: 1} | ||
fields: { | ||
func: {type: 'function', required: true, max: 1} | ||
} | ||
} | ||
, source = {func: function noop(foo, bar){}} | ||
, source = { | ||
func: function noop(foo, bar){ | ||
foo(); | ||
bar(); | ||
} | ||
} | ||
, schema; | ||
@@ -8,0 +15,0 @@ |
@@ -5,5 +5,7 @@ // clone default messages | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
@@ -10,0 +12,0 @@ } |
// override error message with function | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function(msg, parameters) { | ||
return this.format( | ||
'name must be specified (field: %s)', this.field); | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function() { | ||
return this.format( | ||
'name must be specified (field: %s)', this.field); | ||
} | ||
} | ||
@@ -11,0 +13,0 @@ } |
@@ -5,5 +5,7 @@ // override default error message | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
@@ -10,0 +12,0 @@ } |
// override error message | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'name must be specified' | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'name must be specified' | ||
} | ||
} | ||
@@ -9,0 +11,0 @@ } |
// validate a field has a minimum length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
func: {type: 'function', required: true, min: 1} | ||
fields: { | ||
func: {type: 'function', required: true, min: 1} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {func: function noop(){}} |
// validate a field as one of multiple types | ||
var Schema = require('../..') | ||
, descriptor = { | ||
flag: {type: ['boolean', Boolean], required: true} | ||
fields: { | ||
flag: {type: ['boolean', Boolean], required: true} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {flag: 'foo'} |
// validate a field as matching a pattern | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} | ||
fields: { | ||
name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {name: '-name'} |
// use a placeholder to set a default value | ||
var Schema = require('../..') | ||
, descriptor = { | ||
list: { | ||
type: 'array', | ||
values: {type: 'integer'}, | ||
placeholder: function() { | ||
return []; | ||
fields: { | ||
list: { | ||
type: 'array', | ||
values: {type: 'integer'}, | ||
placeholder: function() { | ||
return []; | ||
} | ||
} | ||
@@ -18,4 +20,4 @@ } | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
schema.validate(source, function() { | ||
console.dir(source); | ||
}); |
// validate a field with a plugin rule | ||
var Schema = require('../..') | ||
, descriptor = { | ||
id: {type: 'id', required: true} | ||
fields: { | ||
id: {type: 'id', required: true} | ||
} | ||
} | ||
@@ -13,3 +15,3 @@ , source = {id: '-foo'} | ||
function plugin() { | ||
var pattern = /^[a-z0-9-]$/i; | ||
var pattern = /^[a-z0-9]$/i; | ||
// create static rule function | ||
@@ -16,0 +18,0 @@ this.main.id = function id(cb) { |
// validate a field has a length within a range | ||
var Schema = require('../..') | ||
, descriptor = { | ||
func: {type: 'function', required: true, min: 1, max: 2} | ||
fields: { | ||
func: {type: 'function', required: true, min: 1, max: 2} | ||
} | ||
} | ||
, source = {func: function noop(foo, bar, qux){}} | ||
, source = { | ||
func: function noop(foo, bar, qux){ | ||
foo(); | ||
bar(); | ||
qux(); | ||
} | ||
} | ||
, schema; | ||
@@ -8,0 +16,0 @@ |
// validate a field as required | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: {type: 'string', required: true} | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {} |
// pass state information between rule test functions | ||
var Schema = require('../..') | ||
, url = require('url') | ||
, dns = require('dns') | ||
@@ -8,28 +7,30 @@ , state = {} | ||
, descriptor = { | ||
email: [ | ||
{type: 'string', required: true, pattern: /^.+@.+\..+/}, | ||
function parse(cb) { | ||
var at = this.value.indexOf('@') | ||
, user = this.value.substr(0, at) | ||
, domain = this.value.substr(at + 1); | ||
// assign to validation state | ||
this.state.email = {user: user, domain: domain}; | ||
cb(); | ||
}, | ||
function lookup(cb) { | ||
function resolve(err, addr) { | ||
if(err && err.code === 'ENOTFOUND') { | ||
this.raise( | ||
'%s: could not resolve dns for domain %s', | ||
this.field, | ||
this.state.email.domain); | ||
}else if(err) { | ||
return cb(err); | ||
fields: { | ||
email: [ | ||
{type: 'string', required: true, pattern: /^.+@.+\..+/}, | ||
function parse(cb) { | ||
var at = this.value.indexOf('@') | ||
, user = this.value.substr(0, at) | ||
, domain = this.value.substr(at + 1); | ||
// assign to validation state | ||
this.state.email = {user: user, domain: domain}; | ||
cb(); | ||
}, | ||
function lookup(cb) { | ||
function resolve(err, addr) { | ||
if(err && err.code === 'ENOTFOUND') { | ||
this.raise( | ||
'%s: could not resolve dns for domain %s', | ||
this.field, | ||
this.state.email.domain); | ||
}else if(err) { | ||
return cb(err); | ||
} | ||
this.state.addr = addr; | ||
cb(); | ||
} | ||
this.state.addr = addr; | ||
cb(); | ||
dns.resolve(this.state.email.domain, resolve.bind(this)); | ||
} | ||
dns.resolve(this.state.email.domain, resolve.bind(this)); | ||
} | ||
] | ||
] | ||
} | ||
} | ||
@@ -36,0 +37,0 @@ // force dns failure with random domain |
// validate a field type | ||
var Schema = require('../..') | ||
, descriptor = { | ||
flag: {type: 'boolean', required: true} | ||
fields: { | ||
flag: {type: 'boolean', required: true} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {flag: 'foo'} |
// validate a field as whitespace | ||
var Schema = require('../..') | ||
, descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
fields: { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
} | ||
@@ -6,0 +8,0 @@ , source = {name: ' '} |
@@ -11,3 +11,6 @@ ## Guide | ||
var descriptor = { | ||
name: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -32,6 +35,9 @@ ``` | ||
module.exports = { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -46,9 +52,10 @@ ``` | ||
type: 'object', | ||
address: address | ||
fields: { | ||
address: address | ||
} | ||
} | ||
, invoice = { | ||
type: 'object', | ||
bill: { | ||
type: 'object', | ||
address: address | ||
fields: { | ||
bill: address | ||
} | ||
@@ -76,5 +83,8 @@ } | ||
var descriptor = { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
type: 'object', | ||
fields: { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
@@ -90,8 +100,11 @@ } | ||
var descriptor = { | ||
id: { | ||
foo: 'bar', | ||
test: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
type: 'object', | ||
fields: { | ||
id: { | ||
foo: 'bar', | ||
test: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
@@ -124,3 +137,6 @@ } | ||
var descriptor = { | ||
id: {type: 'id'} | ||
type: 'object', | ||
fields: { | ||
id: {type: 'id'} | ||
} | ||
} | ||
@@ -137,10 +153,13 @@ ``` | ||
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(); | ||
} | ||
] | ||
type: 'object', | ||
fields: { | ||
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(); | ||
} | ||
] | ||
} | ||
} | ||
@@ -155,10 +174,13 @@ ``` | ||
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"} | ||
type: 'object', | ||
fields: { | ||
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"} | ||
} | ||
} | ||
@@ -179,10 +201,13 @@ } | ||
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} | ||
type: 'object', | ||
fields: { | ||
roles: { | ||
type: "array", | ||
required: true, | ||
len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
@@ -278,3 +303,6 @@ } | ||
var descriptor = { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
@@ -299,6 +327,9 @@ ``` | ||
var descriptor = { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
@@ -305,0 +336,0 @@ } |
@@ -15,7 +15,10 @@ ### Messages | ||
var descriptor = { | ||
name: { | ||
type: "string", | ||
required: true, | ||
message: function(message, parameters) { | ||
return this.field + ' is required'; | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: "string", | ||
required: true, | ||
message: function(message, parameters) { | ||
return this.field + ' is required'; | ||
} | ||
} | ||
@@ -31,3 +34,8 @@ } | ||
, messages = require('async-validate/messages') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name:{type: "string", required: true}} | ||
} | ||
} | ||
, schema; | ||
@@ -45,3 +53,8 @@ messages.required = "%s is a required field"; | ||
, messages = require('messages-es') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name:{type: "string", required: true}} | ||
} | ||
} | ||
, schema = new Schema(descriptor, {messages: messages}); | ||
@@ -48,0 +61,0 @@ ``` |
var Schema = require('..') | ||
, descriptor = { | ||
name: { | ||
type: "string", | ||
required: true, pattern: /^[a-z]+$/, | ||
transform: function(value) { | ||
return value.trim(); | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: "string", | ||
required: true, pattern: /^[a-z]+$/, | ||
transform: function(value) { | ||
return value.trim(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
, schema = new Schema(descriptor) | ||
@@ -20,4 +23,4 @@ , source = {name: " user "}; | ||
schema.validate(source, function(err, res) { | ||
schema.validate(source, function() { | ||
console.dir(source.name); | ||
}); |
var Schema = require('async-validate') | ||
, descriptor = {name: {type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name: {type: "string", required: true} | ||
} | ||
} | ||
, schema = new Schema(descriptor) | ||
@@ -4,0 +9,0 @@ , source = {}; |
324
EXAMPLE.md
@@ -13,2 +13,3 @@ Table of Contents | ||
* [len](#len) | ||
* [match](#match) | ||
* [max](#max) | ||
@@ -20,4 +21,4 @@ * [message-clone](#message-clone) | ||
* [min](#min) | ||
* [multiple-rules](#multiple-rules) | ||
* [multiple-types](#multiple-types) | ||
* [multiple](#multiple) | ||
* [pattern](#pattern) | ||
@@ -57,3 +58,8 @@ * [placeholder](#placeholder) | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
zip: { | ||
type: 'string', | ||
required: true, | ||
len: 8, | ||
message: 'Invalid zip' | ||
} | ||
} | ||
@@ -70,3 +76,3 @@ } | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
@@ -97,14 +103,16 @@ } | ||
, descriptor = { | ||
id: { | ||
expected: 'foo', | ||
test: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
fields: { | ||
id: { | ||
expected: 'foo', | ||
test: function(cb) { | ||
if(this.value !== this.expected) { | ||
this.raise( | ||
this.reason('unexpected-id'), | ||
'id expects %s, got %s', | ||
this.expected, | ||
this.value | ||
) | ||
} | ||
cb(); | ||
} | ||
cb(); | ||
} | ||
@@ -137,9 +145,11 @@ } | ||
, 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} | ||
fields: { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -171,9 +181,11 @@ } | ||
, 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} | ||
fields: { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -206,7 +218,9 @@ } | ||
, descriptor = { | ||
id: function(cb) { | ||
if(~reserved.indexOf(this.value)) { | ||
this.raise('%s is a reserved id', this.value); | ||
fields: { | ||
id: function(cb) { | ||
if(~reserved.indexOf(this.value)) { | ||
this.raise('%s is a reserved id', this.value); | ||
} | ||
cb(); | ||
} | ||
cb(); | ||
} | ||
@@ -238,3 +252,5 @@ } | ||
, descriptor = { | ||
comp: {type: Component, required: true} | ||
fields: { | ||
comp: {type: Component, required: true} | ||
} | ||
} | ||
@@ -264,3 +280,5 @@ , source = {comp: {}} | ||
, descriptor = { | ||
func: {type: 'function', required: true, len: 1} | ||
fields: { | ||
func: {type: 'function', required: true, len: 1} | ||
} | ||
} | ||
@@ -282,2 +300,34 @@ , source = {func: function noop(){}} | ||
#### match | ||
* [doc/example/match](https://github.com/freeformsystems/async-validate/blob/master/doc/example/match.js). | ||
```javascript | ||
// validate all fields of an object | ||
var Schema = require('async-validate') | ||
, descriptor = { | ||
type: 'object', | ||
required: true, | ||
fields: { | ||
all: { | ||
match: /./, | ||
type: 'string' | ||
} | ||
} | ||
} | ||
, source = {address1: 'foo', address2: 'bar', address3: false} | ||
, schema; | ||
require('async-validate/plugin/all'); | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
console.dir(res.errors); | ||
}); | ||
``` | ||
``` | ||
[ { [Error: address3 is not a string] field: 'address3', reason: { id: 'type' } } ] | ||
``` | ||
#### max | ||
@@ -291,5 +341,12 @@ | ||
, descriptor = { | ||
func: {type: 'function', required: true, max: 1} | ||
fields: { | ||
func: {type: 'function', required: true, max: 1} | ||
} | ||
} | ||
, source = {func: function noop(foo, bar){}} | ||
, source = { | ||
func: function noop(foo, bar){ | ||
foo(); | ||
bar(); | ||
} | ||
} | ||
, schema; | ||
@@ -318,5 +375,7 @@ | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
@@ -351,8 +410,10 @@ } | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function(msg, parameters) { | ||
return this.format( | ||
'name must be specified (field: %s)', this.field); | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function() { | ||
return this.format( | ||
'name must be specified (field: %s)', this.field); | ||
} | ||
} | ||
@@ -385,5 +446,7 @@ } | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true | ||
} | ||
} | ||
@@ -417,6 +480,8 @@ } | ||
, descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'name must be specified' | ||
fields: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'name must be specified' | ||
} | ||
} | ||
@@ -447,3 +512,5 @@ } | ||
, descriptor = { | ||
func: {type: 'function', required: true, min: 1} | ||
fields: { | ||
func: {type: 'function', required: true, min: 1} | ||
} | ||
} | ||
@@ -465,13 +532,26 @@ , source = {func: function noop(){}} | ||
#### multiple-types | ||
#### multiple-rules | ||
* [doc/example/multiple-types](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple-types.js). | ||
* [doc/example/multiple-rules](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple-rules.js). | ||
```javascript | ||
// validate a field as one of multiple types | ||
// validate a field with multiple rules | ||
var Schema = require('async-validate') | ||
, data = {bar: 'qux'} | ||
, descriptor = { | ||
flag: {type: ['boolean', Boolean], required: true} | ||
fields: { | ||
id: [ | ||
{type: 'string', required: true}, | ||
function exists(cb) { | ||
if(!data[this.value]) { | ||
this.raise( | ||
this.reason('missing-id'), | ||
'id %s does not exist', this.value); | ||
} | ||
cb(); | ||
} | ||
] | ||
} | ||
} | ||
, source = {flag: 'foo'} | ||
, source = {id: 'foo'} | ||
, schema; | ||
@@ -488,27 +568,18 @@ | ||
``` | ||
[ { [Error: flag is not one of the allowed types boolean, Boolean] field: 'flag', reason: { id: 'type' } } ] | ||
[ { [Error: id foo does not exist] field: 'id', reason: { id: 'missing-id' } } ] | ||
``` | ||
#### multiple | ||
#### multiple-types | ||
* [doc/example/multiple](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple.js). | ||
* [doc/example/multiple-types](https://github.com/freeformsystems/async-validate/blob/master/doc/example/multiple-types.js). | ||
```javascript | ||
// validate a field with multiple rules | ||
// validate a field as one of multiple types | ||
var Schema = require('async-validate') | ||
, data = {bar: 'qux'} | ||
, descriptor = { | ||
id: [ | ||
{type: 'string', required: true}, | ||
function exists(cb) { | ||
if(!data[this.value]) { | ||
this.raise( | ||
this.reason('missing-id'), | ||
'id %s does not exist', this.value); | ||
} | ||
cb(); | ||
} | ||
] | ||
fields: { | ||
flag: {type: ['boolean', Boolean], required: true} | ||
} | ||
} | ||
, source = {id: 'foo'} | ||
, source = {flag: 'foo'} | ||
, schema; | ||
@@ -525,3 +596,3 @@ | ||
``` | ||
[ { [Error: id foo does not exist] field: 'id', reason: { id: 'missing-id' } } ] | ||
[ { [Error: flag is not one of the allowed types boolean, Boolean] field: 'flag', reason: { id: 'type' } } ] | ||
``` | ||
@@ -537,3 +608,5 @@ | ||
, descriptor = { | ||
name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} | ||
fields: { | ||
name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} | ||
} | ||
} | ||
@@ -563,7 +636,9 @@ , source = {name: '-name'} | ||
, descriptor = { | ||
list: { | ||
type: 'array', | ||
values: {type: 'integer'}, | ||
placeholder: function() { | ||
return []; | ||
fields: { | ||
list: { | ||
type: 'array', | ||
values: {type: 'integer'}, | ||
placeholder: function() { | ||
return []; | ||
} | ||
} | ||
@@ -578,3 +653,3 @@ } | ||
schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
schema.validate(source, function() { | ||
console.dir(source); | ||
@@ -596,3 +671,5 @@ }); | ||
, descriptor = { | ||
id: {type: 'id', required: true} | ||
fields: { | ||
id: {type: 'id', required: true} | ||
} | ||
} | ||
@@ -606,3 +683,3 @@ , source = {id: '-foo'} | ||
function plugin() { | ||
var pattern = /^[a-z0-9-]$/i; | ||
var pattern = /^[a-z0-9]$/i; | ||
// create static rule function | ||
@@ -638,5 +715,13 @@ this.main.id = function id(cb) { | ||
, descriptor = { | ||
func: {type: 'function', required: true, min: 1, max: 2} | ||
fields: { | ||
func: {type: 'function', required: true, min: 1, max: 2} | ||
} | ||
} | ||
, source = {func: function noop(foo, bar, qux){}} | ||
, source = { | ||
func: function noop(foo, bar, qux){ | ||
foo(); | ||
bar(); | ||
qux(); | ||
} | ||
} | ||
, schema; | ||
@@ -664,3 +749,5 @@ | ||
, descriptor = { | ||
name: {type: 'string', required: true} | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -712,3 +799,2 @@ , source = {} | ||
var Schema = require('async-validate') | ||
, url = require('url') | ||
, dns = require('dns') | ||
@@ -718,28 +804,30 @@ , state = {} | ||
, descriptor = { | ||
email: [ | ||
{type: 'string', required: true, pattern: /^.+@.+\..+/}, | ||
function parse(cb) { | ||
var at = this.value.indexOf('@') | ||
, user = this.value.substr(0, at) | ||
, domain = this.value.substr(at + 1); | ||
// assign to validation state | ||
this.state.email = {user: user, domain: domain}; | ||
cb(); | ||
}, | ||
function lookup(cb) { | ||
function resolve(err, addr) { | ||
if(err && err.code === 'ENOTFOUND') { | ||
this.raise( | ||
'%s: could not resolve dns for domain %s', | ||
this.field, | ||
this.state.email.domain); | ||
}else if(err) { | ||
return cb(err); | ||
fields: { | ||
email: [ | ||
{type: 'string', required: true, pattern: /^.+@.+\..+/}, | ||
function parse(cb) { | ||
var at = this.value.indexOf('@') | ||
, user = this.value.substr(0, at) | ||
, domain = this.value.substr(at + 1); | ||
// assign to validation state | ||
this.state.email = {user: user, domain: domain}; | ||
cb(); | ||
}, | ||
function lookup(cb) { | ||
function resolve(err, addr) { | ||
if(err && err.code === 'ENOTFOUND') { | ||
this.raise( | ||
'%s: could not resolve dns for domain %s', | ||
this.field, | ||
this.state.email.domain); | ||
}else if(err) { | ||
return cb(err); | ||
} | ||
this.state.addr = addr; | ||
cb(); | ||
} | ||
this.state.addr = addr; | ||
cb(); | ||
dns.resolve(this.state.email.domain, resolve.bind(this)); | ||
} | ||
dns.resolve(this.state.email.domain, resolve.bind(this)); | ||
} | ||
] | ||
] | ||
} | ||
} | ||
@@ -759,3 +847,3 @@ // force dns failure with random domain | ||
``` | ||
[ { [Error: email: could not resolve dns for domain 1442469859410.com] field: 'email' } ] | ||
[ { [Error: email: could not resolve dns for domain 1442560140154.com] field: 'email' } ] | ||
``` | ||
@@ -771,3 +859,5 @@ | ||
, descriptor = { | ||
flag: {type: 'boolean', required: true} | ||
fields: { | ||
flag: {type: 'boolean', required: true} | ||
} | ||
} | ||
@@ -797,3 +887,5 @@ , source = {flag: 'foo'} | ||
, descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
fields: { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
} | ||
@@ -800,0 +892,0 @@ , source = {name: ' '} |
@@ -28,7 +28,2 @@ var iterator = require('./iterator') | ||
// wrap shorthand declaration without `fields` | ||
if(!rules.fields && !opts._deep && !rules.type) { | ||
rules = {fields: rules}; | ||
} | ||
this.rules = rules; | ||
@@ -75,5 +70,3 @@ } | ||
var series = [] | ||
, i | ||
, k | ||
@@ -83,2 +76,3 @@ , z | ||
, matchtmp | ||
, fields = this.rules.fields | ||
, state = opts.state || {} | ||
@@ -90,22 +84,18 @@ // iterator function series/parallel | ||
series = Array.isArray(this.rules) ? this.rules : [this.rules]; | ||
for(i = 0;i < series.length;i++) { | ||
for(k in series[i]) { | ||
if(typeof series[i][k] === 'object' | ||
&& (series[i][k].match instanceof RegExp)) { | ||
matcher = series[i][k]; | ||
for(z in source) { | ||
if(matcher.match.test(z)) { | ||
series[i].fields = series[i].fields || {}; | ||
matchtmp = Schema.clone(matcher); | ||
delete matchtmp.match; | ||
series[i].fields[z] = getRule(matchtmp, z, source[z]); | ||
} | ||
} | ||
for(k in fields) { | ||
if(typeof fields[k] === 'object' | ||
&& (fields[k].match instanceof RegExp)) { | ||
matcher = fields[k]; | ||
delete fields[k]; | ||
for(z in source) { | ||
if(matcher.match.test(z)) { | ||
matchtmp = Schema.clone(matcher); | ||
delete matchtmp.match; | ||
fields[z] = getRule(matchtmp, z, source[z]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
//console.dir(series); | ||
series = Array.isArray(this.rules) ? this.rules : [this.rules]; | ||
@@ -177,7 +167,2 @@ function getRule(rule, field, value) { | ||
// should already have been expanded | ||
if(rule.match instanceof RegExp) { | ||
return callback(); | ||
} | ||
// assign rule fields first | ||
@@ -216,7 +201,5 @@ for(k in rule) { | ||
if(err) { | ||
return callback(err); | ||
return callback(err, scope.errors); | ||
} | ||
//var errors = scope.errors; | ||
// not deep so continue on to next in series | ||
@@ -223,0 +206,0 @@ if(!isDeep) { |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.10.2", | ||
"version": "0.11.2", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
178
README.md
@@ -94,3 +94,8 @@ Table of Contents | ||
var Schema = require('async-validate') | ||
, descriptor = {name: {type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name: {type: "string", required: true} | ||
} | ||
} | ||
, schema = new Schema(descriptor) | ||
@@ -128,3 +133,6 @@ , source = {}; | ||
var descriptor = { | ||
name: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -149,6 +157,9 @@ ``` | ||
module.exports = { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -163,9 +174,10 @@ ``` | ||
type: 'object', | ||
address: address | ||
fields: { | ||
address: address | ||
} | ||
} | ||
, invoice = { | ||
type: 'object', | ||
bill: { | ||
type: 'object', | ||
address: address | ||
fields: { | ||
bill: address | ||
} | ||
@@ -193,5 +205,8 @@ } | ||
var descriptor = { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
type: 'object', | ||
fields: { | ||
id: function(cb) { | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
@@ -207,8 +222,11 @@ } | ||
var descriptor = { | ||
id: { | ||
foo: 'bar', | ||
test: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
type: 'object', | ||
fields: { | ||
id: { | ||
foo: 'bar', | ||
test: function(cb) { | ||
console.log(this.foo); | ||
// if this.value has error condition call this.raise() | ||
cb(); | ||
} | ||
} | ||
@@ -241,3 +259,6 @@ } | ||
var descriptor = { | ||
id: {type: 'id'} | ||
type: 'object', | ||
fields: { | ||
id: {type: 'id'} | ||
} | ||
} | ||
@@ -254,10 +275,13 @@ ``` | ||
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(); | ||
} | ||
] | ||
type: 'object', | ||
fields: { | ||
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(); | ||
} | ||
] | ||
} | ||
} | ||
@@ -272,10 +296,13 @@ ``` | ||
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"} | ||
type: 'object', | ||
fields: { | ||
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"} | ||
} | ||
} | ||
@@ -296,10 +323,13 @@ } | ||
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} | ||
type: 'object', | ||
fields: { | ||
roles: { | ||
type: "array", | ||
required: true, | ||
len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
} | ||
} | ||
@@ -395,3 +425,6 @@ } | ||
var descriptor = { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
@@ -416,6 +449,9 @@ ``` | ||
var descriptor = { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
@@ -616,7 +652,10 @@ } | ||
var descriptor = { | ||
name: { | ||
type: "string", | ||
required: true, | ||
message: function(message, parameters) { | ||
return this.field + ' is required'; | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: "string", | ||
required: true, | ||
message: function(message, parameters) { | ||
return this.field + ' is required'; | ||
} | ||
} | ||
@@ -632,3 +671,8 @@ } | ||
, messages = require('async-validate/messages') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name:{type: "string", required: true}} | ||
} | ||
} | ||
, schema; | ||
@@ -646,3 +690,8 @@ messages.required = "%s is a required field"; | ||
, messages = require('messages-es') | ||
, descriptor = {name:{type: "string", required: true}} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
name:{type: "string", required: true}} | ||
} | ||
} | ||
, schema = new Schema(descriptor, {messages: messages}); | ||
@@ -662,10 +711,13 @@ ``` | ||
, descriptor = { | ||
name: { | ||
type: "string", | ||
required: true, pattern: /^[a-z]+$/, | ||
transform: function(value) { | ||
return value.trim(); | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: "string", | ||
required: true, pattern: /^[a-z]+$/, | ||
transform: function(value) { | ||
return value.trim(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
, schema = new Schema(descriptor) | ||
@@ -680,3 +732,3 @@ , source = {name: " user "}; | ||
schema.validate(source, function(err, res) { | ||
schema.validate(source, function() { | ||
console.dir(source.name); | ||
@@ -683,0 +735,0 @@ }); |
// schema for a schema definition | ||
var schema = { | ||
type: 'object', | ||
required: true, | ||
all: { | ||
match: /./, | ||
fields: { | ||
additional: {type: 'boolean'}, | ||
list: {type: 'array'}, | ||
fields: {type: ['object', 'array']}, | ||
format: {type: 'string'}, | ||
len: {type: 'integer'}, | ||
min: {type: 'integer'}, | ||
max: {type: 'integer'}, | ||
pattern: {type: 'regexp'}, | ||
placeholder: {type: 'function'}, | ||
required: {type: 'boolean'}, | ||
test: {type: 'function'}, | ||
type: {type: ['string', 'function'], required: true}, | ||
values: {type: ['object', 'array']}, | ||
whitespace: {type: 'boolean'} | ||
} | ||
fields: { | ||
additional: {type: 'boolean'}, | ||
fields: {type: 'object'}, | ||
format: {type: 'string'}, | ||
len: {type: 'integer'}, | ||
list: {type: 'array'}, | ||
local: {type: 'boolean'}, | ||
min: {type: 'integer'}, | ||
max: {type: 'integer'}, | ||
match: {type: 'regexp'}, | ||
pattern: {type: 'regexp'}, | ||
placeholder: {type: 'function'}, | ||
required: {type: 'boolean'}, | ||
test: {type: 'function'}, | ||
type: { | ||
type: ['string', 'function'], | ||
required: true | ||
}, | ||
values: {type: ['object', 'array']}, | ||
whitespace: {type: 'boolean'} | ||
} | ||
@@ -24,0 +25,0 @@ } |
var data = { | ||
foo: {id: 'foo'}, | ||
bar: {id: 'bar'}, | ||
bar: {id: 'bar'} | ||
} | ||
@@ -5,0 +5,0 @@ |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/additional'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
type: 'object', | ||
additional: false, | ||
fields: { | ||
address: { | ||
type: 'object', | ||
required: true, | ||
additional: false, | ||
fields: { | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
} | ||
} | ||
} | ||
it('should error on invalid object (additional properties)', function(done) { | ||
@@ -34,8 +18,8 @@ var opts = { | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
} | ||
var validator = new Schema(descriptor); | ||
validator.validate(source, opts, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, opts, function(err, res) { | ||
//console.dir(res); | ||
@@ -56,7 +40,7 @@ expect(res.errors.length).to.eql(2); | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
} | ||
var validator = new Schema(descriptor); | ||
validator.validate(source, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -63,0 +47,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/typed-array') | ||
, mixed = require('../fixtures/schema/typed-array-mixed') | ||
, empty = require('../fixtures/schema/typed-array-empty'); | ||
// an error message with the array index is not very useful | ||
// in this instance use the value instead | ||
function value(message, parameters) { | ||
parameters[0] = this.value; | ||
return this.format.apply(this, [message].concat(parameters)); | ||
} | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
list: { | ||
type: 'array', | ||
values: { | ||
type: 'integer', | ||
message: value | ||
} | ||
}, | ||
} | ||
it('should error on invalid array value type', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [1,2,3,'foo']}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: [1,2,3,'foo']}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -33,14 +19,4 @@ expect(res.errors[0].message).to.eql('foo is not an integer'); | ||
it('should error on invalid array value types', function(done) { | ||
var descriptor = { | ||
list: { | ||
type: 'array', | ||
values: [ | ||
{type: 'integer', message: value}, | ||
{type: 'string', message: value}, | ||
{type: 'float', message: value} | ||
] | ||
}, | ||
} | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [16,'foo', 12]}, function(err, res) { | ||
var schema = new Schema(mixed); | ||
schema.validate({list: [16,'foo', 12]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -53,4 +29,4 @@ expect(res.errors[0].message).to.eql('12 is not a float'); | ||
it('should validate array values', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [1,2,3]}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: [1,2,3]}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -63,4 +39,4 @@ expect(res).to.eql(null); | ||
it('should validate array values with empty array', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: []}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: []}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -73,4 +49,4 @@ expect(res).to.eql(null); | ||
it('should validate array values with no field', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -83,10 +59,4 @@ expect(res).to.eql(null); | ||
it('should ignore validation with empty array as values', function(done) { | ||
var descriptor = { | ||
list: { | ||
type: 'array', | ||
values: [] | ||
}, | ||
} | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [1,2,3, 'foo']}, function(err, res) { | ||
var schema = new Schema(empty); | ||
schema.validate({list: [1,2,3, 'foo']}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -93,0 +63,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/array') | ||
, min = require('../fixtures/schema/array-min') | ||
, max = require('../fixtures/schema/array-max') | ||
, range = require('../fixtures/schema/array-range') | ||
@@ -7,5 +11,2 @@ describe('async-validate:', function() { | ||
it('should error on non-array type', function(done) { | ||
var descriptor = { | ||
list: {type: 'array'}, | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -21,6 +22,3 @@ schema.validate({list: false}, function(err, res) { | ||
it('should error on array length minimum', function(done) { | ||
var descriptor = { | ||
list: {type: 'array', min: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(min); | ||
schema.validate({list: [1]}, function(err, res) { | ||
@@ -35,7 +33,3 @@ expect(res.errors.length).to.eql(1); | ||
it('should error on array length maximum', function(done) { | ||
var descriptor = { | ||
list: {type: 'array', max: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(max); | ||
schema.validate({list: [1,2,3]}, function(err, res) { | ||
@@ -50,7 +44,3 @@ expect(res.errors.length).to.eql(1); | ||
it('should error on array length range', function(done) { | ||
var descriptor = { | ||
list: {type: 'array', min: 1, max: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(range); | ||
schema.validate({list: [1,2,3]}, function(err, res) { | ||
@@ -57,0 +47,0 @@ expect(res.errors.length).to.eql(1); |
@@ -1,23 +0,9 @@ | ||
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
var Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/assigned-rule'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
flag: {type: 'boolean'}, | ||
} | ||
var descriptor = { | ||
id: { | ||
foo: 'bar', | ||
test: function(cb) { | ||
expect(this.foo).to.eql('bar'); | ||
cb(); | ||
} | ||
} | ||
} | ||
it('should access rule property from validator function', function(done) { | ||
it('should access rule property from test function', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({id: 'mock'}, function(err, res) { | ||
schema.validate({id: 'mock'}, function() { | ||
done(); | ||
@@ -24,0 +10,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/boolean'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
flag: {type: 'boolean'}, | ||
} | ||
it('should error on non-boolean type', function(done) { | ||
@@ -11,0 +8,0 @@ var schema = new Schema(descriptor); |
@@ -13,6 +13,7 @@ var expect = require('chai').expect | ||
it('should clone array', function(done) { | ||
var value = [1,2,3]; | ||
expect(Schema.clone(value)).to.eql([1,2,3]); | ||
var value = [1,2,['foo']]; | ||
expect(Schema.clone(value)).to.eql([1,2,['foo']]); | ||
done(); | ||
}); | ||
}); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/date') | ||
, noformat = require('../fixtures/schema/date-no-format') | ||
, pattern = require('../fixtures/schema/date-pattern') | ||
, range = require('../fixtures/schema/date-range'); | ||
describe('async-validate:', function(done) { | ||
describe('async-validate:', function() { | ||
var ptn = /^([\d]{4})-([\d]{2})-([\d]{2})$/; | ||
var date = { | ||
type: 'date', | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/, | ||
format: 'YYYY-MM-DD' | ||
} | ||
it('should error on invalid date value using a format', function(done) { | ||
var descriptor = { | ||
active: {type: 'date', format: 'YYYY-MM-DD'} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -28,6 +22,3 @@ schema.validate({active: '2013-06-50'}, function(err, res) { | ||
function(done) { | ||
var descriptor = { | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(noformat); | ||
schema.validate({active: '2011-10-10T10:20:90'}, function(err, res) { | ||
@@ -44,6 +35,3 @@ expect(res.errors.length).to.eql(1); | ||
function(done) { | ||
var descriptor = { | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(noformat); | ||
schema.validate({active: 'not a date'}, function(err, res) { | ||
@@ -60,14 +48,8 @@ expect(res.errors.length).to.eql(1); | ||
function(done) { | ||
var descriptor = { | ||
active: { | ||
type: 'date', | ||
format: 'YYYY-MM-DD', | ||
pattern: ptn | ||
} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(pattern); | ||
schema.validate({active: '13-06-24'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'active value 13-06-24 does not match pattern ' + ptn); | ||
'active value 13-06-24 does not match pattern ' | ||
+ pattern.fields.active.pattern); | ||
done(); | ||
@@ -79,5 +61,2 @@ }); | ||
it('should validate date value using a format', function(done) { | ||
var descriptor = { | ||
active: {type: 'date', format: 'YYYY-MM-DD'} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -116,7 +95,3 @@ schema.validate({active: '2013-06-24'}, function(err, res) { | ||
it('should validate optional date range reference', function(done) { | ||
var descriptor = { | ||
start: date, | ||
end: date | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(range); | ||
schema.validate({start: '', end: '2013-06-24'}, function(err, res) { | ||
@@ -123,0 +98,0 @@ expect(err).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/deep') | ||
, required = require('../fixtures/schema/deep-required') | ||
, details = require('../fixtures/schema/deep-details') | ||
, arrayFields = require('../fixtures/schema/deep-array') | ||
, deepObject = require('../fixtures/schema/deep-object'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
address: { | ||
type: 'object', | ||
fields: { | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
} | ||
} | ||
var required = { | ||
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 details = { | ||
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'} | ||
} | ||
} | ||
} | ||
//it('should error on deep rule (with bail out options)', function(done) { | ||
//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 schema = new Schema(descriptor); | ||
//schema.validate({address: {}}, function(err, res) { | ||
//expect(res.errors.length).to.eql(2); | ||
//expect(res.errors[0].message).to.eql('name is required'); | ||
//expect(res.errors[1].message).to.eql('street is required'); | ||
////expect(res.errors[2].message).to.eql('city is required'); | ||
////expect(res.errors[3].message).to.eql('invalid zip'); | ||
//done(); | ||
//}); | ||
//}); | ||
it('should error on invalid deep rule (required/no matching property)', | ||
@@ -75,3 +22,3 @@ function(done) { | ||
it('should error on invalid deep rule (required and validation failure on deep rule)', | ||
it('should error on invalid deep rule (required and failure on deep rule)', | ||
function(done) { | ||
@@ -91,13 +38,3 @@ var schema = new Schema(details); | ||
it('should error on deep rule (array type length mismatch)', function(done) { | ||
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} | ||
} | ||
} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(arrayFields); | ||
schema.validate({ roles: ['admin', 'user'] }, function(err, res) { | ||
@@ -112,19 +49,3 @@ expect(res.errors.length).to.eql(2); | ||
it('should error on invalid multiple deep rule', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: 'object', required: true, | ||
fields: { | ||
house: { | ||
type: 'object', required: true, | ||
fields: { | ||
name: {type: 'string', required: true}, | ||
number: {type: 'string', required: true} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(deepObject); | ||
schema.validate({ address: {house: {}} }, function(err, res) { | ||
@@ -150,13 +71,3 @@ expect(res.errors.length).to.eql(2); | ||
it('should validate deep rule (array type)', function(done) { | ||
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} | ||
} | ||
} | ||
} | ||
var schema = new Schema(descriptor) | ||
var schema = new Schema(arrayFields) | ||
, source = {roles: ['admin', 'user', 'guest']}; | ||
@@ -170,3 +81,2 @@ schema.validate(source, function(err, res) { | ||
}); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/enum'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
role: {type: 'enum', list: ['admin', 'user', 'guest']} | ||
} | ||
it('should error on invalid enum value', function(done) { | ||
@@ -11,0 +8,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/float'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
ratio: {type: 'float'}, | ||
} | ||
it('should error when a number is not a float', function(done) { | ||
@@ -11,0 +8,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/function') | ||
, length = require('../fixtures/schema/function-length') | ||
, min = require('../fixtures/schema/function-min') | ||
, max = require('../fixtures/schema/function-max') | ||
, range = require('../fixtures/schema/function-range'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
mock: {type: 'function'}, | ||
} | ||
it('should error on value that is not a function', function(done) { | ||
@@ -20,6 +21,3 @@ var schema = new Schema(descriptor); | ||
it('should error on invalid arity (len: 1)', function(done) { | ||
var descriptor = { | ||
mock: {type: 'function', len: 1}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(length); | ||
schema.validate({mock: function(){}}, function(err, res) { | ||
@@ -33,8 +31,4 @@ expect(res.errors.length).to.eql(1); | ||
it('should error on invalid arity (min: 1)', function(done) { | ||
var descriptor = { | ||
mock: {type: 'function', min: 1}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(min); | ||
schema.validate({mock: function(){}}, function(err, res) { | ||
@@ -49,7 +43,4 @@ expect(res.errors.length).to.eql(1); | ||
it('should error on invalid arity (max: 0)', function(done) { | ||
var descriptor = { | ||
mock: {type: 'function', max: 0}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({mock: function(foo){}}, function(err, res) { | ||
var schema = new Schema(max); | ||
schema.validate({mock: function(foo){foo();}}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -63,7 +54,6 @@ expect(res.errors[0].message).to.eql( | ||
it('should error on invalid arity (min: 0, max: 1)', function(done) { | ||
var descriptor = { | ||
mock: {type: 'function', min: 0, max: 1}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({mock: function(foo, bar){}}, function(err, res) { | ||
var schema = new Schema(range) | ||
, source = {mock: function(foo, bar){foo();bar()}}; | ||
schema.validate(source, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -70,0 +60,0 @@ expect(res.errors[0].message).to.eql( |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, Component = require('../fixtures/component') | ||
, descriptor = require('../fixtures/schema/instanceof') | ||
, anonymous = require('../fixtures/schema/instanceof-anonymous') | ||
, message = require('../fixtures/schema/instanceof-message'); | ||
function Component(){} | ||
describe("async-validate:", function() { | ||
it("should error on type as class (instanceof)", function(done) { | ||
var descriptor = { | ||
instance: { | ||
type: Component | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Array()}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({instance: []}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -24,9 +21,4 @@ expect(res.errors[0].message).to.eql( | ||
it("should error on type as class (instanceof) anonymous", function(done) { | ||
var descriptor = { | ||
instance: { | ||
type: function(){} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Array()}, function(err, res) { | ||
var schema = new Schema(anonymous); | ||
schema.validate({instance: []}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -40,14 +32,4 @@ expect(res.errors[0].message).to.eql( | ||
it("should error on type as class (instanceof) w/ message", function(done) { | ||
var descriptor = { | ||
instance: { | ||
type: Component, | ||
message: function(message, parameters) { | ||
message = '%s is not a %s'; | ||
parameters[1] = this.rule.Type.name; | ||
return this.format.apply(this, [message].concat(parameters)); | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Array()}, function(err, res) { | ||
var schema = new Schema(message); | ||
schema.validate({instance: []}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -60,14 +42,4 @@ expect(res.errors[0].message).to.eql('instance is not a Component'); | ||
it("should validate on type as class (instanceof)", function(done) { | ||
var descriptor = { | ||
instance: { | ||
type: Component, | ||
message: function(message, parameters) { | ||
message = '%s is not a %s'; | ||
parameters[1] = this.rule.Type.name; | ||
return this.format.apply(this, [message].concat(parameters)); | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Component()}, function(err, res) { | ||
var schema = new Schema(message); | ||
schema.validate({instance: new Component()}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -74,0 +46,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/integer'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
port: {type: 'integer'}, | ||
} | ||
it('should error on number not an integer', function(done) { | ||
@@ -11,0 +8,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/internal-error'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
mock: function(cb) { | ||
cb(new Error('query error')); | ||
}, | ||
next: function(cb) { | ||
throw new Error('rule validation function invoked unexpectedly'); | ||
} | ||
} | ||
it('should callback with error', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(err, res) { | ||
schema.validate({}, function(err) { | ||
function fn() { | ||
@@ -19,0 +11,0 @@ throw err; |
@@ -27,3 +27,3 @@ var expect = require('chai').expect | ||
}, | ||
function complete(err, results) { | ||
function complete(err) { | ||
function fn() { | ||
@@ -42,3 +42,3 @@ throw err; | ||
}, | ||
function complete(err, results) { | ||
function complete(err) { | ||
function fn() { | ||
@@ -45,0 +45,0 @@ throw err; |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/length-string') | ||
, number = require('../fixtures/schema/length-number') | ||
, array = require('../fixtures/schema/length-array'); | ||
@@ -7,9 +10,7 @@ describe("async-validate:", function() { | ||
it("should error on invalid string length", function(done) { | ||
var descriptor = { | ||
name: {type: "string", len: 10}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "user"}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name must be exactly 10 characters'); | ||
expect(res.errors[0].message).to.eql( | ||
'name must be exactly 8 characters'); | ||
done(); | ||
@@ -20,6 +21,3 @@ }); | ||
it("should error on invalid number length", function(done) { | ||
var descriptor = { | ||
port: {type: "number", len: 80}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(number); | ||
schema.validate({port: 8080}, function(err, res) { | ||
@@ -33,9 +31,7 @@ expect(res.errors.length).to.eql(1); | ||
it("should error on invalid array length", function(done) { | ||
var descriptor = { | ||
roles: {type: "array", len: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(array); | ||
schema.validate({roles: ["user"]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('roles must be exactly 2 in length'); | ||
expect(res.errors[0].message).to.eql( | ||
'roles must be exactly 2 in length'); | ||
done(); | ||
@@ -46,5 +42,2 @@ }); | ||
it("should validate string length", function(done) { | ||
var descriptor = { | ||
name: {type: "string", len: 8}, | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -59,6 +52,3 @@ schema.validate({name: "username"}, function(err, res) { | ||
it("should validate number length", function(done) { | ||
var descriptor = { | ||
port: {type: "number", len: 80}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(number); | ||
schema.validate({port: 80}, function(err, res) { | ||
@@ -72,6 +62,3 @@ expect(err).to.eql(null); | ||
it("should validate array length", function(done) { | ||
var descriptor = { | ||
roles: {type: "array", len: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(array); | ||
schema.validate({roles: ["user", "admin"]}, function(err, res) { | ||
@@ -78,0 +65,0 @@ expect(err).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/match') | ||
, source = { | ||
nonmatch: 'foo', | ||
address1: 'foo', | ||
address2: 'bar', | ||
address3: false | ||
}; | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
type: 'object', | ||
required: true, | ||
name: {type: 'string', required: true}, | ||
all: { | ||
match: /./, | ||
type: 'string' | ||
} | ||
} | ||
, source = {address1: 'foo', address2: 'bar', address3: false}; | ||
it('should error on invalid expanded property (match)', function(done) { | ||
@@ -18,0 +14,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/message-object'); | ||
@@ -7,13 +8,4 @@ describe('async-validate:', function() { | ||
it('should validate using a custom error message as min', function(done) { | ||
var descriptor = { | ||
num: { | ||
type: 'number', min: 0, max: 10, | ||
message: { | ||
min: 'Number may not be below zero', | ||
max: 'Number may not be above ten', | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({num: -1}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({num: -1}, function(err, res) { | ||
expect(res.errors[0].message).to.eql('Number may not be below zero'); | ||
@@ -25,13 +17,4 @@ done(); | ||
it('should validate using a custom error message as max', function(done) { | ||
var descriptor = { | ||
num: { | ||
type: 'number', min: 0, max: 10, | ||
message: { | ||
min: 'Number may not be below zero', | ||
max: 'Number may not be above ten', | ||
} | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({num: 11}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({num: 11}, function(err, res) { | ||
expect(res.errors[0].message).to.eql('Number may not be above ten'); | ||
@@ -38,0 +21,0 @@ done(); |
var expect = require('chai').expect | ||
, schema = require('../../index') | ||
, msg = require('../../messages'); | ||
, Schema = require('../../index') | ||
, msg = require('../../messages') | ||
, descriptor = require('../fixtures/schema/message-string') | ||
, func = require('../fixtures/schema/message-function') | ||
, funcerror = require('../fixtures/schema/message-function-error') | ||
, override = require('../fixtures/schema/message-string-override'); | ||
@@ -8,3 +12,4 @@ describe('async-validate:', function() { | ||
// clone of the default messages | ||
var clone = schema.clone(msg); | ||
var clone = Schema.clone(msg); | ||
// change a message | ||
@@ -14,7 +19,4 @@ clone.required = '%s is a required field'; | ||
it('should validate using a custom error message', function(done) { | ||
var descriptor = { | ||
name: {type: 'string', required: true, message: 'Name is required'}, | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -27,15 +29,4 @@ expect(res.errors[0].message).to.eql('Name is required'); | ||
it('should validate using a custom error message function', function(done) { | ||
var descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function(message, parameters) { | ||
expect(message).to.be.a('string'); | ||
expect(parameters).to.be.an('array'); | ||
return 'Name is required'; | ||
} | ||
}, | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
var schema = new Schema(func); | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -49,15 +40,4 @@ expect(res.errors[0].message).to.eql('Name is required'); | ||
function(done) { | ||
var descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: function(message, parameters) { | ||
expect(message).to.be.a('string'); | ||
expect(parameters).to.be.an('array'); | ||
return new Error('Name is required'); | ||
} | ||
}, | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
var schema = new Schema(funcerror); | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -71,11 +51,8 @@ expect(res.errors[0].message).to.eql('Name is required'); | ||
it('should validate using custom messages', function(done) { | ||
var descriptor = { | ||
name: {type: 'string', required: true}, | ||
} | ||
var validator = new schema(descriptor); | ||
var schema = new Schema(override); | ||
// assign updated messages to the schema | ||
validator.messages(clone); | ||
// assign updated messages to the Schema | ||
schema.messages(clone); | ||
validator.validate({}, function(err, res) { | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -87,17 +64,2 @@ expect(res.errors[0].message).to.eql('name is a required field'); | ||
it('should use raise() helper method', function(done) { | ||
var descriptor = { | ||
name: function(cb) { | ||
this.raise('%s is a required field', this.field); | ||
cb(); | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name is a required field'); | ||
done(); | ||
}); | ||
}); | ||
}); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/multiple-types') | ||
, required = require('../fixtures/schema/multiple-types-required'); | ||
describe('async-validate:', function() { | ||
function Component(){}; | ||
var descriptor = { | ||
prop: {type: [Boolean, 'string', Component, function(){}]}, | ||
} | ||
var required = { | ||
prop: {type: [Boolean, 'string', Component, function(){}], required: true}, | ||
} | ||
it('should error on invalid type with multiple types array', function(done) { | ||
@@ -17,0 +9,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/null'); | ||
describe("async-validate:", function() { | ||
var descriptor = { | ||
value: { | ||
type: 'null' | ||
} | ||
} | ||
it("should error on non-null value", function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({value: true}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({value: true}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -21,4 +16,4 @@ expect(res.errors[0].message).to.eql('value is not null'); | ||
it("should validate on type null", function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({value: null}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({value: null}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -31,4 +26,4 @@ expect(res).to.eql(null); | ||
it("should validate on type null with no value", function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -35,0 +30,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/number') | ||
, min = require('../fixtures/schema/number-min') | ||
, max = require('../fixtures/schema/number-max') | ||
, range = require('../fixtures/schema/number-range'); | ||
@@ -7,5 +11,2 @@ describe('async-validate:', function() { | ||
it('should error on not a number', function(done) { | ||
var descriptor = { | ||
port: {type: 'number'}, | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -21,6 +22,3 @@ schema.validate({port: '80'}, function(err, res) { | ||
it('should error on number greater than a minimum value', function(done) { | ||
var descriptor = { | ||
port: {type: 'number', min: 8080}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(min); | ||
schema.validate({port: 80}, function(err, res) { | ||
@@ -34,8 +32,5 @@ expect(res.errors.length).to.eql(1); | ||
it('should error on number number greater than a minimum value', | ||
it('should error on number greater than a maximum value', | ||
function(done) { | ||
var descriptor = { | ||
port: {type: 'number', max: 80}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(max); | ||
schema.validate({port: 8080}, function(err, res) { | ||
@@ -52,6 +47,3 @@ expect(res.errors.length).to.eql(1); | ||
function(done) { | ||
var descriptor = { | ||
port: {type: 'number', min: 80, max: 1024}, | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(range); | ||
schema.validate({port: 8080}, function(err, res) { | ||
@@ -58,0 +50,0 @@ expect(res.errors.length).to.eql(1); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/object-additional'); | ||
@@ -7,14 +8,2 @@ describe('async-validate:', function() { | ||
it('should error on invalid object (additional properties)', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: 'object', | ||
required: true, | ||
additional: false, | ||
fields: { | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
} | ||
} | ||
var source = { | ||
@@ -25,7 +14,7 @@ address: { | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
var expected = 'extraneous fields (name) found in address'; | ||
@@ -39,14 +28,2 @@ expect(res.errors.length).to.eql(1); | ||
it('should validate with no additional properties', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: 'object', | ||
required: true, | ||
additional: false, | ||
fields: { | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
} | ||
} | ||
var source = { | ||
@@ -56,7 +33,7 @@ address: { | ||
city: 'Mock City', | ||
zip: '12345678', | ||
zip: '12345678' | ||
} | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -63,0 +40,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/object'); | ||
describe("async-validate:", function() { | ||
var descriptor = { | ||
address: {type: "object", required: true} | ||
} | ||
it("should error on invalid object (array specified)", function(done) { | ||
@@ -11,0 +8,0 @@ var schema = new Schema(descriptor); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/options') | ||
, pattern = require('../fixtures/schema/options-pattern'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
firstname: {type: 'string', required: true}, | ||
surname: {type: 'string', required: true} | ||
} | ||
it('should error with multiple errors', function(done) { | ||
@@ -36,6 +33,3 @@ var schema = new Schema(descriptor); | ||
it('should error with single option', function(done) { | ||
var descriptor = { | ||
name: {type: 'string', required: true, min: 10, pattern: /^[^-].*$/} | ||
} | ||
var schema = new Schema(descriptor) | ||
var schema = new Schema(pattern) | ||
, source = {name: '-name'} | ||
@@ -42,0 +36,0 @@ , opts = {first: true, single: true}; |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/parallel'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
name: {type: 'string', len: 8}, | ||
} | ||
it('should use default series iteration', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: 'username', surname: 'foo'}, | ||
schema.validate({}, | ||
function(err, res) { | ||
@@ -23,5 +20,3 @@ expect(err).to.eql(null); | ||
var schema = new Schema(descriptor) | ||
, source = {name: 'username', surname: 'foo'}; | ||
schema.validate(source, {parallel: true}, | ||
schema.validate({}, {parallel: true}, | ||
function(err, res) { | ||
@@ -28,0 +23,0 @@ expect(err).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/placeholder'); | ||
describe("async-validate:", function() { | ||
var descriptor = { | ||
list: { | ||
type: 'array', | ||
values: {type: 'integer'}, | ||
placeholder: function() { | ||
return []; | ||
} | ||
} | ||
} | ||
@@ -18,3 +10,3 @@ it("should set default value for field (placeholder)", function(done) { | ||
, source = {}; | ||
schema.validate(source, function(err, res) { | ||
schema.validate(source, function() { | ||
expect(source.list).to.eql([]); | ||
@@ -21,0 +13,0 @@ done(); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/plugin'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
id: {type: 'id'}, | ||
} | ||
before(function(done) { | ||
// load plugin definition | ||
schema.plugin([require('../fixtures/plugin')]); | ||
Schema.plugin([require('../fixtures/plugin')]); | ||
done(); | ||
@@ -17,4 +14,4 @@ }); | ||
it('should error using custom plugin', function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({id: '-hyphen'}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({id: '-hyphen'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
@@ -27,4 +24,4 @@ expect(res.errors[0].message).to.eql('id is not a valid identifier'); | ||
it('should validate custom plugin', function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({id: 'my-valid-id'}, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({id: 'my-valid-id'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -31,0 +28,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/regexp'); | ||
describe("async-validate:", function() { | ||
var descriptor = { | ||
re: {type: "regexp"}, | ||
} | ||
it("should error on regexp string (positive lookbehind unsupported)", | ||
@@ -14,3 +11,2 @@ function(done) { | ||
, source = {re: "(?<=(category=))[a-z-]+"}; | ||
schema.validate(source, function(err, res) { | ||
@@ -17,0 +13,0 @@ expect(res.errors.length).to.eql(1); |
@@ -13,12 +13,5 @@ var expect = require('chai').expect | ||
it("should define without fields", function(done) { | ||
var schema = new Schema({name: {type: 'string'}}); | ||
expect(schema.rules).to.be.an('object'); | ||
expect(schema.rules.fields).to.be.an('object'); | ||
done(); | ||
}); | ||
it("should error with no rules", function(done) { | ||
function fn() { | ||
var schema = new Schema(); | ||
new Schema(); | ||
} | ||
@@ -33,3 +26,3 @@ expect(fn).throws(Error); | ||
function fn() { | ||
var schema = new Schema(null); | ||
new Schema(null); | ||
} | ||
@@ -41,12 +34,2 @@ expect(fn).throws(Error); | ||
//it("should error on validate with no rules", function(done) { | ||
//var schema = new Schema({}); | ||
//function fn() { | ||
//schema.validate({}); | ||
//} | ||
//expect(fn).throws(Error); | ||
//expect(fn).throws(/cannot validate with no rules/i); | ||
//done(); | ||
//}); | ||
it("should error on validate with no source", function(done) { | ||
@@ -72,5 +55,12 @@ var schema = new Schema({name: function(cb){cb()}}); | ||
it("should error on validate with unknown type", function(done) { | ||
var descriptor = { | ||
fields: { | ||
name: { | ||
type: 'unknown-type' | ||
} | ||
} | ||
} | ||
, schema = new Schema(descriptor); | ||
it("should error on validate with unknown type", function(done) { | ||
var schema = new Schema({name: {type: 'unknown-type'}}); | ||
function fn() { | ||
@@ -84,3 +74,2 @@ schema.validate({}, function noop(){}); | ||
}); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/source-additional'); | ||
describe("async-validate:", function() { | ||
var descriptor = { | ||
type: 'object', | ||
additional: false, | ||
// trigger code path whereby transform cannot assign inline | ||
// on root object as there is no parent object to assign to | ||
transform: function(value) { | ||
return value; | ||
}, | ||
fields: { | ||
address: { | ||
type: "object", | ||
required: true | ||
} | ||
} | ||
} | ||
it("should error on invalid source object (additional properties)", | ||
@@ -28,4 +13,4 @@ function(done) { | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
// NOTE: `source` is the default field name for root object | ||
@@ -44,4 +29,4 @@ var expected = 'extraneous fields (name) found in source'; | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(err, res) { | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -48,0 +33,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, arr = {type: 'array'} | ||
, bool = {type: 'boolean'} | ||
, float = {type: 'float'} | ||
, integer = {type: 'integer'} | ||
, number = {type: 'number'} | ||
, object = {type: 'object'} | ||
, date = {type: 'date'} | ||
, regexp = {type: 'regexp'} | ||
, nil = {type: 'null'} | ||
, func = {type: 'function'} | ||
, string = {type: 'string'} | ||
, pattern = {type: 'string', pattern: /^foo$/} | ||
, dateFormat = {type: 'date', format: 'YYYY-MM-DD'} | ||
, enumerable = {type: 'enum', list: ['foo', 'bar']} | ||
@@ -7,7 +21,6 @@ describe("async-validate:", function() { | ||
it("should error on source as array type", function(done) { | ||
var descriptor = {type: 'array'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(arr) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -22,7 +35,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as array type", function(done) { | ||
var descriptor = {type: 'array'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(arr) | ||
, source = []; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -35,7 +47,6 @@ expect(res).to.eql(null); | ||
it("should error on source as boolean type", function(done) { | ||
var descriptor = {type: 'boolean'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(bool) | ||
, source = {}; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -50,7 +61,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as boolean type", function(done) { | ||
var descriptor = {type: 'boolean'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(bool) | ||
, source = true; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -63,7 +73,6 @@ expect(res).to.eql(null); | ||
it("should error on source as date type", function(done) { | ||
var descriptor = {type: 'date', format: 'YYYY-MM-DD'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(dateFormat) | ||
, source = '2013-06-50'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -78,7 +87,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as date type", function(done) { | ||
var descriptor = {type: 'date'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(date) | ||
, source = true; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -92,7 +100,6 @@ expect(res).to.eql(null); | ||
it("should error on source as enum type", function(done) { | ||
var descriptor = {type: 'enum', list: ['foo', 'bar']} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(enumerable) | ||
, source = 'qux'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -107,7 +114,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as enum type", function(done) { | ||
var descriptor = {type: 'enum', list: ['foo', 'bar']} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(enumerable) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -120,7 +126,6 @@ expect(res).to.eql(null); | ||
it("should error on source as float type", function(done) { | ||
var descriptor = {type: 'float'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(float) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -135,7 +140,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as float type", function(done) { | ||
var descriptor = {type: 'float'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(float) | ||
, source = 1.667; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -148,7 +152,6 @@ expect(res).to.eql(null); | ||
it("should error on source as integer type", function(done) { | ||
var descriptor = {type: 'integer'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(integer) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -163,7 +166,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as integer type", function(done) { | ||
var descriptor = {type: 'integer'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(integer) | ||
, source = 1024; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -176,7 +178,6 @@ expect(res).to.eql(null); | ||
it("should error on source as function type", function(done) { | ||
var descriptor = {type: 'function'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(func) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -191,7 +192,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as function type", function(done) { | ||
var descriptor = {type: 'function'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(func) | ||
, source = function noop(){}; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -204,7 +204,6 @@ expect(res).to.eql(null); | ||
it("should error on source as null type", function(done) { | ||
var descriptor = {type: 'null'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(nil) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -219,7 +218,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as null type", function(done) { | ||
var descriptor = {type: 'null'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(nil) | ||
, source = null; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -232,7 +230,6 @@ expect(res).to.eql(null); | ||
it("should error on source as number type", function(done) { | ||
var descriptor = {type: 'number'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(number) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -247,7 +244,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as number type", function(done) { | ||
var descriptor = {type: 'number'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(number) | ||
, source = 3; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -260,7 +256,6 @@ expect(res).to.eql(null); | ||
it("should error on source as object type", function(done) { | ||
var descriptor = {type: 'object'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(object) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -275,7 +270,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as object type", function(done) { | ||
var descriptor = {type: 'object'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(object) | ||
, source = {}; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -288,7 +282,6 @@ expect(res).to.eql(null); | ||
it("should error on source as pattern type", function(done) { | ||
var descriptor = {type: 'string', pattern: /^foo$/} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(pattern) | ||
, source = 'bar'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -303,7 +296,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as pattern type", function(done) { | ||
var descriptor = {type: 'string', pattern: /^foo$/} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(pattern) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -316,7 +308,6 @@ expect(res).to.eql(null); | ||
it("should error on source as regexp type", function(done) { | ||
var descriptor = {type: 'regexp'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(regexp) | ||
, source = '+'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -331,7 +322,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as regexp type", function(done) { | ||
var descriptor = {type: 'regexp'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(regexp) | ||
, source = /^foo$/; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -343,9 +333,7 @@ expect(res).to.eql(null); | ||
it("should error on source as string type", function(done) { | ||
var descriptor = {type: 'string'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(string) | ||
, source = {}; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -360,7 +348,6 @@ expect(res.errors.length).to.eql(1); | ||
it("should validate on source as string type", function(done) { | ||
var descriptor = {type: 'string'} | ||
, validator = new schema(descriptor) | ||
var schema = new Schema(string) | ||
, source = 'foo'; | ||
validator.validate(source, function(err, res) { | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
@@ -367,0 +354,0 @@ expect(res).to.eql(null); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/state'); | ||
@@ -8,22 +9,2 @@ describe('async-validate:', function() { | ||
, opts = {state: state} | ||
, descriptor = { | ||
email: [ | ||
{type: 'string', required: true, pattern: /^.+@.+\..+/}, | ||
function parse(cb) { | ||
var at = this.value.indexOf('@') | ||
, user = this.value.substr(0, at) | ||
, domain = this.value.substr(at + 1); | ||
// assign to validation state | ||
this.state.email = {user: user, domain: domain}; | ||
cb(); | ||
}, | ||
function assert(cb) { | ||
expect(this.state).to.equal(state); | ||
expect(this.state.email).to.be.an('object'); | ||
expect(this.state.email.user).to.eql('user'); | ||
expect(this.state.email.domain).to.eql('example.com'); | ||
cb(); | ||
} | ||
] | ||
} | ||
, source = {email: 'user@example.com'}; | ||
@@ -33,3 +14,3 @@ | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, opts, function(err, res) { | ||
schema.validate(source, opts, function() { | ||
expect(state.email).to.be.an('object'); | ||
@@ -36,0 +17,0 @@ expect(state.email.user).to.eql('user'); |
@@ -6,27 +6,10 @@ var expect = require('chai').expect | ||
it('should error on a custom schema function', function(done) { | ||
it('should error on required string field', function(done) { | ||
var descriptor = { | ||
name: function(cb) { | ||
if(!/^[a-z0-9]+$/.test(this.value)) { | ||
this.raise( | ||
'%s must be lowercase alphanumeric characters', | ||
this.field); | ||
} | ||
cb(); | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: 'Firstname'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name must be lowercase alphanumeric characters'); | ||
done(); | ||
}); | ||
}); | ||
it('should error on required string field', function(done) { | ||
var descriptor = { | ||
name: {type: 'string', required: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({noname: 'field'}, function(err, res) { | ||
@@ -42,3 +25,6 @@ expect(res.errors.length).to.eql(1); | ||
var descriptor = { | ||
name: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -56,3 +42,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string'} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string'} | ||
} | ||
} | ||
@@ -70,3 +59,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, min: 8} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, min: 8} | ||
} | ||
} | ||
@@ -86,3 +78,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, max: 2} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, max: 2} | ||
} | ||
} | ||
@@ -102,3 +97,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, min: 6, max: 8} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, min: 6, max: 8} | ||
} | ||
} | ||
@@ -118,3 +116,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, min: 2, max: 4} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, min: 2, max: 4} | ||
} | ||
} | ||
@@ -133,3 +134,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', pattern: /^[0-9]+$/} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', pattern: /^[0-9]+$/} | ||
} | ||
} | ||
@@ -149,3 +153,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', whitespace: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', whitespace: true} | ||
} | ||
} | ||
@@ -164,3 +171,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
} | ||
@@ -178,3 +188,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true} | ||
} | ||
} | ||
@@ -193,3 +206,6 @@ var schema = new Schema(descriptor); | ||
var descriptor = { | ||
name: {type: 'string', required: true, min: 6, max: 20} | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string', required: true, min: 6, max: 20} | ||
} | ||
} | ||
@@ -205,20 +221,2 @@ var schema = new Schema(descriptor); | ||
it('should validate after failure', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: ''}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name cannot be empty'); | ||
schema.validate({name: 'user'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
} | ||
); | ||
}); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
, Schema = require('../../index') | ||
, descriptor = require('../fixtures/schema/transform'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
name: { | ||
type: 'string', | ||
required: true, pattern: /^[a-z]+$/, | ||
transform: function(value) { | ||
return value.trim(); | ||
} | ||
} | ||
} | ||
it('should transform by stripping whitespace', function(done) { | ||
@@ -17,0 +8,0 @@ var schema = new Schema(descriptor) |
@@ -6,8 +6,83 @@ var expect = require('chai').expect | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
name: {type: 'string'}, | ||
age: {type: 'integer', required: false} | ||
} | ||
} | ||
var string = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'string', required: false} | ||
} | ||
} | ||
var func = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'function', required: false} | ||
} | ||
} | ||
var regexp = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'regexp', required: false} | ||
} | ||
} | ||
var float = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'float', required: false} | ||
} | ||
} | ||
var number = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'number', required: false} | ||
} | ||
} | ||
var object = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'object', required: false} | ||
} | ||
} | ||
var arr = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'array', required: false} | ||
} | ||
} | ||
var bool = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'boolean', required: false} | ||
} | ||
} | ||
var date = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'date', required: false} | ||
} | ||
} | ||
var enumerable = { | ||
type: 'object', | ||
fields: { | ||
mock: {type: 'enum', required: false} | ||
} | ||
} | ||
it('should error on invalid integer field if not required (first/single)', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: 'string'}, | ||
age: {type: 'integer', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -26,6 +101,2 @@ var source = {age: 'abc', name : 'User'}; | ||
it('should allow undefined integer field if not required', function(done) { | ||
var descriptor = { | ||
name: {type: 'string'}, | ||
age: {type: 'integer', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -43,6 +114,2 @@ var source = {age: undefined, name : 'User'}; | ||
function(done) { | ||
var descriptor = { | ||
name: {type: 'string'}, | ||
age: {type: 'integer', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -60,6 +127,3 @@ var source = {age: undefined, name : 'User'}; | ||
it('should allow undefined array field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'array', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(arr); | ||
var source = {mock: undefined}; | ||
@@ -75,6 +139,3 @@ var opts = {}; | ||
it('should allow undefined boolean field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'boolean', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(bool); | ||
var source = {mock: undefined}; | ||
@@ -90,6 +151,3 @@ var opts = {}; | ||
it('should allow undefined date field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'date', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(date); | ||
var source = {mock: undefined}; | ||
@@ -105,6 +163,3 @@ var opts = {}; | ||
it('should allow undefined enum field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'enum', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(enumerable); | ||
var source = {mock: undefined}; | ||
@@ -120,6 +175,3 @@ var opts = {}; | ||
it('should allow undefined float field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'float', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(float); | ||
var source = {mock: undefined}; | ||
@@ -135,6 +187,3 @@ var opts = {}; | ||
it('should allow undefined function field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'function', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(func); | ||
var source = {mock: undefined}; | ||
@@ -150,6 +199,3 @@ var opts = {}; | ||
it('should allow undefined number field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'number', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(number); | ||
var source = {mock: undefined}; | ||
@@ -165,6 +211,3 @@ var opts = {}; | ||
it('should allow undefined object field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'object', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(object); | ||
var source = {mock: undefined}; | ||
@@ -180,6 +223,3 @@ var opts = {}; | ||
it('should allow undefined regexp field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'regexp', required: false} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(regexp); | ||
var source = {mock: undefined}; | ||
@@ -195,6 +235,3 @@ var opts = {}; | ||
it('should allow undefined string field if not required', function(done) { | ||
var descriptor = { | ||
mock: {type: 'string'} | ||
} | ||
var schema = new Schema(descriptor); | ||
var schema = new Schema(string); | ||
var source = {mock: undefined}; | ||
@@ -201,0 +238,0 @@ var opts = {}; |
var expect = require('chai').expect | ||
, Schema = require('../../index') | ||
, Model = require('../fixtures/model'); | ||
, Model = require('../fixtures/model') | ||
, descriptor = require('../fixtures/schema/vars'); | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
id: {type: 'id'}, | ||
} | ||
before(function(done) { | ||
@@ -21,3 +18,2 @@ // load plugin definition | ||
, source = {id: 'qux'}; | ||
schema.validate(source, opts, function(err, res) { | ||
@@ -24,0 +20,0 @@ expect(res.errors.length).to.eql(1); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
200632
186
4321
960