async-validate
Advanced tools
Comparing version 0.12.0 to 0.12.1
@@ -215,20 +215,26 @@ ## Guide | ||
See the [system schema](/system.js). | ||
##### Type Identifier | ||
* `type <string|function>`: Type identifier or constructor function. | ||
The `type` property indicates the type of rule to use, a type corresponds to a plugin function and the plugin should have been loaded. | ||
A type identifier is required if the rule is not an inline or assigned rule. | ||
Recognised type values are: | ||
* `string`: Must be of type `string`. | ||
* `number`: Must be of type `number`. | ||
* `array`: Must be an array as determined by `Array.isArray`. | ||
* `boolean`: Must be of type `boolean`. | ||
* `date`: Value must be valid as determined by `moment().isValid()`. | ||
* `enum`: Value must exist in the `list`. | ||
* `float`: Must be of type `number` and a floating point number. | ||
* `function`: Must be of type `function`. | ||
* `integer`: Must be of type `number` and an integer. | ||
* `null`: Must strictly equal `null`. | ||
* `number`: Must be of type `number`. | ||
* `object`: Must be of type `object` and not `Array.isArray`. | ||
* `regexp`: Must be an instance of `RegExp` or a valid string 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 `list`. | ||
* `date`: Value must be valid as determined by `moment().isValid()`. | ||
* `string`: Must be of type `string`. | ||
@@ -243,32 +249,89 @@ When the `object` plugin has been loaded the `type` field may be a function in which case the value must be an `instanceof` the function assigned to `type`. | ||
##### Test | ||
##### Enumerable | ||
The function to use for rule validation. | ||
* `list <array>`: The list of enumerable values. | ||
##### Additional | ||
To validate a value from a list of possible values use the `enum` type with a `list` property containing the valid values for the field, for example: | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
``` | ||
##### Fields | ||
##### Date Format | ||
Rules of the `object` and `array` type may declare a `fields` object which declares a nested schema, see [deep rules](#deep-rules). | ||
* `format <string>`: Date format string. | ||
* `local <boolean>`: Use local time rather than UTC. | ||
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 = { | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
} | ||
``` | ||
##### Message | ||
The `message` rule property defines the error message when validation fails, it overrides any default message. The property may be a `string` or `function`, see [messages](#messages). | ||
* `message <string|function>`: Custom error message. | ||
The `message` property defines the error message when validation fails, it overrides any default message. The property may be a `string` or `function`, see [messages](#messages). | ||
##### Required | ||
The `required` rule property indicates that the field must exist on the source object being validated. | ||
* `required <boolean>`: Field is required flag. | ||
The `required` property indicates that the field must exist on the source object being validated. | ||
##### Additional | ||
* `additional <boolean>`: Determines if additional properties are allowed. | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
##### Fields | ||
* `fields <object>`: Map containing rules for object properties. | ||
Rules of the `object` and `array` type may declare a `fields` object which creates a nested schema, see [deep rules](#deep-rules). | ||
##### Pattern | ||
The `pattern` rule property is a regular expression that the value must match to pass validation. | ||
* `pattern <regexp>`: Pattern match regular expression. | ||
The `pattern` property is a regular expression that the value must match to pass validation. | ||
##### Placeholder | ||
A `function` that may return a default value for a field, it is invoked when the field value is `undefined` and the return value is assigned to the field. | ||
* `placeholder <function>`: Placeholder function. | ||
A `function` that may return a default value for a field, it is invoked when the field value is `undefined` and the return value is assigned to the property. | ||
##### Range | ||
* `min <integer>`: Minimum length value. | ||
* `max <integer>`: Maximum length value. | ||
A range is defined using the `min` and `max` properties. For `string`, `function` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`. | ||
@@ -278,2 +341,4 @@ | ||
* `len <integer>`: Length constraint. | ||
To validate an exact length of a field specify the `len` property. For `string`, `function` 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`. | ||
@@ -285,2 +350,4 @@ | ||
* `values <array>`: Array of rules for array types. | ||
Used with the `array` type as a shorthand for validating array values, may be an `object` or `array` containing validation rules. | ||
@@ -292,44 +359,26 @@ | ||
##### Enumerable | ||
##### Match | ||
To validate a value from a list of possible values use the `enum` type with a `list` property containing the valid values for the field, for example: | ||
* `match <regexp>`: Expands a rule to multiple properties. | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
``` | ||
The `match` property may be used to apply a rule to multiple properties of the same object, the rule is cloned for each property name that matches the regular expression and applied to the matched property. | ||
##### Date Format | ||
In this scenario specifying `required` on the match rule would be a non-operation. | ||
Validating dates can be complex but using [moment](http://momentjs.com/) date validation is substantially easier. | ||
This is useful when you have a sequence of properties that share the same rules: | ||
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. | ||
```javascript | ||
{match: /^address[1-3]$/, type: 'string'} | ||
``` | ||
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. | ||
##### Test | ||
The important part is: | ||
* `test <function>`: Rule function. | ||
> 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. | ||
The function to use for rule validation. | ||
This limitation may be overcome by combining a `pattern` in a date rule, for example: | ||
##### Whitespace | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
} | ||
``` | ||
* `whitespace <boolean>`: Determines if whitespace input should be an error. | ||
##### 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. | ||
@@ -336,0 +385,0 @@ |
{ | ||
"name": "async-validate", | ||
"description": "Asynchronous validation for node and the browser", | ||
"version": "0.12.0", | ||
"version": "0.12.1", | ||
"author": "muji <noop@xpm.io>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
157
README.md
@@ -21,7 +21,8 @@ Table of Contents | ||
* [Type Identifier](#type-identifier) | ||
* [Test](#test) | ||
* [Enumerable](#enumerable) | ||
* [Date Format](#date-format) | ||
* [Message](#message) | ||
* [Required](#required) | ||
* [Additional](#additional) | ||
* [Fields](#fields) | ||
* [Message](#message) | ||
* [Required](#required) | ||
* [Pattern](#pattern) | ||
@@ -32,4 +33,4 @@ * [Placeholder](#placeholder) | ||
* [Values](#values) | ||
* [Enumerable](#enumerable) | ||
* [Date Format](#date-format) | ||
* [Match](#match) | ||
* [Test](#test) | ||
* [Whitespace](#whitespace) | ||
@@ -338,20 +339,26 @@ * [Errors](#errors) | ||
See the [system schema](https://github.com/freeformsystems/async-validate/blob/master/system.js). | ||
##### Type Identifier | ||
* `type <string|function>`: Type identifier or constructor function. | ||
The `type` property indicates the type of rule to use, a type corresponds to a plugin function and the plugin should have been loaded. | ||
A type identifier is required if the rule is not an inline or assigned rule. | ||
Recognised type values are: | ||
* `string`: Must be of type `string`. | ||
* `number`: Must be of type `number`. | ||
* `array`: Must be an array as determined by `Array.isArray`. | ||
* `boolean`: Must be of type `boolean`. | ||
* `date`: Value must be valid as determined by `moment().isValid()`. | ||
* `enum`: Value must exist in the `list`. | ||
* `float`: Must be of type `number` and a floating point number. | ||
* `function`: Must be of type `function`. | ||
* `integer`: Must be of type `number` and an integer. | ||
* `null`: Must strictly equal `null`. | ||
* `number`: Must be of type `number`. | ||
* `object`: Must be of type `object` and not `Array.isArray`. | ||
* `regexp`: Must be an instance of `RegExp` or a valid string 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 `list`. | ||
* `date`: Value must be valid as determined by `moment().isValid()`. | ||
* `string`: Must be of type `string`. | ||
@@ -366,32 +373,88 @@ When the `object` plugin has been loaded the `type` field may be a function in which case the value must be an `instanceof` the function assigned to `type`. | ||
##### Test | ||
##### Enumerable | ||
The function to use for rule validation. | ||
* `list <array>`: The list of enumerable values. | ||
##### Additional | ||
To validate a value from a list of possible values use the `enum` type with a `list` property containing the valid values for the field, for example: | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
``` | ||
##### Fields | ||
##### Date Format | ||
Rules of the `object` and `array` type may declare a `fields` object which declares a nested schema, see [deep rules](#deep-rules). | ||
* `format <string>`: Date format string. | ||
* `local <boolean>`: Use local time rather than UTC. | ||
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 = { | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
} | ||
``` | ||
##### Message | ||
The `message` rule property defines the error message when validation fails, it overrides any default message. The property may be a `string` or `function`, see [messages](#messages). | ||
* `message <string|function>`: Custom error message. | ||
The `message` property defines the error message when validation fails, it overrides any default message. The property may be a `string` or `function`, see [messages](#messages). | ||
##### Required | ||
The `required` rule property indicates that the field must exist on the source object being validated. | ||
* `required <boolean>`: Field is required flag. | ||
The `required` property indicates that the field must exist on the source object being validated. | ||
##### Additional | ||
* `additional <boolean>`: Determines if additional properties are allowed. | ||
When a rule is of the `object` type and `additional` is set to `false` an error is raised if the source object contains any properties not in the schema. | ||
##### Fields | ||
* `fields <object>`: Map containing rules for object properties. | ||
Rules of the `object` and `array` type may declare a `fields` object which creates a nested schema, see [deep rules](#deep-rules). | ||
##### Pattern | ||
The `pattern` rule property is a regular expression that the value must match to pass validation. | ||
* `pattern <regexp>`: Pattern match regular expression. | ||
The `pattern` property is a regular expression that the value must match to pass validation. | ||
##### Placeholder | ||
A `function` that may return a default value for a field, it is invoked when the field value is `undefined` and the return value is assigned to the field. | ||
* `placeholder <function>`: Placeholder function. | ||
A `function` that may return a default value for a field, it is invoked when the field value is `undefined` and the return value is assigned to the property. | ||
##### Range | ||
* `min <integer>`: Minimum length value. | ||
* `max <integer>`: Maximum length value. | ||
A range is defined using the `min` and `max` properties. For `string`, `function` and `array` types comparison is performed against the `length`, for `number` types the number must not be less than `min` nor greater than `max`. | ||
@@ -401,2 +464,4 @@ | ||
* `len <integer>`: Length constraint. | ||
To validate an exact length of a field specify the `len` property. For `string`, `function` 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`. | ||
@@ -408,2 +473,4 @@ | ||
* `values <array>`: Array of rules for array types. | ||
Used with the `array` type as a shorthand for validating array values, may be an `object` or `array` containing validation rules. | ||
@@ -415,44 +482,26 @@ | ||
##### Enumerable | ||
##### Match | ||
To validate a value from a list of possible values use the `enum` type with a `list` property containing the valid values for the field, for example: | ||
* `match <regexp>`: Expands a rule to multiple properties. | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
role: {type: "enum", list: ['admin', 'user', 'guest']} | ||
} | ||
} | ||
``` | ||
The `match` property may be used to apply a rule to multiple properties of the same object, the rule is cloned for each property name that matches the regular expression and applied to the matched property. | ||
##### Date Format | ||
In this scenario specifying `required` on the match rule would be a non-operation. | ||
Validating dates can be complex but using [moment](http://momentjs.com/) date validation is substantially easier. | ||
This is useful when you have a sequence of properties that share the same rules: | ||
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. | ||
```javascript | ||
{match: /^address[1-3]$/, type: 'string'} | ||
``` | ||
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. | ||
##### Test | ||
The important part is: | ||
* `test <function>`: Rule function. | ||
> 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. | ||
The function to use for rule validation. | ||
This limitation may be overcome by combining a `pattern` in a date rule, for example: | ||
##### Whitespace | ||
```javascript | ||
var descriptor = { | ||
type: 'object', | ||
fields: { | ||
active: { | ||
type: "date", | ||
format: "YYYY-MM-DD", | ||
pattern: /^([\d]{4})-([\d]{2})-([\d]{2})$/ | ||
} | ||
} | ||
} | ||
``` | ||
* `whitespace <boolean>`: Determines if whitespace input should be an error. | ||
##### 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. | ||
@@ -459,0 +508,0 @@ |
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
205864
1009