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

Viewed 237

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?

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.

Related