async-validate
Advanced tools
Comparing version 0.11.2 to 0.12.0
// assign a function to a rule | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ id: { |
@@ -5,2 +5,3 @@ // bail on first error encountered | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ address: { |
// validate properties of a nested object | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ address: { |
@@ -5,2 +5,3 @@ // assign a function as a rule | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ id: function(cb) { |
@@ -5,2 +5,3 @@ // validate a field is an instanceof a function | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ comp: {type: Component, required: true} |
// validate a field length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ func: {type: 'function', required: true, len: 1} |
// validate a field has a maximum length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ func: {type: 'function', required: true, max: 1} |
@@ -5,2 +5,3 @@ // clone default messages | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ name: { |
// override error message with function | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ name: { |
@@ -5,2 +5,3 @@ // override default error message | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ name: { |
// override error message | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ name: { |
// validate a field has a minimum length | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ func: {type: 'function', required: true, min: 1} |
@@ -5,2 +5,3 @@ // validate a field with multiple rules | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -7,0 +8,0 @@ id: [ |
// validate a field as one of multiple types | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ flag: {type: ['boolean', Boolean], required: true} |
// validate a field as matching a pattern | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} |
// use a placeholder to set a default value | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ list: { |
// validate a field with a plugin rule | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ id: {type: 'id', required: true} |
// validate a field has a length within a range | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ func: {type: 'function', required: true, min: 1, max: 2} |
// validate a field as required | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ name: {type: 'string', required: true} |
@@ -7,2 +7,3 @@ // pass state information between rule test functions | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -9,0 +10,0 @@ email: [ |
// validate a field type | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ flag: {type: 'boolean', required: true} |
// validate a field as whitespace | ||
var Schema = require('../..') | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -5,0 +6,0 @@ name: {type: 'string', required: true, whitespace: true} |
@@ -99,2 +99,3 @@ Table of Contents | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -141,2 +142,3 @@ id: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -177,2 +179,3 @@ address: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -214,2 +217,3 @@ address: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -248,2 +252,3 @@ id: function(cb) { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -276,2 +281,3 @@ comp: {type: Component, required: true} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -336,2 +342,3 @@ func: {type: 'function', required: true, len: 1} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -370,2 +377,3 @@ func: {type: 'function', required: true, max: 1} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -405,2 +413,3 @@ name: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -441,2 +450,3 @@ name: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -475,2 +485,3 @@ name: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -507,2 +518,3 @@ name: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -536,2 +548,3 @@ func: {type: 'function', required: true, min: 1} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -574,2 +587,3 @@ id: [ | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -602,2 +616,3 @@ flag: {type: ['boolean', Boolean], required: true} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -630,2 +645,3 @@ name: {type: 'string', required: true, pattern: /^[a-z0-9]+$/i} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -664,2 +680,3 @@ list: { | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -707,2 +724,3 @@ id: {type: 'id', required: true} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -741,2 +759,3 @@ func: {type: 'function', required: true, min: 1, max: 2} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -795,2 +814,3 @@ name: {type: 'string', required: true} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -838,3 +858,3 @@ email: [ | ||
``` | ||
[ { [Error: email: could not resolve dns for domain 1442560140154.com] field: 'email' } ] | ||
[ { [Error: email: could not resolve dns for domain 1442566773189.com] field: 'email' } ] | ||
``` | ||
@@ -850,2 +870,3 @@ | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -878,2 +899,3 @@ flag: {type: 'boolean', required: true} | ||
, descriptor = { | ||
type: 'object', | ||
fields: { | ||
@@ -880,0 +902,0 @@ name: {type: 'string', required: true, whitespace: true} |
@@ -6,2 +6,35 @@ var iterator = require('./iterator') | ||
/** | ||
* @private | ||
* | ||
* Validate the type field. | ||
*/ | ||
function validateRule(rule) { | ||
var i; | ||
if(typeof(rule) === 'function' || typeof(rule.test) === 'function') { | ||
return true; | ||
} | ||
function invalid() { | ||
throw new Error( | ||
'type property must be string or function: ' + rule.field); | ||
} | ||
function isValid(type) { | ||
return type | ||
&& (typeof(type) === 'string' || typeof(type) === 'function'); | ||
} | ||
if(Array.isArray(rule.type)) { | ||
for(i = 0;i < rule.type.length;i++) { | ||
if(!isValid(rule.type[i])) { | ||
invalid(); | ||
} | ||
} | ||
}else if(!isValid(rule.type)) { | ||
invalid(); | ||
} | ||
} | ||
/** | ||
* Encapsulates a validation schema. | ||
@@ -126,5 +159,3 @@ * | ||
if(!rule.type && !opts._deep) { | ||
rule.type = typeof(source); | ||
} | ||
validateRule(rule); | ||
@@ -288,4 +319,2 @@ if(typeof(rule.test) !== 'function' | ||
//console.log('run test function %s', rule.test.name); | ||
// invoke rule validation function | ||
@@ -292,0 +321,0 @@ rule.test.call(scope, onValidate); |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.11.2", | ||
"version": "0.12.0", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
var ptn = /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
, schema = { | ||
type: 'object', | ||
fields: { | ||
@@ -4,0 +5,0 @@ active: { |
var date = require('./date-pattern').fields.active | ||
, schema = { | ||
type: 'object', | ||
fields: { | ||
@@ -4,0 +5,0 @@ start: date, |
var expect = require('chai').expect | ||
, schema = { | ||
type: 'object', | ||
fields: { | ||
@@ -4,0 +5,0 @@ email: [ |
@@ -68,3 +68,6 @@ var expect = require('chai').expect | ||
var descriptor = { | ||
active: {type: 'date', format: 'YYYY-MM-DD', local: true} | ||
type: 'object', | ||
fields: { | ||
active: {type: 'date', format: 'YYYY-MM-DD', local: true} | ||
} | ||
} | ||
@@ -80,5 +83,2 @@ var schema = new Schema(descriptor); | ||
it('should validate date value no format (ISO 8601)', function(done) { | ||
var descriptor = { | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
@@ -85,0 +85,0 @@ schema.validate({active: '2011-10-10T10:20:30'}, function(err, res) { |
@@ -6,4 +6,22 @@ var expect = require('chai').expect | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
var unknown = { | ||
type: 'object', | ||
fields: { | ||
name: { | ||
type: 'unknown-type' | ||
} | ||
} | ||
} | ||
it("should define with fields", function(done) { | ||
var schema = new Schema({fields: {name: {type: 'string'}}}); | ||
var schema = new Schema(descriptor); | ||
expect(schema.rules).to.be.an('object'); | ||
@@ -34,3 +52,3 @@ expect(schema.rules.fields).to.be.an('object'); | ||
it("should error on validate with no source", function(done) { | ||
var schema = new Schema({name: function(cb){cb()}}); | ||
var schema = new Schema(descriptor); | ||
function fn() { | ||
@@ -45,3 +63,3 @@ schema.validate(); | ||
it("should error on validate with no callback", function(done) { | ||
var schema = new Schema({name: function(cb){cb()}}); | ||
var schema = new Schema(descriptor); | ||
function fn() { | ||
@@ -56,10 +74,3 @@ schema.validate({}); | ||
it("should error on validate with unknown type", function(done) { | ||
var descriptor = { | ||
fields: { | ||
name: { | ||
type: 'unknown-type' | ||
} | ||
} | ||
} | ||
, schema = new Schema(descriptor); | ||
var schema = new Schema(unknown); | ||
@@ -74,2 +85,23 @@ function fn() { | ||
it("should error with no rule type", function(done) { | ||
function fn() { | ||
var schema = new Schema({}); | ||
schema.validate({}, function noop(){}); | ||
} | ||
expect(fn).throws(Error); | ||
expect(fn).throws(/type property must be string or function/i); | ||
done(); | ||
}); | ||
it("should error with no rule type (multiple types)", function(done) { | ||
var descriptor = {type: [{}]}; | ||
function fn() { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function noop(){}); | ||
} | ||
expect(fn).throws(Error); | ||
expect(fn).throws(/type property must be string or function/i); | ||
done(); | ||
}); | ||
}); |
@@ -180,3 +180,3 @@ var expect = require('chai').expect | ||
fields: { | ||
name: {type: 'string', required: true} | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
@@ -183,0 +183,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
202838
4399