keyword | signature | summary | kind | instance | specification | metaschema | introduced_in | related | |||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
prefixItems |
Array<Schema> |
Validation succeeds if each element of the instance validates against the schema at the same position, if any. |
|
|
2020-12 |
|
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.
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
andunevaluatedItems
. items
is used to validate all items in an array that are not covered byprefixItems
, whileprefixItems
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 Schema with 'prefixItems' keyword
>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [ { "type": "number" } ]
}
{{}}
{{<instance-pass An array instance with first item as numeric values is valid
>}}
[ 2, false ]
{{}}
{{}} [ // ... { "valid": true, "keywordLocation": "/prefixItems", "instanceLocation": "", "annotation": 0 }, // ... ] {{}}
{{<instance-fail An array instance containing a string value is invalid
>}}
[ "2", 3 ]
{{}}
{{<schema Schema with 'prefixItems' keyword
>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "boolean" },
{ "type": "number" }
]
}
{{}}
{{<instance-pass Items of the array instance adhering to the corresponding subschema in 'prefixItems' is valid
>}}
[ false, 35, [ "foo" ] ]
{{}}
{{}} [ // ... { "valid": true, "keywordLocation": "/prefixItems", "instanceLocation": "", "annotation": 1 }, // ... ] {{}}
{{<schema Schema with 'prefixItems' and 'items' keyword
>}}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "boolean" },
{ "type": "string" }
],
"items": { "type": "number" }
}
{{}}
{{<instance-pass An array instance adhering to the schema is valid
>}}
[ false, "44", -5 ]
{{}}
{{}} [ // ... { "valid": true, "keywordLocation": "/prefixItems", "instanceLocation": "", "annotation": 1 }, { "valid": true, "keywordLocation": "/items", "instanceLocation": "", "annotation": true }, // ... ] {{}}
{{<instance-fail The prefix items of the array instance not adhering to the corresponding subschema in 'prefixItems' is invalid
>}}
[ 2, 3, "44", -5 ]
{{}}