条件未按预期工作时的 JOI 验证
JOI validation when condition not working as expected
我定义了以下 JOI 模式;
schema = Joi.object({
flag: Joi.boolean()
.required(),
toolDetail: Joi.array().items(
Joi.object({
amount: Joi.number()
.min(0)
.max(499.99)
.precision(2)
.options({ convert: false })
.when('flag', {
is: true,
then: Joi.number().required(),
otherwise: Joi.number()
.strip()
.optional()
.allow(''),
}),
tool: Joi.string()
.min(0)
.max(500)
.when('flag', {
is: true,
then: Joi.string().required(),
otherwise: Joi.string()
.strip()
.optional()
.allow(''),
}),
}),
),
});
我已确认标志设置正确,但似乎 'amount' 和 'tool' 总是遇到其他情况,无论 'flag' 中设置的值如何.
是不是我的定义有误?
在我向标志添加 4 点前缀后按预期工作
.when('....flag', {
我定义了以下 JOI 模式;
schema = Joi.object({
flag: Joi.boolean()
.required(),
toolDetail: Joi.array().items(
Joi.object({
amount: Joi.number()
.min(0)
.max(499.99)
.precision(2)
.options({ convert: false })
.when('flag', {
is: true,
then: Joi.number().required(),
otherwise: Joi.number()
.strip()
.optional()
.allow(''),
}),
tool: Joi.string()
.min(0)
.max(500)
.when('flag', {
is: true,
then: Joi.string().required(),
otherwise: Joi.string()
.strip()
.optional()
.allow(''),
}),
}),
),
});
我已确认标志设置正确,但似乎 'amount' 和 'tool' 总是遇到其他情况,无论 'flag' 中设置的值如何.
是不是我的定义有误?
在我向标志添加 4 点前缀后按预期工作
.when('....flag', {