contains : Schema

contains

Schema

Validation 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.

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

A schema that constrains array instances to contain at least one even number Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "contains": {
    "type": "number",
    "multipleOf": 2
  }
}
Valid An array value with one even number is valid Instance
[ "foo", 2, false, [ "bar" ], -5 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": [ 1 ] }
Valid An array value with multiple even numbers is valid Instance
[ "foo", 2, false, 3, 4, [ "bar" ], -5, -3.0 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": [ 1, 4, 7 ] }
Valid An array value that solely consists of even numbers is valid Instance
[ 2, 4, 6, 8, 10, 12 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": true }
Invalid An array value without any even number is invalid Instance
[ "foo", true ]
Invalid An empty array value is invalid Instance
[]
Valid A non-array value is valid Instance
"Hello World"