contains : Schema

contains

Schema

Validation succeeds if the instance contains an element that validates against this schema.

Kind Applicator Annotation
Applies To Array
Dialect 2020-12
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
Affected By
Also see

Annotations

This keyword produces an annotation value which is an array of the indexes to which this keyword validates successfully when applying its subschema, in ascending order. The value is the empty array if the instance is empty. The value may be a boolean true if the subschema validates successfully when applied to every index of the instance.

Explanation

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.

  • The value of this keyword must be a valid JSON Schema.
  • 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.
  • The annotation produced by this keyword affects the behavior of unevaluatedItems in the Unevaluated vocabulary.

Examples

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