1
votes

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"?

1

1 Answers

4
votes

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');