简体   繁体   中英

JSON Schema: Using anyOf, oneOf, allOf within additionalProperties

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM