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": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"patternProperties": {
"^[Nn]ame$": { "type": "string" },
"^[Aa]ge$": { "type": "number" }
}
}
{ "name": "John Doe", "age": 21 }
{ "keyword": "/patternProperties", "instance": "", "value": [ "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" }
{ "keyword": "/patternProperties", "instance": "", "value": [] }
- Note: If
patternProperties
does not match anything, it is still expected to produce an empty array annotation.
{ "foo": "foo", "bar": "bar" }
{ "foo": "foo" }
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"foo": { "type": "string" }
},
"patternProperties": {
"^f": { "type": "string" }
}
}
{ "foo": [ "bar" ] }
{ "foo": "bar" }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/patternProperties", "instance": "", "value": [ "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"
}
{ "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 withinpatternProperties
are evaluated againstadditionalProperties
.