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": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"patternProperties": {
"^[Nn]ame$": { "type": "string" },
"^[Aa]ge$": { "type": "number" }
}
}
{ "name": "John Doe", "age": 21 }
[
// ...
{
"valid": true,
"keywordLocation": "/patternProperties",
"instanceLocation": "",
"annotation": [ "name", "age" ]
},
// ...
]
{ "name": "John Doe", "age": "21" }
- Annotations are not produced when validation fails.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties": {
"^f.*": true,
"^b.*": false
}
}
{ "zbaz": "zbaz" }
[
// ...
{
"valid": true,
"keywordLocation": "/patternProperties",
"instanceLocation": "",
"annotation": []
},
// ...
]
- Note: If
patternProperties
does not match anything, it is still expected to produce an empty array annotation.
{ "foo": "foo", "bar": "bar" }
{ "foo": "foo" }
[
// ...
{
"valid": true,
"keywordLocation": "/patternProperties",
"instanceLocation": "",
"annotation": [ "foo" ]
},
// ...
]
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"foo": { "type": "string" }
},
"patternProperties": {
"^f": { "type": "string" }
}
}
{ "foo": [ "bar" ] }
{ "foo": "bar" }
[
// ...
{
"valid": true,
"keywordLocation": "/properties",
"instanceLocation": "",
"annotation": [ "foo" ]
},
{
"valid": true,
"keywordLocation": "/patternProperties",
"instanceLocation": "",
"annotation": [ "foo" ]
},
// ...
]
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" }
},
"patternProperties": {
"[Aa]ge$": { "type": "number" }
},
"additionalProperties": true
}
{
"name": [ "John", "Doe" ],
"Age": 21,
"email": "foo@bar.com"
}
{
"name": "John Doe",
"Age": 21,
"email": "foo@bar.com"
}
[
// ...
{
"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 withinpatternProperties
are evaluated againstadditionalProperties
.