Pragonauts Isomorphic Validator
Node.js/Browser validation utility, which allows us to use single validator across multiple endpoints. Uses (npm/validator)[https://www.npmjs.com/package/validator] rules.
const Validator = ; { super; thisCONTEXT_REGISTER_WITH_PASSWORD = 'registerWithPassword'; thisCONTEXT_CREATE = 'create'; thisCONTEXT_UPDATE = 'update'; this ; this ; this ; } const catchAllErrors = false;const validator = ; validator ;
API
Classes
ValidationError
Kind: global class
validationError.message
Kind: instance property of ValidationError
Properties
Name | Type | Description |
---|---|---|
message | string |
validator message |
validationError.property
Kind: instance property of ValidationError
Properties
Name | Type | Description |
---|---|---|
property | string |
name of the property |
validationError.type
Kind: instance property of ValidationError
Properties
Name | Type | Description |
---|---|---|
type | string |
validator name (or function) |
Validator
Kind: global class
- Validator
- new Validator()
- .add(property) ⇒
Rule
- .validateProp(property, value, [catchAllErrors], [data]) ⇒
Promise
- .validate(data, [context], [catchAllErrors]) ⇒
Promise.<object>
new Validator()
Single entity validator. Just extend this class
Rule
validator.add(property) ⇒ Add another property to validate
Kind: instance method of Validator
Param | Type | Description |
---|---|---|
property | string |
name of the property |
Promise
validator.validateProp(property, value, [catchAllErrors], [data]) ⇒ Validate single 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 |
Promise.<object>
validator.validate(data, [context], [catchAllErrors]) ⇒ Kind: instance method of 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
Kind: global class
- Rule
- new Rule()
- new Rule(rules)
- .is(action, [message], [args]) ⇒
this
- .to(action, [args]) ⇒
this
- .if(action) ⇒
this
- .endIf() ⇒
this
- .default(value) ⇒
this
- .contains(string, [message]) ⇒
this
- .isNumeric([message]) ⇒
this
- .isEmail([message]) ⇒
this
- .isUrl([message], [options]) ⇒
this
- .isRequired([message]) ⇒
this
- .isRequiredIfPresent([message]) ⇒
this
- .isFileMime(message, types) ⇒
this
- .isFileMaxLength(message, types) ⇒
this
- .toInt([message]) ⇒
this
- .toBoolean([message]) ⇒
this
- .toFileData() ⇒
this
new Rule()
Single attribute rule contructor
new Rule(rules)
Creates an instance of Rule.
Param | Type |
---|---|
rules | Array.<object> |
this
rule.is(action, [message], [args]) ⇒ Add any validator to rule
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
validator
this
rule.to(action, [args]) ⇒ Adds sanitizer (filter) which converts value to different type
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
action | string | function |
|
[args] | any |
arguments to pass to the validator |
Example
validator ;
this
rule.if(action) ⇒ Adds confition. It can filter validators by context or with custom function
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
action | string | array | function |
context name, array of context names or function |
Example
validator ;
this
rule.endIf() ⇒ Ends condition
Kind: instance method of Rule
this
rule.default(value) ⇒ Sets default value, when item is not filled. Empty string is considered as a value.
Kind: instance method of Rule
Param | Type |
---|---|
value | any |
this
rule.contains(string, [message]) ⇒ Searches for occourence of the string
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
string | string |
|
[message] | string |
null |
this
rule.isNumeric([message]) ⇒ Input shoud be numeric
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isEmail([message]) ⇒ Input should be the email
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isUrl([message], [options]) ⇒ Validates non-empty strings as url
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
[options] | Object |
|
Example
validator
this
rule.isRequired([message]) ⇒ Input is required
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isRequiredIfPresent([message]) ⇒ Input is required, only when atributte is not missing (not undefined)
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isFileMime(message, types) ⇒ Validates File mime types. Compatible with browser-side File class and prg-uploader
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
message | string |
error message |
types | Array.<string> | string | regexp | Array.<regexp> |
validate theese types |
this
rule.isFileMaxLength(message, types) ⇒ Validates File mime types. Compatible with browser-side File class and prg-uploader
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
message | string |
error message |
types | Array.<string> | string | regexp | Array.<regexp> |
validate theese types |
this
rule.toInt([message]) ⇒ Makes the integer from an input
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.toBoolean([message]) ⇒ Makes the boolean from an input
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.toFileData() ⇒ Substracts data from the file. It actually extracts ".data" property from object, but just on the serverside
Kind: instance method of Rule
Rule
Kind: global class
- Rule
- new Rule()
- new Rule(rules)
- .is(action, [message], [args]) ⇒
this
- .to(action, [args]) ⇒
this
- .if(action) ⇒
this
- .endIf() ⇒
this
- .default(value) ⇒
this
- .contains(string, [message]) ⇒
this
- .isNumeric([message]) ⇒
this
- .isEmail([message]) ⇒
this
- .isUrl([message], [options]) ⇒
this
- .isRequired([message]) ⇒
this
- .isRequiredIfPresent([message]) ⇒
this
- .isFileMime(message, types) ⇒
this
- .isFileMaxLength(message, types) ⇒
this
- .toInt([message]) ⇒
this
- .toBoolean([message]) ⇒
this
- .toFileData() ⇒
this
new Rule()
Single attribute rule contructor
new Rule(rules)
Creates an instance of Rule.
Param | Type |
---|---|
rules | Array.<object> |
this
rule.is(action, [message], [args]) ⇒ Add any validator to rule
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
validator
this
rule.to(action, [args]) ⇒ Adds sanitizer (filter) which converts value to different type
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
action | string | function |
|
[args] | any |
arguments to pass to the validator |
Example
validator ;
this
rule.if(action) ⇒ Adds confition. It can filter validators by context or with custom function
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
action | string | array | function |
context name, array of context names or function |
Example
validator ;
this
rule.endIf() ⇒ Ends condition
Kind: instance method of Rule
this
rule.default(value) ⇒ Sets default value, when item is not filled. Empty string is considered as a value.
Kind: instance method of Rule
Param | Type |
---|---|
value | any |
this
rule.contains(string, [message]) ⇒ Searches for occourence of the string
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
string | string |
|
[message] | string |
null |
this
rule.isNumeric([message]) ⇒ Input shoud be numeric
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isEmail([message]) ⇒ Input should be the email
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isUrl([message], [options]) ⇒ Validates non-empty strings as url
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
[options] | Object |
|
Example
validator
this
rule.isRequired([message]) ⇒ Input is required
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isRequiredIfPresent([message]) ⇒ Input is required, only when atributte is not missing (not undefined)
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.isFileMime(message, types) ⇒ Validates File mime types. Compatible with browser-side File class and prg-uploader
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
message | string |
error message |
types | Array.<string> | string | regexp | Array.<regexp> |
validate theese types |
this
rule.isFileMaxLength(message, types) ⇒ Validates File mime types. Compatible with browser-side File class and prg-uploader
Kind: instance method of Rule
Param | Type | Description |
---|---|---|
message | string |
error message |
types | Array.<string> | string | regexp | Array.<regexp> |
validate theese types |
this
rule.toInt([message]) ⇒ Makes the integer from an input
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.toBoolean([message]) ⇒ Makes the boolean from an input
Kind: instance method of Rule
Param | Type | Default |
---|---|---|
[message] | string |
null |
this
rule.toFileData() ⇒ Substracts data from the file. It actually extracts ".data" property from object, but just on the serverside
Kind: instance method of Rule