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 ECMA-262 regular expression and each value is a valid JSON Schema
Kind Applicator Annotation
Applies To Object
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 is a variant of properties with regular expression support. It maps regular expressions to schemas. If a property name matches the given regular expression, the property value must validate against the corresponding schema.

Examples

Schema with 'patternProperties' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "patternProperties": {
    "^[Nn]ame$": { "type": "string" },
    "^[Aa]ge$": { "type": "number" }
  }
}
Valid An object instance with properties matching the regex and conforming to the corresponding schema is valid Instance
{ "name": "John Doe", "age": 21 }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "name", "age" ] }
Invalid An object instance with properties matching the regex and not conforming to the corresponding schema is invalid Instance
{ "name": "John Doe", "age": "21" }
  • Annotations are not produced when validation fails.
Schema with patternProperties with boolean schemas Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "patternProperties": {
    "^f.*": true,
    "^b.*": false
  }
}
Valid An instance with properties not matching any regex is valid Instance
{ "zbaz": "zbaz" }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [] }
  • Note: If patternProperties does not match anything, it is still expected to produce an empty array annotation.
Invalid An instance with properties matching the regex with a 'false' schema is invalid Instance
{ "foo": "foo", "bar": "bar" }
Valid An instance with properties matching the regex with a 'true' schema, or/and with additional properties is valid Instance
{ "foo": "foo" }
Annotations
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
Schema with overlap between 'patternProperties' and 'properties' Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "foo": { "type": "string" }
  },
  "patternProperties": {
    "^f": { "type": "string" }
  }
}
Invalid The value of 'foo' property must be a string Instance
{ "foo": [ "bar" ] }
Valid An object instance with properties conforming to the schema is valid Instance
{ "foo": "bar" }
Annotations
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
Schema with 'patternProperties', 'properties' and 'additionalProperties' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" }
  },
  "patternProperties": {
    "[Aa]ge$": { "type": "number" }
  },
  "additionalProperties": true
}
Invalid The value of the 'name' property must be a string Instance
{
  "name": [ "John", "Doe" ],
  "Age": 21,
  "email": "foo@bar.com"
}
Valid An object instance with properties conforming to the schema is valid Instance
{
  "name": "John Doe",
  "Age": 21,
  "email": "foo@bar.com"
}
Annotations
{ "keyword": "/properties", "instance": "", "value": [ "name" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "Age" ] }
{ "keyword": "/additionalProperties", "instance": "", "value": [ "email" ] }
  • Instance properties (keys) not present in properties or not matching any regex within patternProperties are evaluated against additionalProperties.