我正在尝试验证一个可以拥有任意键的对象,其值是一个看起来像这样的对象:
{ "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"
}
是否有可能,如果是,那么正确的语法是什么?
我的假设是,这不起作用,因为oneOf|anyOf|allOf
additionalProperties
中的传递架构必须适用于additionalProperties
下的所有密钥。
答案 0 :(得分:4)
“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"]
}
}
]
}
}