Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JOI How to define recursive array of objects validation with n depth

Tags:

joi

let obj = Joi.object().keys({
      "id":  Joi.string().required(),
      "array": Joi.array().items(obj).required()//array contains multiple
});

is there any way to define recursive array validation in JOI obj.array contains n number of obj

like image 836
Ganesh kharche Avatar asked Jul 23 '18 11:07

Ganesh kharche


1 Answers

Recursive schemas can be achieved using Joi's lazy(fn) function. The following example from the documentation can be adapted to your schema however I'm not sure how you'd be able to define a max depth.

const Person = Joi.object({
    firstName: Joi.string().required(),
    lastName: Joi.string().required(),
    children: Joi.array().items(Joi.lazy(() => Person).description('Person schema'))
});
like image 155
Ankh Avatar answered Sep 28 '22 18:09

Ankh