pattern : String

pattern

String

A string instance is considered valid if the regular expression matches the instance successfully.

Value This keyword must be set to a regular expression, preferrably using the ECMA-262 flavour
Kind Assertion
Applies To String
Base Dialect 2020-12
Changed In Draft 4
Introduced In Draft 1
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.3.3
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Official Tests
Default ".*"
Annotation None
Affected By None
Affects None
Also See

The pattern keyword restricts string instances to match the given regular expression.

Remember that JSON Schema is a constraint-driven language. Therefore, non-string instances successfully validate against this keyword. If needed, make use of the type keyword to constraint the accepted type accordingly.

Examples

A schema that constrains string instances to look like e-mail addresses Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
}
Valid A string value that matches the regular expression is valid Instance
"john.doe@example.com"
Invalid A string value that does not match the regular expression is invalid Instance
"foo"
Valid A non-string value is valid Instance
1234