I am trying to validate a schema that, along other fields, has an array of self-referenced objects like this:
export const answer = answerModel.concat(Joi.object().keys({
childAnswers: Joi.array().items(answer),
numArray: Joi.array().items(Joi.number()).required()
}))
My problem is that I cannot reference answer schema inside answers schema Joi.array().items(answer) since I cannot use it before I declare it.
Question is "is there any way to self-reference in this nested format for validation"?
You can use Joi.link() for this purpose:
const person = Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
friends: Joi.array().items(Joi.link('#person'))
}).id('person');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With