async-validate
Advanced tools
Comparing version 0.3.4 to 0.3.6
@@ -32,14 +32,17 @@ ### API | ||
Rules may be functions that perform validation. | ||
Rules are functions that perform validation. They are invoked in the scope of a [validator](/lib/validator.js) so you should not call `bind()` on validation rule functions. | ||
```javascript | ||
function(opts, cb) | ||
function(cb) | ||
``` | ||
##### Options | ||
* `cb`: Callback function to invoke when validation completes, expects array of errors instances to be passed. | ||
The options object has the following fields: | ||
##### Scope | ||
The scope of the rule function exposes the fields: | ||
* `rule`: The validation rule in the source descriptor that corresponds to the field name being validated. It is always assigned a `field` property with the name of the field being validated. | ||
* `value`: The value of the source object property being validated. | ||
* `field`: The name of the field being validated. | ||
* `source`: The source object that was passed to the `validate` method. | ||
@@ -49,7 +52,1 @@ * `options`: The options passed to `validate()`. | ||
* `errors`: Array of errors for the field validation. | ||
The options passed to `validate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`. | ||
### Callback | ||
The callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. |
@@ -31,4 +31,13 @@ ## Guide | ||
This section describes the rule fields recognised by the module plugins. | ||
This section describes the rule fields recognised by the module plugins, typically you would create a type plugin so that the type is reusable but you may also inline rule functions: | ||
```javascript | ||
var descriptor = { | ||
name: function(cb) { | ||
// if has error condition call this.raise() | ||
cb(this.errors); | ||
} | ||
} | ||
``` | ||
#### Type | ||
@@ -35,0 +44,0 @@ |
@@ -160,3 +160,3 @@ var iterator = require('./iterator') | ||
var opts = getValidationOptions(rule, data, options, cb); | ||
var opts = getValidationOptions(rule, data, options); | ||
rule.validator.call(opts, cb); | ||
@@ -168,6 +168,6 @@ }, function(err, results) { | ||
function getValidationOptions(rule, data, options, cb) { | ||
function getValidationOptions(rule, data, options) { | ||
return Validator({ | ||
callback: cb, | ||
rule: rule, | ||
field: rule.field, | ||
value: data.value, | ||
@@ -174,0 +174,0 @@ source: data.source, |
@@ -23,16 +23,15 @@ var plugsys = require('zephyr') | ||
* | ||
* @param rule The validation rule. | ||
* @param message A custom messaage for the error. | ||
* @param ... Replacement parameters passed to format. | ||
*/ | ||
function error(rule, message) { | ||
var msg = rule.message | ||
|| message || format(require('../messages').default, rule.field); | ||
function error(message) { | ||
var msg = this.rule.message | ||
|| message || format(require('../messages').default, this.field); | ||
if(arguments.length > 2) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
if(arguments.length > 1) { | ||
var args = Array.prototype.slice.call(arguments); | ||
msg = format.apply(null, args); | ||
} | ||
var err = new ValidationError(msg); | ||
err.field = rule.field; | ||
err.field = this.field; | ||
return err; | ||
@@ -44,6 +43,6 @@ } | ||
*/ | ||
function raise(rule, message, parameters) { | ||
if(arguments.length > 2 | ||
function raise(message, parameters) { | ||
if(arguments.length > 1 | ||
&& !Array.isArray(parameters)) { | ||
parameters = Array.prototype.slice.call(arguments, 2); | ||
parameters = Array.prototype.slice.call(arguments, 1); | ||
} | ||
@@ -53,3 +52,3 @@ if(typeof message === 'string' && Array.isArray(parameters)) { | ||
} | ||
var err = this.error(rule, message); | ||
var err = this.error(message); | ||
this.errors.push(err); | ||
@@ -65,3 +64,3 @@ } | ||
|| this.value === undefined || this.value === null)) { | ||
this.raise(this.rule, this.messages.required, this.rule.field); | ||
this.raise(this.messages.required, this.field); | ||
} | ||
@@ -75,3 +74,3 @@ } | ||
if(/^\s+$/.test(this.value) || this.value === '') { | ||
this.raise(this.rule, this.messages.whitespace, this.rule.field); | ||
this.raise(this.messages.whitespace, this.field); | ||
} | ||
@@ -86,3 +85,3 @@ } | ||
if(list.indexOf(this.value) === -1) { | ||
this.raise(this.rule, this.messages.enum, this.rule.field, list.join(', ')); | ||
this.raise(this.messages.enum, this.field, list.join(', ')); | ||
} | ||
@@ -97,4 +96,4 @@ } | ||
if(!this.rule.pattern.test(this.value)) { | ||
this.raise(this.rule, this.messages.pattern.mismatch, | ||
this.rule.field, this.value, this.rule.pattern); | ||
this.raise(this.messages.pattern.mismatch, | ||
this.field, this.value, this.rule.pattern); | ||
} | ||
@@ -136,9 +135,9 @@ } | ||
if(len && (val !== rule.len)) { | ||
this.raise(rule, this.messages[key].len, rule.field, rule.len); | ||
this.raise(this.messages[key].len, rule.field, rule.len); | ||
}else if( min && !max && val < rule.min ) { | ||
this.raise(rule, this.messages[key].min, rule.field, rule.min); | ||
this.raise(this.messages[key].min, rule.field, rule.min); | ||
}else if( max && !min && val > rule.max ) { | ||
this.raise(rule, this.messages[key].max, rule.field, rule.max); | ||
this.raise(this.messages[key].max, rule.field, rule.max); | ||
}else if(min && max && (val < rule.min || val > rule.max) ) { | ||
this.raise(rule, this.messages[key].range, rule.field, rule.min, rule.max); | ||
this.raise(this.messages[key].range, rule.field, rule.min, rule.max); | ||
} | ||
@@ -157,3 +156,3 @@ } | ||
// if value is required and value is undefined | ||
// no need to add this error message | ||
// no need to add this error message | ||
if(rule.required && (value === undefined || value === null)) { | ||
@@ -165,10 +164,21 @@ return; | ||
if(!types[type](value)) { | ||
this.raise(rule, this.messages.types[type], rule.field, rule.type); | ||
this.raise(this.messages.types[type], rule.field, rule.type); | ||
} | ||
// straight typeof check | ||
}else if(type && typeof(value) !== rule.type) { | ||
this.raise(rule, this.messages.types[type], rule.field, rule.type); | ||
this.raise(this.messages.types[type], rule.field, rule.type); | ||
} | ||
} | ||
/** | ||
* Determine if validation is required for a field. | ||
*/ | ||
function shouldValidate() { | ||
if(this.value === undefined && !this.rule.required) { | ||
return false; | ||
} | ||
return this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.field)); | ||
} | ||
Validator.prototype.error = error; | ||
@@ -178,2 +188,3 @@ Validator.prototype.raise = raise; | ||
Validator.prototype.required = required; | ||
Validator.prototype.shouldValidate = shouldValidate; | ||
Validator.prototype.whitespace = whitespace; | ||
@@ -180,0 +191,0 @@ Validator.prototype.enumerable = enumerable; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.3.4", | ||
"version": "0.3.6", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -7,10 +7,3 @@ /** | ||
function array(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -17,0 +10,0 @@ |
@@ -8,10 +8,3 @@ /** | ||
function bool(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -18,0 +11,0 @@ this.type(); |
@@ -22,3 +22,2 @@ var format = require('../lib/format') | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.parse, this.rule.field, this.value)); | ||
@@ -28,3 +27,2 @@ }else if(!dt.isValid()) { | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.format, | ||
@@ -34,3 +32,2 @@ this.rule.field, this.value, this.rule.format)); | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.invalid, this.rule.field, this.value)); | ||
@@ -53,5 +50,2 @@ } | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
this.required(); | ||
@@ -58,0 +52,0 @@ this.pattern(); |
@@ -7,9 +7,3 @@ /** | ||
function enumerable(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ this.enumerable(); |
@@ -7,9 +7,3 @@ /** | ||
function fraction(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ this.type(); |
@@ -7,9 +7,3 @@ /** | ||
function integer(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ this.type(); |
@@ -7,9 +7,3 @@ /** | ||
function method(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ this.type(); |
@@ -7,10 +7,3 @@ /** | ||
function number(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -17,0 +10,0 @@ this.type(); |
@@ -7,9 +7,3 @@ /** | ||
function object(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ if(this.rule.required || this.value !== undefined) { |
@@ -10,9 +10,3 @@ /** | ||
function pattern(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.pattern(); | ||
@@ -19,0 +13,0 @@ } |
@@ -7,9 +7,3 @@ /** | ||
function regexp(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -16,0 +10,0 @@ this.type(); |
@@ -7,10 +7,3 @@ /** | ||
function string(cb) { | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
if(this.shouldValidate()) { | ||
this.required(); | ||
@@ -17,0 +10,0 @@ this.type(); |
@@ -31,4 +31,3 @@ Table of Contents | ||
* [Rules](#rules) | ||
* [Options](#options-1) | ||
* [Callback](#callback) | ||
* [Scope](#scope) | ||
* [Developer](#developer) | ||
@@ -109,4 +108,13 @@ * [Test](#test) | ||
This section describes the rule fields recognised by the module plugins. | ||
This section describes the rule fields recognised by the module plugins, typically you would create a type plugin so that the type is reusable but you may also inline rule functions: | ||
```javascript | ||
var descriptor = { | ||
name: function(cb) { | ||
// if has error condition call this.raise() | ||
cb(this.errors); | ||
} | ||
} | ||
``` | ||
#### Type | ||
@@ -409,14 +417,17 @@ | ||
Rules may be functions that perform validation. | ||
Rules are functions that perform validation. They are invoked in the scope of a [validator](https://github.com/freeformsystems/async-validate/blob/master/lib/validator.js) so you should not call `bind()` on validation rule functions. | ||
```javascript | ||
function(opts, cb) | ||
function(cb) | ||
``` | ||
##### Options | ||
* `cb`: Callback function to invoke when validation completes, expects array of errors instances to be passed. | ||
The options object has the following fields: | ||
##### Scope | ||
The scope of the rule function exposes the fields: | ||
* `rule`: The validation rule in the source descriptor that corresponds to the field name being validated. It is always assigned a `field` property with the name of the field being validated. | ||
* `value`: The value of the source object property being validated. | ||
* `field`: The name of the field being validated. | ||
* `source`: The source object that was passed to the `validate` method. | ||
@@ -427,8 +438,2 @@ * `options`: The options passed to `validate()`. | ||
The options passed to `validate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`. | ||
### Callback | ||
The callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. | ||
## Developer | ||
@@ -435,0 +440,0 @@ |
function id(cb) { | ||
var re = /^[^-][a-zA-Z0-9-]+$/; | ||
if(!re.test(this.value)) { | ||
this.raise(this.rule, '%s is not a valid identifier', this.rule.field); | ||
this.raise('%s is not a valid identifier', this.rule.field); | ||
} | ||
@@ -6,0 +6,0 @@ cb(this.errors); |
@@ -40,4 +40,3 @@ var util = require('util'); | ||
name: function(cb) { | ||
this.raise( | ||
this.rule, "%s is a required field", this.rule.field); | ||
this.raise("%s is a required field", this.field); | ||
cb(this.errors); | ||
@@ -44,0 +43,0 @@ } |
@@ -48,5 +48,3 @@ var util = require('util'); | ||
if(this.value === email) { | ||
this.raise( | ||
this.rule, | ||
"Email address %s already exists", email); | ||
this.raise("Email address %s already exists", email); | ||
} | ||
@@ -53,0 +51,0 @@ cb(this.errors); |
@@ -24,5 +24,4 @@ var util = require('util'); | ||
this.raise( | ||
this.rule, | ||
"%s must be lowercase alphanumeric characters", | ||
this.rule.field); | ||
this.field); | ||
} | ||
@@ -29,0 +28,0 @@ cb(this.errors); |
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
515
93464
1945