gpt4 book ai didi

javascript - Joi 验证器只有一个键

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:10 27 4
gpt4 key购买 nike

我正在开发一个应该允许多个参数的 api,但对于其中的三个,我只想允许其中一个。每个键的值更容易,但我想知道 Joi 是否也允许它,或者我应该在我的服务器中添加额外的验证逻辑。

简而言之,对于键 abc 我想使任何具有三个以上之一的请求失败,所以:

  1. http://myapi.com/?a=value有效请求。

  2. http://myapi.com/?b=value&c=value2 无效

谢谢!

最佳答案

您正在寻找 object.xor(peers)如果恰好需要 abc 之一。

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

  • peers - the exclusive key names that must not appear together but where one of them is required. peers can be a single string value, an array of string values, or each peer provided as an argument.
const schema = Joi.object().keys({
a: Joi.any(),
b: Joi.any(),
c: Joi.any()
}).xor('a', 'b', 'c');

或者,object.oxor(peers)如果仅允许 abc 之一,但都不是必需的。

Defines an exclusive relationship between a set of keys where only one is allowed but none are required where:

  • peers - the exclusive key names that must not appear together but where none are required.
const schema = Joi.object().keys({
a: Joi.any(),
b: Joi.any(),
c: Joi.any()
}).oxor('a', 'b', 'c');

关于javascript - Joi 验证器只有一个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54695534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com