gpt4 book ai didi

python - 如何使用枚举包含 Jsonschema 多类型参数?

转载 作者:行者123 更新时间:2023-12-01 08:52:08 25 4
gpt4 key购买 nike

通过下面的示例问题会变得更加清晰:

data = {"type": "object", "properties": {"product": {"type": ["boolean","string"]}}}

它包括booleanstring类型。它有效,但我想将字符串部分限制为枚举列表:

["off", "on", "semi-on"]

type不是list时它可以工作,但是当我将其作为列表时,我无法为enum提供字符串类型。

下面没有 boolean 的示例也可以工作:

data = {"type": "object", "properties": {"product": {"type": "string", "enum": ["off", "on", "semi-on"]}}}

应该拆分架构,还是有其他方法?

<小时/>

EDIT-1:真实架构如下:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"is_trusted": {"type": "boolean"},
"is_active": {"type": "boolean"},
"addresses": {"type": "array"},
"attachments": {
"type": "object",
"properties": {
"send_web_push": {"type": "boolean"},
"receive_web_push": {"type": "boolean"}
},
"required": ["send_web_push", "receive_web_push"],
"additionalProperties": false
}
},
"required": ["is_trusted", "is_active", "addresses"],
"additionalProperties": false
}

最佳答案

JSON 架构定义了验证关键字,并按其应用于实例的方式进行划分。

enum 位于 Validation Keywords for Any Instance Type 下部分。

这意味着它适用于任何实例类型,包括 bool 值。

因此,您必须在有效值枚举中包含 bool 值 truefalse。 (在此示例中,这确实使 type 变得多余)。

{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"product": {
"type": [
"boolean",
"string"
],
"enum": [
"off",
"on",
"semi-on",
true,
false
]
}
}
}

下面的实例现在对于上面的架构有效。在您的第一个架构中,值为 trueproduct 将失败 enum 验证,因为 true 未包含在枚举。

{
"product": true
}

关于python - 如何使用枚举包含 Jsonschema 多类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53041395/

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