JSON模式:在AdditionalProperties中使用anyOf,oneOf,allOf

Phi*_*ter 5 validation json jsonschema

我正在尝试验证可以具有任意键的对象,这些键的值可以是看起来像这样的对象: { "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"
}
Run Code Online (Sandbox Code Playgroud)

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

我的假设是,这将不起作用,因为in oneOf|anyOf|allOf中传递的架构additionalProperties必须适用于属于下的所有键additionalProperties

Ped*_*dro 4

“required” 应该是v4中强制属性的数组。

或“必需”: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"]
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)