Skip to main content
JSON Schema provides several keywords for validating string instances.

minLength

Specifies the minimum length of a string.

Syntax

The value must be a non-negative integer.

Validation Rules

A string instance is valid if its length is greater than or equal to the specified value. The length is defined as the number of Unicode code points in the string.

Examples

Valid instances:
Invalid instances:

Default Behavior

Omitting minLength is equivalent to minLength: 0, which means empty strings are allowed.

maxLength

Specifies the maximum length of a string.

Syntax

The value must be a non-negative integer.

Validation Rules

A string instance is valid if its length is less than or equal to the specified value. The length is defined as the number of Unicode code points in the string.

Examples

Valid instances:
Invalid instances:

pattern

Validates that a string matches a regular expression.

Syntax

The value must be a string containing a valid regular expression.

Validation Rules

A string instance is valid if the regular expression matches the string successfully. Important: Regular expressions are not implicitly anchored. To match the entire string, use anchors like ^ (start) and $ (end).

Regular Expression Dialect

The pattern should be a valid regular expression according to ECMA-262. See the interoperability considerations in the specification for details.

Examples

Email Pattern

Valid instances:
Invalid instances:

Phone Number Pattern

Valid instances:

Alphanumeric Pattern

Valid instances:
Invalid instances:

Partial Matching

This matches any string containing β€œJSON” anywhere:

Combining String Keywords

Multiple string validation keywords can be used together.

Username Validation

This validates a username that:
  • Is between 3 and 20 characters long
  • Contains only letters, numbers, underscores, and hyphens
Valid instances:
Invalid instances:

Password Validation

This validates a password that:
  • Is at least 8 characters long
  • Contains at least one lowercase letter, one uppercase letter, one digit, and one special character

Product Code

Valid instance:

Unicode Considerations

The minLength and maxLength keywords count Unicode code points, not bytes or visible characters.
The string "πŸ˜€πŸ‘βœ¨" has a length of 3 code points and is valid, even though it may appear as multiple characters depending on how emojis are rendered.