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.
Common Pitfall
This keyword is evaluated independently of the
properties
keyword. If an
object property is described by both keywords, then both subschemas must
successfully validate against the given property for validation to succeed.
Furthermore, an instance property may match more than one regular expression
set with this keyword, in which case the property is expected to validate
against all matching subschemas.
Digging Deeper
While the specification suggests the use of
ECMA-262
regular expressions for interoperability purposes, the use of different
flavours like PCRE or POSIX (Basic or Extended) is permitted. Also, the
specification does not impose the use of any particular regular expression
flag. By convention (and somewhat enforced by the official JSON Schema test
suite), regular expressions are not implicitly
anchored and are always
treated as case-sensitive. It is also common for the
DOTALL
flag to be enabled, permitting the dot character class to match new lines.
To avoid interoperability issues, stick to ECMA-262, and don’t assume the use of any regular expression flag.
Common Pitfall
Regular expressions often make use of characters that need to be escaped when making use of them as part of JSON strings. For example, the reverse solidus character (more commonly known as the backslash character) and the double quote character need to be escaped. Failure to do so will result in an invalid JSON document. Applications to work with regular expressions, like Regex Forge, typically provide convenient functionality to copy a regular expression for use in JSON.
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
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties": {
"^[a-z]+$": { "type": "integer" }
}
}
{ "foo": 1, "bar": 2, "baz": 3 }
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo", "bar", "baz" ] }
{ "CamelCase": true, "alphanumeric123": "anything is valid" }
{}
{ "foo": "should have been an integer" }
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties": {
"^f": { "type": "string" },
"o$": { "minLength": 3 }
}
}
{ "foo": "long string" }
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
{ "boo": 1 }
{ "keyword": "/patternProperties", "instance": "", "value": [ "boo" ] }
{ "foo": "xx" }
{ "boo": "xx" }
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"patternProperties": {
"^f": { "minLength": 3 }
},
"properties": {
"foo": { "type": "string" }
}
}
{ "foo": "long string" }
{ "keyword": "/patternProperties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/properties", "instance": "", "value": [ "foo" ] }
{ "football": 3 }
{ "keyword": "/patternProperties", "instance": "", "value": [ "football" ] }
{ "foo": "xx" }
{}
"Hello World"