async-validate
Advanced tools
Comparing version 0.5.7 to 0.6.1
@@ -11,5 +11,5 @@ var Schema = require('../../') | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(errors, fields) { | ||
schema.validate(source, function(err, res) { | ||
// error on missing name field | ||
console.dir(errors); | ||
console.dir(res.errors); | ||
}); |
@@ -11,5 +11,5 @@ var Schema = require('../../') | ||
var schema = new Schema(descriptor); | ||
schema.validate(source, function(errors, fields) { | ||
schema.validate(source, function(err, res) { | ||
// error on name field | ||
console.dir(errors); | ||
console.dir(res.errors); | ||
}); |
@@ -128,4 +128,4 @@ ## Guide | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// errors for name, street, city, zip | ||
validator.validate({address: {}}, function(err, res) { | ||
// res.errors contains errors for name, street, city, zip | ||
}); | ||
@@ -153,4 +153,4 @@ ``` | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// now only errors for name and street | ||
validator.validate({address: {}}, function(err, res) { | ||
// now res.errors only contains errors for name and street | ||
}); | ||
@@ -157,0 +157,0 @@ ``` |
@@ -16,4 +16,4 @@ var schema = require('..') | ||
validator.validate(source, function(errors, fields) { | ||
validator.validate(source, function(err, res) { | ||
console.dir(source.name); | ||
}); |
@@ -8,10 +8,14 @@ var Schema = require('..') | ||
schema.validate(source, function(errors, fields) { | ||
if(errors) { | ||
schema.validate(source, function(err, res) { | ||
if(err) { | ||
throw err; | ||
} | ||
if(res) { | ||
// validation failed, errors is an array of all errors | ||
// fields is an object keyed by field name with an array of | ||
// errors per field | ||
return console.dir(errors) | ||
return console.dir(res.errors) | ||
} | ||
// validation passed | ||
}); |
@@ -176,3 +176,3 @@ var iterator = require('./iterator') | ||
function onValidate() { | ||
function onValidate(err) { | ||
var errors = validator.errors; | ||
@@ -182,3 +182,3 @@ | ||
if(options.first && errors && errors.length) { | ||
return complete(errors, options, cb); | ||
return complete(err, errors, options, cb); | ||
} | ||
@@ -188,3 +188,3 @@ | ||
if(!deep) { | ||
callback(null, errors); | ||
callback(err, errors); | ||
@@ -207,5 +207,7 @@ // generate temp schema for nested rules | ||
schema.validate( | ||
rule.value, rule.options || options, function(errs, fields) { | ||
errors = errors.concat(errs || []); | ||
callback(null, errors); | ||
rule.value, rule.options || options, function(err, res) { | ||
if(res) { | ||
errors = errors.concat(res.errors); | ||
} | ||
callback(err, errors); | ||
}); | ||
@@ -217,3 +219,3 @@ } | ||
}, function(err, results) { | ||
complete(results, options, cb); | ||
complete(err, results, options, cb); | ||
}); | ||
@@ -289,3 +291,3 @@ } | ||
*/ | ||
function complete(results, options, callback) { | ||
function complete(err, results, options, callback) { | ||
var i | ||
@@ -300,5 +302,3 @@ , field | ||
if(!errors.length) { | ||
errors = null, fields = null; | ||
}else{ | ||
if(errors.length) { | ||
if(options.single) { | ||
@@ -317,3 +317,3 @@ errors = errors.slice(0,1); | ||
callback(errors, fields); | ||
callback(err, !errors.length ? null : {errors: errors, fields: fields}); | ||
} | ||
@@ -320,0 +320,0 @@ |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.5.7", | ||
"version": "0.6.1", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -85,8 +85,12 @@ Table of Contents | ||
schema.validate(source, function(errors, fields) { | ||
if(errors) { | ||
schema.validate(source, function(err, res) { | ||
if(err) { | ||
throw err; | ||
} | ||
if(res) { | ||
// validation failed, errors is an array of all errors | ||
// fields is an object keyed by field name with an array of | ||
// errors per field | ||
return console.dir(errors) | ||
return console.dir(res.errors) | ||
} | ||
@@ -224,4 +228,4 @@ // validation passed | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// errors for name, street, city, zip | ||
validator.validate({address: {}}, function(err, res) { | ||
// res.errors contains errors for name, street, city, zip | ||
}); | ||
@@ -249,4 +253,4 @@ ``` | ||
var validator = new schema(descriptor); | ||
validator.validate({address: {}}, function(errors, fields) { | ||
// now only errors for name and street | ||
validator.validate({address: {}}, function(err, res) { | ||
// now res.errors only contains errors for name and street | ||
}); | ||
@@ -500,3 +504,3 @@ ``` | ||
validator.validate(source, function(errors, fields) { | ||
validator.validate(source, function(err, res) { | ||
console.dir(source.name); | ||
@@ -503,0 +507,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
address: { | ||
type: "object", | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
@@ -19,3 +19,3 @@ } | ||
it("should error on invalid object (additional properties)", function(done) { | ||
it('should error on invalid object (additional properties)', function(done) { | ||
var opts = { | ||
@@ -37,7 +37,7 @@ rules: {type: 'object', additional: false}, | ||
var validator = new Schema(descriptor); | ||
validator.validate(source, opts, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql( | ||
validator.validate(source, opts, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql( | ||
'extraneous fields (name) found in root'); | ||
expect(errors[1].message).to.eql( | ||
expect(res.errors[1].message).to.eql( | ||
'extraneous fields (name) found in address'); | ||
@@ -48,3 +48,3 @@ done(); | ||
it("should validate with no additional properties", function(done) { | ||
it('should validate with no additional properties', function(done) { | ||
var opts = { | ||
@@ -61,4 +61,5 @@ rules: {type: 'object', additional: false} | ||
var validator = new Schema(descriptor); | ||
validator.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -65,0 +66,0 @@ }); |
@@ -11,7 +11,7 @@ var expect = require('chai').expect | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
list: { | ||
type: "array", | ||
type: 'array', | ||
values: { | ||
@@ -24,7 +24,7 @@ type: 'integer', | ||
it("should error on invalid array value type", function(done) { | ||
it('should error on invalid array value type', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [1,2,3,'foo']}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("foo is not an integer"); | ||
validator.validate({list: [1,2,3,'foo']}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('foo is not an integer'); | ||
done(); | ||
@@ -34,6 +34,17 @@ }); | ||
it("should validate array values", function(done) { | ||
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: [1,2,3]}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({list: [16,'foo', 12]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('12 is not a float'); | ||
done(); | ||
@@ -43,6 +54,7 @@ }); | ||
it("should validate array values with empty array", function(done) { | ||
it('should validate array values', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: []}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({list: [1,2,3]}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -52,6 +64,7 @@ }); | ||
it("should validate array values with no field", function(done) { | ||
it('should validate array values with empty array', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({list: []}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -61,12 +74,7 @@ }); | ||
it("should ignore validation with empty array as values", function(done) { | ||
var descriptor = { | ||
list: { | ||
type: "array", | ||
values: [] | ||
}, | ||
} | ||
it('should validate array values with no field', function(done) { | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [1,2,3, 'foo']}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -76,17 +84,13 @@ }); | ||
it("should error on invalid array value types", function(done) { | ||
it('should ignore validation with empty array as values', function(done) { | ||
var descriptor = { | ||
list: { | ||
type: "array", | ||
values: [ | ||
{type: 'integer', message: value}, | ||
{type: 'string', message: value}, | ||
{type: 'float', message: value} | ||
] | ||
type: 'array', | ||
values: [] | ||
}, | ||
} | ||
var validator = new Schema(descriptor); | ||
validator.validate({list: [16,'foo', 12]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("12 is not a float"); | ||
validator.validate({list: [1,2,3, 'foo']}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -93,0 +97,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
it("should error on non-array type", function(done) { | ||
it('should error on non-array type', function(done) { | ||
var descriptor = { | ||
list: {type: "array"}, | ||
list: {type: 'array'}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: false}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({list: false}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'list is not an array'); | ||
@@ -19,10 +19,10 @@ done(); | ||
it("should error on array length minimum", function(done) { | ||
it('should error on array length minimum', function(done) { | ||
var descriptor = { | ||
list: {type: "array", min: 2}, | ||
list: {type: 'array', min: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: [1]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({list: [1]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'list cannot be less than 2 in length'); | ||
@@ -33,11 +33,11 @@ done(); | ||
it("should error on array length maximum", function(done) { | ||
it('should error on array length maximum', function(done) { | ||
var descriptor = { | ||
list: {type: "array", max: 2}, | ||
list: {type: 'array', max: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: [1,2,3]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({list: [1,2,3]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'list cannot be greater than 2 in length'); | ||
@@ -48,11 +48,11 @@ done(); | ||
it("should error on array length range", function(done) { | ||
it('should error on array length range', function(done) { | ||
var descriptor = { | ||
list: {type: "array", min: 1, max: 2}, | ||
list: {type: 'array', min: 1, max: 2}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({list: [1,2,3]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({list: [1,2,3]}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'list must be between 1 and 2 in length'); | ||
@@ -59,0 +59,0 @@ done(); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
flag: {type: "boolean"}, | ||
flag: {type: 'boolean'}, | ||
} | ||
it("should error on non-boolean type", function(done) { | ||
it('should error on non-boolean type', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({flag: "false"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('flag is not a boolean'); | ||
schema.validate({flag: 'false'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('flag is not a boolean'); | ||
done(); | ||
@@ -19,6 +19,7 @@ }); | ||
it("should validate boolean pass", function(done) { | ||
it('should validate boolean pass', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({flag: true}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({flag: true}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -25,0 +26,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function(done) { | ||
describe('async-validate:', function(done) { | ||
var ptn = /^([\d]{4})-([\d]{2})-([\d]{2})$/; | ||
var date = { | ||
type: "date", | ||
type: 'date', | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/, | ||
format: "YYYY-MM-DD" | ||
format: 'YYYY-MM-DD' | ||
} | ||
it("should error on invalid date value using a format", function(done) { | ||
it('should error on invalid date value using a format', function(done) { | ||
var descriptor = { | ||
active: {type: "date", format: "YYYY-MM-DD"} | ||
active: {type: 'date', format: 'YYYY-MM-DD'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "2013-06-50"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({active: '2013-06-50'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'active date 2013-06-50 is invalid for format YYYY-MM-DD'); | ||
@@ -24,11 +25,12 @@ done(); | ||
}); | ||
it("should error on invalid date value no format (ISO 8601)", | ||
it('should error on invalid date value no format (ISO 8601)', | ||
function(done) { | ||
var descriptor = { | ||
active: {type: "date"} | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "2011-10-10T10:20:90"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({active: '2011-10-10T10:20:90'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'active date 2011-10-10T10:20:90 is invalid'); | ||
@@ -39,11 +41,12 @@ done(); | ||
); | ||
it("should error on invalid date value no format (bad input)", | ||
it('should error on invalid date value no format (bad input)', | ||
function(done) { | ||
var descriptor = { | ||
active: {type: "date"} | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "not a date"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({active: 'not a date'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'active date not a date is invalid'); | ||
@@ -54,9 +57,9 @@ done(); | ||
); | ||
var ptn = /^([\d]{4})-([\d]{2})-([\d]{2})$/; | ||
it("should error on invalid date value using a format and pattern", | ||
it('should error on invalid date value using a format and pattern', | ||
function(done) { | ||
var descriptor = { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
type: 'date', | ||
format: 'YYYY-MM-DD', | ||
pattern: ptn | ||
@@ -66,5 +69,5 @@ } | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "13-06-24"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
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); | ||
@@ -76,9 +79,10 @@ done(); | ||
it("should validate date value using a format", function(done) { | ||
it('should validate date value using a format', function(done) { | ||
var descriptor = { | ||
active: {type: "date", format: "YYYY-MM-DD"} | ||
active: {type: 'date', format: 'YYYY-MM-DD'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "2013-06-24"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({active: '2013-06-24'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -88,9 +92,10 @@ }); | ||
it("should validate date value using a format and local", function(done) { | ||
it('should validate date value using a format and local', function(done) { | ||
var descriptor = { | ||
active: {type: "date", format: "YYYY-MM-DD", local: true} | ||
active: {type: 'date', format: 'YYYY-MM-DD', local: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "2013-06-24"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({active: '2013-06-24'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -100,9 +105,10 @@ }); | ||
it("should validate date value no format (ISO 8601)", function(done) { | ||
it('should validate date value no format (ISO 8601)', function(done) { | ||
var descriptor = { | ||
active: {type: "date"} | ||
active: {type: 'date'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({active: "2011-10-10T10:20:30"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({active: '2011-10-10T10:20:30'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -112,3 +118,3 @@ }); | ||
it("should validate optional date range reference", function(done) { | ||
it('should validate optional date range reference', function(done) { | ||
var descriptor = { | ||
@@ -119,4 +125,5 @@ start: date, | ||
var schema = new Schema(descriptor); | ||
schema.validate({start: "", end: "2013-06-24"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({start: '', end: '2013-06-24'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -123,0 +130,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
address: { | ||
type: "object", | ||
type: 'object', | ||
fields: { | ||
street: {type: "string", required: true}, | ||
city: {type: "string", required: true}, | ||
zip: {type: "string", required: true, len: 8, message: "Invalid zip"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
@@ -19,7 +19,7 @@ } | ||
address: { | ||
type: "object", required: true, | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
@@ -30,9 +30,9 @@ } | ||
var details = { | ||
name: {type: "string", required: true}, | ||
name: {type: 'string', required: true}, | ||
address: { | ||
type: "object", required: true, | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'invalid zip'} | ||
} | ||
@@ -42,8 +42,8 @@ } | ||
it("should error on invalid deep rule (required/no matching property)", | ||
it('should error on invalid deep rule (required/no matching property)', | ||
function(done) { | ||
var schema = new Schema(required); | ||
schema.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('address is required'); | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('address is required'); | ||
done(); | ||
@@ -54,11 +54,11 @@ }); | ||
it("should error on invalid deep rule (required and validation failure on deep rule)", | ||
it('should error on invalid deep rule (required and validation failure on deep rule)', | ||
function(done) { | ||
var schema = new Schema(details); | ||
schema.validate({ address: {} }, function(errors, fields) { | ||
expect(errors.length).to.eql(4); | ||
expect(errors[0].message).to.eql('name is required'); | ||
expect(errors[1].message).to.eql('street is required'); | ||
expect(errors[2].message).to.eql('city is required'); | ||
expect(errors[3].message).to.eql('invalid zip'); | ||
schema.validate({ address: {} }, function(err, res) { | ||
expect(res.errors.length).to.eql(4); | ||
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(); | ||
@@ -68,19 +68,19 @@ }); | ||
); | ||
it("should error on deep rule (with bail out options)", function(done) { | ||
it('should error on deep rule (with bail out options)', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, options: {single: true, first: true}, | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'invalid zip'} | ||
} | ||
}, | ||
name: {type: "string", required: true} | ||
name: {type: 'string', required: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({ address: {} }, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql('street is required'); | ||
expect(errors[1].message).to.eql('name is required'); | ||
schema.validate({ address: {} }, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql('street is required'); | ||
expect(res.errors[1].message).to.eql('name is required'); | ||
done(); | ||
@@ -90,10 +90,10 @@ }); | ||
it("should error on deep rule (array type length mismatch)", function(done) { | ||
it('should error on deep rule (array type length mismatch)', function(done) { | ||
var descriptor = { | ||
roles: { | ||
type: "array", required: true, len: 3, | ||
type: 'array', required: true, len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
0: {type: 'string', required: true}, | ||
1: {type: 'string', required: true}, | ||
2: {type: 'string', required: true} | ||
} | ||
@@ -103,6 +103,6 @@ } | ||
var schema = new Schema(descriptor); | ||
schema.validate({ roles: ["admin", "user"] }, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql('roles must be exactly 3 in length'); | ||
expect(errors[1].message).to.eql('2 is required'); | ||
schema.validate({ roles: ['admin', 'user'] }, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql('roles must be exactly 3 in length'); | ||
expect(res.errors[1].message).to.eql('2 is required'); | ||
done(); | ||
@@ -112,13 +112,13 @@ }); | ||
it("should error on invalid multiple deep rule", function(done) { | ||
it('should error on invalid multiple deep rule', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: "object", required: true, | ||
type: 'object', required: true, | ||
fields: { | ||
house: { | ||
type: "object", required: true, | ||
type: 'object', required: true, | ||
fields: { | ||
name: {type: "string", required: true}, | ||
number: {type: "string", required: true} | ||
name: {type: 'string', required: true}, | ||
number: {type: 'string', required: true} | ||
} | ||
@@ -131,6 +131,6 @@ } | ||
var schema = new Schema(descriptor); | ||
schema.validate({ address: {house: {}} }, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql('name is required'); | ||
expect(errors[1].message).to.eql('number is required'); | ||
schema.validate({ address: {house: {}} }, 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('number is required'); | ||
done(); | ||
@@ -140,7 +140,8 @@ }); | ||
it("should validate deep rule (not required/no matching property)", | ||
it('should validate deep rule (not required/no matching property)', | ||
function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -151,10 +152,10 @@ }); | ||
it("should validate deep rule (array type)", function(done) { | ||
it('should validate deep rule (array type)', function(done) { | ||
var descriptor = { | ||
roles: { | ||
type: "array", required: true, len: 3, | ||
type: 'array', required: true, len: 3, | ||
fields: { | ||
0: {type: "string", required: true}, | ||
1: {type: "string", required: true}, | ||
2: {type: "string", required: true} | ||
0: {type: 'string', required: true}, | ||
1: {type: 'string', required: true}, | ||
2: {type: 'string', required: true} | ||
} | ||
@@ -164,5 +165,6 @@ } | ||
var schema = new Schema(descriptor) | ||
, source = {roles: ["admin", "user", "guest"]}; | ||
schema.validate(source, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
, source = {roles: ['admin', 'user', 'guest']}; | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -169,0 +171,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
role: {type: 'enum', list: ['admin', 'user', 'guest']} | ||
} | ||
it("should error on invalid enum value", function(done) { | ||
it('should error on invalid enum value', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({role: "manager"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('role must be one of admin, user, guest'); | ||
schema.validate({role: 'manager'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'role must be one of admin, user, guest'); | ||
done(); | ||
@@ -19,6 +20,7 @@ }); | ||
it("should validate enum value", function(done) { | ||
it('should validate enum value', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({role: "user"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({role: 'user'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -25,0 +27,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
ratio: {type: "float"}, | ||
ratio: {type: 'float'}, | ||
} | ||
it("should error when a number is not a float", function(done) { | ||
it('should error when a number is not a float', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({ratio: 1618}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('ratio is not a float'); | ||
schema.validate({ratio: 1618}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('ratio is not a float'); | ||
done(); | ||
@@ -19,6 +19,7 @@ }); | ||
it("should validate a number is a float", function(done) { | ||
it('should validate a number is a float', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({ratio: 1.667}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({ratio: 1.667}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -25,0 +26,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
port: {type: "integer"}, | ||
port: {type: 'integer'}, | ||
} | ||
it("should error on number as an integer", function(done) { | ||
it('should error on number not an integer', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 1.618}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port is not an integer'); | ||
schema.validate({port: 1.618}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('port is not an integer'); | ||
done(); | ||
@@ -19,6 +19,7 @@ }); | ||
it("should validate integer type", function(done) { | ||
it('should validate integer type', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 2048}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({port: 2048}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -25,0 +26,0 @@ }); |
@@ -11,5 +11,5 @@ var expect = require('chai').expect | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "user"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name must be exactly 10 characters'); | ||
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'); | ||
done(); | ||
@@ -24,5 +24,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 8080}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port must equal 80'); | ||
schema.validate({port: 8080}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('port must equal 80'); | ||
done(); | ||
@@ -37,5 +37,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({roles: ["user"]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('roles must be exactly 2 in length'); | ||
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'); | ||
done(); | ||
@@ -50,4 +50,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "username"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({name: "username"}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -62,4 +63,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 80}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({port: 80}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -74,4 +76,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({roles: ["user", "admin"]}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({roles: ["user", "admin"]}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -78,0 +81,0 @@ }); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
it("should validate using a custom error message as min", function(done) { | ||
it('should validate using a custom error message as min', function(done) { | ||
var descriptor = { | ||
num: { | ||
type: "number", min: 0, max: 10, | ||
type: 'number', min: 0, max: 10, | ||
message: { | ||
@@ -17,4 +17,4 @@ min: 'Number may not be below zero', | ||
var validator = new schema(descriptor); | ||
validator.validate({num: -1}, function(errors, fields) { | ||
expect(errors[0].message).to.eql("Number may not be below zero"); | ||
validator.validate({num: -1}, function(err, res) { | ||
expect(res.errors[0].message).to.eql('Number may not be below zero'); | ||
done(); | ||
@@ -24,6 +24,6 @@ }); | ||
it("should validate using a custom error message as max", function(done) { | ||
it('should validate using a custom error message as max', function(done) { | ||
var descriptor = { | ||
num: { | ||
type: "number", min: 0, max: 10, | ||
type: 'number', min: 0, max: 10, | ||
message: { | ||
@@ -36,4 +36,4 @@ min: 'Number may not be below zero', | ||
var validator = new schema(descriptor); | ||
validator.validate({num: 11}, function(errors, fields) { | ||
expect(errors[0].message).to.eql("Number may not be above ten"); | ||
validator.validate({num: 11}, function(err, res) { | ||
expect(res.errors[0].message).to.eql('Number may not be above ten'); | ||
done(); | ||
@@ -40,0 +40,0 @@ }); |
@@ -5,3 +5,3 @@ var expect = require('chai').expect | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
@@ -11,12 +11,12 @@ // clone of the default messages | ||
// change a message | ||
clone.required = "%s is a required field"; | ||
clone.required = '%s is a required field'; | ||
it("should validate using a custom error message", function(done) { | ||
it('should validate using a custom error message', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, message: "Name is required"}, | ||
name: {type: 'string', required: true, message: 'Name is required'}, | ||
} | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("Name is required"); | ||
validator.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('Name is required'); | ||
done(); | ||
@@ -26,6 +26,6 @@ }); | ||
it("should validate using a custom error message function", function(done) { | ||
it('should validate using a custom error message function', function(done) { | ||
var descriptor = { | ||
name: { | ||
type: "string", | ||
type: 'string', | ||
required: true, | ||
@@ -40,5 +40,5 @@ message: function(message, parameters) { | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("Name is required"); | ||
validator.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('Name is required'); | ||
done(); | ||
@@ -48,5 +48,5 @@ }); | ||
it("should validate using custom messages", function(done) { | ||
it('should validate using custom messages', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true}, | ||
name: {type: 'string', required: true}, | ||
} | ||
@@ -58,5 +58,5 @@ var validator = new schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("name is a required field"); | ||
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(); | ||
@@ -66,6 +66,6 @@ }); | ||
it("should use raise() helper method", function(done) { | ||
it('should use raise() helper method', function(done) { | ||
var descriptor = { | ||
name: function(cb) { | ||
this.raise("%s is a required field", this.field); | ||
this.raise('%s is a required field', this.field); | ||
cb(); | ||
@@ -75,5 +75,5 @@ } | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("name is a required field"); | ||
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(); | ||
@@ -80,0 +80,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
mock: {type: "method"}, | ||
mock: {type: 'method'}, | ||
} | ||
it("should error on value that is not a function", function(done) { | ||
it('should error on value that is not a function', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({mock: 80}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('mock is not a method'); | ||
schema.validate({mock: 80}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('mock is not a method'); | ||
done(); | ||
@@ -19,6 +19,7 @@ }); | ||
it("should validate function type", function(done) { | ||
it('should validate function type', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({mock: function(){}}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({mock: function(){}}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -25,0 +26,0 @@ }); |
@@ -5,9 +5,9 @@ var expect = require('chai').expect | ||
describe("async-validate:", function(done) { | ||
describe('async-validate:', function(done) { | ||
it("should error on multiple validation rules for a field", | ||
it('should error on multiple validation rules for a field', | ||
function(done) { | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
{type: 'string', required: true}, | ||
{pattern: email, required: true} | ||
@@ -17,6 +17,6 @@ ] | ||
var schema = new Schema(descriptor); | ||
schema.validate({address: "joe@example"}, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql('email is required'); | ||
expect(errors[1].message).to.eql( | ||
schema.validate({address: 'joe@example'}, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql('email is required'); | ||
expect(res.errors[1].message).to.eql( | ||
'email value undefined does not match pattern ' + email); | ||
@@ -27,7 +27,7 @@ done(); | ||
); | ||
it("should error on multiple validation rules for a field single failure", | ||
it('should error on multiple validation rules for a field single failure', | ||
function(done) { | ||
var descriptor = { | ||
email: [ | ||
{type: "string", required: true}, | ||
{type: 'string', required: true}, | ||
{pattern: email} | ||
@@ -37,5 +37,5 @@ ] | ||
var schema = new Schema(descriptor); | ||
schema.validate({email: "user@example"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({email: 'user@example'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'email value user@example does not match pattern ' + email); | ||
@@ -47,3 +47,3 @@ done(); | ||
it("should error on multiple validation rules with a validation function", | ||
it('should error on multiple validation rules with a validation function', | ||
function(done) { | ||
@@ -54,5 +54,5 @@ var descriptor = { | ||
function(cb) { | ||
var email = "user@example.com"; | ||
var email = 'user@example.com'; | ||
if(this.value === email) { | ||
this.raise("Email address %s already exists", email); | ||
this.raise('Email address %s already exists', email); | ||
} | ||
@@ -64,7 +64,7 @@ cb(); | ||
var schema = new Schema(descriptor) | ||
, source = {email: "user@example.com"}; | ||
, source = {email: 'user@example.com'}; | ||
schema.validate(source, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate(source, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'Email address user@example.com already exists'); | ||
@@ -71,0 +71,0 @@ done(); |
@@ -15,5 +15,5 @@ var expect = require('chai').expect | ||
var validator = new schema(descriptor); | ||
validator.validate({value: true}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('value is not null'); | ||
validator.validate({value: true}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('value is not null'); | ||
done(); | ||
@@ -25,4 +25,5 @@ }); | ||
var validator = new schema(descriptor); | ||
validator.validate({value: null}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({value: null}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -34,4 +35,5 @@ }); | ||
var validator = new schema(descriptor); | ||
validator.validate({}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -38,0 +40,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
it("should error on not a number", function(done) { | ||
it('should error on not a number', function(done) { | ||
var descriptor = { | ||
port: {type: "number"}, | ||
port: {type: 'number'}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: "80"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port is not a number'); | ||
schema.validate({port: '80'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'port is not a number'); | ||
done(); | ||
@@ -18,10 +19,11 @@ }); | ||
it("should error on number greater than a minimum value", function(done) { | ||
it('should error on number greater than a minimum value', function(done) { | ||
var descriptor = { | ||
port: {type: "number", min: 8080}, | ||
port: {type: 'number', min: 8080}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 80}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port cannot be less than 8080'); | ||
schema.validate({port: 80}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'port cannot be less than 8080'); | ||
done(); | ||
@@ -31,11 +33,12 @@ }); | ||
it("should error on number number greater than a minimum value", | ||
it('should error on number number greater than a minimum value', | ||
function(done) { | ||
var descriptor = { | ||
port: {type: "number", max: 80}, | ||
port: {type: 'number', max: 80}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 8080}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port cannot be greater than 80'); | ||
schema.validate({port: 8080}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'port cannot be greater than 80'); | ||
done(); | ||
@@ -46,11 +49,12 @@ }); | ||
it("should error on number out of range", | ||
it('should error on number out of range', | ||
function(done) { | ||
var descriptor = { | ||
port: {type: "number", min: 80, max: 1024}, | ||
port: {type: 'number', min: 80, max: 1024}, | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({port: 8080}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('port must be between 80 and 1024'); | ||
schema.validate({port: 8080}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'port must be between 80 and 1024'); | ||
done(); | ||
@@ -57,0 +61,0 @@ }); |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
it("should error on invalid object (additional properties)", function(done) { | ||
it('should error on invalid object (additional properties)', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: "object", | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
@@ -28,6 +28,6 @@ } | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(errors, fields) { | ||
validator.validate(source, function(err, res) { | ||
var expected = 'extraneous fields (name) found in address'; | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql(expected); | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql(expected); | ||
done(); | ||
@@ -37,12 +37,12 @@ }); | ||
it("should validate with no additional properties", function(done) { | ||
it('should validate with no additional properties', function(done) { | ||
var descriptor = { | ||
address: { | ||
type: "object", | ||
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"} | ||
street: {type: 'string', required: true}, | ||
city: {type: 'string', required: true}, | ||
zip: {type: 'string', required: true, len: 8, message: 'Invalid zip'} | ||
} | ||
@@ -59,4 +59,5 @@ } | ||
var validator = new schema(descriptor); | ||
validator.validate(source, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -63,0 +64,0 @@ }); |
@@ -12,5 +12,5 @@ var expect = require('chai').expect | ||
var schema = new Schema(descriptor); | ||
schema.validate({address: []}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('address is not an object'); | ||
schema.validate({address: []}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('address is not an object'); | ||
done(); | ||
@@ -22,5 +22,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(errors[0].message).to.eql('address is required'); | ||
schema.validate({}, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql('address is required'); | ||
done(); | ||
@@ -32,4 +32,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({address: {}}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({address: {}}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -36,0 +37,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
firstname: {type: "string", required: true}, | ||
surname: {type: "string", required: true} | ||
firstname: {type: 'string', required: true}, | ||
surname: {type: 'string', required: true} | ||
} | ||
it("should error with multiple errors", function(done) { | ||
it('should error with multiple errors', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, {first: false}, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
schema.validate({}, {first: false}, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
done(); | ||
@@ -19,6 +19,6 @@ }); | ||
it("should error with keys option", function(done) { | ||
it('should error with keys option', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, {keys: ["firstname"]}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
schema.validate({}, {keys: ['firstname']}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
done(); | ||
@@ -28,6 +28,6 @@ }); | ||
it("should error on first error", function(done) { | ||
it('should error on first error', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({}, {first: true}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
schema.validate({}, {first: true}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
done(); | ||
@@ -37,13 +37,13 @@ }); | ||
it("should error with single option", function(done) { | ||
it('should error with single option', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, min: 10, pattern: /^[^-].*$/} | ||
name: {type: 'string', required: true, min: 10, pattern: /^[^-].*$/} | ||
} | ||
var schema = new Schema(descriptor) | ||
, source = {name: "-name"} | ||
, source = {name: '-name'} | ||
, opts = {first: true, single: true}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(fields.name.length).to.eql(1); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.fields.name.length).to.eql(1); | ||
done(); | ||
@@ -50,0 +50,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
name: {type: "string", len: 8}, | ||
name: {type: 'string', len: 8}, | ||
} | ||
it("should use default series iteration", function(done) { | ||
it('should use default series iteration', function(done) { | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "username", surname: 'foo'}, | ||
function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({name: 'username', surname: 'foo'}, | ||
function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -20,9 +21,10 @@ } | ||
it("should use parallel iteration", function(done) { | ||
it('should use parallel iteration', function(done) { | ||
var schema = new Schema(descriptor) | ||
, source = {name: "username", surname: 'foo'}; | ||
, source = {name: 'username', surname: 'foo'}; | ||
schema.validate(source, {parallel: true}, | ||
function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -29,0 +31,0 @@ } |
var expect = require('chai').expect | ||
, schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
id: {type: 'id'}, | ||
} | ||
before(function(done) { | ||
@@ -12,10 +16,7 @@ // load plugin definition | ||
it("should error using custom plugin", function(done) { | ||
var descriptor = { | ||
id: {type: "id"}, | ||
} | ||
it('should error using custom plugin', function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({id: "-hyphen"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql("id is not a valid identifier"); | ||
validator.validate({id: '-hyphen'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('id is not a valid identifier'); | ||
done(); | ||
@@ -25,9 +26,7 @@ }); | ||
it("should validate custom plugin", function(done) { | ||
var descriptor = { | ||
id: {type: "id"}, | ||
} | ||
it('should validate custom plugin', function(done) { | ||
var validator = new schema(descriptor); | ||
validator.validate({id: "my-valid-id"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({id: 'my-valid-id'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -34,0 +33,0 @@ }); |
@@ -15,5 +15,5 @@ var expect = require('chai').expect | ||
schema.validate(source, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('re is not a valid regexp'); | ||
schema.validate(source, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('re is not a valid regexp'); | ||
done(); | ||
@@ -26,4 +26,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({re: "^[a-z]+$"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({re: "^[a-z]+$"}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -35,4 +36,5 @@ }); | ||
var schema = new Schema(descriptor); | ||
schema.validate({re: /^[a-z]+$/}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({re: /^[a-z]+$/}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -39,0 +41,0 @@ }); |
@@ -21,7 +21,7 @@ var expect = require('chai').expect | ||
var validator = new schema(descriptor); | ||
validator.validate(source, opts, function(errors, fields) { | ||
validator.validate(source, opts, function(err, res) { | ||
// NOTE: `source` is the default field name for root object | ||
var expected = 'extraneous fields (name) found in source'; | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql(expected); | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql(expected); | ||
done(); | ||
@@ -46,4 +46,5 @@ }); | ||
var validator = new schema(descriptor); | ||
validator.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -50,0 +51,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
it("should error on a custom schema function", function(done) { | ||
it('should error on a custom schema function', function(done) { | ||
var descriptor = { | ||
@@ -11,3 +11,3 @@ name: function(cb) { | ||
this.raise( | ||
"%s must be lowercase alphanumeric characters", | ||
'%s must be lowercase alphanumeric characters', | ||
this.field); | ||
@@ -19,5 +19,5 @@ } | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "Firstname"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
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'); | ||
@@ -28,11 +28,11 @@ done(); | ||
it("should error on required string field", function(done) { | ||
it('should error on required string field', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true} | ||
name: {type: 'string', required: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({noname: "field"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(fields.name.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name is required'); | ||
schema.validate({noname: 'field'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.fields.name.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name is required'); | ||
done(); | ||
@@ -42,11 +42,11 @@ }); | ||
it("should error on required string field (null)", function(done) { | ||
it('should error on required string field (null)', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true} | ||
name: {type: 'string', required: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: null}, function(errors, fields) { | ||
expect(errors.length).to.eql(2); | ||
expect(fields.name.length).to.eql(2); | ||
expect(errors[0].message).to.eql('name is required'); | ||
schema.validate({name: null}, function(err, res) { | ||
expect(res.errors.length).to.eql(2); | ||
expect(res.fields.name.length).to.eql(2); | ||
expect(res.errors[0].message).to.eql('name is required'); | ||
done(); | ||
@@ -56,10 +56,10 @@ }); | ||
it("should error on non-string type", function(done) { | ||
it('should error on non-string type', function(done) { | ||
var descriptor = { | ||
name: {type: "string"} | ||
name: {type: 'string'} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: 10}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name is not a string'); | ||
schema.validate({name: 10}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name is not a string'); | ||
done(); | ||
@@ -69,11 +69,11 @@ }); | ||
it("should error on required string field with minimum length", | ||
it('should error on required string field with minimum length', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, min: 8} | ||
name: {type: 'string', required: true, min: 8} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "field"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({name: 'field'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name must be at least 8 characters'); | ||
@@ -85,11 +85,11 @@ done(); | ||
it("should error on required string field with maximum length", | ||
it('should error on required string field with maximum length', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, max: 2} | ||
name: {type: 'string', required: true, max: 2} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "field"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({name: 'field'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name cannot be longer than 2 characters'); | ||
@@ -101,11 +101,11 @@ done(); | ||
it("should error on required string field is less than a length range", | ||
it('should error on required string field is less than a length range', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, min: 6, max: 8} | ||
name: {type: 'string', required: true, min: 6, max: 8} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "field"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({name: 'field'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name must be between 6 and 8 characters'); | ||
@@ -117,11 +117,11 @@ done(); | ||
it("should error on required string field is greater than a length range", | ||
it('should error on required string field is greater than a length range', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, min: 2, max: 4} | ||
name: {type: 'string', required: true, min: 2, max: 4} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "field"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({name: 'field'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name must be between 2 and 4 characters'); | ||
@@ -132,3 +132,3 @@ done(); | ||
); | ||
it("should error on regular expression pattern mismatch", | ||
it('should error on regular expression pattern mismatch', | ||
function(done) { | ||
@@ -139,5 +139,5 @@ var descriptor = { | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "alpha"}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql( | ||
schema.validate({name: 'alpha'}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql( | ||
'name value alpha does not match pattern /^[0-9]+$/'); | ||
@@ -149,11 +149,11 @@ done(); | ||
it("should error on string consisting of whitespace", | ||
it('should error on string consisting of whitespace', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", whitespace: true} | ||
name: {type: 'string', whitespace: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: " "}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name cannot be empty'); | ||
schema.validate({name: ' '}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name cannot be empty'); | ||
done(); | ||
@@ -164,11 +164,11 @@ }); | ||
it("should error on empty string", | ||
it('should error on empty string', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, whitespace: true} | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: ""}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name cannot be empty'); | ||
schema.validate({name: ''}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('name cannot be empty'); | ||
done(); | ||
@@ -179,9 +179,10 @@ }); | ||
it("should validate on required string field", function(done) { | ||
it('should validate on required string field', function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true} | ||
name: {type: 'string', required: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "field"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({name: 'field'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -192,10 +193,11 @@ }); | ||
it("should validate on required string field in range", | ||
it('should validate on required string field in range', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, min: 6, max: 20} | ||
name: {type: 'string', required: true, min: 6, max: 20} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: "valid field"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate({name: 'valid field'}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -206,13 +208,14 @@ }); | ||
it("should validate after failure", | ||
it('should validate after failure', | ||
function(done) { | ||
var descriptor = { | ||
name: {type: "string", required: true, whitespace: true} | ||
name: {type: 'string', required: true, whitespace: true} | ||
} | ||
var schema = new Schema(descriptor); | ||
schema.validate({name: ""}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('name cannot be empty'); | ||
schema.validate({name: "user"}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
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(); | ||
@@ -219,0 +222,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
var descriptor = { | ||
name: { | ||
type: "string", | ||
type: 'string', | ||
required: true, pattern: /^[a-z]+$/, | ||
@@ -16,9 +16,9 @@ transform: function(value) { | ||
it("should transform by stripping whitespace", function(done) { | ||
it('should transform by stripping whitespace', function(done) { | ||
var schema = new Schema(descriptor) | ||
, source = {name: " user "}; | ||
, source = {name: ' user '}; | ||
schema.validate(source, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
expect(fields).to.eql(null); | ||
schema.validate(source, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
expect(source.name).to.eql('user'); | ||
@@ -25,0 +25,0 @@ done(); |
@@ -20,5 +20,5 @@ var expect = require('chai').expect | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Array()}, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('instance is not a Component'); | ||
validator.validate({instance: new Array()}, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('instance is not a Component'); | ||
done(); | ||
@@ -40,4 +40,5 @@ }); | ||
var validator = new schema(descriptor); | ||
validator.validate({instance: new Component()}, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
validator.validate({instance: new Component()}, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -44,0 +45,0 @@ }); |
var expect = require('chai').expect | ||
, Schema = require('../../index'); | ||
describe("async-validate:", function() { | ||
describe('async-validate:', function() { | ||
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); | ||
var source = {age: undefined, name : "User"}; | ||
var opts = {first : false, single: true}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
it("should allow undefined integer field if not required (first)", | ||
it('should error on invalid integer field if not required (first/single)', | ||
function(done) { | ||
@@ -27,6 +13,8 @@ var descriptor = { | ||
var schema = new Schema(descriptor); | ||
var source = {age: undefined, name : "User"}; | ||
var source = {age: 'abc', name : 'User'}; | ||
var opts = {first : true, single: true}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(res.errors.length).to.eql(1); | ||
expect(res.errors[0].message).to.eql('age is not an integer'); | ||
expect(res.errors[0].field).to.eql('age'); | ||
done(); | ||
@@ -37,3 +25,18 @@ }); | ||
it("should error on invalid integer field if not required (first/single)", | ||
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); | ||
var source = {age: undefined, name : 'User'}; | ||
var opts = {first : false, single: true}; | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
}); | ||
}); | ||
it('should allow undefined integer field if not required (first)', | ||
function(done) { | ||
@@ -45,8 +48,7 @@ var descriptor = { | ||
var schema = new Schema(descriptor); | ||
var source = {age: 'abc', name : "User"}; | ||
var source = {age: undefined, name : 'User'}; | ||
var opts = {first : true, single: true}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors.length).to.eql(1); | ||
expect(errors[0].message).to.eql('age is not an integer'); | ||
expect(errors[0].field).to.eql('age'); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -57,4 +59,3 @@ }); | ||
it("should allow undefined array field if not required", function(done) { | ||
it('should allow undefined array field if not required', function(done) { | ||
var descriptor = { | ||
@@ -66,4 +67,5 @@ mock: {type: 'array', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -73,3 +75,3 @@ }); | ||
it("should allow undefined boolean field if not required", function(done) { | ||
it('should allow undefined boolean field if not required', function(done) { | ||
var descriptor = { | ||
@@ -81,4 +83,5 @@ mock: {type: 'boolean', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -88,3 +91,3 @@ }); | ||
it("should allow undefined date field if not required", function(done) { | ||
it('should allow undefined date field if not required', function(done) { | ||
var descriptor = { | ||
@@ -96,4 +99,5 @@ mock: {type: 'date', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -103,3 +107,3 @@ }); | ||
it("should allow undefined enum field if not required", function(done) { | ||
it('should allow undefined enum field if not required', function(done) { | ||
var descriptor = { | ||
@@ -111,4 +115,5 @@ mock: {type: 'enum', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -118,3 +123,3 @@ }); | ||
it("should allow undefined float field if not required", function(done) { | ||
it('should allow undefined float field if not required', function(done) { | ||
var descriptor = { | ||
@@ -126,4 +131,5 @@ mock: {type: 'float', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -133,3 +139,3 @@ }); | ||
it("should allow undefined method field if not required", function(done) { | ||
it('should allow undefined method field if not required', function(done) { | ||
var descriptor = { | ||
@@ -141,4 +147,5 @@ mock: {type: 'method', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -148,3 +155,3 @@ }); | ||
it("should allow undefined number field if not required", function(done) { | ||
it('should allow undefined number field if not required', function(done) { | ||
var descriptor = { | ||
@@ -156,4 +163,5 @@ mock: {type: 'number', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -163,3 +171,3 @@ }); | ||
it("should allow undefined object field if not required", function(done) { | ||
it('should allow undefined object field if not required', function(done) { | ||
var descriptor = { | ||
@@ -171,4 +179,5 @@ mock: {type: 'object', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -178,3 +187,3 @@ }); | ||
it("should allow undefined pattern field if not required", function(done) { | ||
it('should allow undefined pattern field if not required', function(done) { | ||
var descriptor = { | ||
@@ -186,4 +195,5 @@ mock: {type: 'pattern', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -193,3 +203,3 @@ }); | ||
it("should allow undefined regexp field if not required", function(done) { | ||
it('should allow undefined regexp field if not required', function(done) { | ||
var descriptor = { | ||
@@ -201,4 +211,5 @@ mock: {type: 'regexp', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -208,3 +219,3 @@ }); | ||
it("should allow undefined string field if not required", function(done) { | ||
it('should allow undefined string field if not required', function(done) { | ||
var descriptor = { | ||
@@ -216,4 +227,5 @@ mock: {type: 'string', required: false} | ||
var opts = {}; | ||
schema.validate(source, opts, function(errors, fields) { | ||
expect(errors).to.eql(null); | ||
schema.validate(source, opts, function(err, res) { | ||
expect(err).to.eql(null); | ||
expect(res).to.eql(null); | ||
done(); | ||
@@ -220,0 +232,0 @@ }); |
127964
2764
734