gpt4 book ai didi

JSON Schema : Using anyOf, oneOf, allOf 在 additionalProperties 中

转载 作者:行者123 更新时间:2023-12-04 19:34:51 26 4
gpt4 key购买 nike

我正在尝试验证一个对象,该对象可以具有任意键,其值是一个如下所示的对象:
{ "href": "some string" }

或包含与上述匹配的对象的数组。

这是我目前拥有但不起作用的内容:

{
"$schema": "http://json-schema.org/schema#",
"id": "https://turnstyle.io/schema/links.json",
"type": "object",
"additionalProperties": {
"oneOf": [
{
"type": "object",
"required": "href"
},
{
"type": "array",
"items": {
"type": "object",
"required": "href"
}
}
]
}
}



Passing example:
{
"customer": { "href": "/customer/1" },
"products": [
{ "href": "/product/1" },
{ "href": "/product/2" }
]
}

Failing example:
{
"customer": { "wrongKey": "/customer/1" },
"products": "aString"
}

是否有可能,如果有,正确的语法是什么?

我的假设是这不会起作用,因为 oneOf|anyOf|allOf 中的传递模式的 additionalProperties必须适用于属于 additionalProperties 的所有 key .

最佳答案

"required"应该是 v4 中强制的属性数组.

或“必需”:真(或假)作为 v3 中属性的一部分。

尝试这个:

{
"$schema": "http://json-schema.org/schema#",
"id": "https://turnstyle.io/schema/links.json",
"type": "object",
"additionalProperties": {
"oneOf": [
{
"type": "object",
"properties": {
"href": {"type": "string"}
},
"required": ["href"]
},
{
"type": "array",
"items": {
"type": "object",
"properties": {
"href": {"type": "string"}
},
"required": ["href"]
}
}
]
}
}

关于JSON Schema : Using anyOf, oneOf, allOf 在 additionalProperties 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41107693/

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