json - 从 json-schema 引用远程枚举值

标签 json jsonschema json-schema-validator

在我的架构定义中,我有一个带有整数属性的类型,该属性应该是任何“固定”数字集。问题是这个“固定集”可能会经常改变。

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

是否有任何方法可以从远程服务引用此数组(该服务将具有最新的集合)?

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

我尝试了 $ref,但没有成功。 如果“ref”是解决方案的一部分,那么后端应该返回什么?

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

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

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

最佳答案

主要架构:

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

子架构:

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

请注意,您必须使用支持 Draft2019-09 的评估器,才能将 $ref 识别为同级关键字。否则,您需要将其包装在 allOf 中:

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

关于json - 从 json-schema 引用远程枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63252555/

相关文章:

javascript - Rails 应用程序的选项卡式 javascript 小部件

javascript - 如何为 jsonschema 生成时间字符串

javascript - 使用 Dojo 时无法识别外部库 (AJV)

java - 使用 jsonschema2pojo 或 com.sun.codemodel 时在类的开头添加注释

json - 如何根据模式中的定义进行验证?

java - Json 架构验证失败,并出现 MalformedURLException : unknown protocol: classpath error

javascript - 使用唯一键/值过滤 JSON

Python 从 JSON 文件创建树

c# - 在 UWP 的 gridview 中检索 JSON 数据

python - 正则表达式将 IP 与 jsonschema 中的掩码进行匹配