I'm looking if it's possible to reference property names as enum values in AJV definitions.
Here is an example:
{
"$id": "modes.json",
"description": "Example modes",
"type": "object",
"properties": {
"MODE_WALK": { "$ref": "walk.json" },
"MODE_BICYCLE": { "$ref": "bicycle.json" },
}
}
Then I have another file with:
{
"$id": "another.json",
"description": "Example object",
"type": "object",
"properties": {
"text": {
"type": "string",
"maxLength": 128
},
"mode": {
"description": "Allowed modes",
"type": "string",
"enum": [
"MODE_WALK",
"MODE_BICYCLE"
]
},
},
"additionalProperties": false,
"required": ["text", "mode"]
}
Right now, enum has hardcoded values: MODE_WALK and MODE_BICYCLE - can I reference property names from the first file?
No, you cannot do this using JSON Schema.
You may consider a pre-processing step to build your schemas.
A common approach to this is to use Jsonnet: https://jsonnet.org
It has been sucessfully used in large scale projects such as the UK GOV website publication platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With