Isomorphic Validation framework
npm install prg-validatorNode.js/Browser validation utility, which allows us to use single validator across multiple endpoints. Uses (npm/validator)[https://www.npmjs.com/package/validator] rules.
``javascript
const Validator = require('prg-validator');
class UserValidator extends Validator {
constructor () {
super();
this.CONTEXT_REGISTER_WITH_PASSWORD = 'registerWithPassword';
this.CONTEXT_CREATE = 'create';
this.CONTEXT_UPDATE = 'update';
this.add('email')
.if([this.CONTEXT_REGISTER_WITH_PASSWORD, this.CONTEXT_CREATE])
.isRequired('Email is required')
.endIf()
.isEmail('The email has to be valid');
this.add('username')
.if([this.CONTEXT_CREATE])
.isRequired('The username is required')
.endIf()
.is('isLength', 'The username has to be at least 1 character long.', {
min: 1
});
this.add('password')
.if([
this.CONTEXT_REGISTER_WITH_PASSWORD,
this.CONTEXT_SET_PASSWORD,
this.CONTEXT_CREATE
])
.isRequired('Password is required')
.endIf()
.if(val => val)
.is('isLength', 'The password has to be at least 7 characters long.', { min: 7 })
.endIf();
}
}
const catchAllErrors = false;
const validator = new UserValidator();
validator.validate({ email: 'ab' }, validator.CONTEXT_UPDATE, catchAllErrors)
.then((validData) => {
// successful validation, data are filtered
})
.catch((e) => {
// e instanceof ValidationError, or ValidationError[] when catchAllErrors is true
});
`
-----------------
* ValidationError
* .message
* .property
* .type
ValidationError | Name | Type | Description |
| --- | --- | --- |
| message | string | validator message |
ValidationError | Name | Type | Description |
| --- | --- | --- |
| property | string | name of the property |
ValidationError | Name | Type | Description |
| --- | --- | --- |
| type | string | validator name (or function) |
* Validator
* new Validator()
* .add(property) ⇒ Rule
* [.validateProp(property, value, [catchAllErrors], [data])](#Validator+validateProp) ⇒ Promise
* [.validate(data, [context], [catchAllErrors])](#Validator+validate) ⇒ Promise.<object>
Kind: instance method of Validator
| Param | Type | Description |
| --- | --- | --- |
| property | string | name of the property |
Kind: instance method of Validator
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| property | string | | name of property |
| value | any | | |
| [catchAllErrors] | boolean | false | stop on first error or return all found errors |
| [data] | object | {} | other data to use for conditions |
Validator | Param | Type | Default | Description |
| --- | --- | --- | --- |
| data | object | | |
| [context] | string | null | name of validation context, which limits validaton |
| [catchAllErrors] | boolean | false | stop on first error or return all found errors |
* Rule
* new Rule()
* new Rule(rules)
* [.is(action, [message], [args])](#Rule+is) ⇒ this
* [.to(action, [args])](#Rule+to) ⇒ this
* .if(action) ⇒ this
* .endIf() ⇒ this
* .default(value) ⇒ this
* [.contains(string, [message])](#Rule+contains) ⇒ this
* [.isNumeric([message])](#Rule+isNumeric) ⇒ this
* [.isEmail([message])](#Rule+isEmail) ⇒ this
* [.isUrl([message], [options])](#Rule+isUrl) ⇒ this
* [.isRequired([message])](#Rule+isRequired) ⇒ this
* [.isRequiredIfPresent([message])](#Rule+isRequiredIfPresent) ⇒ this
* .isFileMime(message, types) ⇒ this
* .isFileMaxLength(message, types) ⇒ this
* [.toInt([message])](#Rule+toInt) ⇒ this
* [.toBoolean([message])](#Rule+toBoolean) ⇒ this
* .toFileData() ⇒ this
| Param | Type |
| --- | --- |
| rules | Array.<object> |
Kind: instance method of Rule
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| action | string | function | | name of the validator |
| [message] | any | | error message |
| [args] | any | | arguments to pass to the validator |
Example
`javascript`
validator.add('property')
.is(value => value.match(/xy/))
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| action | string | function | |
| [args] | any | arguments to pass to the validator |
Example
`javascript`
validator.add('property')
.to(value => parseInt(value, 10));
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| action | string | array | function | context name, array of context names or function |
Example
`javascript`
validator.add('property')
.if((value, data, context) => data.otherProperty)
.isRequire('Should be filled')
.endIf();
Kind: instance method of Rule
Kind: instance method of Rule
| Param | Type |
| --- | --- |
| value | any |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| string | string | |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
| [options] | Object | |
Example
`javascript`
validator.isUrl('Message', {
protocols: ['http','https','ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_valid_protocol: true,
allow_underscores: false,
host_whitelist: false,
host_blacklist: false,
allow_trailing_dot: false,
allow_protocol_relative_urls: false
})
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| message | string | error message |
| types | Array.<string> | string | regexp | Array.<regexp> | validate theese types |
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| message | string | error message |
| types | Array.<string> | string | regexp | Array.<regexp> | validate theese types |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
* Rule
* new Rule()
* new Rule(rules)
* [.is(action, [message], [args])](#Rule+is) ⇒ this
* [.to(action, [args])](#Rule+to) ⇒ this
* .if(action) ⇒ this
* .endIf() ⇒ this
* .default(value) ⇒ this
* [.contains(string, [message])](#Rule+contains) ⇒ this
* [.isNumeric([message])](#Rule+isNumeric) ⇒ this
* [.isEmail([message])](#Rule+isEmail) ⇒ this
* [.isUrl([message], [options])](#Rule+isUrl) ⇒ this
* [.isRequired([message])](#Rule+isRequired) ⇒ this
* [.isRequiredIfPresent([message])](#Rule+isRequiredIfPresent) ⇒ this
* .isFileMime(message, types) ⇒ this
* .isFileMaxLength(message, types) ⇒ this
* [.toInt([message])](#Rule+toInt) ⇒ this
* [.toBoolean([message])](#Rule+toBoolean) ⇒ this
* .toFileData() ⇒ this
| Param | Type |
| --- | --- |
| rules | Array.<object> |
Kind: instance method of Rule
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| action | string | function | | name of the validator |
| [message] | any | | error message |
| [args] | any | | arguments to pass to the validator |
Example
`javascript`
validator.add('property')
.is(value => value.match(/xy/))
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| action | string | function | |
| [args] | any | arguments to pass to the validator |
Example
`javascript`
validator.add('property')
.to(value => parseInt(value, 10));
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| action | string | array | function | context name, array of context names or function |
Example
`javascript`
validator.add('property')
.if((value, data, context) => data.otherProperty)
.isRequire('Should be filled')
.endIf();
Kind: instance method of Rule
Kind: instance method of Rule
| Param | Type |
| --- | --- |
| value | any |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| string | string | |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
| [options] | Object | |
Example
`javascript``
validator.isUrl('Message', {
protocols: ['http','https','ftp'],
require_tld: true,
require_protocol: false,
require_host: true,
require_valid_protocol: true,
allow_underscores: false,
host_whitelist: false,
host_blacklist: false,
allow_trailing_dot: false,
allow_protocol_relative_urls: false
})
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| message | string | error message |
| types | Array.<string> | string | regexp | Array.<regexp> | validate theese types |
Kind: instance method of Rule
| Param | Type | Description |
| --- | --- | --- |
| message | string | error message |
| types | Array.<string> | string | regexp | Array.<regexp> | validate theese types |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule
| Param | Type | Default |
| --- | --- | --- |
| [message] | string | null |
Kind: instance method of Rule