const { validate } = require('indicative')
Run validations on a set of data using pre-defined rules. This method stops after the first validation failure.
data |
Object |
rules |
Object |
messages |
Object optional |
formatter |
Formatter optional |
The full build exports the validate
method from the top level object.
const { validate } = require('indicative')
When using customized build, you need to configure the validator instance before you can make use of the validate
method.
import Validator from 'indicative/builds/validator'
import { email } from 'indicative/builds/validations'
import { Vanilla } from 'indicative/builds/formatters'
const { validate } = Validator({ email }, Vanilla)
A miminum of 2 parameters are required to call validate
method.
validate(data, rules)
.then(() => {
})
.catch(() => {
})
Optionally you can pass a custom set of messages.
const messages = {
'username.required': 'Username is required to continue'
}
validate(data, rules, messages)
Optionally pass an inbuilt or a custom formatter.
import { JsonApi } from 'indicative/builds/formatters'
validate(data, rules, messages, JsonApi)
// full build
const { formatters } = require('indicative')
validate(data, rules, messages, formatters.JsonApi)