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

家住魔仙堡 提交于 2021-02-05 07:47:26

问题


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:


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.



来源:https://stackoverflow.com/questions/60864040/is-it-possible-to-reference-property-name-in-enum-from-another-ajv-definitions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!