【发布时间】:2020-10-20 07:33:38
【问题描述】:
我尝试使用 Joi 实现条件验证。我有一个可以接受的端点:
{
from: 'abc'
}
或
{
type: 'some-type'
}
如果type 字段不存在,from 字段是必填字段,如果from 字段不存在,type 字段是必填字段。 type 只能接受一组值。
我尝试了以下方法但没有成功:
type: joi.alternatives().conditional('from', { is: joi.string().empty(), then: joi.string().required().valid('val1', 'val2'), otherwise: joi.optional() })
.messages({
'any.valid': 'type.not.supported.value',
'any.required': 'type.required'
}),
from: joi.alternatives().conditional('type', { is: joi.string().empty(), then: joi.required(), otherwise: joi.optional() })
.messages({
'any.valid': 'from.not.supported.value',
'any.required': 'from.required'
})
感谢您的帮助! 蒂埃里
【问题讨论】:
标签: joi