0

Below is an extract from my JSON schema.

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

Additionally I want to specify:

  • when algorithm is algorithm1, then results must be one of results1, results2, or results3.
  • when algorithm is algorithm2, then results must be one of results2, results3, or results4.

Is this possible?

        "algorithm": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "algorithm1",
                "algorithm2"
            ]
        },
        "results": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "results1",
                "results2",
                "results3",
                "results4"
            ]
        },
     "required": ["algorithm", "results"]
2
  • 1
    Enforcing schemas depending on enum values can be done as suggested here: stackoverflow.com/questions/18375506/… Commented Apr 1, 2015 at 8:33
  • Thanks @jruizaranguren. I was able to figure it out from your helpful link. Commented Apr 1, 2015 at 13:08

1 Answer 1

1

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" }
                ]
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.