Validates input object with given assertions.
- Supports validator validators.
- Asynchronous - ES6 Promise based
Install with npm install js-object-validator
.
General syntax: objectValidator.validate(subject, assertions)
subject
{
email: 'abraham.pigeon@example.com',
secondaryEmail: 'pipi@examplecom',
parents: [
{label: 'Dad', age: 68},
{label: 'Mom', age: 160},
],
}
assertions
Assertions are a set of individual rules describing validation assertions for that object.
- key: Path to value in specified context. Usually object property name.
- value: One or an array of Assertions.
{
email: 'isEmail',
secondaryEmail: 'isEmail',
'parents.children': {
age: [
{assert: between, args: [25, 120]},
{assert: 'isInt'}
]
},
gender: {assert: 'isIn', args: [['male', 'female']]}
}
output
objectValidator#validate
returns a Promise, that always resolves with an object, describing subject validation errors.
- key: Corresponds with descriptor key. See objectDescriptor above.
- value: String[] in case of simple properties, or array of validation output for each member in case of collections.
{
"secondaryEmail": [
"Invalid secondary email"
],
"gender": [
"isInRejected"
],
"parents.children": [
{},
{
"age": [
"between"
]
}
]
}
An assertion specifies, how to validate a value and what error to place to that value in case of failure.
Assertion may have different forms, it may be
- a
String
in case you want to use npm validator lib function and you don't care about the message, - a
Function
, in case you want to use custom validator function, - an
Object
in case you want to customize assertion - error message, additional validator arguments, - or an
Array
of any mentioned above to apply multiple assertions for a value.
Key | Value |
---|---|
message |
String . Error message returned in validator output. |
validator |
Function |String . Custom validator function (See Validator how to build one) or function name defined in npm validator lib
|
args |
Array . Array of arguments that are passed to validator function
|
required |
String |Boolean Mark attribute as required. If it is a String , that string is used as the error message. Value passes this check iff it's not undefined
|
optional |
Function |Boolean Mark attribute as optional. Optional true : if value is considered empty, validation asserts are not applied. Consideration is made by lodash's isUndefined by default, pass custom func Boolean <- fn(value) to make the decision by yourself. Optional false is the same as marking attribute as required=true
|
Suppose you want to use custom validator function, like this
function isX(value) {
return value === 'x';
}
This is a shorthand for {assert: function}
, so you don't have to specify other properties if not neccessary.
A shorthand for {assert: validatorFunctionName}
, where validatorFunctionName
(String) is a name of an validator function, defined in npm validator lib.
Validator is a pure function, that accepts following arguments:
validator(value, [args[0], args[1], ...], additionalData)
-
value
is a value the validator should validate. -
args[0], args[1], ...args[..]
, that are arguments specified in descriptor object -
additionalData
is data passed from object validator it self. It is an object:{ data: {*} Object being validated key: {String} Key assertion is defined for }
And returns or resolves with a Boolean (true
/false
).
You define a custom validator function like this:
function between(value, lowerbound, upperbound) {
return value >= lowerbound && value <= upperbound;
}
Sometimes, validating each item in a collection/array is needed.
This can be invoked directly by calling objectValidator.validate(array, assertions)
, where assertions
is applied for each item in an array and result is an array of error results for individual objects.
To validate an array of items on input object, name the key on descriptor object as <COLLECTION_NAME>.children
, so that validator knows, it has to validate each item, and not the array as a whole.
Can be used for translating validation errors.
.replaceMessages(errors, hash)
.
- Errors is an validator output,
- hash is expected to be an object, representing a translation table [from]: to.
.replaceMessagesFunc(errors, handler)
.
- Errors is an validator output,
- handler a function that accepts a string value and is expected to return a string.
objectValidator.replaceMessagesFunc(errors, value => req.i18n.__(value));