prefixItems : Array<Schema>

prefixItems

Array<Schema>

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

Kind Applicator Annotation
Applies To Array
Dialect 2020-12
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
Also see

Annotations

This keyword produces an annotation value which is the largest index to which this keyword applied a subschema. The value MAY be a boolean true if a subschema was applied to every index of the instance.

Explanation

The prefixItems keyword is used to validate arrays by applying a schema to each corresponding index of the array. It differs from the items keyword in that it validates only a prefix of the array, up to the length of the prefixItems array. Each schema specified in prefixItems corresponds to an index in the input array.

  • The value of this keyword must be a non-empty array of valid JSON Schemas.
  • The annotation produced by this keyword affects the behavior of items and unevaluatedItems.
  • items is used to validate all items in an array that are not covered by prefixItems, while prefixItems validates only a prefix of the array.
  • prefixItems keyword does not constrain the length of the array. If the array is longer than this keyword’s value, this keyword validates only the prefix of matching length.
Schema with 'prefixItems' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [ { "type": "number" } ]
}
An array instance with first item as numeric values is valid Instance
[ 2, false ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/prefixItems",
    "instanceLocation": "",
    "annotation": 0
  },
  // ...
]
An array instance containing a string value is invalid Instance
[ "2", 3 ]
Schema with 'prefixItems' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [
    { "type": "boolean" },
    { "type": "number" }
  ]
}
Items of the array instance adhering to the corresponding subschema in 'prefixItems' is valid Instance
[ false, 35, [ "foo" ] ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/prefixItems",
    "instanceLocation": "",
    "annotation": 1
  },
  // ...
]
Schema with 'prefixItems' and 'items' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [
    { "type": "boolean" },
    { "type": "string" }
  ],
  "items": { "type": "number" }
}
An array instance adhering to the schema is valid Instance
[ false, "44", -5 ]
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/prefixItems",
    "instanceLocation": "",
    "annotation": 1
  },
  {
    "valid": true,
    "keywordLocation": "/items",
    "instanceLocation": "",
    "annotation": true
  },
  // ...
]
The prefix items of the array instance not adhering to the corresponding subschema in 'prefixItems' is invalid Instance
[ 2, 3, "44", -5 ]