使用 joi 验证数组包含特定值

nir*_*sky 6 javascript joi

我正在寻找一种方法来验证数组是否包含使用joi所需的值。
在网上找到这些问题 - #1 , #2,但没有一个有明确的答案。
我尝试了几种东西,但它们似乎不起作用,例如:
joi.array().items(joi.string().allow('required-string').required()), joi.array().items(joi.string().label('required-string').required()), joi.array().items(joi.string().valid('required-string'))

这就是我想要实现的目标:

公认:

['required-string'], ['required-string', 'other'], ['other','required-string'], ['other',...,'required-string',....,'more-other']
Run Code Online (Sandbox Code Playgroud)

拒绝:

[], ['other'], [null], etc..
Run Code Online (Sandbox Code Playgroud)

Car*_*ten 3

您可以array.items通过列出所有允许的类型来使用。如果给定类型,.required()则数组中必须有匹配项: joi API 参考

joi.array().items(joi.string().valid('required-string').required(), joi.string())
Run Code Online (Sandbox Code Playgroud)