Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema conditional dependency on value

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?
like image 1000
wesleyy Avatar asked Dec 05 '17 08:12

wesleyy


People also ask

What is anyOf in JSON Schema?

allOf: (AND) Must be valid against all of the subschemas. anyOf: (OR) Must be valid against any of the subschemas. oneOf: (XOR) Must be valid against exactly one of the subschemas.

What is oneOf in JSON Schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.

What are dependencies in JSON Schema?

There are two forms of dependencies we should look at: Property dependencies which state that if a specified property is present, other properties must be present as well, and. Schema dependencies which define a change in the schema when a given property is present.

Does JSON have schema validation?

JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.


1 Answers

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"
  ]
}
like image 191
Henry Andrews Avatar answered Sep 28 '22 01:09

Henry Andrews