Skip to main content

Conditionals

Conditionals (if/ then/ else) allow you to set the state and value of one or more properties in a schema, based on the state of another property. For example, you can use conditionals to enable or disable a property based on a user selection, or hide or show one or a group of properties.

On this page we've provided examples of some of the common uses for conditionals. See conditionals- schema examples for more.

Info

We don't currently support all of the features specified in the JSON schema conditionals reference. In particular, wrapping multiple if/then conditions inside allOf is not supported. However using content palettes with objects does provide an alternative way to structure your schema to support choosing one from a number of options.

Hiding and showing properties
Link copied!

In this example the date property is shown or hidden based on the state of the hasDate boolean.

{
"$schema":"http://json-schema.org/draft-07/schema#",
"$id":"https://examples.com/conditionals-example",
"title":"Conditionals example",
"description":"Conditionals- date shown on checkbox selected",
"allOf":[
{
"$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/content"
}
],
"type":"object",
"properties":{
"event":{
"title":"Event",
"description":"Our client meetings",
"type":"object",
"properties":{
"title":{
"title":"Event Name",
"type":"string"
},
"hasDate":{
"type":"boolean"
}
},
"if":{
"properties":{
"hasDate":{
"const":false
}
}
},
"then":{

},
"else":{
"properties":{
"date":{
"type":"string",
"format":"date"
}
}
}
}
},
"propertyOrder":[

]
}

Hiding and showing properties based on validation
Link copied!

In the following example additionalNotes is shown if the email property is a valid email address, according to the regular expression defined in the pattern.

{
"$schema":"http://json-schema.org/draft-07/schema#",
"$id":"https://example.com/conditional-pattern",
"title":"Conditional Example",
"description":"Conditional Example",
"allOf":[
{
"$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/content"
}
],
"type":"object",
"properties":{
"email":{
"type":"string"
}
},
"if":{
"properties":{
"email":{
"type":"string",
"title":"Email address",
"pattern":"^[a-z0-9][-a-z0-9._]+@([-a-z0-9]+\\.)+[a-z]{2,5}$"
}
},
"required":[
"email"
]
},
"then":{
"properties":{
"additionalNotes":{
"type":"string",
"title":"Additional notes",
"ui:component":{
"name":"text-area"
}
}
}
}
}

Conditional validation
Link copied!

In the example shown below, the country property is used to determine the validation used for the addressCode property. Different validation is used for a UK or US address.

{
"$schema":"http://json-schema.org/draft-07/schema#",
"$id":"https://examples.com/conditional-validation",
"title":"Conditional validation",
"description":"Validation set on country selection",
"allOf":[
{
"$ref":"http://bigcontent.io/cms/schema/v1/core#/definitions/content"
}
],
"type":"object",
"properties":{
"addresssValidation":{
"type":"object",
"title":"Address validation",
"properties":{
"country":{
"type":"string",
"title":"Choose Country",
"default":"uk",
"oneOf":[
{
"const":"uk",
"title":"United Kingdom"
},
{
"const":"us",
"title":"United States"
}
]
}
},
"if":{
"properties":{
"country":{
"const":"uk"
}
}
},
"then":{
"properties":{
"addressCode":{
"type":"string",
"title":"Post Code",
"pattern":"^[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][A-Z]{2}$"
}
}
},
"else":{
"properties":{
"addressCode":{
"type":"string",
"title":"Zip code",
"pattern":"^[0-9]{5}(?:-[0-9]{4})?$"
}
}
}
}
},
"propertyOrder":[

]
}