Skip to main content

Validation

The values authors can enter for a specific property can be constrained using validation keywords.

These validation keywords can be used individually or combined.

{
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 0,
"maxLength": 50
}
},
"required": ["name"]
}

They can be used anywhere a value type is defined in the schema, including properties and array items.

{
"type": "array",
"items": {
"type": "string",
"minLength": 0,
"maxLength": 50
}
}

Below is a summary of the available validation keywords

Validation TypeDescription
Enumerated valuesRestricts the value to a fixed set of possible values. Properties using this validation will display as a dropdown.
{"enum": ["red", "blue", "green"]} {"enum": [1,2,3]}
Constant valuesRestricts the value to a constant value. Users will be unable to change the value in the authoring interface.
{"const": "BannerComponent"}
Regular expressionValidates the format of text values matches a custom regular expression
{"pattern": "[a-z]+"}
Text minimum lengthValidates the minimum length of a text value
{"minLength": 1}
Text maximum lengthValidates the maximum length of a text value
{"maxLength": 50}
Text formatValidates the format of text values matches the specified built-in format
{"format": "date"} see below for a full list of formats
Number minimum valueValidates that the value entered is more than or equal to the minimum value.
{"minimum": 0} or {"exclusiveMinimum": 0} for just more than
Number maximum valueValidates that the value entered is less than or equal to maximum value.
{"maximum": 10} or {"exclusiveMaximum": 0} for just less than
Number multipleValidates that the value entered is a multiple of the multipleOf value
{"multipleOf": 2}
Required propertiesValidates that the specified properties must be populated.
{"properties": {"name": {"type": "string"}}, "required": ["name"]}
Array minimum itemsValidates that the Array value contains at least the minimum number of items
{"minItems": 1}
Array maximum itemsValidates that the Array value does not contain more than the maximum number of items
{"maxItems": 1}
Array unique itemsValidates that every item in the Array is unique
{"uniqueItems": true}

Generic
Link copied!

Enumerated values
Link copied!

{ "type": "string", "enum": ["small", "medium", "large"] }

✅ "small" ❌ "xlarge"

{ "type": "number", "enum": [2, 4, 8, 16] }

✅ 2 ❌ 3

The enum keyword restricts the values users can select to a fixed list.

Properties using this validation will display as a dropdown.

Constant values
Link copied!

{
"properties": {
"component": { "type": "string", "const": "BannerComponent" }
}
}

The const keyword can be applied to any property to hard-code the value.

Properties using this keyword will be automatically set to the const value and will be immutable.

Strings
Link copied!

Regular Expression
Link copied!

{ "type": "string", "pattern": "^[a-zA-Z0-9_-]+$" }

✅ "summer-sale" ❌ "summer sale"

The pattern keyword can be applied to any string property to validate the value matches a custom regular expression.

The pattern keyword must be a valid ECMA-262 regular expression.

Text Minimum Length
Link copied!

{ "type": "string", "minLength": 10 }

✅ "hello world" ❌ "hello"

The minLength keyword can be applied to any string property to validate the number of characters is greater than, or equal to, the value of the keyword.

Text Maximum Length
Link copied!

{ "type": "string", "maxLength": 10 }

✅ "hello" ❌ "hello world"

The maxLength keyword can be applied to any string property to validate the number of characters is less than, or equal to, the value of the keyword.

Text Format
Link copied!

{ "type": "string", "format": "date" }

The format keyword can be applied to any string property to validate common data formats that do not have a native JSON representation.

The supported formats are detailed below:

FormatDescription
"color"RGB or RGBA color, e.g. "rgb(255,255,255)". Will display as a color picker.
"date"RFC 3339 Date e.g. "2018-11-20"
"time"RFC 3339 Time e.g. "20:20:39" or "20:20:39+00:00"
"date-time"RFC 3339 Date-time e.g. "2018-11-13T20:20:39+00:00"
"markdown"Markdown rich text, e.g. "# Heading 1". Will display as a rich text editor.
"uri"RFC 3986 URI, e.g. "https://example.com/"
"uri-reference"RFC 3986 URI Reference, e.g. "../page"
"uri-template"RFC 6570 URI template, e.g. "http://example.com/search{?q,lang}"
"email"RFC 5321 Internet email address, e.g. "example@example.com"
"hostname"RFC 1123 Hostname, e.g. "example.com"
"ipv4"RFC 2673 V4 IP Address, e.g. "192.168.16.1"
"ipv6"RFC 2373 V6 IP Address, e.g. "1080:0:0:0:8:800:200C:417A"
"json-pointer"RFC 6901 JSON Pointer, e.g. "/foo/bar"
"relative-json-pointer"Relative JSON Pointer, e.g. "0#"
"regex"ECMA 262 Regular expression, e.g. "[a-z]+"

Numbers
Link copied!

Minimum value
Link copied!

{ "type": "number", "minimum": 0 }

✅ 0 ❌ -1

{ "type": "number", "exclusiveMinimum": 0 }

✅ 1 ❌ 0

The minimum keyword can be applied to any number property to validate the value is greater than, or equal to, the value of the keyword.

The exclusiveMinimum keyword can be applied to any number property to validate the value is greater than the value of the keyword.

Maximum value
Link copied!

{ "type": "number", "maximum": 100 }

✅ 100 ❌ 101

{ "type": "number", "exclusiveMaximum": 100 }

✅ 99 ❌ 100

The maximum keyword can be applied to any number property to validate the value is less than, or equal to, the value of the keyword.

The exclusiveMaximum keyword can be applied to any number property to validate the value is less than the value of the keyword.

Multiple
Link copied!

{ "multipleOf": 2 }

✅ 4 ❌ 3

The multipleOf keyword can be applied to any number property to validate the value is a multiple of the value of the keyword.

Objects
Link copied!

Required Properties
Link copied!

{
"type": "object",
"properties": {
"title": { "type": "string" },
"sku": { "type": "string" }
},
"required": ["title", "sku"]
}

✅ {"title": "Summer Dress", "sku": "xa83748"} ❌ {"title": "Summer Dress"} ❌ {"sku": "xa83748"}

The required keyword can be applied to any object property to validate the specified properties are populated.

The required keyword must be an array containing a unique set of property names, which must correspond to a property defined in the properties object.

Arrays
Link copied!

Array minimum items
Link copied!

{
"type": "Array",
"items": { "type": "string" },
"minItems": 2
}

✅ ["item 1", "item 2", "item 3"] ✅ ["item 1", "item 2"] ❌ ["item 1"]

The minItems keyword can be applied to any array property to validate the length of the array is greater than, or equal to, the value of the keyword.

Array maximum items
Link copied!

{
"type": "Array",
"items": { "type": "string" },
"maxItems": 2
}

✅ ["item 1"] ✅ ["item 1", "item 2"] ❌ ["item 1", "item 2", "item 3"]

The maxItems keyword can be applied to any array property to validate the length of the array is less than, or equal to, the value of the keyword.

Array unique items
Link copied!

{
"type": "Array",
"items": { "type": "string" },
"uniqueItems": true
}

✅ ["item 1", "item 2"] ❌ ["item 1", "item 1"]

The uniqueItems keyword can be applied to any array property to validate the items inside the array are unique.