[英]How to use references (object values) at validation point in Joi?
I am trying to conditionally validate a key key1
based on the value of different key key2
. 我试图根据不同密钥key2
的值有条件地验证密钥key1
。 The key1
is from some set of values, lets say ['a', 'b', 'c']
and key2
should be also constrained depending on the value of key1
{a: ['a1','a2','a3'], b: ['b1','b2','b3'], c: ['c1','c2','c3'] }
. key1
来自某些值,假设['a', 'b', 'c']
和key2
也应该受到约束,具体取决于key1
的值{a: ['a1','a2','a3'], b: ['b1','b2','b3'], c: ['c1','c2','c3'] }
。
If key1
is a
then key2
has to be one of ['a1','a2','a3']
etc. The set of values of key2
is not computable from key1
. 如果key1
是a
则key2
必须是['a1','a2','a3']
等key2
的值集不能从key1
。
I tried following schemas, using ref, hoping joi.ref('key1')
, just gets me the value: 我尝试了以下模式,使用ref,希望joi.ref('key1')
,只是得到我的值:
const obj = {a: ['a1','a2','a3'], b: ['b1','b2','b3'], c: ['c1','c2','c3'] }
const schema = {
key1: joi.only(['a', 'b', 'c']),
key2: joi.only(obj[joi.ref('key1')])
}
and I get the error: 我收到错误:
"Cannot call allow/valid/invalid with undefined" “无法使用undefined调用allow / valid / invalid”
I've tried it with options map specified: 我已经尝试了指定的选项图:
const schema = {
key1: joi.only(['a', 'b', 'c']),
key2: joi.only(joi.ref('key1', {map: [['a', ['a1','a2','a3']],['b', ['b1','b2','b3']],['c', ['c1','c2','c3']]]}))
}
For {key1:'a', key2:'a1'}
I get ValidationError: 对于{key1:'a', key2:'a1'}
我得到ValidationError:
"child "key2" fails because ["key2" must be one of [ref:key1]] “child”key2“失败,因为[”key2“必须是[ref:key1]之一]
But curiously {key1:'a', key2:'a'}
gets validated without error, which suggests the fallback of joi.ref
to the original value. 但奇怪的是, {key1:'a', key2:'a'}
被验证没有错误,这表明joi.ref
回joi.ref
原始值。
So refs did not work as expected lets try when and switch: 因此refs没有按预期工作让我们尝试何时切换:
const schema = {
key1: joi.only(['a', 'b', 'c']),
key2: joi.when('key1', {switch: [{is: 'a', then: joi.only(['a1','a2','a3'])},{is: 'b', then: joi.only(['b1','b2','b3'])},{is: 'c', then: joi.only(['c1','c2','c3'])}]})
}
But this gets me only: 但这只能让我:
"options must have at least one of 'then' or 'otherwise'" “选项必须至少有一个'那么'或'否则'”
And if I supply otherwise
, it also requires is
. 如果我供应otherwise
,它也需要is
。 Is there any simple way how to tackle this problem? 有没有简单的方法来解决这个问题?
I suggest you to try two different approaches in order to solve your problem. 我建议你尝试两种不同的方法来解决你的问题。
The first one uses multiple Joi.when()
conditions: 第一个使用多个Joi.when()
条件:
const schema = joi.object({
key1: joi.array().items(joi.string().valid('a','b','c')).unique(),
key2: joi.array()
.when('key1', { is: ['a'], then: joi.array().items(joi.string().valid('a1','a2','a3')).unique(), otherwise: joi.forbidden() })
.when('key1', { is: ['b'], then: joi.array().items(joi.string().valid('b1','b2','b3')).unique(), otherwise: joi.forbidden() })
.when('key1', { is: ['c'], then: joi.array().items(joi.string().valid('c1','c2','c3')).unique(), otherwise: joi.forbidden() })
})
The second one uses the Joi.alternatives()
: 第二个使用Joi.alternatives()
:
const schema = joi.alternatives().try(
joi.object({
key1: joi.array().items(joi.string().valid('a')).unique().required(),
key2: joi.array().items(joi.string().valid('a1','a2','a3')).unique().required()
}),
joi.object({
key1: joi.array().items(joi.string().valid('b')).unique().required(),
key2: joi.array().items(joi.string().valid('b1','b2','b3')).unique().required()
}),
joi.object({
key1: joi.array().items(joi.string().valid('c')).unique().required(),
key2: joi.array().items(joi.string().valid('c1','c2','c3')).unique().required()
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.