maxLength : Integer

maxLength

Integer

A string instance is valid against this keyword if its length is less than, or equal to, the value of this keyword.

Kind Assertion
Applies To String
Dialect 2020-12
Introduced In Draft 1
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.3.1
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Also see

The maxLength keyword is used to specify the maximum length of a string instance. It is used to enforce a constraint on the maximum number of characters allowed for a string instance.

  • Applies only to string data types.
  • Value must be a non-negative integer.
  • String length is counted in characters, not bytes.
  • Validation succeeds if the string length is less than or equal to the specified maxLength.

Examples

Schema restricting string length to a maximum of 10 characters Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "string",
  "maxLength": 10
}
An instance with a string length less than or equal to 10 is valid Instance
"foo"
An instance with a string length greater than 10 is invalid Instance
"This is an invalid string"
Schema allowing either a string with a maximum of 20 characters or a numeric value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": [ "string", "number" ],
  "maxLength": 20
}
An instance with a string length less than or equal to 20 is valid Instance
"This is a valid string"
An instance with a string length greater than 20 is invalid Instance
"This description is too long"
An instance with a numeric value is valid Instance
55
Schema for maximum string length validation with Unicode characters Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "string",
  "maxLength": 3
}
An instance with 3 or less characters is valid Instance
"\u0066\u006F\u006F" // --> "foo"
An instance with more than 3 characters is invalid Instance
"\u0048\u0065\u006C\u006C\u006F" // --> "Hello"