JSON Schema: using anyOf, oneOf, allOf in advanced properties

I am trying to test an object that can have arbitrary keys whose values ​​are either an object that looks like this: { "href": "some string" }

OR an array containing the object corresponding to the above.

Here is what I DO NOT WORK now:

{
    "$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"
}

Is this possible, and if so, what is the correct syntax?

My guess is that this will not work, because the passing scheme in oneOf|anyOf|allOfof additionalPropertiesshould apply to all the keys underneath additionalProperties.

+4
source share
1 answer

"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"]
                }
            }
        ]
    }
}
+4

Source: https://habr.com/ru/post/1663550/


All Articles