I want to create a JSON schema (v2020-12) which can validate design tokens
The problem here is, that those tokens could be nested infinitively and each nested property could have any name.
The schema should validate that a property can have either one other property with unknown name or a design token. Other properties should not be allowed.
This should be valid:
{
"unknown_property_1": {
"unknown_property_2": {
<< nesting with unknown depth >>
"unknown_property_n": {
"$value": "#fff",
"$type": "this is a token with mandatory $value and $type"
}
}
}
}
This not:
{
"unknown_property_1": {
"fancy_unwanted_property": 123,
"unknown_property_2": {
"unknown_property_n": {
"$value": "#fff",
"$type": "this is a token with mandatory $value and $type"
}
}
}
}
I experimented with "anyOf" and "$ref" but couldnt get any close.
CodePudding user response:
Recursive structures can be defined quite elegantly in JSON Schema. Here's the simplest version.
{
"type": "object",
"properties": {
"$value": { "type": "string" },
"$type": { "type": "string" }
},
"additionalProperties": { "$ref": "#" }
}
If you need a more strict version, this is an alternative.
{
"anyOf": [
{
"type": "object",
"properties": {
"$value": { "type": "string" },
"$type": { "type": "string" }
},
"additionalProperties": false
},
{
"type": "object",
"patternProperties": {
"": { "$ref": "#" }
}
}
]
}