contains : Schema
contains
SchemaValidation succeeds if the instance contains an element that validates against this schema.
Value | This keyword must be set to a valid JSON Schema |
---|---|
Kind | Applicator Annotation |
Applies To | Array |
Base Dialect | 2020-12 |
Changed In | 2019-09 |
Introduced In | Draft 6 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.1.3 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/contains.json |
Default |
{}
|
Annotation | Array Boolean A potentially empty array of the indexes to which this keyword's subschema validated successfully to (in ascending order), or a boolean true if it applied to every item of the instance |
Affected By |
|
Affects |
|
Also See |
|
The contains
keyword restricts array instances to include one or more items
(at any location of the array) that validate against the given subschema. The
lower and upper bounds that are allowed to validate against the given subschema
can be controlled using the minContains
and maxContains
keywords. Information about the items
that were successfully validated against the given subschema is reported using
annotations.
Common Pitfall
Keep in mind that when collecting annotations, the evaluator might need to exhaustively check every item in the array past the containment lower bound instead of short-circuiting validation, potentially introducing additional computational overhead.
For example, consider an array of 10 items where 5 of its items validate
against the contains
subschema (and neither minContains
nor maxContains
are declared, for simplicity). When not collecting annotations, validation will
stop after encountering the first match. However, when collecting annotations,
validation will have to proceed past the first match to report the 5 matching
indexes.
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",
"contains": {
"type": "number",
"multipleOf": 2
}
}
[ "foo", 2, false, [ "bar" ], -5 ]
{ "keyword": "/contains", "instance": "", "value": [ 1 ] }
[ "foo", 2, false, 3, 4, [ "bar" ], -5, -3.0 ]
{ "keyword": "/contains", "instance": "", "value": [ 1, 4, 7 ] }
[ 2, 4, 6, 8, 10, 12 ]
{ "keyword": "/contains", "instance": "", "value": true }
[ "foo", true ]
[]
"Hello World"