首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从json-schema引用远程枚举值

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

Stack Overflow用户
提问于 2020-08-05 01:55:03
回答 1查看 308关注 0票数 2

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

代码语言:javascript
复制
   "person": {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "enum": [1, 12, 30 ... , 1000]
        },
      }
    },

有没有办法从远程服务引用这个数组(它将拥有最新的集合)?

代码语言:javascript
复制
   "person": {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "$ref": "http://localhost:8080/fixed_set"
        },
      }
    },

我试过$ref,但没成功。如果"ref“是解决方案的一部分,那么de backend应该返回什么?

代码语言:javascript
复制
{
  "enum": [1, 12, 30 ... , 1000]
}

代码语言:javascript
复制
  "enum": [1, 12, 30 ... , 1000]

代码语言:javascript
复制
  [1, 12, 30 ... , 1000]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-05 03:57:59

主模式:

代码语言:javascript
复制
    {
      "$schema": "https://json-schema.org/draft/2019-09/schema",
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "$ref": "http://localhost:8080/fixed_set"
        },
      }
    }

子模式:

代码语言:javascript
复制
{
  "$id": "http://localhost:8080/fixed_set",
  "enum": [1, 12, 30 ... , 1000]
}

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

代码语言:javascript
复制
    {
      "type": "object",
      "properties": {
        "aproperty": {
          "type": "integer",
          "allOf": [
            { "$ref": "http://localhost:8080/fixed_set" }
          ]
        },
      }
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63252555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档