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

Examples

Schema with 'prefixItems' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "prefixItems": [ { "type": "number" } ]
}
Valid An array instance with first item as numeric values is valid Instance
[ 2, false ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
Invalid 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" }
  ]
}
Valid Items of the array instance adhering to the corresponding subschema in 'prefixItems' is valid Instance
[ false, 35, [ "foo" ] ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 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" }
}
Valid An array instance adhering to the schema is valid Instance
[ false, "44", -5 ]
Annotations
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
{ "keyword": "/items", "instance": "", "value": true }
Invalid The prefix items of the array instance not adhering to the corresponding subschema in 'prefixItems' is invalid Instance
[ 2, 3, "44", -5 ]