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
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 None
Also See

The contains keyword is used to check if at least one element in an array instance validates against a specified sub-schema. It offers flexibility compared to items, which requires all elements to adhere to a single schema.

An array instance is valid against contains if at least one of its elements is valid against the given schema, except when minContains is present and has a value of 0, in which case an array instance must be considered valid against the contains keyword, even if none of its elements is valid against the given schema.

Similarly, if maxContains is present alongside contains, the instance will be considered valid as long as the number of elements successfully validating against the contains subschema does not exceed the specified limit defined by maxContains.

  • For data validation, items validates all array elements against a single schema, prefixItems validates a fixed-length sequence at the array’s beginning, and contains checks for at least one element matching a schema anywhere in the array.
  • The subschema must be applied to every array element, even after the first match has been found, to collect annotations for use by other keywords.

Examples

Schema with 'contains' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "contains": { "type": "number" }
}
Valid An array instance with at least one item as a numeric value is valid Instance
[ "foo", 3, false, [ "bar" ], -5 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": [ 1, 4 ] }
Invalid An array instance containing no string value is invalid Instance
[ "foo", true ]
Schema with 'contains' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "contains": { "type": "string" }
}
Valid An array instance with all items as string values is valid Instance
[ "foo", "bar", "baz" ]
Annotations
{ "keyword": "/contains", "instance": "", "value": true }
  • The annotation value is a boolean ‘true’ if the subschema successfully validates when applied to every index of the instance.