patternProperties : Object<String, Schema>

patternProperties

Object<String, Schema>

Validation succeeds if, for each instance name that matches any regular expressions that appear as a property name in this keyword’s value, the child instance for that name successfully validates against each schema that corresponds to a matching regular expression.

Value This keyword must be set to an object where each key is a valid regular expression, preferrably using the ECMA-262 flavour, and each value is a valid JSON Schema
Kind Applicator Annotation
Applies To Object
Base Dialect 2020-12
Changed In Draft 4
Introduced In Draft 3
Vocabulary Applicator
Specification https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.2.2
Metaschema https://json-schema.org/draft/2020-12/meta/applicator
Official Tests draft2020-12/patternProperties.json
Default {}
Annotation Array The set of instance property names validated by this keyword's subschema
Affected By None
Affects
Also See

The patternProperties keyword restricts properties of an object instance that match certain regular expressions to match their corresponding subschemas definitions. Information about the properties that this keyword was evaluated for is reported using annotations.

Remember that JSON Schema is a constraint-driven language. Therefore, non-object 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 object instances to enforce that lowercase properties are integers Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "patternProperties": {
    "^[a-z]+$": { "type": "integer" }
  }
}
Valid An object value that defines only lowercase integer properties is valid Instance
{ "foo": 1, "bar": 2, "baz": 3 }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo", "bar", "baz" ] }
Valid An object value that defines non-lowercase properties is valid Instance
{ "CamelCase": true, "alphanumeric123": "anything is valid" }
Valid An empty object value is valid Instance
{}
Invalid An object value that defines a lowercase non-integer properties is invalid Instance
{ "foo": "should have been an integer" }
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances with two potentially overlapping regular expressions Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "patternProperties": {
    "^f": { "type": "string" },
    "o$": { "minLength": 3 }
  }
}
Valid An object value that defines a property that matches both regular expressions and schemas is valid Instance
{ "foo": "long string" }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
Valid An object value that defines a property that matches one regular expression and its corresponding schema is valid Instance
{ "boo": 1 }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "boo" ] }
Invalid An object value that defines a property that matches both regular expressions but does not match one of the schemas is invalid Instance
{ "foo": "xx" }
Invalid An object value that defines a property that matches one regular expression but does not match its schema is invalid Instance
{ "boo": "xx" }
Valid A non-object value is valid Instance
"Hello World"
A schema that constrains object instances with overlapping static and regular expression definitions Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "patternProperties": {
    "^f": { "minLength": 3 }
  },
  "properties": {
    "foo": { "type": "string" }
  }
}
Valid An object value that defines a property that matches both definitions and schemas is valid Instance
{ "foo": "long string" }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
Valid An object value that defines a property that matches only the regular expression definition and its schema is valid Instance
{ "football": 3 }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "football" ] }
Invalid An object value that defines a property that matches both definitions but only matches one schema is invalid Instance
{ "foo": "xx" }
Valid An empty object value is valid Instance
{}
Valid A non-object value is valid Instance
"Hello World"