Validator
A wrapper around Joi validate
method which provides support for promises
Example
'use strict'
const Validator = require('@c8/joi-validator-promised')
const opts = {
abortEarly: true
}
const myValidator = new Validator(opts)
const joiSchema = {
id: Joi.number().min(1),
name: Joi.string()
}
const data = {
id: 2,
name: 'C8 Management'
}
myValidator.validate(data, joiSchema).then(
(result) => {
},
(err) => {
throw new Validator.ValidationError('A validation error occurred')
}
)
let result = myValidator.validateSync(data, joiSchema)
if (!result) {
} else {
throw new Validator.ValidationError('A validation error occurred')
}
As this package is simply a wrapper around Joi validate
method, the opts
object that is passed to the constructor supports all Joi
options