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.

Kind Applicator Annotation
Applies To Object
Dialect 2020-12
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
Also see

Annotations

The annotation result of this keyword is the set of instance property names matched by this keyword.

Explanation

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.

The annotation result of this keyword is the set of instance property names matched by this keyword. This annotation affects the behavior of additionalProperties and unevaluatedProperties.

  • The value of patternProperties must be an object.
  • Each property name of this object should be a valid regular expression, according to the ECMA-262 regular expression dialect.
  • Each property value of this object must be a valid JSON Schema.
  • Omitting this keyword has the same assertion behavior as an empty object.

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" }
  }
}
An object instance with properties matching the regex and conforming to the corresponding schema is valid Instance
{ "name": "John Doe", "age": 21 }
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/patternProperties",
    "instanceLocation": "",
    "annotation": [ "name", "age" ]
  },
  // ...
]
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
  }
}
An instance with properties not matching any regex is valid Instance
{ "zbaz": "zbaz" }
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/patternProperties",
    "instanceLocation": "",
    "annotation": []
  },
  // ...
]
  • Note: If patternProperties does not match anything, it is still expected to produce an empty array annotation.
An instance with properties matching the regex with a 'false' schema is invalid Instance
{ "foo": "foo", "bar": "bar" }
An instance with properties matching the regex with a 'true' schema, or/and with additional properties is valid Instance
{ "foo": "foo" }
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/patternProperties",
    "instanceLocation": "",
    "annotation": [ "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" }
  }
}
The value of 'foo' property must be a string Instance
{ "foo": [ "bar" ] }
An object instance with properties conforming to the schema is valid Instance
{ "foo": "bar" }
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/properties",
    "instanceLocation": "",
    "annotation": [ "foo" ]
  },
  {
    "valid": true,
    "keywordLocation": "/patternProperties",
    "instanceLocation": "",
    "annotation": [ "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
}
The value of the 'name' property must be a string Instance
{
  "name": [ "John", "Doe" ],
  "Age": 21,
  "email": "foo@bar.com"
}
An object instance with properties conforming to the schema is valid Instance
{
  "name": "John Doe",
  "Age": 21,
  "email": "foo@bar.com"
}
Annotations
[
  // ...
  {
    "valid": true,
    "keywordLocation": "/properties",
    "instanceLocation": "",
    "annotation": [ "name" ]
  },
  {
    "valid": true,
    "keywordLocation": "/patternProperties",
    "instanceLocation": "",
    "annotation": [ "Age" ]
  },
  {
    "valid": true,
    "keywordLocation": "/additionalProperties",
    "instanceLocation": "",
    "annotation": [ "email" ]
  },
  // ...
]
  • Instance properties (keys) not present in properties or not matching any regex within patternProperties are evaluated against additionalProperties.