14

I know there is a similar question here, but it didn't really address my issue. In short, I want one my fields to be dependent on the other field's value. But for some values, I don't want any field to be required. Here is an example:

Schema

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {

    "colour": {
      "type": "string",
      "enum": ["red", "black", "blue"]
    },

    "blackQuote": {
      "type": "string",
      "maxLength": 11
    },

    "redQuote": {
      "type": "string",
      "maxLength": 11
    }
  },

  "oneOf": [
      {
        "properties": {
          "colour": {"enum": ["red"]}
        },
        "required": ["redQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["black"]}
        },
        "required": ["blackQuote"]
      }
  ],

  "required": [
    "colour"
  ]
}

This works like this:

  • IF the colour is "red" THEN "redQuote" (but not "blackQuote") is required: this is fine
  • IF the colour is "black" THEN "blackQuote" (but not "redQuote") is required: this is also fine
  • BUT if I put the colour "blue" in my JSON, then the validator says that the properties "redQuote" and "blackQuote" are missing... I don't want that, I only want dependecies for "red" and "black", but if the colour is "blue" I don't want anything required. How to achieve this?
wesleyy
  • 2,575
  • 9
  • 34
  • 54

3 Answers3

13

You can do this with a boolean logic concept called implication (!A or B). It can be used like an "if-then" statement. For example, either "color" is not "red" or "redQuote" is required. Any time I need to use this, I break it down with definitions so it reads as nice as possible.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "colour": { "enum": ["red", "black", "blue"] },
    "blackQuote": { "type": "string", "maxLength": 11 },
    "redQuote": { "type": "string", "maxLength": 11 }
  },
  "allOf": [
    { "$ref": "#/definitions/red-requires-redQuote" },
    { "$ref": "#/definitions/black-requires-blackQuote" }
  ],
  "required": ["colour"],
  "definitions": {
    "red-requires-redQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-red" } },
        { "required": ["redQuote"] }
      ]
    },
    "black-requires-blackQuote": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/is-black" } },
        { "required": ["blackQuote"] }
      ]
    },
    "is-red": {
      "properties": {
        "colour": { "enum": ["red"] }
      },
      "required": ["colour"]
    },
    "is-black": {
      "properties": {
        "colour": { "enum": ["black"] }
      },
      "required": ["colour"]
    }
  }
}
Jason Desrosiers
  • 22,479
  • 5
  • 47
  • 53
6

Simplest answer in draft-04 (as noted by Ganesh in a comment):

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {

    "colour": {
      "type": "string",
      "enum": ["red", "black", "blue"]
    },

    "blackQuote": {
      "type": "string",
      "maxLength": 11
    },

    "redQuote": {
      "type": "string",
      "maxLength": 11
    }
  },

  "oneOf": [
      {
        "properties": {
          "colour": {"enum": ["red"]}
        },
        "required": ["redQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["black"]}
        },
        "required": ["blackQuote"]
      },
      {
        "properties": {
          "colour": {"enum": ["blue"]}
        }
      }
  ],

  "required": [
    "colour"
  ]
}
Henry Andrews
  • 663
  • 3
  • 6
  • 1
    I think @jason's answer is slightly better. This way requires you to re-specify blue as a possible value. By using "implication" (explained in his answer), that is avoided. – Alexander Bird Sep 13 '18 at 20:45
5

If you move the specifics of what you want into the OneOf's then you can keep it all pretty simple, especially if you end up with a load of other values for color.

enter image description here

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "oneOf": [
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "red"
                    ]
                },
                "redQuote": {
                    "type": "string",
                    "maxLength": 11
                }
            },
            "required": [
                "redQuote"
            ]
        },
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "black"
                    ]
                },
                "blackQuote": {
                    "type": "string",
                    "maxLength": 11
                }
            },
            "required": [
                "blackQuote"
            ]
        },
        {
            "type": "object",
            "properties": {
                "colour": {
                    "type": "string",
                    "enum": [
                        "blue"
                    ]
                }
            }
        }
    ],
    "definitions": {}
}
Sprotty
  • 5,676
  • 3
  • 33
  • 52
  • 1
    Thanks, that's a great answer! However, one more detail that is bugging me. What if I want to be able to have a field "redQuote", no matter if field "colour" is present? Now it's ok, if the field "colour" equals "red" then "redQuote" is required. But I also want a JSON with only a field "redQuote" (without the field "colour") to also pass a validation. So if no "colour" is specified, then there is no requirement for the "redQuote" field and it can freely be there. How is that possible? – wesleyy Dec 06 '17 at 21:22