Joi嵌套时
Joi nested when
const schema = {
a: Joi.any(),
b: Joi
.boolean()
.default(false),
c: Joi
.boolean()
.default(false)
}
如何修复上述 Joi 架构以匹配以下规则
b
或c
之一可以同时true
- 当
b
或c
为true
时,a
为forbidden
否则a
为required
Joi.object( {
a: Joi
.boolean()
.default(false),
b: Joi
.boolean()
.default(false),
c: Joi.string().when(
'a', {
is: false,
then: Joi.when(
'b', {
is: false,
then: Joi.string().required()
}
)
}
),
})
我在提问前试过嵌套when
,但由于规则的顺序,它之前不起作用
Joi.object( {
c: Joi.string().when(
'a', {
is: false,
then: Joi.when(
'b', {
is: false,
then: Joi.string().required()
}
)
}
),
a: Joi
.boolean()
.default(false),
b: Joi
.boolean()
.default(false),
})
第二个模式中的default
值在when
检查时没有设置,所以如果你验证一个空对象{}
,第一个模式有Validation Error: "c" is required
错误,而第二个 Passed
与 {a: false, b: false}
我在这里开了个issue https://github.com/sideway/joi/issues/2683
const schema = {
a: Joi.any(),
b: Joi
.boolean()
.default(false),
c: Joi
.boolean()
.default(false)
}
如何修复上述 Joi 架构以匹配以下规则
b
或c
之一可以同时true
- 当
b
或c
为true
时,a
为forbidden
否则a
为required
Joi.object( {
a: Joi
.boolean()
.default(false),
b: Joi
.boolean()
.default(false),
c: Joi.string().when(
'a', {
is: false,
then: Joi.when(
'b', {
is: false,
then: Joi.string().required()
}
)
}
),
})
我在提问前试过嵌套when
,但由于规则的顺序,它之前不起作用
Joi.object( {
c: Joi.string().when(
'a', {
is: false,
then: Joi.when(
'b', {
is: false,
then: Joi.string().required()
}
)
}
),
a: Joi
.boolean()
.default(false),
b: Joi
.boolean()
.default(false),
})
第二个模式中的default
值在when
检查时没有设置,所以如果你验证一个空对象{}
,第一个模式有Validation Error: "c" is required
错误,而第二个 Passed
与 {a: false, b: false}
我在这里开了个issue https://github.com/sideway/joi/issues/2683