Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema: Using anyOf, oneOf, allOf within additionalProperties

Tags:

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

OR an array containing object's matching the above.

Here is what I currently have and DOES NOT WORK:

{
    "$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 it possible, and if so what is the proper syntax?

My assumption is that this will not work because the passing schema(s) in oneOf|anyOf|allOf of additionalProperties must apply to ALL keys falling under additionalProperties.

like image 486
Philippe Schwyter Avatar asked Dec 12 '16 19:12

Philippe Schwyter


People also ask

What does allOf mean in JSON Schema?

By the definition of this keyword, it meant that the instance had to be valid against the current schema and all schemas specified in extends ; basically, draft v4's allOf is draft v3's extends .

What is JSON Schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What is anyOf allOf?

allOf – the value validates against all the subschemas. anyOf – the value validates against any of the subschemas. oneOf – the value validates against exactly one of the subschemas.

What is the difference between oneOf and anyOf?

Difference Between anyOf and oneOfoneOf matches exactly one subschema, and anyOf can match one or more subschemas.


1 Answers

"required" should be an array of the properties which are mandatory in v4.

Or "required": true (or false) as part of the property in v3.

Try this:

{
    "$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"]
                }
            }
        ]
    }
}
like image 185
Pedro Avatar answered Sep 23 '22 16:09

Pedro