maxContains : Integer
maxContains
IntegerThe 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.
Digging Deeper
Using
contains
with both minContains
and maxContains
set to the
same value restricts arrays to contain exactly that number of items that match
the given subschema. Furthermore, setting these keywords to zero is a common
trick to restrict arrays to not contain an item that matches the given
subschema without making use of the not
applicator.
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",
"maxContains": 2,
"contains": {
"type": "number",
"multipleOf": 2
}
}
[ "foo", 2, false, 3, 4, [ "bar" ], -5 ]
{ "keyword": "/contains", "instance": "", "value": [ 1, 4 ] }
[ "foo", 2, false, [ "bar" ], -5 ]
{ "keyword": "/contains", "instance": "", "value": [ 1 ] }
[ "foo", 2, false, 3, 4, [ "bar" ], -5, -3.0 ]
[ "foo", true ]
[]
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"minContains": 0,
"maxContains": 0,
"contains": {
"multipleOf": 2
}
}
[ "foo", 3, false ]
[ "foo", 2, false ]
[ "foo", 2, 4 ]
[]
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"maxContains": 2
}
[ "John", false, 29, { "foo": "bar" }, [ 5, 7 ] ]
[]
"Hello World"