【发布时间】:2020-01-16 19:30:02
【问题描述】:
我有一个复杂的验证,它会根据 JSON 中的 a 值而变化。
{ type: 'a', thing: 1, foo: 'abc' }
{ type: 'b', thing: 2, bar: 123 }
我想验证如果类型是a,则使用一组兄弟,如果是b,则使用另一组兄弟
我想使用when switch,但不知道如何在根目录下执行此操作。
Joi.object({
type: Joi.string().valid('a','b').required(),
thing: Joi.number().required()
}).when('type', {
switch: [
{ is: 'a', then: Joi.object({ foo: Joi.string() }) },
{ is: 'b', then: Joi.object({ bar: Joi.number() }) },
],
otherwise: Joi.forbidden(),
});
但是这会产生以下错误:
错误:无效引用超出了架构根:ref:type
这有点像错误,但我不知道如何重组它以使其在根应用选择器。
我正在使用最新的 JOI (16.0.1)
【问题讨论】:
标签: joi