maxProperties : Integer

maxProperties

Integer

An object instance is valid if its number of properties is less than, or equal to, the value of this keyword.

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.

  • It applies specifically to object instances.
  • The value of this keyword must be a non-negative integer (0 or greater).
  • Setting maxProperties to 0 enforces an empty object instance.

Examples

Schema with 'maxProperties' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "maxProperties": 2
}
An instance with 2 or less properties is valid Instance
{ "foo": 3, "bar": "hi" }
'minProperties' has no effect on values other than objects Instance
false
An instance with more than 2 properties is invalid Instance
{ "foo": 3, "bar": "hi", "baz": true }
Schema with 'maxProperties' and 'properties' keywords Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "age": { "type": "integer" },
    "address": { "type": "string" }
  },
  "maxProperties": 2
}
An instance with 2 or less properties is valid Instance
{ "name": "John", "age": 2 }
An instance with more than 2 properties is invalid Instance
{ "name": "John", "age": 2, "address": "22/3, GCET Road, Ahmedabad, Gujarat" }
Schema with 'maxProperties', 'patternProperties' and 'additionalProperties' keywords Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "patternProperties": {
    "^[Aa]ge$": { "type": "integer" }
  },
  "additionalProperties": { "type": "boolean" },
  "maxProperties": 2
}
An instance with 2 or less properties is valid Instance
{ "Age": 22 }
The value of 'eligible' property must be a boolean Instance
{ "Age": 21, "eligible": "yes" }
An instance with 2 or less properties is valid Instance
{ "Age": 21, "eligible": true }
An instance with more than 2 properties is invalid Instance
{ "Age": 21, "eligible": true, "isGraduated": true }
Schema with 'maxProperties' and 'required' keywords Schema
{
  "$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
}
An instance without 'address' property is invalid Instance
{ "name": "John", "age": 42 }
An instance with more than 2 properties is invalid Instance
{ "name": "John", "age": 42, "address": "some address" }
  • It is important to note that one should be cautious when using the required and maxProperties 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 with 'maxProperties' and 'minProperties' keywords Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "maxProperties": 2,
  "minProperties": 4
}
All object instances are invalid against the above schema Instance
{ "name": "John", "age": 42 }
Any instance with a value other than an object is valid Instance
{ "name": "John", "age": 42 }
  • When using maxProperties and minProperties together in a schema to add extra constraints on the instance, one must make sure that the value of minProperties is not greater than maxProperties; otherwise, no object instance will be valid against that schema.