【发布时间】:2022-01-26 16:49:20
【问题描述】:
我有联合类型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。
如何获得“嵌套”错误而不是替代的通用错误?
【问题讨论】:
标签: node.js typescript joi