🎁 validateProvidedData Hook
Introducing the validateProvidedData
Hook
The validateProvidedData
hook is just like validate.form
, but it only validates the attributes from the schema which are actually present in the request's data
object. In short, it allows partial validation of the schema attributes. Using it as a hook looks like this:
const validate = require('@featehrs-plus/validate-joi')
const attrs = require('./faqs.model')
const hooks = {
before: {
patch: [
validate.validateProvidedData(attrs, { abortEarly: false })
]
}
}
The above example supposes that you have an /faqs
service with a model that looks like the following. Notice how the attrs
are defined as a separate object, then they are used in the schema and made available in the export. The validateProvidedData
hook uses the individual attrs to validate each individual item in the request's data
object.
// src/services/faqs/faqs.model.js
const Joi = require('@hapi/joi')
const { objectId } = require('@feathers-plus/validate-joi-mongodb')
const attrs = {
_id: objectId(),
question: Joi.string().disallow(null).required(),
answer: Joi.string().disallow(null).required(),
isPublic: Joi.boolean().default(false),
createdBy: objectId().disallow(null).required()
}
module.exports = {
attrs,
schema: Joi.object(attrs)
}