Reference remote enum values from json-schema

时间秒杀一切 提交于 2021-02-10 12:54:06

问题


In my schema definitions, I have a type, with an integer property which should be any of a "fixed" set of numbers. The problem is that this "fixed set" may be changed often.

   "person": {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "enum": [1, 12, 30 ... , 1000]
        },
      }
    },

Is there any way to reference this array from a remote service (which will have the most updated set)?

   "person": {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "$ref": "http://localhost:8080/fixed_set"
        },
      }
    },

I tried $ref, but no luck. If "ref" is part of the solution, what should de backend return?

{
  "enum": [1, 12, 30 ... , 1000]
}

or

  "enum": [1, 12, 30 ... , 1000]

or

  [1, 12, 30 ... , 1000]

回答1:


main schema:

    {
      "$schema": "https://json-schema.org/draft/2019-09/schema",
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "$ref": "http://localhost:8080/fixed_set"
        },
      }
    }

subschema:

{
  "$id": "http://localhost:8080/fixed_set",
  "enum": [1, 12, 30 ... , 1000]
}

Note that you must be using an evaluator that supports draft2019-09 for the $ref to be recognized as a sibling keyword. Otherwise, you need to wrap it in an allOf:

    {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "allOf": [
            { "$ref": "http://localhost:8080/fixed_set" }
          ]
        },
      }
    }


来源:https://stackoverflow.com/questions/63252555/reference-remote-enum-values-from-json-schema

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