prefixItems : Array<Schema>

prefixItems

Array<Schema>

Validation succeeds if each element of the instance validates against the schema at the same position, if any.

Value This keyword must be set to a non-empty array, where each item is a valid JSON Schema
Kind Applicator Annotation
Applies To Array
Base Dialect 2020-12
Changed In None
Introduced In 2020-12
Vocabulary Applicator
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.1.1
Metaschema https://json-schema.org/draft/2020-12/meta/applicator
Official Tests draft2020-12/prefixItems.json
Default As if it was set to the (invalid) value: []
Annotation Number Boolean The largest index to which this keyword applied its subschema, or a boolean true if it was applied to every item of the instance
Affected By None
Affects
Also See

The prefixItems keyword restricts a number of items from the start of an array instance to validate against the given sequence of subschemas, where the item at a given index in the array instance is evaluated against the subschema at the given index in the prefixItems array, if any. Information about the number of subschemas that were evaluated against the array instance is reported using annotations.

Array items outside the range described by the prefixItems keyword is evaluated against the items keyword, if present.

Remember that JSON Schema is a constraint-driven language. Therefore, non-array instances successfully validate against this keyword. If needed, make use of the type keyword to constraint the accepted type accordingly.

Examples

A schema that constrains array instances to start with a boolean item followed by a number item Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "prefixItems": [ { "type": "boolean" }, { "type": "number" } ]
}
Valid An empty array value is valid Instance
[]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
Valid An array value that consists of a boolean item is valid Instance
[ false ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
Valid An array value that consists of a boolean item followed by a number item is valid Instance
[ false, 35 ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": true }
Valid An array value that consists of a boolean item followed by a number item and other items is valid Instance
[ false, 35, "something", "else" ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 2 }
Invalid An array value that does not consist of a boolean item followed by a number item is invalid Instance
[ true, false ]
Valid A non-array value is valid Instance
"Hello World"
A schema that constrains array instances to start with a boolean item followed by a number item followed by strings Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "prefixItems": [ { "type": "boolean" }, { "type": "number" } ],
  "items": { "type": "string" }
}
Valid An empty array value is valid Instance
[]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
Valid An array value that consists of a boolean item is valid Instance
[ false ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
Valid An array value that consists of a boolean item followed by a number item is valid Instance
[ false, 35 ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": true }
Valid An array value that consists of a boolean item followed by a number item and other string items is valid Instance
[ false, 35, "foo", "bar" ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 2 }
Annotations
{ "keyword": "/items", "instance": "", "value": true }
Invalid An array value that consists of a boolean item followed by a number item and other non-string items is invalid Instance
[ false, 35, { "foo": "bar" } ]
Valid A non-array value is valid Instance
"Hello World"