我有联合型PaymentTerm
type PaymentTerm =
| { type: 'AdvancePayment' }
| { type: 'PaymentGoal'; netPaymentDays: number }我想用Joi.alternatives验证它
Joi.object({
type: Joi.string().required().valid('AdvancePayment')
}),
Joi.object({
type: Joi.string().required().valid('PaymentGoal'),
netPaymentDays: Joi.number().required().messages({
'any.required': '{{#label}} is required'
})
})
)
const { error, value } = schema.validate({
type: 'PaymentGoal'
})现在我希望得到"netPaymentDays" is required,但是我得到了"value" does not match any of the allowed types。
如何获得“嵌套”错误而不是替代方案的通用错误?
发布于 2022-01-26 08:49:20
您已经提到了解决此问题的正确方法,但我看不到在您的示例中使用它。
我想用
Joi.alternatives验证它
模式的一个可能解决方案是:
Joi.object().keys({
type: Joi.string().valid('AdvancePayment', 'PaymentGoal').required(),
netPaymentDays: Joi.alternatives().conditional('type', {
is: 'PaymentGoal',
then: Joi.number().required(),
otherwise: Joi.forbidden()
})
});https://stackoverflow.com/questions/70772805
复制相似问题