如何在 joi 验证中应用 .OR
How to apply .OR in joi validation
好吧,我有以下 Joi 对象,我正在尝试使用 Or 方法,如你所见,我有两个对象包含 phone 我想使用 Or 方法但我失败了
body: Joi.object().keys({
member: Joi.object().required().keys({
id: Joi.string().required(),
phone: Joi.string().max(30),
email: Joi.string().required().max(255)
}),
billingAddress: Joi.object().keys({
line1: Joi.string().required(),
line2: Joi.string().max(60),
city: Joi.string().required().max(50),
state: Jio.required(),
postalCode: Joi.string().required(),
phone: Joi.string().max(15)
})
}).or('member.phone','billingAddress.phone')
我想做的是我需要一个 phone 要么在 member.phone 要么在 billingAddress.phone 至少有一个存在
上面的代码不起作用我该怎么办?
您尝试使用的功能最近才进入 Joi 14.0.0 版本,因此第一步是确保您至少需要 Joi v14。
Nested paths on object.or/nand/and/xor/with/without()
Nested paths on those functions are now supported. I consider this a breaking change as you may (but you really really shouldn't) have used dotted paths in those validations because you wanted to check dotted properties. If that's your case there is currently no migration path but we can discuss that in an issue.
对于旧版本,它需要 .when()
或 .alternatives()
的混乱组合、自定义验证器或具有多个模式的手动编写的逻辑。
好吧,我有以下 Joi 对象,我正在尝试使用 Or 方法,如你所见,我有两个对象包含 phone 我想使用 Or 方法但我失败了
body: Joi.object().keys({
member: Joi.object().required().keys({
id: Joi.string().required(),
phone: Joi.string().max(30),
email: Joi.string().required().max(255)
}),
billingAddress: Joi.object().keys({
line1: Joi.string().required(),
line2: Joi.string().max(60),
city: Joi.string().required().max(50),
state: Jio.required(),
postalCode: Joi.string().required(),
phone: Joi.string().max(15)
})
}).or('member.phone','billingAddress.phone')
我想做的是我需要一个 phone 要么在 member.phone 要么在 billingAddress.phone 至少有一个存在 上面的代码不起作用我该怎么办?
您尝试使用的功能最近才进入 Joi 14.0.0 版本,因此第一步是确保您至少需要 Joi v14。
Nested paths on object.or/nand/and/xor/with/without()
Nested paths on those functions are now supported. I consider this a breaking change as you may (but you really really shouldn't) have used dotted paths in those validations because you wanted to check dotted properties. If that's your case there is currently no migration path but we can discuss that in an issue.
对于旧版本,它需要 .when()
或 .alternatives()
的混乱组合、自定义验证器或具有多个模式的手动编写的逻辑。