maxContains : Integer

maxContains

Integer

The number of times that the contains keyword (if set) successfully validates against the instance must be less than or equal to the given integer.

Value This keyword must be set to a zero or positive integer
Kind Assertion
Applies To Array
Base Dialect 2020-12
Changed In None
Introduced In 2019-09
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.4.4
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Official Tests draft2020-12/maxContains.json
Default None
Annotation None
Affected By None
Affects
Also See

The maxContains keyword modifies the contains keyword to constrain array instances to the given maximum number of containment matches. This keyword has no effect if the contains keyword is not declared.

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 most two even numbers Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "maxContains": 2,
  "contains": {
    "type": "number",
    "multipleOf": 2
  }
}
Valid An array value with two even numbers is valid Instance
[ "foo", 2, false, 3, 4, [ "bar" ], -5 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": [ 1, 4 ] }
Valid An array value with one even number is valid Instance
[ "foo", 2, false, [ "bar" ], -5 ]
Annotations
{ "keyword": "/contains", "instance": "", "value": [ 1 ] }
Invalid An array value with more than two even numbers is invalid Instance
[ "foo", 2, false, 3, 4, [ "bar" ], -5, -3.0 ]
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"
A schema that constrains array instances to not contain an even number Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "minContains": 0,
  "maxContains": 0,
  "contains": {
    "multipleOf": 2
  }
}
Valid An array value with no even number is valid Instance
[ "foo", 3, false ]
Invalid An array value with one even number is invalid Instance
[ "foo", 2, false ]
Invalid An array value with multiple even numbers is invalid Instance
[ "foo", 2, 4 ]
Valid An empty array value is invalid Instance
[]
Valid A non-array value is valid Instance
"Hello World"
A schema that incorrectly constrains maximum containment without constrainining containment Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "maxContains": 2
}
Valid An array value with arbitrary items is valid Instance
[ "John", false, 29, { "foo": "bar" }, [ 5, 7 ] ]
Valid An empty array is valid Instance
[]
Valid A non-array value is valid Instance
"Hello World"