使用 Joi 和 Node,需要 2 个字段中的 1 个,但不允许同时存在

Using Joi and Node, require 1 of 2 fields but not allow both to be present

我在 joi 对象中有 2 个字段,我们称它们为 "a" 和 "b",其中 "a" 是一个对象,"b" 是一个对象数组。我想要求 2 个字段中的 1 个,但不允许同时使用,有没有办法用 joi 做到这一点?

你应该使用xor

Defines an exclusive relationship between a set of keys where one of them is required but not at the same time

https://github.com/hapijs/joi/blob/master/API.md#objectxorpeers-options

Joi.object().keys({
    a: Joi.object(),
    b: Joi.array()
}).xor('a', 'b')