patternGroups and patternRequired (v5 proposal) - sgpinkus/json-schema GitHub Wiki
JSON-SCHEMA-ORG/JSON-SCHEMA-SPEC REPOSITORY.
THIS WIKI IS OBSOLETE. PLEASE SEE THE NEWNOTE: Added to new org as issue: https://github.com/json-schema-org/json-schema-spec/issues/117
Proposed keywords
This proposal would introduce two new keywords:
patternGroups
andpatternRequired
these keywords would complement the existing keyword:
patternProperties
Purpose
Currently, schemas can specify a minimum/maximum number of object properties, but they cannot place such constraints on particular groups of properties.
This proposal basically allows us to specify the number of properties that must match a particular pattern.
Values
patternGroups
The value of patternGroups
would be an object. The keys of the object would be regular expressions (exactly like the existing patternProperties
).
The values inside patternGroups
would be objects, containing zero or more of the following properties:
minimum
- the minimum number of properties in the data that MUST match the corresponding patternmaximum
- the maximum number of properties in the data that MUST match the corresponding patternschema
- the schema that matching properties must follow.
minimum
/maximum
would be non-negative integers, and schema
would be a schema.
patternRequired
The value of this keyword should be the array of patterns (to require at least one property matching the pattern)
Behaviour
patternGroups
If the instance is an object, then for every entry in patternGroups
:
- the set of properties matching that pattern is collected
- if
minimum
ormaximum
are specified in thepatternGroups
entry, then the size of this property set must be between these values (inclusive) - if
schema
is specified in thepatternGroups
entry, then for every property in the property set, the corresponding object member in the instance must follow that schema.
patternRequired
If the instance is an object, the data to be valid should have at least one property matching each pattern (the same property can match multiple patterns).
patternGroups
Example {
"type": "object",
"patternGroups": {
"^[a-z]+$": {
"minimum": 1,
"schema": {"type": "string"}
},
"^[0-9]+$": {
"minimum": 1,
"schema": {"type": "integer"}
}
}
}
patternRequired
Example with {
"type": "object",
"patternProperties": {
"^[a-z]+$": {"type": "string"},
"^[0-9]+$": {"type": "integer"}
},
"patternRequired": ["^[a-z]+$", "^[0-9]+$"]
}
Both these schemas expresses the constraint that instance objects must have at least one alphabetic key, and at least one numeric-key. (This constraint is currently not possible to express).
Additionally, the alphabetic keys must hold strings, and the numeric keys must hold integers.
Valid: {"abc": "foo", "123": 456}
Invalid: {"abc": "foo", "def": "bar"}
For the simple case the syntax of patternRequired
is the simpler and consistent with properties
/required
.