我試圖驗證對象,其可具有任意的鍵,其值不是一個對象,看起來像: { "href": "some string" }
JSON模式:使用anyOf,oneOf,allOf內additionalProperties
或含陣列對象的匹配上述。
這是我目前擁有並不起作用:
{
"$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
的oneOf|anyOf|allOf
中的傳遞模式必須適用於所有屬於additionalProperties
的密鑰。
「我的假設是,這是行不通的,因爲在oneOf傳球模式(S)| anyOf | allOf additionalProperties必須適用於根據additionalProperties內的所有鑰匙。」 不,根據oneOf內的不同架構,不同的密鑰可以是有效的。在這種情況下anyOf也更有效率。 – esp