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

被刻印的时光 ゝ 提交于 2020-01-24 05:28:07

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/51477603/using-joi-how-to-define-recursive-array-of-objects-validation-with-n-depth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!