我一直在尝试使用Joi数据验证器为以下输入数据编写Joi模式验证。
{
"submissionReason": "new TPV required",
"alignerInfo": {
"name": "UPPER",
"value": [
{
"name": "set",
"value": "18/20"
},
{
"name": "zenStage",
"value": "2"
}
]
},
"archesToTreat": "BOTH",
"refinementGoals": [
{
"name": "Alignment"
},
{
"name": "Close Residual Space",
"value": [
{
"position": [
"2.1",
"2.2"
],
"value": "0.5"
},
{
"position": [
"2.4",
"2.5"
],
"value": "0.7"
}
]
},
{
"name": "Resolve posterior Open Bite",
"value": [
{
"name": "Attachments Only",
"value": {
"name": "teeth Attachment",
"value": [
"2.1",
"2.2"
]
}
},
{
"name": "Add Button for elastic",
"value": [
"2.1",
"2.2"
]
}
]
}
],
"attachments": "keep all previous attachment and change",
"iprInfo": {
"name": "ipr not performed as prescibed",
"value": [
{
"position": [
"2.1",
"2.2"
],
"value": "0.5"
}
]
},
"specialNotes": "Doctor Notes"
}
这是我目前为止所写的
Joi.object({
submissionReason: Joi.string().required(),
alignerInfo: Joi.object({
name: Joi.string().required(),
value: Joi.array().items(Joi.object({
name: Joi.string().required(),
value: Joi.string().required(),
})).required()
}).required(),
archesToTreat: Joi.string().valid('UPPER', 'LOWER', 'BOTH').required(),
refinementGoals: Joi.array().items(Joi.object({
name: Joi.string().required(),
value: Joi.array().items(Joi.object({
position: Joi.array().items(Joi.string().required()).required(),
value: Joi.string().required(),
name: Joi.string().forbidden()
})).when('name', {
is: 'Close Residual Space',
then: Joi.required(),
otherwise: Joi.forbidden()
})
})).required(),
attachments: Joi.string().required(),
iprInfo: Joi.object({
name: Joi.string().required(),
value: Joi.array().items(Joi.object({
position: Joi.array().items(Joi.string().required()).required(),
value: Joi.string().required()
})).required()
}).required(),
specialNotes: Joi.string().required()
})
但这不起作用,我得到以下错误:
验证错误:不允许“refinementGoals[2].value”
请帮助我正确验证
2条答案
按热度按时间2nc8po8w1#
您可以使用express-validator,它提供适合您的用例的基于模式的验证。
这里是express-validator的schema方法的链接。https://express-validator.github.io/docs/api/check-schema
vwoqyblh2#
您的验证允许2个级别的
value
:但是示例对象的refinementGoals的值有3个级别:
我建议使用joi.alternatives,它允许你设置多个类型为有效类型。就像这样: