Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-validate

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-validate - npm Package Compare versions

Comparing version 0.5.6 to 0.5.7

8

doc/readme/api.md

@@ -153,10 +153,2 @@ ### API

##### type
```javascript
function type()
```
Validates the type of a value, typically invoked from a rule function, raises an error if a value is not of the correct type.
##### range

@@ -163,0 +155,0 @@

26

doc/readme/guide.md
## Guide
### Descriptor
A descriptor is a collection of validation rules as a map of fields to rules, rules may be declared as an `object`, `array` or `function`.
#### Object Definition
```javascript
var descriptor = {
name: {type: 'string', required: true}
}
```
#### Array Definition
You may declare an `array` to use multiple validation rules per field, see [multiple rules](#multiple-rules).
#### Function Definition
Use an inline function definition for application specific rules, see [inline rule](#inline-rule).
### Rules

@@ -209,5 +229,5 @@

### Descriptor
### Rule Properties
A descriptor defines the validation rules as a map of fields to rules, this section describes the recognised rule properties.
This section describes the recognised rule properties and their behaviour, if you are using an [assigned rule](#assigned-rule) or [plugin rule](#plugin-rule) you can define properties on the rule object and they are available to the rule function via `this.rule`.

@@ -241,3 +261,3 @@ #### Type Identifier

Rules of the `object` and `array` type may declare a `fields` object which declares a nested descriptor, see [deep rules](#deep-rules).
Rules of the `object` and `array` type may declare a `fields` object which declares a nested schema, see [deep rules](#deep-rules).

@@ -244,0 +264,0 @@ #### Message

## Usage
Usage involves defining a descriptor, assigning it to a schema using the necessary plugins and calling validate:
Define validation rules, assign them to a schema using the necessary plugins and call validate:

@@ -190,4 +190,2 @@ var iterator = require('./iterator')

}else{
/* istanbul ignore next: always testing with errors array */
errors = errors || [];

@@ -194,0 +192,0 @@ // if rule is required but the target object

{
"name": "async-validate",
"description": "Asynchronous validation for object properties.",
"version": "0.5.6",
"description": "Asynchronous validation for node and the browser",
"version": "0.5.7",
"author": "muji <noop@xpm.io>",

@@ -33,3 +33,3 @@ "license": "MIT",

"browserify": "browserify -o async-validate.js -e ./lib/schema.js && du -bh async-validate.js",
"clean": "rm -rf coverage ./async-validate.js",
"clean": "rm -rf coverage ./async-validate.js ./test/spec.js",
"spec": "browserify -o test/spec.js -e test/index.js",

@@ -36,0 +36,0 @@ "test": "NODE_ENV=test mocha test/global ${SPEC:-test/spec}",

@@ -1,22 +0,22 @@

/**
* Validates an array.
*
* @param cb The callback function.
*/
function array(cb) {
if(this.shouldValidate()) {
this.required();
if(!Array.isArray(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
module.exports = function() {
/**
* Validates an array.
*
* @param cb The callback function.
*/
this.main.array = function array(cb) {
if(this.shouldValidate()) {
this.required();
if(!Array.isArray(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
this.range();
}
this.range();
cb();
}
cb();
}
module.exports = function() {
this.main.array = array;
}

@@ -1,24 +0,23 @@

/**
* Validates a boolean.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function bool(cb) {
if(this.shouldValidate()) {
this.required();
module.exports = function() {
// straight typeof check
if(typeof(this.value) !== this.rule.type) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
/**
* Validates a boolean.
*
* @param cb The callback function.
*/
this.main.boolean = function bool(cb) {
if(this.shouldValidate()) {
this.required();
// straight typeof check
if(typeof(this.value) !== this.rule.type) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.boolean = bool;
}

@@ -1,3 +0,1 @@

var moment = require('moment');
/**

@@ -7,2 +5,3 @@ * Rule for validating a date against a format.

function validator() {
var moment = require('moment');
var mmt = this.rule.local ? moment : moment.utc;

@@ -23,23 +22,23 @@ var dt = !this.rule.format

/**
* Validates a date against the format property.
*
* @param cb The callback function.
*/
function date(cb) {
var validate = this.rule.required
|| (!this.rule.required && this.source.hasOwnProperty(this.field)
&& this.source[this.field]);
module.exports = function() {
if(validate) {
this.required();
this.pattern();
validator.call(this);
/**
* Validates a date against the format property.
*
* @param cb The callback function.
*/
this.main.date = function date(cb) {
var validate = this.rule.required
|| (!this.rule.required && this.source.hasOwnProperty(this.field)
&& this.source[this.field]);
if(validate) {
this.required();
this.pattern();
validator.call(this);
}
cb();
}
cb();
}
module.exports = function() {
this.main.date = date;
}

@@ -1,22 +0,22 @@

/**
* Validates an enumerable list.
*
* @param cb The callback function.
*/
function enumerable(cb) {
if(this.shouldValidate()) {
this.required();
var list = this.rule.list;
if(!~list.indexOf(this.value)) {
this.raise(
this.reasons.enumerable,
this.messages.enum,
this.field, list.join(', '));
module.exports = function() {
/**
* Validates an enumerable list.
*
* @param cb The callback function.
*/
this.main.enum = function enumerable(cb) {
if(this.shouldValidate()) {
this.required();
var list = this.rule.list;
if(!~list.indexOf(this.value)) {
this.raise(
this.reasons.enumerable,
this.messages.enum,
this.field, list.join(', '));
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.enum = enumerable;
}

@@ -1,25 +0,25 @@

/**
* Validates a number is a floating point number.
*
* @param cb The callback function.
*/
function fraction(cb) {
if(this.shouldValidate()) {
this.required();
module.exports = function() {
if(typeof(this.value) !== 'number'
|| Number(this.value) === parseInt(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
/**
* Validates a number is a floating point number.
*
* @param cb The callback function.
*/
this.main.float = function fraction(cb) {
if(this.shouldValidate()) {
this.required();
if(typeof(this.value) !== 'number'
|| Number(this.value) === parseInt(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
this.range();
}
this.range();
cb();
}
cb();
}
module.exports = function() {
this.main.float = fraction;
}

@@ -1,25 +0,25 @@

/**
* Validates a number is an integer.
*
* @param cb The callback function.
*/
function integer(cb) {
if(this.shouldValidate()) {
this.required();
module.exports = function() {
if(typeof(this.value) !== 'number'
|| Number(this.value) !== parseInt(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
/**
* Validates a number is an integer.
*
* @param cb The callback function.
*/
this.main.integer = function integer(cb) {
if(this.shouldValidate()) {
this.required();
if(typeof(this.value) !== 'number'
|| Number(this.value) !== parseInt(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
this.range();
}
this.range();
cb();
}
cb();
}
module.exports = function() {
this.main.integer = integer;
}

@@ -1,21 +0,21 @@

/**
* Validates a function.
*
* @param cb The callback function.
*/
function method(cb) {
if(this.shouldValidate()) {
this.required();
if(typeof this.value !== 'function') {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
module.exports = function() {
/**
* Validates a function.
*
* @param cb The callback function.
*/
this.main.method = function method(cb) {
if(this.shouldValidate()) {
this.required();
if(typeof this.value !== 'function') {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.method = method;
}
module.exports = function() {
/**
* Validates a value is null.
*
* @param cb The callback function.
*/
this.main.null = function validate(cb) {

@@ -14,2 +20,3 @@ if(this.shouldValidate()) {

}
}

@@ -1,23 +0,23 @@

/**
* Validates a number.
*
* @param cb The callback function.
*/
function number(cb) {
if(this.shouldValidate()) {
this.required();
// straight typeof check
if(typeof(this.value) !== this.rule.type) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
module.exports = function() {
/**
* Validates a number.
*
* @param cb The callback function.
*/
this.main.number = function number(cb) {
if(this.shouldValidate()) {
this.required();
// straight typeof check
if(typeof(this.value) !== this.rule.type) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
this.range();
}
this.range();
cb();
}
cb();
}
module.exports = function() {
this.main.number = number;
}

@@ -8,43 +8,43 @@ function isObject(value, Type) {

/**
* Validates an object.
*
* @param cb The callback function.
*/
function object(cb) {
var expected, additional;
module.exports = function() {
if(this.shouldValidate()) {
this.required();
/**
* Validates an object.
*
* @param cb The callback function.
*/
this.main.object = function object(cb) {
var expected, additional;
if(!isObject(this.value, this.rule.Type)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
if(this.shouldValidate()) {
this.required();
// nested deep properties
if(this.rule.additional === false) {
if(!isObject(this.value, this.rule.Type)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
// NOTE: Object.keys() will throw if you declare `additional`
// NOTE: for the `object` type but do not declare nested `fields` object
expected = Array.isArray(this.rule.keys)
? this.rule.keys : Object.keys(this.rule.fields);
// nested deep properties
if(this.rule.additional === false) {
additional = this.hasAdditionalFields(
expected, Object.keys(this.value));
// NOTE: Object.keys() will throw if you declare `additional`
// NOTE: for the `object` type but do not declare nested `fields` object
expected = Array.isArray(this.rule.keys)
? this.rule.keys : Object.keys(this.rule.fields);
if(additional) {
this.raise(
this.reasons.additional,
this.messages.additional, additional.join(', '), this.field);
additional = this.hasAdditionalFields(
expected, Object.keys(this.value));
if(additional) {
this.raise(
this.reasons.additional,
this.messages.additional, additional.join(', '), this.field);
}
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.object = object;
}

@@ -1,18 +0,18 @@

/**
* Validates a regular expression pattern.
*
* Performs validation when a rule only contains
* a pattern property but is not declared as a string type.
*
* @param cb The callback function.
*/
function pattern(cb) {
if(this.shouldValidate()) {
this.pattern();
module.exports = function() {
/**
* Validates a regular expression pattern.
*
* Performs validation when a rule only contains
* a pattern property but is not declared as a string type.
*
* @param cb The callback function.
*/
this.main.pattern = function pattern(cb) {
if(this.shouldValidate()) {
this.pattern();
}
cb();
}
cb();
}
module.exports = function() {
this.main.pattern = pattern;
}

@@ -1,2 +0,1 @@

function isRegExp(value) {

@@ -13,21 +12,21 @@ if(value instanceof RegExp) {

/**
* Validates the regular expression type.
*
* @param cb The callback function.
*/
function regexp(cb) {
if(this.shouldValidate()) {
this.required();
if(!isRegExp(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type], this.field, this.rule.type);
module.exports = function() {
/**
* Validates the regular expression type.
*
* @param cb The callback function.
*/
this.main.regexp = function regexp(cb) {
if(this.shouldValidate()) {
this.required();
if(!isRegExp(this.value)) {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type], this.field, this.rule.type);
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.regexp = regexp;
}

@@ -1,39 +0,39 @@

/**
* Performs validation for string types.
*
* @param cb The callback function.
*/
function string(cb) {
if(this.shouldValidate()) {
this.required();
module.exports = function() {
// if value is required and value is undefined
// no need to add this error message
if(this.rule.required && this.value === undefined) {
return cb();
}
/**
* Performs validation for string types.
*
* @param cb The callback function.
*/
this.main.string = function string(cb) {
if(this.shouldValidate()) {
this.required();
if(typeof this.value !== 'string') {
this.raise(
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
// if value is required and value is undefined
// no need to add this error message
if(this.rule.required && this.value === undefined) {
return cb();
}
this.range();
this.pattern();
if(this.rule.whitespace === true) {
if(/^\s+$/.test(this.value) || this.value === '') {
if(typeof this.value !== 'string') {
this.raise(
this.reasons.whitespace,
this.messages.whitespace, this.field);
this.reasons.type,
this.messages.types[this.rule.type],
this.field, this.rule.type);
}
this.range();
this.pattern();
if(this.rule.whitespace === true) {
if(/^\s+$/.test(this.value) || this.value === '') {
this.raise(
this.reasons.whitespace,
this.messages.whitespace, this.field);
}
}
}
cb();
}
cb();
}
module.exports = function() {
this.main.string = string;
}

@@ -8,2 +8,6 @@ Table of Contents

* [Guide](#guide)
* [Descriptor](#descriptor)
* [Object Definition](#object-definition)
* [Array Definition](#array-definition)
* [Function Definition](#function-definition)
* [Rules](#rules)

@@ -17,3 +21,3 @@ * [Inline Rule](#inline-rule)

* [Plugins](#plugins)
* [Descriptor](#descriptor)
* [Rule Properties](#rule-properties)
* [Type Identifier](#type-identifier)

@@ -49,3 +53,2 @@ * [Additional](#additional)

* [pattern](#pattern)
* [type](#type)
* [range](#range)

@@ -75,3 +78,3 @@ * [Developer](#developer)

Usage involves defining a descriptor, assigning it to a schema using the necessary plugins and calling validate:
Define validation rules, assign them to a schema using the necessary plugins and call validate:

@@ -99,2 +102,22 @@ ```javascript

### Descriptor
A descriptor is a collection of validation rules as a map of fields to rules, rules may be declared as an `object`, `array` or `function`.
#### Object Definition
```javascript
var descriptor = {
name: {type: 'string', required: true}
}
```
#### Array Definition
You may declare an `array` to use multiple validation rules per field, see [multiple rules](#multiple-rules).
#### Function Definition
Use an inline function definition for application specific rules, see [inline rule](#inline-rule).
### Rules

@@ -306,5 +329,5 @@

### Descriptor
### Rule Properties
A descriptor defines the validation rules as a map of fields to rules, this section describes the recognised rule properties.
This section describes the recognised rule properties and their behaviour, if you are using an [assigned rule](#assigned-rule) or [plugin rule](#plugin-rule) you can define properties on the rule object and they are available to the rule function via `this.rule`.

@@ -338,3 +361,3 @@ #### Type Identifier

Rules of the `object` and `array` type may declare a `fields` object which declares a nested descriptor, see [deep rules](#deep-rules).
Rules of the `object` and `array` type may declare a `fields` object which declares a nested schema, see [deep rules](#deep-rules).

@@ -638,10 +661,2 @@ #### Message

##### type
```javascript
function type()
```
Validates the type of a value, typically invoked from a rule function, raises an error if a value is not of the correct type.
##### range

@@ -648,0 +663,0 @@

var expect = require('chai').expect
, schema = require('../../index');
, Schema = require('../../index');
describe("async-validate:", function() {
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"}
}
}
}
it("should error on invalid object (additional properties)", function(done) {

@@ -12,14 +25,2 @@ var opts = {

}
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 = {

@@ -34,3 +35,4 @@ name: 'Opps',

}
var validator = new schema(descriptor);
var validator = new Schema(descriptor);
validator.validate(source, opts, function(errors, fields) {

@@ -50,14 +52,2 @@ expect(errors.length).to.eql(2);

}
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 = {

@@ -70,3 +60,3 @@ address: {

}
var validator = new schema(descriptor);
var validator = new Schema(descriptor);
validator.validate(source, opts, function(errors, fields) {

@@ -73,0 +63,0 @@ expect(errors).to.eql(null);

@@ -1,13 +0,15 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate array type", function(done) {
it("should error on non-array type", function(done) {
var descriptor = {
list: {type: "array"},
}
var validator = new schema(descriptor);
validator.validate({list: false}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "list is not an 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(
'list is not an array');
done();

@@ -17,36 +19,43 @@ });

it("should validate array length minimum", function(done) {
it("should error on array length minimum", function(done) {
var descriptor = {
list: {type: "array", min: 2},
}
var validator = new schema(descriptor);
validator.validate({list: [1]}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "list cannot be less than 2 in length");
var schema = new Schema(descriptor);
schema.validate({list: [1]}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'list cannot be less than 2 in length');
done();
});
});
it("should validate array length maximum", function(done) {
it("should error on array length maximum", function(done) {
var descriptor = {
list: {type: "array", max: 2},
}
var validator = new schema(descriptor);
validator.validate({list: [1,2,3]}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "list cannot be greater than 2 in length");
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(
'list cannot be greater than 2 in length');
done();
});
});
it("should validate array length range", function(done) {
it("should error on array length range", function(done) {
var descriptor = {
list: {type: "array", min: 1, max: 2},
}
var validator = new schema(descriptor);
validator.validate({list: [1,2,3]}, function(errors, fields) {
assert.equal(errors.length, 1);
//console.log(errors[0].message);
assert.equal(errors[0].message, "list must be between 1 and 2 in length");
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(
'list must be between 1 and 2 in length');
done();
});
});
});

@@ -1,27 +0,27 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate boolean type", function(done) {
var descriptor = {
flag: {type: "boolean"},
}
var validator = new schema(descriptor);
validator.validate({flag: "false"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "flag is not a boolean");
var descriptor = {
flag: {type: "boolean"},
}
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');
done();
});
});
it("should validate boolean pass", function(done) {
var descriptor = {
flag: {type: "boolean"},
}
var validator = new schema(descriptor);
validator.validate({flag: true}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({flag: true}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,5 +0,12 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function(done) {
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) {

@@ -9,51 +16,57 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({active: "2013-06-50"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"active date 2013-06-50 is invalid for 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(
'active date 2013-06-50 is invalid for format YYYY-MM-DD');
done();
});
});
it("should error on invalid date value no format (ISO 8601)", function(done) {
var descriptor = {
active: {type: "date"}
it("should error on invalid date value no format (ISO 8601)",
function(done) {
var descriptor = {
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(
'active date 2011-10-10T10:20:90 is invalid');
done();
});
}
var validator = new schema(descriptor);
validator.validate({active: "2011-10-10T10:20:90"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"active date 2011-10-10T10:20:90 is invalid");
done();
});
});
it("should error on invalid date value no format (bad input)", function(done) {
var descriptor = {
active: {type: "date"}
);
it("should error on invalid date value no format (bad input)",
function(done) {
var descriptor = {
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(
'active date not a date is invalid');
done();
});
}
var validator = new schema(descriptor);
validator.validate({active: "not a date"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"active date not a date is invalid");
done();
});
});
);
var ptn = /^([\d]{4})-([\d]{2})-([\d]{2})$/;
it("should error on invalid date value using a format and pattern", function(done) {
var descriptor = {
active: {
type: "date",
format: "YYYY-MM-DD",
pattern: ptn
it("should error on invalid date value using a format and pattern",
function(done) {
var descriptor = {
active: {
type: "date",
format: "YYYY-MM-DD",
pattern: ptn
}
}
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(
'active value 13-06-24 does not match pattern ' + ptn);
done();
});
}
var validator = new schema(descriptor);
validator.validate({active: "13-06-24"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"active value 13-06-24 does not match pattern " + ptn);
done();
});
});
);

@@ -64,6 +77,5 @@ it("should validate date value using a format", function(done) {

}
var validator = new schema(descriptor);
validator.validate({active: "2013-06-24"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({active: "2013-06-24"}, function(errors, fields) {
expect(errors).to.eql(null);
done();

@@ -77,6 +89,5 @@ });

}
var validator = new schema(descriptor);
validator.validate({active: "2013-06-24"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({active: "2013-06-24"}, function(errors, fields) {
expect(errors).to.eql(null);
done();

@@ -90,14 +101,9 @@ });

}
var validator = new schema(descriptor);
validator.validate({active: "2011-10-10T10:20:30"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({active: "2011-10-10T10:20:30"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
var date = {
type: "date",
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/,
format: "YYYY-MM-DD"
}
it("should validate optional date range reference", function(done) {

@@ -108,9 +114,9 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({start: "", end: "2013-06-24"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({start: "", end: "2013-06-24"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,63 +0,64 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate deep rule (not required/no matching property)", function(done) {
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 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 validator = new schema(descriptor);
validator.validate({}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
done();
});
});
it("should error on invalid deep rule (required/no matching property)", function(done) {
var descriptor = {
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 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 validator = new schema(descriptor);
validator.validate({}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "address is required");
done();
});
});
it("should error on invalid deep rule (required and validation failure on deep rule)", function(done) {
var descriptor = {
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"}
}
},
name: {type: "string", required: true}
}
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"}
}
}
var validator = new schema(descriptor);
validator.validate({ address: {} }, function(errors, fields) {
assert.equal(errors.length, 4);
assert.equal(errors[0].message, "street is required");
assert.equal(errors[1].message, "city is required");
assert.equal(errors[2].message, "invalid zip");
assert.equal(errors[3].message, "name is required");
done();
});
});
}
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');
done();
});
}
);
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');
done();
});
}
);
it("should error on deep rule (with bail out options)", function(done) {

@@ -75,7 +76,7 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({ address: {} }, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "street is required");
assert.equal(errors[1].message, "name is required");
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');
done();

@@ -96,7 +97,7 @@ });

}
var validator = new schema(descriptor);
validator.validate({ roles: ["admin", "user"] }, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "roles must be exactly 3 in length");
assert.equal(errors[1].message, "2 is required");
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');
done();

@@ -106,21 +107,4 @@ });

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 validator = new schema(descriptor);
validator.validate({ roles: ["admin", "user", "guest"] }, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
done();
});
});
it("should error on invalid multiple deep rule", function(done) {
var descriptor = {

@@ -140,10 +124,42 @@ address: {

}
var validator = new schema(descriptor);
validator.validate({ address: {house: {}} }, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "name is required");
assert.equal(errors[1].message, "number is required");
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');
done();
});
});
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);
done();
});
}
);
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)
, source = {roles: ["admin", "user", "guest"]};
schema.validate(source, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,27 +0,27 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
var descriptor = {
role: {type: "enum", list: ['admin', 'user', 'guest']}
}
it("should error on invalid enum value", function(done) {
var descriptor = {
role: {type: "enum", list: ['admin', 'user', 'guest']}
}
var validator = new schema(descriptor);
validator.validate({role: "manager"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "role must be one of admin, user, guest");
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');
done();
});
});
it("should validate enum value", function(done) {
var descriptor = {
role: {type: "enum", list: ['admin', 'user', 'guest']}
}
var validator = new schema(descriptor);
validator.validate({role: "user"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({role: "user"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,15 +0,23 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
var descriptor = {
ratio: {type: "float"},
}
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');
done();
});
});
it("should validate a number is a float", function(done) {
var descriptor = {
ratio: {type: "float"},
}
var validator = new schema(descriptor);
validator.validate({ratio: 1618}, function(errors, fields) {
assert.equal(errors.length, 1);
//console.log(errors[0].message);
assert.equal(errors[0].message, "ratio is not a float");
var schema = new Schema(descriptor);
schema.validate({ratio: 1.667}, function(errors, fields) {
expect(errors).to.eql(null);
done();

@@ -16,0 +24,0 @@ });

@@ -1,14 +0,15 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate a number is an integer", function(done) {
var descriptor = {
port: {type: "integer"},
}
var validator = new schema(descriptor);
validator.validate({port: 1.618}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port is not an integer");
var descriptor = {
port: {type: "integer"},
}
it("should error on number as 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');
done();

@@ -18,2 +19,10 @@ });

it("should validate integer type", function(done) {
var schema = new Schema(descriptor);
schema.validate({port: 2048}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,5 +0,6 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should error on invalid string length", function(done) {

@@ -9,9 +10,10 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: "user"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "name must be exactly 10 characters");
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');
done();
});
});
it("should error on invalid number length", function(done) {

@@ -21,9 +23,10 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({port: 8080}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port must equal 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 must equal 80');
done();
});
});
it("should error on invalid array length", function(done) {

@@ -33,9 +36,10 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({roles: ["user"]}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "roles must be exactly 2 in length");
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');
done();
});
});
it("should validate string length", function(done) {

@@ -45,20 +49,20 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: "username"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({name: "username"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
it("should validiate number length", function(done) {
it("should validate number length", function(done) {
var descriptor = {
port: {type: "number", len: 80},
}
var validator = new schema(descriptor);
validator.validate({port: 80}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({port: 80}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
it("should validate array length", function(done) {

@@ -68,9 +72,9 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({roles: ["user", "admin"]}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({roles: ["user", "admin"]}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,14 +0,15 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate a value is a function", function(done) {
var descriptor = {
mock: {type: "method"},
}
var validator = new schema(descriptor);
validator.validate({mock: 80}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "mock is not a method");
var descriptor = {
mock: {type: "method"},
}
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');
done();

@@ -18,2 +19,10 @@ });

it("should validate function type", function(done) {
var schema = new Schema(descriptor);
schema.validate({mock: function(){}}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,58 +0,69 @@

var assert = require('chai').assert
, schema = require('../../index')
var expect = require('chai').expect
, Schema = require('../../index')
, email = /^.+@.+\..+/;
describe("async-validate:", function(done) {
it("should validate using multiple validation rules for a field", function(done) {
var descriptor = {
email: [
{type: "string", required: true},
{pattern: email, required: true}
]
it("should error on multiple validation rules for a field",
function(done) {
var descriptor = {
email: [
{type: "string", required: true},
{pattern: email, required: true}
]
}
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(
'email value undefined does not match pattern ' + email);
done();
});
}
var validator = new schema(descriptor);
validator.validate({address: "joe@example"}, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "email is required");
assert.equal(errors[1].message,
"email value undefined does not match pattern " + email);
done();
});
});
it("should validate using multiple validation rules for a field single failure", function(done) {
var descriptor = {
email: [
{type: "string", required: true},
{pattern: email}
]
);
it("should error on multiple validation rules for a field single failure",
function(done) {
var descriptor = {
email: [
{type: "string", required: true},
{pattern: email}
]
}
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(
'email value user@example does not match pattern ' + email);
done();
});
}
var validator = new schema(descriptor);
validator.validate({email: "user@example"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"email value user@example does not match pattern " + email);
done();
});
});
it("should validate using multiple validation rules with a validation function", function(done) {
var descriptor = {
email: [
{type: 'string', pattern: email, required: true},
function(cb) {
var email = "user@example.com";
if(this.value === email) {
this.raise("Email address %s already exists", email);
);
it("should error on multiple validation rules with a validation function",
function(done) {
var descriptor = {
email: [
{type: 'string', pattern: email, required: true},
function(cb) {
var email = "user@example.com";
if(this.value === email) {
this.raise("Email address %s already exists", email);
}
cb();
}
cb();
}
]
]
}
var schema = new Schema(descriptor)
, source = {email: "user@example.com"};
schema.validate(source, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'Email address user@example.com already exists');
done();
});
}
var validator = new schema(descriptor);
validator.validate({email: "user@example.com"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"Email address user@example.com already exists");
done();
});
});
);
});

@@ -1,51 +0,58 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate a value is a number", function(done) {
it("should error on not a number", function(done) {
var descriptor = {
port: {type: "number"},
}
var validator = new schema(descriptor);
validator.validate({port: "80"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port is not a 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');
done();
});
});
it("should validate a number is 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},
}
var validator = new schema(descriptor);
validator.validate({port: 80}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port cannot be less than 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');
done();
});
});
it("should validate a number is greater than a minimum value", function(done) {
var descriptor = {
port: {type: "number", max: 80},
it("should error on number number greater than a minimum value",
function(done) {
var descriptor = {
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');
done();
});
}
var validator = new schema(descriptor);
validator.validate({port: 8080}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port cannot be greater than 80");
done();
});
});
it("should validate a number is greater than a minimum value", function(done) {
var descriptor = {
port: {type: "number", min: 80, max: 1024},
);
it("should error on number out of range",
function(done) {
var descriptor = {
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');
done();
});
}
var validator = new schema(descriptor);
validator.validate({port: 8080}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "port must be between 80 and 1024");
done();
});
});
);
});

@@ -1,38 +0,36 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
var descriptor = {
address: {type: "object", required: true}
}
it("should error on invalid object (array specified)", function(done) {
var descriptor = {
address: {type: "object", required: true}
}
var validator = new schema(descriptor);
validator.validate({address: []}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "address is not an object");
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');
done();
});
});
it("should error on invalid object (required but not specified)", function(done) {
var descriptor = {
address: {type: "object", required: true}
}
var validator = new schema(descriptor);
validator.validate({}, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "address is required");
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');
done();
});
});
it("should validate object (empty object)", function(done) {
var descriptor = {
address: {type: "object", required: true}
}
var validator = new schema(descriptor);
validator.validate({address: {}}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({address: {}}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
});

@@ -1,49 +0,50 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate multiple errors", function(done) {
var descriptor = {
firstname: {type: "string", required: true},
surname: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({}, {first: false}, function(errors, fields) {
assert.equal(errors.length, 2);
var descriptor = {
firstname: {type: "string", required: true},
surname: {type: "string", required: true}
}
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);
done();
});
});
it("should validate keys option", function(done) {
var descriptor = {
firstname: {type: "string", required: true},
surname: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({}, {keys: ["firstname"]}, function(errors, fields) {
assert.equal(errors.length, 1);
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);
done();
});
});
it("should validate fail on first error", function(done) {
var descriptor = {
firstname: {type: "string", required: true},
surname: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({}, {first: true}, function(errors, fields) {
assert.equal(errors.length, 1);
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);
done();
});
});
it("should validate single option", function(done) {
it("should error with single option", function(done) {
var descriptor = {
name: {type: "string", required: true, min: 10, pattern: /^[^-].*$/}
}
var validator = new schema(descriptor);
validator.validate({name: "-name"}, {first: true, single: true}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(fields.name.length, 1);
var schema = new Schema(descriptor)
, 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);
done();
});
});
});

@@ -1,14 +0,15 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
var descriptor = {
name: {type: "string", len: 8},
}
it("should use default series iteration", function(done) {
var descriptor = {
name: {type: "string", len: 8},
}
var validator = new schema(descriptor);
validator.validate({name: "username", surname: 'foo'},
var schema = new Schema(descriptor);
schema.validate({name: "username", surname: 'foo'},
function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
expect(errors).to.eql(null);
done();

@@ -20,10 +21,8 @@ }

it("should use parallel iteration", function(done) {
var descriptor = {
name: {type: "string", len: 8},
}
var validator = new schema(descriptor);
validator.validate({name: "username", surname: 'foo'}, {parallel: true},
var schema = new Schema(descriptor)
, source = {name: "username", surname: 'foo'};
schema.validate(source, {parallel: true},
function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
expect(errors).to.eql(null);
done();

@@ -33,2 +32,3 @@ }

});
});

@@ -1,38 +0,39 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate regexp type (positive lookbehind unsupported)", function(done) {
var descriptor = {
re: {type: "regexp"},
var descriptor = {
re: {type: "regexp"},
}
it("should error on regexp string (positive lookbehind unsupported)",
function(done) {
var schema = new Schema(descriptor)
, source = {re: "(?<=(category=))[a-z-]+"};
schema.validate(source, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql('re is not a valid regexp');
done();
});
}
var validator = new schema(descriptor);
validator.validate({re: "(?<=(category=))[a-z-]+"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "re is not a valid regexp");
);
it("should validate valid regexp string", function(done) {
var schema = new Schema(descriptor);
schema.validate({re: "^[a-z]+$"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
it("should validate regexp pass", function(done) {
var descriptor = {
re: {type: "regexp"},
}
var validator = new schema(descriptor);
validator.validate({re: "^[a-z]+$"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
it("should validate native regexp instance", function(done) {
var schema = new Schema(descriptor);
schema.validate({re: /^[a-z]+$/}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
it("should validate native regexp pass", function(done) {
var descriptor = {
re: {type: "regexp"},
}
var validator = new schema(descriptor);
validator.validate({re: /^[a-z]+$/}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
done();
});
});
});

@@ -1,18 +0,7 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should validate a required string field is valid", function(done) {
var descriptor = {
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({name: "field"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
done();
});
});
it("should validate using a custom validator function", function(done) {
it("should error on a custom schema function", function(done) {
var descriptor = {

@@ -28,75 +17,74 @@ name: function(cb) {

}
var validator = new schema(descriptor);
validator.validate({name: "Firstname"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message,
"name must be lowercase alphanumeric characters");
var schema = new Schema(descriptor);
schema.validate({name: "Firstname"}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'name must be lowercase alphanumeric characters');
done();
});
});
it("should validate a required string field", function(done) {
it("should error on required string field", function(done) {
var descriptor = {
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({noname: "field"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "name is required");
assert.equal(fields.name.length, 1);
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');
done();
});
});
it("should validate a required string field (null)", function(done) {
it("should error on required string field (null)", function(done) {
var descriptor = {
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({name: null}, function(errors, fields) {
assert.equal(errors.length, 2);
assert.equal(errors[0].message, "name is required");
assert.equal(fields.name.length, 2);
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');
done();
});
});
it("should validate a field of type string is of the correct type", function(done) {
it("should error on non-string type", function(done) {
var descriptor = {
name: {type: "string"}
}
var validator = new schema(descriptor);
validator.validate({name: 10}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "name is not a 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');
done();
});
});
it("should validate a required string field with minimum length", function(done) {
var descriptor = {
name: {type: "string", required: true, min: 8}
it("should error on required string field with minimum length",
function(done) {
var descriptor = {
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(
'name must be at least 8 characters');
done();
});
}
var validator = new schema(descriptor);
validator.validate({name: "field"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "name must be at least 8 characters");
done();
});
});
it("should validate a required string field with maximum length", function(done) {
var descriptor = {
name: {type: "string", required: true, max: 2}
}
var validator = new schema(descriptor);
validator.validate({name: "field"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "name cannot be longer than 2 characters");
done();
});
});
it("should validate a required string field is valid and in the range", function(done) {
);
it("should error on required string field with maximum length",
function(done) {
var descriptor = {
name: {type: "string", required: true, min: 6, max: 20}
name: {type: "string", required: true, max: 2}
}
var validator = new schema(descriptor);
validator.validate({name: "valid field"}, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
var schema = new Schema(descriptor);
schema.validate({name: "field"}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'name cannot be longer than 2 characters');
done();

@@ -106,3 +94,4 @@ });

);
it("should validate a required string field is less than a length range",
it("should error on required string field is less than a length range",
function(done) {

@@ -112,8 +101,7 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: "field"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(
errors[0].message,
"name must be between 6 and 8 characters");
var schema = new Schema(descriptor);
schema.validate({name: "field"}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'name must be between 6 and 8 characters');
done();

@@ -123,3 +111,4 @@ });

);
it("should validate a required string field is greater than a length range",
it("should error on required string field is greater than a length range",
function(done) {

@@ -129,8 +118,7 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: "field"}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(
errors[0].message,
"name must be between 2 and 4 characters");
var schema = new Schema(descriptor);
schema.validate({name: "field"}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'name must be between 2 and 4 characters');
done();

@@ -140,3 +128,3 @@ });

);
it("should validate a regular expression pattern mismatch",
it("should error on regular expression pattern mismatch",
function(done) {

@@ -146,9 +134,7 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: "alpha"}, function(errors, fields) {
assert.equal(errors.length, 1);
//console.log(errors[0].message);
assert.equal(
errors[0].message,
"name value alpha does not match pattern /^[0-9]+$/");
var schema = new Schema(descriptor);
schema.validate({name: "alpha"}, function(errors, fields) {
expect(errors.length).to.eql(1);
expect(errors[0].message).to.eql(
'name value alpha does not match pattern /^[0-9]+$/');
done();

@@ -158,3 +144,4 @@ });

);
it("should validate a string consisting of whitespace",
it("should error on string consisting of whitespace",
function(done) {

@@ -164,7 +151,6 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: " "}, function(errors, fields) {
assert.equal(errors.length, 1);
//console.log(errors[0].message);
assert.equal( errors[0].message, "name cannot be empty");
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');
done();

@@ -174,3 +160,4 @@ });

);
it("should validate the empty string",
it("should error on empty string",
function(done) {

@@ -180,7 +167,6 @@ var descriptor = {

}
var validator = new schema(descriptor);
validator.validate({name: ""}, function(errors, fields) {
assert.equal(errors.length, 1);
//console.log(errors[0].message);
assert.equal( errors[0].message, "name cannot be empty");
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');
done();

@@ -190,15 +176,39 @@ });

);
it("should revalidate after failure",
it("should validate on required string field", function(done) {
var descriptor = {
name: {type: "string", required: true}
}
var schema = new Schema(descriptor);
schema.validate({name: "field"}, function(errors, fields) {
expect(errors).to.eql(null);
done();
});
});
it("should validate on required string field in range",
function(done) {
var descriptor = {
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);
done();
});
}
);
it("should validate after failure",
function(done) {
var descriptor = {
name: {type: "string", required: true, whitespace: true}
}
var validator = new schema(descriptor);
validator.validate({name: ""}, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal( errors[0].message, "name cannot be empty");
validator.validate({name: "user"}, function(errors, fields) {
//console.log("after revalidation %j", errors);
assert.isNull(errors);
assert.isNull(fields);
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);
done();

@@ -209,2 +219,3 @@ });

);
});

@@ -1,24 +0,28 @@

var assert = require('chai').assert;
var schema = require('../../index');
var expect = require('chai').expect
, Schema = require('../../index');
describe("async-validate:", function() {
it("should transform by stripping whitespace", function(done) {
var descriptor = {
name: {
type: "string",
required: true, pattern: /^[a-z]+$/,
transform: function(value) {
return value.trim();
}
var descriptor = {
name: {
type: "string",
required: true, pattern: /^[a-z]+$/,
transform: function(value) {
return value.trim();
}
}
var validator = new schema(descriptor);
var source = {name: " user "};
validator.validate(source, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
assert.equal(source.name, "user");
}
it("should transform by stripping whitespace", function(done) {
var schema = new Schema(descriptor)
, source = {name: " user "};
schema.validate(source, function(errors, fields) {
expect(errors).to.eql(null);
expect(fields).to.eql(null);
expect(source.name).to.eql('user');
done();
});
});
});

@@ -1,6 +0,4 @@

var assert = require('chai').assert
, expect = require('chai').expect;
var expect = require('chai').expect
, Schema = require('../../index');
var schema = require('../../index');
describe("async-validate:", function() {

@@ -13,8 +11,7 @@

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {age: undefined, name : "User"};
var opts = {first : false, single: true};
validator.validate(source, opts, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);
done();

@@ -30,8 +27,7 @@ });

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {age: undefined, name : "User"};
var opts = {first : true, single: true};
validator.validate(source, opts, function(errors, fields) {
assert.isNull(errors);
assert.isNull(fields);
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);
done();

@@ -48,9 +44,9 @@ });

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {age: 'abc', name : "User"};
var opts = {first : true, single: true};
validator.validate(source, opts, function(errors, fields) {
assert.equal(errors.length, 1);
assert.equal(errors[0].message, "age is not an integer");
assert.equal(errors[0].field, 'age');
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');
done();

@@ -66,6 +62,6 @@ });

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -80,6 +76,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -94,6 +90,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -108,6 +104,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -122,6 +118,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -136,6 +132,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -150,6 +146,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -164,6 +160,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -178,6 +174,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -192,6 +188,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -206,6 +202,6 @@ done();

}
var validator = new schema(descriptor);
var schema = new Schema(descriptor);
var source = {mock: undefined};
var opts = {};
validator.validate(source, opts, function(errors, fields) {
schema.validate(source, opts, function(errors, fields) {
expect(errors).to.eql(null);

@@ -212,0 +208,0 @@ done();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc