New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.3.2 to 0.3.4

doc/readme/api.md

6

doc/readme/introduction.md
Asynchronous validation for [node](http://nodejs.org) and the browser.
## Synopsis
None of the existing validation packages met the requirement to asynchronously validate user input. The [validator](https://github.com/chriso/node-validator) package is a helpful utility while [validate](https://github.com/eivindfjeldstad/validate) was closer as it took a more declarative approach but does not support asynchronous operations such as checking for existence of a database or validating that an email address already exists in a database.
So this package was created to allow for asynchronous validation of user input using a declarative schema based approach.

1

doc/readme/links.md
[node]: http://nodejs.org
[npm]: http://www.npmjs.org
[mdp]: https://github.com/freeformsystems/mdp
[zephyr]: https://github.com/socialally/zephyr
## Usage
Note that as of the `0.2.x` series, this module no longer ships any built in type validators, you need to call `plugin()`, for example to load all plugins (backward compatibility):
Usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to `validate()`:
```javascript
require('async-validate/plugin/all');
```
Otherwise only load the validators for the types you are using:
```javascript
var schema = require('async-validate');
schema.plugin([
require('async-validate/plugin/array'),
require('async-validate/plugin/boolean'),
require('async-validate/plugin/number'),
require('async-validate/plugin/string')
])
```
Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the `validate` method of the schema:
```javascript
var schema = require('async-validate');
var descriptor = {
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({name: "muji"}, function(errors, fields) {
if(errors) {
// validation failed, errors is an array of all errors
// fields is an object keyed by field name with an array of
// errors per field
return handleErrors(errors, fields);
}
// validation passed
});
```
## Validate
```javascript
function(source, [options], callback)
```
* `source`: The object to validate (required).
* `options`: An object describing processing options for the validation (optional).
* `callback`: A callback function to invoke when validation completes (required).
### Options
* `first`: Invoke `callback` when the first validation rule generates an error, no more validation rules are processed. If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.
* `single`: Only ever return a single error. Typically used in conjunction with `first` when a validation rule could generate multiple errors.
* `keys`: Specifies the keys on the source object to be validated. Use this option to validate fields in a determinate order or to validate a subset of the rules assigned to a schema.
* `parallel`: A boolean indicating that the validation should be executed in parallel.
Consider the rule:
```javascript
{name: {type: "string", required: true, min: 10, pattern: /^[^-].*$/}}
```
When supplied with a source object such as `{name: "-name"}` the validation rule would generate two errors, as the pattern does not match and the string length is less then the required minimum length for the field.
In this instance when you only want the first error encountered use the `single` option.
## Rules
Rules may be functions that perform validation.
```javascript
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.
* `source`: The source object that was passed to the `validate` method.
* `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`.
### 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 descriptor = {
name: function(opts, cb) {
if(!/^[a-z0-9]+$/.test(opts.value)) {
this.raise(opts.rule, '%s must be lowercase alphanumeric characters', opts.rule.field);
}
cb(opts.errors);
}
}
var validator = new schema(descriptor);
validator.validate({name: "Firstname"}, function(errors, fields) {
if(errors) {
return handleErrors(errors, fields);
}
// validation passed
});
```
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example:
```javascript
var descriptor = {
email: [
{type: "string", required: true, pattern: schema.pattern.email},
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
cb(errors);
}
]
}
```
### Type
Indicates the `type` of validator to use. Recognised type values are:
* `string`: Must be of type `string`.
* `number`: Must be of type `number`.
* `boolean`: Must be of type `boolean`.
* `method`: Must be of type `function`.
* `regexp`: Must be an instance of `RegExp` or a string that does not generate an exception when creating a new `RegExp`.
* `integer`: Must be of type `number` and an integer.
* `float`: Must be of type `number` and a floating point number.
* `array`: Must be an array as determined by `Array.isArray`.
* `object`: Must be of type `object` and not `Array.isArray`.
* `enum`: Value must exist in the `enum`.
* `date`: Value must be valid as determined by `moment().isValid()`.
### Required
The `required` rule property indicates that the field must exist on the source object being validated.
### Pattern
The `pattern` rule property indicates a regular expression that the value must match to pass validation.
### Range
A range is defined using the `min` and `max` properties. For `string` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`.
### Length
To validate an exact length of a field specify the `len` property. For `string` and `array` types comparison is performed on the `length` property, for the `number` type this property indicates an exact match for the `number`, ie, it may only be strictly equal to `len`.
If the `len` property is combined with the `min` and `max` range properties, `len` takes precedence.
### Enumerable
To validate a value from a list of possible values use the `enum` type with a `enum` property listing the valid values for the field, for example:
```javascript
var descriptor = {
role: {type: "enum", enum: ['admin', 'user', 'guest']}
}
```
### Date Format
Validating dates can be complex but using [moment](http://momentjs.com/) date validation is substantially easier.
If no `format` is specified for a rule that is a `date` type then it is assumed the date is ISO 8601. If a format is specified then the date is validated according to the specified format.
It is recommended you read the [moment documentation](http://momentjs.com/docs/#/parsing/is-valid/) on the `isValid` method to understand what validation is performed.
The important part is:
> Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.
This limitation may be overcome by combining a `pattern` in a date rule, for example:
```javascript
var descriptor = {
active: {
type: "date",
format: "YYYY-MM-DD",
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/
}
}
```
### Whitespace
It is typical to treat required fields that only contain whitespace as errors. To add an additional test for a string that consists solely of whitespace add a `whitespace` property to a rule with a value of `true`. The rule must be a `string` type.
You may wish to sanitize user input instead of testing for whitespace, see [transform](#transform) for an example that would allow you to strip whitespace.
### Deep Rules
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule.
```javascript
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 validator = new schema(descriptor);
validator.validate({ address: {} }, function(errors, fields) {
// errors for street, city, zip and name
});
```
Note that if you do not specify the `required` property on the parent rule it is perfectly valid for the field not to be declared on the source object and the deep validation rules will not be executed as there is nothing to validate against.
Deep rule validation creates a schema for the nested rules so you can also specify the `options` passed to the `schema.validate()` method.
```javascript
var descriptor = {
address: {
type: "object", required: true, options: {single: true, first: true},
fields: {
street: {type: "string", required: true},
city: {type: "string", required: true},
zip: {type: "string", required: true, len: 8, message: "invalid zip"}
}
},
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({ address: {} }, function(errors, fields) {
// now only errors for street and name
});
```
The parent rule is also validated so if you have a set of rules such as:
```javascript
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}
}
}
}
```
And supply a source object of `{roles: ["admin", "user"]}` then two errors will be created. One for the array length mismatch and one for the missing required array entry at index 2.
### Transform
Sometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and re-assigned to the source object to mutate the value of the property in place.
```javascript
var schema = require('async-validate');
var sanitize = require('validator').sanitize;
var descriptor = {
name: {
type: "string",
required: true, pattern: /^[a-z]+$/,
transform: function(value) {
return sanitize(value).trim();
}
}
}
var validator = new schema(descriptor);
var source = {name: " user "};
validator.validate(source, function(errors, fields) {
assert.equal(source.name, "user");
});
```
Without the `transform` function validation would fail due to the pattern not matching as the input contains leading and trailing whitespace, but by adding the transform function validation passes and the field value is sanitized at the same time.
## Messages
Depending upon your application requirements, you may need i18n support or you may prefer different validation error messages.
The easiest way to achieve this is to assign a `message` to a rule:
```javascript
{name:{type: "string", required: true, message: "Name is required"}}
```
If you just want to change the default messages:
```javascript
var schema = require('async-validate');
schema.messages.required = "%s is a required field"; // change the message
...
```
Potentially you may require the same schema validation rules for different languages, in which case duplicating the schema rules for each language does not make sense.
In this scenario you could just require your own messages file for the language and assign it to the schema:
```javascript
var schema = require('async-validate');
var es = require('messages-es');
var descriptor = {name:{type: "string", required: true}};
var validator = new schema(descriptor);
validator.messages(es);
...
```
Or you could clone a default messages instance and then assign language specific messages to the schema using the `messages` method.
```javascript
var schema = require('async-validate');
var es = schema.messages.clone();
es.required = "%s es un campo obligatorio"; // change the message
var descriptor = {name:{type: "string", required: true}};
var validator = new schema(descriptor);
validator.messages(es); // ensure this schema uses the altered messages
...
```
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.
## Standard Rules
Some standard rules for common validation requirements are accessible, you may wish to reference these rules or copy and modify them.
As of `v0.2.0` these rules must be required: `require('async-validate/std-rules')` and are no longer available via the schema class.
### Field
A typical required field:
```javascript
{type: "string", required: true, whitespace: true}
```
### Email
A basic email validation rule using a pattern:
```javascript
{type: "string", required: true, pattern: pattern.email}
```
Note validating email addresses with a regular expression is [fraught with pitfalls](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address/201378#201378), use this with caution.
### URL
A simple http(s) URL rule:
```javascript
{type: "string", required: true, pattern: pattern.url}
```
### Hex
A rule for hexadecimal color values with optional leading hash:
```javascript
{type: "string", required: true, pattern: pattern.hex}
```

@@ -161,3 +161,3 @@ var iterator = require('./iterator')

var opts = getValidationOptions(rule, data, options, cb);
rule.validator.call(opts, opts, cb);
rule.validator.call(opts, cb);
}, function(err, results) {

@@ -164,0 +164,0 @@ complete(results);

{
"name": "async-validate",
"description": "Asynchronous validation for object properties.",
"version": "0.3.2",
"version": "0.3.4",
"author": "muji <noop@xpm.io>",

@@ -49,3 +49,26 @@ "license": "MIT",

"install.md",
"usage.md",
"usage.md"
]
},
{
"inc": "../usage.js",
"type": "code",
"language": "javascript"
},
{
"inc": [
"guide.md",
"transform.md",
"messages.md",
"standard-rules.md"
]
},
{
"inc": "../transform.js",
"type": "code",
"language": "javascript"
},
{
"inc": [
"api.md",
"developer.md",

@@ -52,0 +75,0 @@ "browser.md",

/**
* Validates an array.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function array(opts, cb) {
function array(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

@@ -7,3 +7,3 @@ /**

*/
function bool(opts, cb) {
function bool(cb) {
var validate = this.rule.required

@@ -10,0 +10,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

@@ -43,6 +43,5 @@ var format = require('../lib/format')

*
* @param opts The validation options.
* @param cb The callback function.
*/
function date(opts, cb) {
function date(cb) {
var validate = this.rule.required

@@ -49,0 +48,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field)

/**
* Validates an enumerable list.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function enumerable(opts, cb) {
function enumerable(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates a number is a floating point number.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function fraction(opts, cb) {
function fraction(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates a number is an integer.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function integer(opts, cb) {
function integer(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates a function.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function method(opts, cb) {
function method(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates a number.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function number(opts, cb) {
function number(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates an object.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function object(opts, cb) {
function object(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

@@ -7,6 +7,5 @@ /**

*
* @param opts The validation options.
* @param cb The callback function.
*/
function pattern(opts, cb) {
function pattern(cb) {
var validate = this.rule.required

@@ -13,0 +12,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Validates the regular expression type.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function regexp(opts, cb) {
function regexp(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

/**
* Performs validation for string types.
*
* @param opts The validation options.
* @param cb The callback function.
*/
function string(opts, cb) {
function string(cb) {
var validate = this.rule.required

@@ -9,0 +8,0 @@ || (!this.rule.required && this.source.hasOwnProperty(this.rule.field));

@@ -5,27 +5,30 @@ Table of Contents

* [Async Validate](#async-validate)
* [Synopsis](#synopsis)
* [Install](#install)
* [Usage](#usage)
* [Validate](#validate)
* [Options](#options)
* [Rules](#rules)
* [Options](#options-1)
* [Guide](#guide)
* [Plugins](#plugins)
* [Descriptor](#descriptor)
* [Type](#type)
* [Required](#required)
* [Pattern](#pattern)
* [Range](#range)
* [Length](#length)
* [Enumerable](#enumerable)
* [Date Format](#date-format)
* [Whitespace](#whitespace)
* [Multiple Rules](#multiple-rules)
* [Deep Rules](#deep-rules)
* [Transform](#transform)
* [Messages](#messages)
* [Standard Rules](#standard-rules)
* [Field](#field)
* [Email](#email)
* [URL](#url)
* [Hex](#hex)
* [API](#api)
* [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)
* [Messages](#messages)
* [Standard Rules](#standard-rules)
* [Field](#field)
* [Email](#email)
* [URL](#url)
* [Hex](#hex)
* [Developer](#developer)

@@ -46,8 +49,2 @@ * [Test](#test)

## Synopsis
None of the existing validation packages met the requirement to asynchronously validate user input. The [validator](https://github.com/chriso/node-validator) package is a helpful utility while [validate](https://github.com/eivindfjeldstad/validate) was closer as it took a more declarative approach but does not support asynchronous operations such as checking for existence of a database or validating that an email address already exists in a database.
So this package was created to allow for asynchronous validation of user input using a declarative schema based approach.
## Install

@@ -61,29 +58,14 @@

Note that as of the `0.2.x` series, this module no longer ships any built in type validators, you need to call `plugin()`, for example to load all plugins (backward compatibility):
Usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to `validate()`:
```javascript
require('async-validate/plugin/all');
```
var schema = require('..')
, descriptor = {name: {type: "string", required: true}}
, validator = new schema(descriptor)
, source = {};
Otherwise only load the validators for the types you are using:
schema.plugin(
[require('../plugin/string')]);
```javascript
var schema = require('async-validate');
schema.plugin([
require('async-validate/plugin/array'),
require('async-validate/plugin/boolean'),
require('async-validate/plugin/number'),
require('async-validate/plugin/string')
])
```
Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the `validate` method of the schema:
```javascript
var schema = require('async-validate');
var descriptor = {
name: {type: "string", required: true}
}
var validator = new schema(descriptor);
validator.validate({name: "muji"}, function(errors, fields) {
validator.validate(source, function(errors, fields) {
if(errors) {

@@ -93,3 +75,3 @@ // validation failed, errors is an array of all errors

// errors per field
return handleErrors(errors, fields);
return console.dir(errors)
}

@@ -100,95 +82,40 @@ // validation passed

## Validate
## Guide
```javascript
function(source, [options], callback)
```
### Plugins
* `source`: The object to validate (required).
* `options`: An object describing processing options for the validation (optional).
* `callback`: A callback function to invoke when validation completes (required).
To use schema types you should load plugins for the types you wish to validate:
### Options
* `first`: Invoke `callback` when the first validation rule generates an error, no more validation rules are processed. If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.
* `single`: Only ever return a single error. Typically used in conjunction with `first` when a validation rule could generate multiple errors.
* `keys`: Specifies the keys on the source object to be validated. Use this option to validate fields in a determinate order or to validate a subset of the rules assigned to a schema.
* `parallel`: A boolean indicating that the validation should be executed in parallel.
Consider the rule:
```javascript
{name: {type: "string", required: true, min: 10, pattern: /^[^-].*$/}}
var schema = require('async-validate');
schema.plugin([
require('async-validate/plugin/array'),
require('async-validate/plugin/boolean'),
require('async-validate/plugin/number'),
require('async-validate/plugin/string')
])
```
When supplied with a source object such as `{name: "-name"}` the validation rule would generate two errors, as the pattern does not match and the string length is less then the required minimum length for the field.
As a shortcut you may use all available types with:
In this instance when you only want the first error encountered use the `single` option.
## Rules
Rules may be functions that perform validation.
```javascript
function(opts, cb)
require('async-validate/plugin/all');
```
### Options
See [plugins](https://github.com/freeformsystems/async-validate/blob/master/plugins) for the type plugins that ship with this module and [zephyr](https://github.com/socialally/zephyr) for documentation on the plugin system.
The options object has the following fields:
The [plugin fixture](https://github.com/freeformsystems/async-validate/blob/master/test/fixtures/plugin.js) and the [plugin test](https://github.com/freeformsystems/async-validate/blob/master/test/spec/plugin.js) provide an example of creating a type plugin.
* `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.
* `source`: The source object that was passed to the `validate` method.
* `options`: The options passed to `validate()`.
* `messages`: Reference to the messages assigned to `options`.
* `errors`: Array of errors for the field validation.
### Descriptor
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`.
A descriptor defines the validation rules as a map of fields to rules.
### Callback
This section describes the rule fields recognised by the module plugins.
The callback function to invoke once validation is complete. It expects to be passed an array of `Error` instances to indicate validation failure.
#### Type
## Example
Indicates the `type` of validator to use. A type corresponds to a plugin function and the plugin should have been loaded.
```javascript
var schema = require('async-validate');
var descriptor = {
name: function(opts, cb) {
if(!/^[a-z0-9]+$/.test(opts.value)) {
this.raise(opts.rule, '%s must be lowercase alphanumeric characters', opts.rule.field);
}
cb(opts.errors);
}
}
var validator = new schema(descriptor);
validator.validate({name: "Firstname"}, function(errors, fields) {
if(errors) {
return handleErrors(errors, fields);
}
// validation passed
});
```
Recognised type values are:
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example:
```javascript
var descriptor = {
email: [
{type: "string", required: true, pattern: schema.pattern.email},
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
cb(errors);
}
]
}
```
### Type
Indicates the `type` of validator to use. Recognised type values are:
* `string`: Must be of type `string`.

@@ -206,15 +133,15 @@ * `number`: Must be of type `number`.

### Required
#### Required
The `required` rule property indicates that the field must exist on the source object being validated.
### Pattern
#### Pattern
The `pattern` rule property indicates a regular expression that the value must match to pass validation.
### Range
#### Range
A range is defined using the `min` and `max` properties. For `string` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`.
### Length
#### Length

@@ -225,3 +152,3 @@ To validate an exact length of a field specify the `len` property. For `string` and `array` types comparison is performed on the `length` property, for the `number` type this property indicates an exact match for the `number`, ie, it may only be strictly equal to `len`.

### Enumerable
#### Enumerable

@@ -236,3 +163,3 @@ To validate a value from a list of possible values use the `enum` type with a `enum` property listing the valid values for the field, for example:

### Date Format
#### Date Format

@@ -261,3 +188,3 @@ Validating dates can be complex but using [moment](http://momentjs.com/) date validation is substantially easier.

### Whitespace
#### Whitespace

@@ -268,4 +195,21 @@ It is typical to treat required fields that only contain whitespace as errors. To add an additional test for a string that consists solely of whitespace add a `whitespace` property to a rule with a value of `true`. The rule must be a `string` type.

### Deep Rules
#### Multiple Rules
It is often useful to test against multiple validation rules for a single field, to do so make the rule an array of objects, for example:
```javascript
var descriptor = {
email: [
{type: "string", required: true},
function(opts, cb) {
// test if email address already exists in a database
// and add a validation error to the errors array if it does
cb(this.errors);
}
]
}
```
#### Deep Rules
If you need to validate deep object properties you may do so for validation rules that are of the `object` or `array` type by assigning nested rules to a `fields` property of the rule.

@@ -334,24 +278,5 @@

```javascript
var schema = require('async-validate');
var sanitize = require('validator').sanitize;
var descriptor = {
name: {
type: "string",
required: true, pattern: /^[a-z]+$/,
transform: function(value) {
return sanitize(value).trim();
}
}
}
var validator = new schema(descriptor);
var source = {name: " user "};
validator.validate(source, function(errors, fields) {
assert.equal(source.name, "user");
});
```
Without the `transform` function validation would fail due to the pattern not matching as the input contains leading and trailing whitespace, but by adding the transform function validation passes and the field value is sanitized at the same time.
## Messages
### Messages

@@ -401,9 +326,9 @@ Depending upon your application requirements, you may need i18n support or you may prefer different validation error messages.

## Standard Rules
### Standard Rules
Some standard rules for common validation requirements are accessible, you may wish to reference these rules or copy and modify them.
As of `v0.2.0` these rules must be required: `require('async-validate/std-rules')` and are no longer available via the schema class.
These rules must be required to be used: `require('async-validate/std-rules')`.
### Field
#### Field

@@ -416,3 +341,3 @@ A typical required field:

### Email
#### Email

@@ -427,3 +352,3 @@ A basic email validation rule using a pattern:

### URL
#### URL

@@ -436,3 +361,3 @@ A simple http(s) URL rule:

### Hex
#### Hex

@@ -445,2 +370,77 @@ A rule for hexadecimal color values with optional leading hash:

```javascript
var schema = require('..')
, descriptor = {
name: {
type: "string",
required: true, pattern: /^[a-z]+$/,
transform: function(value) {
return value.trim();
}
}
}
, validator = new schema(descriptor)
, source = {name: " user "};
schema.plugin([require('../plugin/string')]);
validator.validate(source, function(errors, fields) {
console.dir(source.name);
});
```
### API
#### Validate
```javascript
function(source, [options], callback)
```
* `source`: The object to validate (required).
* `options`: An object describing processing options for the validation (optional).
* `callback`: Callback function to invoke when validation completes (required).
##### Options
* `first`: Invoke `callback` when the first validation rule generates an error, no more validation rules are processed. If your validation involves multiple asynchronous calls (for example, database queries) and you only need the first error use this option.
* `single`: Only ever return a single error. Typically used in conjunction with `first` when a validation rule could generate multiple errors.
* `keys`: Specifies the keys on the source object to be validated. Use this option to validate fields in a determinate order or to validate a subset of the rules assigned to a schema.
* `parallel`: A boolean indicating that the validation should be executed in parallel.
Consider the rule:
```javascript
{name: {type: "string", required: true, min: 10, pattern: /^[^-].*$/}}
```
When supplied with a source object such as `{name: "-name"}` the validation rule would generate two errors, as the pattern does not match and the string length is less then the required minimum length for the field.
In this instance when you only want the first error encountered use the `single` option.
#### Rules
Rules may be functions that perform validation.
```javascript
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.
* `source`: The source object that was passed to the `validate` method.
* `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`.
### 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

@@ -525,1 +525,2 @@

[mdp]: https://github.com/freeformsystems/mdp
[zephyr]: https://github.com/socialally/zephyr

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

function id(opts, cb) {
function id(cb) {
var re = /^[^-][a-zA-Z0-9-]+$/;

@@ -3,0 +3,0 @@ if(!re.test(this.value)) {

@@ -39,7 +39,6 @@ var util = require('util');

var descriptor = {
name: function(opts ,cb) {
var errors = opts.errors;
name: function(cb) {
this.raise(
opts.rule, "%s is a required field", opts.rule.field);
cb(errors);
this.rule, "%s is a required field", this.rule.field);
cb(this.errors);
}

@@ -46,0 +45,0 @@ }

@@ -45,10 +45,10 @@ var util = require('util');

std.email,
function(opts, cb) {
var errors = [];
function(cb) {
var email = "user@example.com";
if(opts.value === email) {
errors.push(new ValidationError(
util.format("Email address %s already exists", email)));
if(this.value === email) {
this.raise(
this.rule,
"Email address %s already exists", email);
}
cb(errors);
cb(this.errors);
}

@@ -55,0 +55,0 @@ ]

@@ -21,11 +21,10 @@ var util = require('util');

var descriptor = {
name: function(opts, cb) {
var errors = opts.errors;
if(!/^[a-z0-9]+$/.test(opts.value)) {
errors.push(
new ValidationError(
util.format("%s must be lowercase alphanumeric characters",
opts.rule.field)));
name: function(cb) {
if(!/^[a-z0-9]+$/.test(this.value)) {
this.raise(
this.rule,
"%s must be lowercase alphanumeric characters",
this.rule.field);
}
cb(errors);
cb(this.errors);
}

@@ -32,0 +31,0 @@ }

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