【问题标题】:Joi validate self-referencing nested schemaJoi 验证自引用嵌套模式
【发布时间】:2020-06-01 07:03:18
【问题描述】:

我正在尝试验证一个模式,该模式与其他字段一起具有一组自引用对象,如下所示:

export const answer = answerModel.concat(Joi.object().keys({
    childAnswers: Joi.array().items(answer),
    numArray: Joi.array().items(Joi.number()).required()
}))

我的问题是我无法在答案架构 Joi.array().items(answer) 中引用答案架构,因为在声明它之前我无法使用它。

问题是“有没有办法以这种嵌套格式进行自我引用以进行验证”?

【问题讨论】:

    标签: typescript joi


    【解决方案1】:

    您可以为此使用Joi.link()

    const person = Joi.object({
      firstName: Joi.string().required(),
      lastName: Joi.string().required(),
      friends: Joi.array().items(Joi.link('#person'))
    }).id('person');
    

    【讨论】:

    • 大卫这个很棒的家伙
    猜你喜欢
    • 2016-09-14
    • 2016-08-12
    • 1970-01-01
    • 2018-02-27
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2016-02-19
    相关资源
    最近更新 更多