items : Schema | Array<Schema>
items
Schema | Array<Schema>If set to a schema, validation succeeds if each element of the instance validates against it, otherwise 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 valid JSON Schema or 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 |
Applies To | Array |
Base Dialect | Draft 7 |
Changed In | 2020-12 |
Introduced In | Draft 1 |
Vocabulary | Validation |
Specification | https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.4.1 |
Metaschema | http://json-schema.org/draft-07/schema# |
Official Tests | draft7/items.json |
Default |
{}
|
Annotation | None |
Affected By |
|
Affects |
|
Also See |
|
The items
keyword is used to
validate array items and has two different modes of operation depending on the
type of its value:
-
Schema: When set to a schema,
items
validates that all items in the array instance validate against the given subschema. -
Array: When set to an array of schemas,
items
validates each item in the array instance against the subschema at the corresponding position. Items beyond the length of theitems
array can be validated using theadditionalItems
keyword.
Common Pitfall
This keyword does not prevent an array instance from being
empty. If needed, use the minItems
to assert on the minimum 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": "http://json-schema.org/draft-07/schema#",
"items": { "type": "number" }
}
[ 1, -3.4, 54 ]
[]
[ 1, -3.4, 54, "foo" ]
"Hello World"
{
"$schema": "http://json-schema.org/draft-07/schema#",
"items": [ { "type": "boolean" }, { "type": "number" } ]
}
[ false, 35 ]
[ false, 35, "foo", "bar" ]
[ "not a boolean", 35 ]
[ false, "not a number" ]
[]
"Hello World"
{
"$schema": "http://json-schema.org/draft-07/schema#",
"items": [ { "type": "boolean" }, { "type": "number" } ],
"additionalItems": { "type": "string" }
}
[ false, 35 ]
[ false, 35, "foo", "bar" ]
[ false, 35, { "foo": "bar" } ]
[]
"Hello World"