【发布时间】:2021-07-04 10:55:28
【问题描述】:
用户将发送联系人信息作为对象数组,如下所示,电话号码是可选的,电子邮件是必需的:
{
"contact": [{
"type": "phone",
"value": "555-555-5555"
}, {
"type": "email",
"value": "test@test.com"
}]
}
我想确保数组中有一个电子邮件对象。我尝试了这样的 Joi 验证:
contact: Joi.array().items(Joi.object().keys({
type: Joi.string().valid('phone', 'email'),
value: Joi.string()
.when('contact.type', { is: 'phone', then: Joi.string() })
.when('contact.type', { is: 'email', then: Joi.string().email().required() })
}))
.when('contact.type', { is: 'phone', then: Joi.array().min(2).required() }),
但我收到以下错误:
Error: Item cannot come after itself: contact
它似乎不喜欢我以这种方式给它一个长度,但我想不出任何其他方法来做到这一点。任何帮助,将不胜感激。谢谢。
【问题讨论】:
标签: node.js validation joi