async-validate
Advanced tools
Comparing version 0.2.4 to 0.3.1
@@ -100,4 +100,2 @@ var iterator = require('./iterator') | ||
options.messages = messages; | ||
options.error = error; | ||
options.exception = ValidationError; | ||
var j, z, arr, value, i, rule, validator, series = []; | ||
@@ -147,4 +145,4 @@ var keys = options.keys || Object.keys(this.rules); | ||
return callback(null, [ | ||
options.error( | ||
rule, format(options.messages.required, rule.field)) | ||
new ValidationError( | ||
format(options.messages.required, rule.field)) | ||
]); | ||
@@ -156,4 +154,2 @@ } | ||
data.rule.options.messages = options.messages; | ||
data.rule.options.exception = options.exception; | ||
data.rule.options.error = options.error; | ||
} | ||
@@ -168,6 +164,3 @@ schema.validate( | ||
var opts = getValidationOptions(rule, data, options, cb); | ||
rule.validator(opts, cb); | ||
//rule.validator( | ||
//rule, data.value, cb, data.source, options); | ||
rule.validator.call(opts, opts, cb); | ||
}, function(err, results) { | ||
@@ -231,26 +224,6 @@ complete(results); | ||
/** | ||
* Register a validator function for a type. | ||
* | ||
* @param type The type for the validation rule. | ||
* @param validator The validation function for the rule. | ||
* | ||
* @api public | ||
*/ | ||
function register(type, validator) { | ||
if(typeof validator !== 'function') { | ||
throw new Error( | ||
'Cannot register a validator by type, validator is not a function'); | ||
} | ||
// validator plugin functions are static methods | ||
var validators = Validator; | ||
validators[type] = validator; | ||
} | ||
Schema.Validator = Validator; | ||
// simpler plugin access | ||
Schema.plugin = Validator.plugin; | ||
Schema.ValidationError = ValidationError; | ||
Schema.register = register; | ||
module.exports = Schema; |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for object properties.", | ||
"version": "0.2.4", | ||
"version": "0.3.1", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -21,6 +21,6 @@ "license": "MIT", | ||
"devDependencies": { | ||
"browserify": "~11.0.1", | ||
"browserify": "~11.1.0", | ||
"chai": "~3.2.0", | ||
"istanbul": "~0.3.17", | ||
"mocha": "~2.2.5", | ||
"mocha": "~2.3.2", | ||
"moment": "~2.10.6", | ||
@@ -30,7 +30,30 @@ "validator": "~4.0.1" | ||
"scripts": { | ||
"docs": "npm run readme", | ||
"readme": "mdp --force -v", | ||
"browserify": "browserify -o async-validate.js -e ./lib/schema.js", | ||
"clean": "rm -rf coverage", | ||
"clean": "rm -rf coverage ./async-validate.js", | ||
"test": "NODE_ENV=test mocha test/global ${SPEC:-test/spec}", | ||
"cover": "NODE_ENV=test istanbul cover _mocha -- test/global ${SPEC:-test/spec}" | ||
}, | ||
"mdp": { | ||
"title": "Async Validate", | ||
"pedantic": true, | ||
"include": "doc/readme", | ||
"require": "lib", | ||
"links": "links.md", | ||
"toc": "Table of Contents", | ||
"base": "https://github.com/freeformsystems/async-validate", | ||
"partial": [ | ||
{ | ||
"inc": [ | ||
"introduction.md", | ||
"install.md", | ||
"usage.md", | ||
"developer.md", | ||
"browser.md", | ||
"license.md" | ||
] | ||
} | ||
] | ||
} | ||
} |
@@ -8,22 +8,18 @@ /** | ||
function array(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
this.required(); | ||
if(rule.required || value !== undefined) { | ||
opts.type(); | ||
opts.range(); | ||
if(this.rule.required || this.value !== undefined) { | ||
this.type(); | ||
this.range(); | ||
} | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -30,0 +26,0 @@ |
@@ -8,18 +8,14 @@ /** | ||
function bool(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
this.required(); | ||
this.type(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -26,0 +22,0 @@ |
@@ -6,17 +6,12 @@ var format = require('../lib/format') | ||
* Rule for validating a date against a format. | ||
* | ||
* @param opts The validation options. | ||
*/ | ||
function validator(opts) { | ||
var rule = opts.rule | ||
, value = opts.value | ||
, options = opts.options | ||
, errors = opts.errors; | ||
if(!rule.required | ||
&& (value === undefined || value === "")) { | ||
function validator() { | ||
if(!this.rule.required | ||
&& (this.value === undefined || this.value === '')) { | ||
return false; | ||
} | ||
var mmt = rule.local ? moment : moment.utc; | ||
var dt = !rule.format ? mmt(new Date(value)) : mmt(value, rule.format); | ||
var mmt = this.rule.local ? moment : moment.utc; | ||
var dt = !this.rule.format | ||
? mmt(new Date(this.value)) : mmt(this.value, this.rule.format); | ||
//console.log('value %s', value); | ||
@@ -27,13 +22,19 @@ //console.log('format %s', rule.format); | ||
if(!dt) { | ||
errors.push(opts.error(rule, | ||
format(options.messages.date.parse, rule.field, value))); | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.parse, this.rule.field, this.value)); | ||
}else if(!dt.isValid()) { | ||
if(rule.format) { | ||
errors.push(opts.error(rule, | ||
format(options.messages.date.format, rule.field, value, rule.format))); | ||
if(this.rule.format) { | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.format, | ||
this.rule.field, this.value, this.rule.format)); | ||
}else{ | ||
errors.push(opts.error(rule, | ||
format(options.messages.date.invalid, rule.field, value))); | ||
this.raise( | ||
this.rule, | ||
format(this.messages.date.invalid, this.rule.field, this.value)); | ||
} | ||
} | ||
return true; | ||
} | ||
@@ -48,20 +49,14 @@ | ||
function date(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field) | ||
&& source[rule.field]); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field) | ||
&& this.source[this.rule.field]); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.pattern(); | ||
validator(opts); | ||
this.required(); | ||
this.pattern(); | ||
validator.call(this); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -68,0 +63,0 @@ |
@@ -8,18 +8,13 @@ /** | ||
function enumerable(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.enumerable(); | ||
this.required(); | ||
this.enumerable(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -26,0 +21,0 @@ |
@@ -8,19 +8,14 @@ /** | ||
function fraction(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
opts.range(); | ||
this.required(); | ||
this.type(); | ||
this.range(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -27,0 +22,0 @@ |
@@ -8,18 +8,14 @@ /** | ||
function integer(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
opts.range(); | ||
this.required(); | ||
this.type(); | ||
this.range(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -26,0 +22,0 @@ |
@@ -8,17 +8,13 @@ /** | ||
function method(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
this.required(); | ||
this.type(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -25,0 +21,0 @@ |
@@ -8,19 +8,15 @@ /** | ||
function number(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
opts.range(); | ||
this.required(); | ||
this.type(); | ||
this.range(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -27,0 +23,0 @@ |
@@ -8,19 +8,15 @@ /** | ||
function object(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
if(rule.required || value !== undefined) { | ||
opts.type(); | ||
this.required(); | ||
if(this.rule.required || this.value !== undefined) { | ||
this.type(); | ||
} | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -27,0 +23,0 @@ |
@@ -11,16 +11,12 @@ /** | ||
function pattern(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.pattern(); | ||
this.pattern(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -27,0 +23,0 @@ |
@@ -8,17 +8,13 @@ /** | ||
function regexp(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
this.required(); | ||
this.type(); | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -25,0 +21,0 @@ |
@@ -8,24 +8,20 @@ /** | ||
function string(opts, cb) { | ||
var errors = opts.errors | ||
, rule = opts.rule | ||
, value = opts.value | ||
, source = opts.source | ||
, validate = rule.required | ||
|| (!rule.required && source.hasOwnProperty(rule.field)); | ||
var validate = this.rule.required | ||
|| (!this.rule.required && this.source.hasOwnProperty(this.rule.field)); | ||
if(validate) { | ||
if(value === undefined && !rule.required) { | ||
if(this.value === undefined && !this.rule.required) { | ||
return cb(); | ||
} | ||
opts.required(); | ||
opts.type(); | ||
opts.range(); | ||
opts.pattern(); | ||
this.required(); | ||
this.type(); | ||
this.range(); | ||
this.pattern(); | ||
if(rule.whitespace === true) { | ||
opts.whitespace(); | ||
if(this.rule.whitespace === true) { | ||
this.whitespace(); | ||
} | ||
} | ||
cb(errors); | ||
cb(this.errors); | ||
} | ||
@@ -32,0 +28,0 @@ |
187
README.md
@@ -1,17 +0,46 @@ | ||
# async-validate | ||
Table of Contents | ||
================= | ||
Asynchronous validation for [node](http://nodejs.org). | ||
* [Async Validate](#async-validate) | ||
* [Synopsis](#synopsis) | ||
* [Install](#install) | ||
* [Usage](#usage) | ||
* [Validate](#validate) | ||
* [Options](#options) | ||
* [Rules](#rules) | ||
* [Options](#options-1) | ||
* [Callback](#callback) | ||
* [Example](#example) | ||
* [Type](#type) | ||
* [Required](#required) | ||
* [Pattern](#pattern) | ||
* [Range](#range) | ||
* [Length](#length) | ||
* [Enumerable](#enumerable) | ||
* [Date Format](#date-format) | ||
* [Whitespace](#whitespace) | ||
* [Deep Rules](#deep-rules) | ||
* [Transform](#transform) | ||
* [Register](#register) | ||
* [Messages](#messages) | ||
* [Standard Rules](#standard-rules) | ||
* [Field](#field) | ||
* [Email](#email) | ||
* [URL](#url) | ||
* [Hex](#hex) | ||
* [Developer](#developer) | ||
* [Test](#test) | ||
* [Cover](#cover) | ||
* [Browserify](#browserify) | ||
* [Clean](#clean) | ||
* [Docs](#docs) | ||
* [Readme](#readme) | ||
* [Browser Support](#browser-support) | ||
* [License](#license) | ||
## Installation | ||
Async Validate | ||
============== | ||
``` | ||
npm i async-validate | ||
``` | ||
Asynchronous validation for [node](http://nodejs.org) and the browser. | ||
## Test | ||
``` | ||
npm test | ||
``` | ||
## Synopsis | ||
@@ -23,2 +52,8 @@ | ||
## Install | ||
``` | ||
npm i async-validate | ||
``` | ||
## Usage | ||
@@ -28,3 +63,3 @@ | ||
``` | ||
```javascript | ||
require('async-validate/plugin/all'); | ||
@@ -35,3 +70,3 @@ ``` | ||
``` | ||
```javascript | ||
var schema = require('async-validate'); | ||
@@ -97,29 +132,32 @@ schema.plugin([ | ||
```javascript | ||
function(rule, value, callback, source, options) | ||
function(opts, cb) | ||
``` | ||
### Options | ||
The options object has the following 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. | ||
* `callback`: A callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure. | ||
* `source`: The source object that was passed to the `validate` method. | ||
* `options`: Additional options. | ||
* `options.messages`: The object containing validation error messages. | ||
* `options.exception`: A reference to the ValidationError class. | ||
* `options.error`: A helper function for generating validation errors. | ||
* `options`: The options passed to `validate()`. | ||
* `messages`: Reference to the messages assigned to `options`. | ||
* `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`, `exception` and `error`. | ||
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. | ||
## Example | ||
```javascript | ||
var schema = require('async-validate'); | ||
var ValidationError = schema.error; | ||
var descriptor = { | ||
name: function(rule, value, callback, source, options) { | ||
var errors = []; | ||
if(!/^[a-z0-9]+$/.test(value)) { | ||
errors.push( | ||
new ValidationError( | ||
util.format("%s must be lowercase alphanumeric characters", | ||
rule.field))); | ||
name: function(opts, cb) { | ||
if(!/^[a-z0-9]+$/.test(opts.value)) { | ||
this.raise(opts.rule, '%s must be lowercase alphanumeric characters', opts.rule.field); | ||
} | ||
callback(errors); | ||
cb(opts.errors); | ||
} | ||
@@ -142,7 +180,7 @@ } | ||
{type: "string", required: true, pattern: schema.pattern.email}, | ||
function(rule, value, callback, source, options) { | ||
var errors = []; | ||
function(opts, cb) { | ||
var errors = opts.errors; | ||
// test if email address already exists in a database | ||
// and add a validation error to the errors array if it does | ||
callback(errors); | ||
cb(errors); | ||
} | ||
@@ -227,3 +265,2 @@ ] | ||
### Deep Rules | ||
@@ -327,3 +364,3 @@ | ||
var schema = require('async-validate'); | ||
var ValidationError = schema.error; | ||
var ValidationError = schema.ValidationError; | ||
var validator = function(rule, value, callback, source, options) { | ||
@@ -428,10 +465,80 @@ var errors = []; | ||
## Developer | ||
### Test | ||
Run the test specifications: | ||
``` | ||
npm test | ||
``` | ||
### Cover | ||
Generate code coverage: | ||
``` | ||
npm run cover | ||
``` | ||
### Browserify | ||
Create a standalone browserify build: | ||
``` | ||
npm run browserify | ||
``` | ||
### Clean | ||
Remove generated files: | ||
``` | ||
npm run clean | ||
``` | ||
### Docs | ||
To generate all documentation: | ||
``` | ||
npm run docs | ||
``` | ||
### Readme | ||
Generate the project readme file [mdp](https://github.com/freeformsystems/mdp): | ||
``` | ||
npm run readme | ||
``` | ||
## Browser Support | ||
![Chrome](https://cloud.githubusercontent.com/assets/398893/3528328/23bc7bc4-078e-11e4-8752-ba2809bf5cce.png) | ![Firefox](https://cloud.githubusercontent.com/assets/398893/3528329/26283ab0-078e-11e4-84d4-db2cf1009953.png) | ![Opera](https://cloud.githubusercontent.com/assets/398893/3528330/27ec9fa8-078e-11e4-95cb-709fd11dac16.png) | ![Safari](https://cloud.githubusercontent.com/assets/398893/3528331/29df8618-078e-11e4-8e3e-ed8ac738693f.png) | ||
--- | --- | --- | --- | ||
Latest ✔ (tested on 39) | Latest ✔ (tested on 33) | Latest ✔ (tested on 25) | Latest ✔ (tested on 8) | ||
<table> | ||
<thead> | ||
<tr> | ||
<th><img src="https://cloud.githubusercontent.com/assets/398893/3528328/23bc7bc4-078e-11e4-8752-ba2809bf5cce.png" alt="Chrome"></th> | ||
<th><img src="https://cloud.githubusercontent.com/assets/398893/3528329/26283ab0-078e-11e4-84d4-db2cf1009953.png" alt="Firefox"></th> | ||
<th><img src="https://cloud.githubusercontent.com/assets/398893/3528330/27ec9fa8-078e-11e4-95cb-709fd11dac16.png" alt="Opera"></th> | ||
<th><img src="https://cloud.githubusercontent.com/assets/398893/3528331/29df8618-078e-11e4-8e3e-ed8ac738693f.png" alt="Safari"></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Latest ✔ (tested on 39)</td> | ||
<td>Latest ✔ (tested on 33)</td> | ||
<td>Latest ✔ (tested on 25)</td> | ||
<td>Latest ✔ (tested on 8)</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
## License | ||
Everything is [MIT](http://en.wikipedia.org/wiki/MIT_License). Read the [license](/LICENSE) if you feel inclined. | ||
Everything is [MIT](http://en.wikipedia.org/wiki/MIT_License). Read the [license](https://github.com/freeformsystems/async-validate/blob/master/LICENSE) if you feel inclined. | ||
Generated by [mdp(1)](https://github.com/freeformsystems/mdp). | ||
[node]: http://nodejs.org | ||
[npm]: http://www.npmjs.org | ||
[mdp]: https://github.com/freeformsystems/mdp |
@@ -40,6 +40,5 @@ var util = require('util'); | ||
name: function(opts ,cb) { | ||
var errors = | ||
opts.options.error( | ||
opts.rule, "%s is a required field", opts.rule.field); | ||
var errors = opts.errors; | ||
this.raise( | ||
opts.rule, "%s is a required field", opts.rule.field); | ||
cb(errors); | ||
@@ -46,0 +45,0 @@ } |
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
98362
54
537
2
1982