javascript - Joi 验证未验证必填字段
问题描述
我有 4 个字段正在尝试使用 Joi 进行验证。我希望以下内容是必需的:要么 first
or second
,And third
or fourth
。
所以以下其中一项应该通过,但没有其他应该通过:
{first: 'yo', third: true}
// or
{second: 'bro', third: true}
// or
{first: 'yo', fourth: true}
// or
{second: 'bro', fourth: true}
我尝试了以下方法,但它让third
并且fourth
是空白的。
function validate(data) {
const schema = Joi.object({
first: Joi.string(),
second: Joi.string(),
third: Joi.boolean(),
fourth: Joi.boolean(),
})
.xor('first', 'second')
.xor('third', 'fourth');
return schema.validate(data);
}
解决方案
推荐阅读
- mdm - 无法创建工作配置文件 安全策略阻止创建受管设备,因为此设备上已安装或已安装自定义操作系统
- c - 使用指针记录和打印数组
- php - 为什么我在结帐页面时无法访问会话中的数据用户
- swift - Swift Core Data - 存储自定义类型的数组
- sql-server - 如何删除位于其他服务器上的表?
- c# - 无法使上下文中的更改持续存在
- python - Django 在静态文件中找不到我的所有文件
- windows - 下载“Windows SDK Installer for windows 10”后,我在 Windows Kits 文件夹中找不到检查工具
- java - DatagramSocket如何每次都接收数据包并且不卡住
- c - C 中 if ....==NULL 的问题