ksl
ksl

Reputation: 4709

JSON schema - how to specify permutations of required properties

Below is an extract from my JSON schema.

I want to specify that both algorithm and results are required.

Additionally I want to specify:

Is this possible?

        "algorithm": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "algorithm1",
                "algorithm2"
            ]
        },
        "results": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "results1",
                "results2",
                "results3",
                "results4"
            ]
        },
     "required": ["algorithm", "results"]

Upvotes: 0

Views: 273

Answers (1)

ksl
ksl

Reputation: 4709

Thanks to the reference above from @jruizaranguren, I was able to figure it out.

"required": ["results"],
"results": {
    "type": "object",
    "oneOf": [
        { "$ref": "#/definitions/Results1" },
        { "$ref": "#/definitions/Results2" }
    ]
},
"definitions": {
    "Results1": {
        "type": "object",
        "required": ["algorithm", "results"],
        "properties": {
            "algorithm": {
                "type": "string",
                "enum": [ "algorithm1" ]
            },
            "results": {
                "type": "string",
                "allOf": [
                    { "result": "results1" },
                    { "result": "results2" },
                    { "result": "results3" }
                ]
            }
        }
    },
    "Results2": {
        "type": "object",
        "required": ["algorithm", "results"],
        "properties": {
            "algorithm": {
                "type": "string",
                "enum": [ "algorithm2" ]
            },
            "results": {
                "type": "string",
                "allOf": [
                    { "result": "results2" },
                    { "result": "results3" },
                    { "result": "results4" }
                ]
            }
        }
    }

Upvotes: 1

Related Questions