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.

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 with 'maxProperties' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "maxProperties": 2
}
Valid An instance with 2 or less properties is valid Instance
{ "foo": 3, "bar": "hi" }
Valid 'minProperties' has no effect on values other than objects Instance
false
Invalid 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
}
Valid An instance with 2 or less properties is valid Instance
{ "name": "John", "age": 2 }
Invalid 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
}
Valid An instance with 2 or less properties is valid Instance
{ "Age": 22 }
Invalid The value of 'eligible' property must be a boolean Instance
{ "Age": 21, "eligible": "yes" }
Valid An instance with 2 or less properties is valid Instance
{ "Age": 21, "eligible": true }
Invalid 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
}
Invalid An instance without 'address' property is invalid Instance
{ "name": "John", "age": 42 }
Invalid 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
}
Invalid All object instances are invalid against the above schema Instance
{ "name": "John", "age": 42 }
Valid 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.