maxItems : Integer

maxItems

Integer

An array instance is valid if its size is less than, or equal to, the value of this keyword.

Value This keyword must be set to a zero or positive integer
Kind Assertion
Applies To Array
Dialect 2020-12
Changed In None
Introduced In Draft 1
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.4.1
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Official Tests draft2020-12/maxItems.json
Default None
Annotation None
Affected By None
Affects None
Also See

The maxItems keyword is used to specify the maximum number of items allowed in an array. It can be used to define constraints on the size of an array within an array instance.

  • An array is valid if it has less than or equal to the specified number of elements.
  • Omitting maxItems means the array has no upper limit (unbounded).

Examples

Schema with 'maxItems' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "maxItems": 3
}
Valid An array instance with 3 or less items is valid Instance
[ 1, true, "hello" ]
Invalid An array instance with more than 3 items is invalid Instance
[ 1, 2, "apple", "banana", true ]
Schema with the 'maxItems' and 'items' keywords Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": { "type": "boolean" },
  "maxItems": 2
}
Valid An array instance containing 2 or less items, which conform to the 'items' subschema, is valid Instance
[ false ]
Invalid An array instance with more than 2 elements is invalid Instance
[ false, false, true ]
Schema with the 'maxItems', 'prefixItems' and 'contains' keywords Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [
    { "type": "number" },
    { "type": "string" }
  ],
  "contains": { "type": "boolean" },
  "maxItems": 3
}
Valid An array instance containing 3 or less items, which successfully validates against 'prefixItems' and 'contains', is valid Instance
[ 1, "John", false ]
Invalid An array instance with more than 3 elements is invalid Instance
[ 1, "John", "Doe", false ]
Invalid First and second items should be number and string respectively Instance
[ "John", 1, false ]