【问题标题】:Reference remote enum values from json-schema从 json-schema 引用远程枚举值
【发布时间】:2020-08-04 17:55:02
【问题描述】:

在我的模式定义中,我有一个类型,它具有一个整数属性,它应该是一组“固定”数字中的任何一个。问题是这个“固定集”可能会经常更改。

   "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]

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    主架构:

        {
          "$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" }
              ]
            },
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 2015-05-30
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 2022-12-17
      • 2019-07-22
      • 2020-07-19
      相关资源
      最近更新 更多