maxProperties : Integer
maxProperties
IntegerAn object instance is valid if its number of properties is less than, or equal to, the value of this keyword.
Value | This keyword must be set to a zero or positive integer |
---|---|
Kind | Assertion |
Applies To | Object |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 4 |
Vocabulary | Validation |
Specification | https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/validation |
Official Tests | draft2020-12/maxProperties.json |
Default | None |
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The maxProperties
keyword is used to specify the maximum number of properties allowed in an object instnace. It is typically used to enforce constraints on the number of properties an object instance can have. If the number of properties in the object exceeds the value specified by maxProperties
, the validation fails.
- Setting
maxProperties
to 0 enforces an empty object instance.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"maxProperties": 2
}
{ "foo": 3, "bar": "hi" }
false
{ "foo": 3, "bar": "hi", "baz": true }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"address": { "type": "string" }
},
"maxProperties": 2
}
{ "name": "John", "age": 2 }
{ "name": "John", "age": 2, "address": "22/3, GCET Road, Ahmedabad, Gujarat" }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"patternProperties": {
"^[Aa]ge$": { "type": "integer" }
},
"additionalProperties": { "type": "boolean" },
"maxProperties": 2
}
{ "Age": 22 }
{ "Age": 21, "eligible": "yes" }
{ "Age": 21, "eligible": true }
{ "Age": 21, "eligible": true, "isGraduated": true }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"address": { "type": "string" }
},
"required": [ "name", "age", "address" ],
"maxProperties": 2
}
{ "name": "John", "age": 42 }
{ "name": "John", "age": 42, "address": "some address" }
- It is important to note that one should be cautious when using the
required
andmaxProperties
keywords together in a schema because it can create a situation where the instance will always fail the validation, as shown in the above example.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"maxProperties": 2,
"minProperties": 4
}
{ "name": "John", "age": 42 }
{ "name": "John", "age": 42 }
- When using
maxProperties
andminProperties
together in a schema to add extra constraints on the instance, one must make sure that the value ofminProperties
is not greater thanmaxProperties
; otherwise, no object instance will be valid against that schema.