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.
Common Pitfall
This keyword does not restrict the size of the array. If the array instance has fewer number of items that the given subschemas, only such items will be validated. If needed, use the
minItems
and the maxItems
keywords to assert on the bounds of the
array.
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
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"prefixItems": [ { "type": "boolean" }, { "type": "number" } ]
}
[]
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
[ false ]
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
[ false, 35 ]
{ "keyword": "/prefixItems", "instance": "", "value": true }
[ false, 35, "something", "else" ]
{ "keyword": "/prefixItems", "instance": "", "value": 2 }
[ true, false ]
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"prefixItems": [ { "type": "boolean" }, { "type": "number" } ],
"items": { "type": "string" }
}
[]
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
[ false ]
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
[ false, 35 ]
{ "keyword": "/prefixItems", "instance": "", "value": true }
[ false, 35, "foo", "bar" ]
{ "keyword": "/prefixItems", "instance": "", "value": 2 }
{ "keyword": "/items", "instance": "", "value": true }
[ false, 35, { "foo": "bar" } ]
"Hello World"