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
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
---|---|
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 inclusive index to which this keyword applied its subschema (starting from zero), or a boolean true if it was applied to every item of the instance
Hint: Use the jsonschema validate command to collect annotations from the command-line
|
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" } ]
}
[]
[ false ]
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
[ false, 35 ]
{ "keyword": "/prefixItems", "instance": "", "value": true }
[ false, 35, "something", "else" ]
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
[ true, false ]
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"prefixItems": [ { "type": "boolean" }, { "type": "number" } ],
"items": { "type": "string" }
}
[]
[ false ]
{ "keyword": "/prefixItems", "instance": "", "value": 0 }
[ false, 35 ]
{ "keyword": "/prefixItems", "instance": "", "value": true }
[ false, 35, "foo", "bar" ]
{ "keyword": "/prefixItems", "instance": "", "value": 1 }
{ "keyword": "/items", "instance": "", "value": true }
[ false, 35, { "foo": "bar" } ]
"Hello World"