Array type

The array type is used for validating ordered lists (indexed arrays).

{
  "type": "array"
}

[] - 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.

  1. minItems
  2. maxItems
  3. unniqueItems
  4. contains
  5. items
  6. additionalItems

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
}

[1, 2, 3] - valid (3 > 2)

["a", "b"] - valid (2 = 2)

["text"] - invalid (1 < 2)

[] - invalid (0 < 2)

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
}

[1, 2] - valid (2 = 2)

["a"] - valid (1 < 2)

[] - valid (0 < 2)

[1, 2, 3] - invalid (3 > 2)

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
}

[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 not that an empty array will never be valid against this keyword.

{
  "type": "array",
  "contains": {
    "type": "integer"
  }
}

[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
  }
}

[1, 2, 3] - valid

[-0, 2.0] - valid

[] - valid

[-2, 3, 4] - invalid

["a", 2] - invalid

{
  "type": "array",
  "items": [
    {"type": "integer"},
    {"type": "string"}
  ]
}

[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"
  }
}

[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