Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reference property name in enum from another AJV definitions?

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?

like image 860
iaforek Avatar asked Jan 27 '26 11:01

iaforek


1 Answers

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.

like image 149
Relequestual Avatar answered Jan 29 '26 00:01

Relequestual



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!