I would like to put together a JSON schema validator for records matching something like the example below:
record_id: 3280
record_type: my_record_type
record_render_rules:
- 3280_my_silly_object:
- 3280_my_silly_predicate
...where I put separate regular expression pattern restrictions on the data occupying 3280_my_silly_object
and 3280_my_silly_predicate
.
Below is what I have come up with so far for the render rules portion.
"record_render_rules": {
"type": "array",
"items": {
"type": "object",
"patternProperties": {
".*": {
"type": "array",
"items": {
"type": "string",
"pattern": "my pattern"
}
}
}
}
},
However, I find this unsatisfactory because it applies a common pattern to both types of data. As stated above, I would like a separate pattern for each of these types of data.
You are exactly correct with the proper syntax but you need to add the regex to validate the naming and constrain the items
to not allow additional properties other than the patternProperties
definition
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"record_id": {
"type": "number"
},
"record_type": {
"type": "string"
},
"record_render_rules": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^[0-9]+_[a-z]+_[a-z]+_[a-z]+$": {
"type": "array",
"items": {
"type": "string",
"pattern": "^[0-9]+_[a-z]+_[a-z]+_[a-z]+$"
}
}
}
}
}
}
}