Array type
Keywords for array type
The array
type is used for validating JSON indexed arrays.
{
"type": "array"
}
Input | Status |
---|---|
[] |
valid - empty array |
[2, 1, "str", false, null, {}] |
valid |
12 |
invalid - is integer/number |
null |
invalid - is null |
"[1, 2, 3]" |
invalid - is string |
{"0": 1, "1": 2, "2": 3} |
invalid - is object |
Validation keywords
The following keywords are supported by the array
type, and evaluated
in the presented order. All keywords are optional.
minItems
An array is valid against this keyword, if the number of items it contains is greater than, or equal to, the value of this keyword. The value of this keyword must be a non-negative integer.
{
"type": "array",
"minItems": 2
}
Input | Status |
---|---|
[1, 2, 3] |
valid - contains more than 2 items |
["a", "b"] |
valid - contains 2 items |
["text"] |
invalid - contains a single item |
[] |
invalid - contains no items |
maxItems
An array is valid against this keyword, if the number of items it contains is lower than, or equal to, the value of this keyword. The value of this keyword must be a non-negative integer.
{
"type": "array",
"maxItems": 2
}
Input | Status |
---|---|
[1, 2] |
valid - contains 2 items |
["a"] |
valid - contains a single item |
[] |
valid - contains no items |
[1, 2, 3] |
invalid - contains more than 2 items |
uniqueItems
An array is valid against this keyword if an item cannot be found
more than once in the array.
The value of this keyword must be a boolean. If set to false
the keyword
validation will be ignored.
{
"type": "array",
"uniqueItems": true
}
Input | Status |
---|---|
[1, 2, 3] |
valid |
["a", "b", "c"] |
valid |
[1, "1"] |
valid |
[[1, 2], [3, 4]] |
valid |
[1, 2, 1] |
invalid - duplicate 1 |
["a", "b", "B", "a"] |
invalid - duplicate a |
[[1, 2], [1, 3], [1, 2]] |
invalid - duplicate [1, 2] |
[{"a": 1, "b": 2}, {"a": 1, "c": 2}, {"a": 1, "b": 2}] |
invalid - duplicate {"a": 1, "b": 2} |
contains
An array is valid against this keyword if at least one item is valid against the schema defined by the keyword value. The value of this keyword must be a valid json schema (object or boolean).
Please note that an empty array will never be valid against this keyword.
{
"type": "array",
"contains": {
"type": "integer"
}
}
Input | Status |
---|---|
[1] |
valid |
[1, 2] |
valid |
["a", "b" -4.0] |
valid |
[] |
invalid |
["a", "b", "1"] |
invalid |
[2.3, 4.5, -6.7] |
invalid |
items
An array is valid against this keyword if items are valid against the corresponding schemas provided by the keyword value. The value of this keyword can be
- a valid json schema (object or boolean), then every item must be valid against this schema
- an array of valid json schemas, then each item must be valid against
the schema defined at the same position (index). Items that don’t have a corresponding
position (array contains 5 items and this keyword only has 3)
will be considered valid, unless the
additionalItems
keyword is present - which will decide the validity.
{
"type": "array",
"items": {
"type": "integer",
"minimum": 0
}
}
Input | Status |
---|---|
[1, 2, 3] |
valid |
[-0, 2.0] |
valid |
[] |
valid |
[-2, 3, 4] |
invalid |
["a", 2] |
invalid |
{
"type": "array",
"items": [
{"type": "integer"},
{"type": "string"}
]
}
Input | Status |
---|---|
[1, "a"] |
valid |
[1.0, "a", 5.6, null, true] |
valid |
[1] |
valid |
[] |
valid |
["a", 1] |
invalid |
[5.5, "a"] |
invalid |
[5, 6] |
invalid |
additionalItems
An array is valid against this keyword if all unchecked items
are valid against the schema defined by the keyword value.
An item is considered unchecked if items
keyword contains
an array of schemas and doesn’t have a corresponding position (index).
If the items
keyword is not an array, then this keyword is ignored.
The value of the keyword must be a valid json schema (object, boolean).
{
"type": "array",
"items": [
{"type": "integer"},
{"type": "string"}
],
"additionalItems": {
"type": "boolean"
}
}
Input | Status |
---|---|
[1, "a", true, false, true, true] |
valid |
[1, "a"] |
valid |
[1] |
valid |
[] |
valid |
[1, "a", 2] |
invalid |
[1, "a", true, 2, false] |
invalid |
[1, true, false] |
invalid |