unevaluatedItems : Schema
unevaluatedItems
SchemaValidates array elements that did not successfully validate against other standard array applicators.
Value |
This keyword must be set to 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 | 2019-09 |
Vocabulary | Unevaluated |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-11.2 |
Metaschema | https://json-schema.org/draft/2020-12/meta/unevaluated |
Official Tests | draft2020-12/unevaluatedItems.json |
Default |
{}
|
Annotation |
Boolean
A boolean true if it applied to any item of the instance
Hint: Use the jsonschema validate command to collect annotations from the command-line
|
Affected By |
|
Affects | None |
Also See |
|
The unevaluatedItems
keyword is a generalisation of the items
keyword that considers related keywords even when they are not direct
siblings of this keyword. More specifically, this keyword is affected by
occurences of prefixItems
,
items
, contains
, and unevaluatedItems
itself, as long as the evaluate path that led to
unevaluatedItems
is a prefix of the evaluate path of the others.
Given its evaluation-dependent nature, this keyword is evaluated after every other keyword from every other vocabulary.
Best Practice
There are two common use cases for this keyword, both for reducing duplication:
(1) Elegantly describing additional array items while declaring the
prefixItems
or
contains
keywords behind
conditional logic without duplicating the items
keyword in every possible branch. (2) Re-using
helpers that consist of the prefixItems
, items
, or contains
keywords, while specialising the helpers as
needed in specific locations without having to inline the entire contents of
the helper.
Digging Deeper
The JSON Schema specification defines the relationship between this keyword and the ones that affect it in terms of annotations. However, in practice, most implementations avoid the use of annotations for performance reasons, as emitting annotations and checking the annotation values of other keywords often involves significant memory allocation and complex data structure traversals.
The paper Elimination of annotation dependencies in validation for Modern JSON Schema is a comprehensive mathematical study of how applicators can be automatically re-written to avoid annotation dependencies, leading to schemas that are simpler to evaluate.
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",
"if": { "maxItems": 3 },
"then": { "prefixItems": [ { "type": "string" } ] },
"else": { "contains": { "type": "boolean" } },
"unevaluatedItems": { "type": "number" }
}
[ "foo", 1, 2 ]
{ "keyword": "/then/prefixItems", "instance": "", "value": 0 }
{ "keyword": "/unevaluatedItems", "instance": "", "value": true }
[ true, 1, false, 2, true, 3 ]
{ "keyword": "/else/contains", "instance": "", "value": [ 0, 2, 4 ] }
{ "keyword": "/unevaluatedItems", "instance": "", "value": true }
[ "foo", "bar", "baz" ]
[ true, 2, "foo", "bar" ]
{}
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "#/$defs/string-first-item",
"unevaluatedItems": false,
"$defs": {
"string-first-item": {
"prefixItems": [ { "type": "string" } ]
}
}
}
[ "foo" ]
{ "keyword": "/$defs/string-first-item/prefixItems", "instance": "", "value": 0 }
[ "foo", 2, 3 ]
{}
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [
{ "items": true },
{ "unevaluatedItems": false }
]
}
[ 1, 2, 3 ]
[]
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"allOf": [ { "unevaluatedItems": true } ],
"unevaluatedItems": false
}
[ 1, 2, 3 ]
{ "keyword": "/allOf/0/unevaluatedItems", "instance": "", "value": true }
{}
"Hello World"