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
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": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [ { "type": "number" } ]
}
[ 2, false ]
[
// ...
{
"valid": true,
"keywordLocation": "/prefixItems",
"instanceLocation": "",
"annotation": 0
},
// ...
]
[ "2", 3 ]
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "boolean" },
{ "type": "number" }
]
}
[ false, 35, [ "foo" ] ]
[
// ...
{
"valid": true,
"keywordLocation": "/prefixItems",
"instanceLocation": "",
"annotation": 1
},
// ...
]
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "boolean" },
{ "type": "string" }
],
"items": { "type": "number" }
}
[ false, "44", -5 ]
[
// ...
{
"valid": true,
"keywordLocation": "/prefixItems",
"instanceLocation": "",
"annotation": 1
},
{
"valid": true,
"keywordLocation": "/items",
"instanceLocation": "",
"annotation": true
},
// ...
]
[ 2, 3, "44", -5 ]