【发布时间】:2021-06-04 16:14:16
【问题描述】:
我一直在尝试嵌套 where 验证,但到目前为止还没有运气。这是我想要实现的目标:
1- 如果 mainType 为 COMMERCIAL,则为 contactMethod 定义所需的模式。 2- 如果 isHousing 为真,则为 contactMethod 定义所需的模式。 3- 如果 mainType 是 REAL_ESTATE 则不允许传递 contactMethod 对象
这是我失败的尝试:
contactMethod: Joi.when('mainType', {
is: 'COMMERCIAL',
then: Joi.object()
.keys({
create: Joi.object({
message: Joi.string().allow(''),
}).required(),
})
.required()),
}).when('isHousing', {
is: true,
then: Joi.object()
.keys({
create: Joi.object({
message: Joi.string().allow(''),
}).required(),
})
.required(),
otherwise: Joi.disallow(),
})
我也试过这个:
contactMethod: Joi.when('mainType', {
switch: [
{
is: 'COMMERCIAL',
then: Joi.object()
.keys({
create: Joi.object({
message: Joi.string().allow(''),
}).required(),
})
.required(),
},
{
is: 'REAL_ESTATE',
then: Joi.when('isHousing', {
is: true,
then: Joi.object()
.keys({
create: Joi.object({
message: Joi.string().allow(''),
}).required(),
})
.required(),
}),
otherwise: Joi.disallow(),
},
],
})
那么我在这里做错了什么?
【问题讨论】: