async-validate
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -14,4 +14,5 @@ ### Messages | ||
```javascript | ||
var schema = require('async-validate'); | ||
schema.messages.required = "%s is a required field"; // change the message | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages'); | ||
messages.required = "%s is a required field"; // change the message | ||
... | ||
@@ -36,4 +37,5 @@ ``` | ||
```javascript | ||
var schema = require('async-validate'); | ||
var es = schema.messages.clone(); | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
var es = schema.clone(messages); | ||
es.required = "%s es un campo obligatorio"; // change the message | ||
@@ -46,3 +48,2 @@ var descriptor = {name:{type: "string", required: true}}; | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function. | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the validation function. |
@@ -7,2 +7,5 @@ var iterator = require('./iterator') | ||
/** | ||
* Clone helper function. | ||
*/ | ||
function clone(source, target) { | ||
@@ -40,9 +43,7 @@ var k | ||
* | ||
* @param descriptor An object declaring validation rules | ||
* for this schema. | ||
* @param descriptor Validation rules for this schema. | ||
* @param opts Options for the schema. | ||
*/ | ||
function Schema(descriptor, opts) { | ||
opts = opts || {}; | ||
this.rules = {}; | ||
this.keys = []; | ||
this.messages(opts.messages || require('../messages')); | ||
@@ -75,8 +76,14 @@ this.define(descriptor); | ||
function define(rules) { | ||
if(!rules) { | ||
throw new Error('Cannot configure a schema with no rules'); | ||
} | ||
if(typeof rules !== 'object' || Array.isArray(rules)) { | ||
throw new Error('Rules must be an object') | ||
} | ||
this.rules = {}; | ||
this.keys = []; | ||
var z, item; | ||
@@ -88,3 +95,3 @@ for(z in rules) { | ||
this.keys = Object.keys(this.rules); | ||
this.keys = Object.keys(rules); | ||
} | ||
@@ -162,2 +169,19 @@ | ||
// rules for the root object | ||
if(options.rules) { | ||
var rules = Array.isArray(options.rules) ? options.rules : [options.rules]; | ||
rules = clone(rules); | ||
for(i = 0;i < rules.length;i++) { | ||
rule = rules[i]; | ||
rule.field = options.field || 'source'; | ||
rule.type = this.getType(rule); | ||
rule.validator = this.getValidationMethod(rule); | ||
rule.keys = this.keys; | ||
rule.value = source; | ||
rule.source = source; | ||
series.push(rule); | ||
} | ||
} | ||
// convert map into iterable array | ||
@@ -194,2 +218,6 @@ // assigning field name to rule and perform transform | ||
func(series, function(rule, callback) { | ||
//console.dir(rule); | ||
//console.dir(rule.field); | ||
var validator = getValidationOptions(rule, options); | ||
@@ -201,3 +229,3 @@ | ||
deep = deep && (rule.required || (!rule.required && rule.value)); | ||
//rule.field = data.field; | ||
function onValidate(errors) { | ||
@@ -209,2 +237,4 @@ // bail on first error | ||
//console.dir(errors); | ||
// not deep so continue on to next in series | ||
@@ -211,0 +241,0 @@ if(!deep) { |
@@ -207,2 +207,7 @@ var plugin = require('zephyr') | ||
function shouldValidate() { | ||
if(this.isRoot()) { | ||
return true; | ||
} | ||
if(this.value === undefined && !this.rule.required) { | ||
@@ -235,2 +240,6 @@ return false; | ||
function isRoot() { | ||
return this.source === this.value; | ||
} | ||
Validator.prototype.error = error; | ||
@@ -246,2 +255,3 @@ Validator.prototype.raise = raise; | ||
Validator.prototype.isRoot = isRoot; | ||
Validator.prototype.shouldValidate = shouldValidate; | ||
@@ -248,0 +258,0 @@ Validator.prototype.hasAdditionalFields = hasAdditionalFields; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -7,3 +7,3 @@ /** | ||
function object(cb) { | ||
var additional; | ||
var expected, additional; | ||
@@ -14,11 +14,14 @@ if(this.shouldValidate()) { | ||
if(this.rule.additional === false | ||
&& this.rule.fields | ||
&& typeof this.rule.fields === 'object') { | ||
// nested deep properties | ||
if(this.rule.additional === false) { | ||
// 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); | ||
additional = this.hasAdditionalFields( | ||
Object.keys(this.rule.fields), Object.keys(this.value)); | ||
expected, Object.keys(this.value)); | ||
if(additional) { | ||
//console.dir('additional error'); | ||
this.reason(this.reasons.additional); | ||
@@ -25,0 +28,0 @@ this.raise(this.messages.additional, additional.join(', '), this.field); |
@@ -285,4 +285,5 @@ Table of Contents | ||
```javascript | ||
var schema = require('async-validate'); | ||
schema.messages.required = "%s is a required field"; // change the message | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages'); | ||
messages.required = "%s is a required field"; // change the message | ||
... | ||
@@ -307,4 +308,5 @@ ``` | ||
```javascript | ||
var schema = require('async-validate'); | ||
var es = schema.messages.clone(); | ||
var schema = require('async-validate') | ||
, messages = require('async-validate/messages') | ||
var es = schema.clone(messages); | ||
es.required = "%s es un campo obligatorio"; // change the message | ||
@@ -317,3 +319,3 @@ var descriptor = {name:{type: "string", required: true}}; | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `options.messages` property within the validation function. | ||
If you are defining your own validation functions it is better practice to assign the message strings to a messages object and then access the messages via the `this.messages` property within the validation function. | ||
@@ -320,0 +322,0 @@ ### Standard Rules |
@@ -6,2 +6,3 @@ var util = require('util'); | ||
describe("async-validate:", function() { | ||
it("should validate a value is a number", function(done) { | ||
@@ -51,25 +52,3 @@ var descriptor = { | ||
}); | ||
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"); | ||
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"); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109514
70
2481
517