joi - 条件未按预期工作时的 JOI 验证
问题描述
我定义了以下 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(''),
}),
}),
),
});
我已经确认标志设置正确,但无论“标志”中设置的值如何,“数量”和“工具”似乎总是达到其他条件。
我的定义有什么不正确的吗?
解决方案
推荐阅读
- python - Python Pandas:在附加到新数据帧时引用来自不同循环级别的变量
- vagrant - 我的厨师厨房的 Vagrant 机器无法访问互联网
- chart.js - Chartjs重叠点击功能不起作用
- angular - Typescript 中反序列化对象的 Getter 方法
- ios - 不同笔尖的collectionView单元格宽度不会改变
- c - C:初始化字符指针(字符串)数组并使用 fgets 遍历文件以将值放入这些字符串中
- doctrine - Symfony3 将两个相关的实体保存到数据库中
- awk - 允许与子集 .fastq 不匹配的 Grep
- ruby-on-rails - AssociationTypeMisMatch:如何使用接受 JSON 的 RESTful API (Rails) 创建对象和关联(嵌套)对象?
- javascript - 在应用程序的另一部分显示 v-menue 的选定选项。验证