我想验证一个只有两个字段(即文本和图像)的输入。这两个文本和图像的字符串,其中一人必须始终存在。当其中一个字段不存在时,另一个字段不能是空字符串。这是我定义的验证。
text: Joi.string()
.when('image',
{
is: Joi.string(),
then: Joi.string().allow(''),
otherwise: Joi.string().required(),
}
),
image: Joi.string().allow(null),
Run Code Online (Sandbox Code Playgroud)
当我使用以下输入时,验证允许数据通过。我不知道如何更改验证以禁止以下输入。
post: {
text: ''
}
Run Code Online (Sandbox Code Playgroud)
在验证时使用exist()代替string():
when('image', {
is: Joi.exist(),
then: Joi.string().allow(''),
otherwise: Joi.string().required(),
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1037 次 |
| 最近记录: |