Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties based on enum value in JSON Schema

I'm building a json schema definition which has a fixed set of controls that I've currently limited with an enum. However, not all properties are relevant for all controls.

I only want to require an options property if the controlType = dropdown

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    }
  }
}

How can I conditionally include / require a field in a json schema?

like image 984
Dvirm Avatar asked Nov 06 '17 14:11

Dvirm


People also ask

What is enum in JSON Schema?

The enum keyword is used to restrict a value to a fixed set of values. It must be an array with at least one element, where each element is unique.

Does JSON support enum?

JSON has no enum type. The two ways of modeling an enum would be: An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values.

What is additional properties in JSON Schema?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

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.


Video Answer


1 Answers

Use IF..Then..Else new in Draft-07

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    },
      
    "if": {
      "properties": {
        "controlType": {"const": "dropdown"}
      }
    },
    "then": {
      "required": ["options"]
    }
  }
}

Use oneOf or anyOf

This can be useful if you have a property that has a limited number of acceptable values (such as an enum), but each possible value needs to be individually mapped.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    },  
    "anyOf": [
      {
        "properties": {
          "controlType": {"const": "dropdown"}
        },
        "required": ["controlType", "options"]
      },
      {
        "properties": {
          "controlType": {"const": "title"}
        },
        "required": ["controlType"]
      },
      {
        "properties": {
          "controlType": {"const": "button"}
        },
        "required": ["controlType"]
      }
    ]
  }
}

Further Reading

  • Conditionally require attribute
  • Applying subschemas conditionally
  • Combining schemas
like image 149
KyleMit Avatar answered Oct 09 '22 06:10

KyleMit