JSON 架构:在 additionalProperties 中使用 anyOf、oneOf、allOf
JSON Schema: Using anyOf, oneOf, allOf within additionalProperties
我正在尝试验证一个对象,该对象可以具有任意键,其值是如下所示的对象:
{ "href": "some string" }
OR 包含与上述匹配的对象的数组。
这是我目前拥有但不起作用的:
{
"$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"
}
是否可行,如果可行,正确的语法是什么?
我的假设是这不会起作用,因为 additionalProperties
的 oneOf|anyOf|allOf
中传递的模式必须应用于 additionalProperties
.
下的所有键
"required" 应该是 v4.
中必需的属性数组
或"required":true(或false)作为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"]
}
}
]
}
}
我正在尝试验证一个对象,该对象可以具有任意键,其值是如下所示的对象:
{ "href": "some string" }
OR 包含与上述匹配的对象的数组。
这是我目前拥有但不起作用的:
{
"$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"
}
是否可行,如果可行,正确的语法是什么?
我的假设是这不会起作用,因为 additionalProperties
的 oneOf|anyOf|allOf
中传递的模式必须应用于 additionalProperties
.
"required" 应该是 v4.
中必需的属性数组或"required":true(或false)作为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"]
}
}
]
}
}