Joi嵌套对象操作
Joi nested object operations
我有一个 Joi 模式类型:
const schema = Joi.object().keys({
a: Joi.string(),
b: Joi.string(),
c: Joi.string()
})
现在,我想添加一个条件,如果 a
不存在,则 b
和 c
都应该存在。我知道有 object.and()
和 object.or()
之类的操作,但我不确定如何在我的情况下使用这些操作,即 a or (b and c)
。谢谢!
您需要使用 Joi 的替代项和条件功能,您可以在 documentation
中阅读相关内容
const schema = {
a: Joi.string(),
b: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
c: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
};
我有一个 Joi 模式类型:
const schema = Joi.object().keys({
a: Joi.string(),
b: Joi.string(),
c: Joi.string()
})
现在,我想添加一个条件,如果 a
不存在,则 b
和 c
都应该存在。我知道有 object.and()
和 object.or()
之类的操作,但我不确定如何在我的情况下使用这些操作,即 a or (b and c)
。谢谢!
您需要使用 Joi 的替代项和条件功能,您可以在 documentation
中阅读相关内容const schema = {
a: Joi.string(),
b: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
c: Joi.when('a', { is: !Joi.exist(), then: Joi.string().required() }),
};