繁体   English   中英

JSON Schema 引用解析

[英]JSON Schema reference resolution

我有一个包含“$ref”标签的 JSON 模式,我正在尝试获取已解析“$ref”标签的 JSON 模式版本。 我只想从 JSON 模式字符串中的定义(标签)中解析“$ref”(即不需要外部解析)。

是否有执行 JSON Schema 解析的库? (我目前正在使用 org.everit.json.schema 库,这很棒,但我找不到如何做我需要的)。

例如,我的原始架构是:

{
  "$id": "https://example.com/arrays.schema.json",
  "description": "A representation of a person, company, organization, or place",
  "title": "complex-schema",
  "type": "object",
  "properties": {
    "fruits": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "vegetables": {
      "type": "array",
      "items": { "$ref": "#/$defs/veggie" }
    }
  },
  "$defs": {
    "veggie": {
      "type": "object",
      "required": [ "veggieName", "veggieLike" ],
      "properties": {
        "veggieName": {
          "type": "string",
          "description": "The name of the vegetable."
        },
        "veggieLike": {
          "type": "boolean",
          "description": "Do I like this vegetable?"
        }
      }
    }
  }
}

这将解析为这样的事情(请注意,“#defs/veggie”解析为其在模式中内联插入的定义):

{
  "$id": "https://example.com/arrays.schema.json",
  "description": "A representation of a person, company, organization, or place",
  "title": "complex-schema",
  "type": "object",
  "properties": {
    "fruits": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "vegetables": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [ "veggieName", "veggieLike" ],
        "properties": {
          "veggieName": {
            "type": "string",
            "description": "The name of the vegetable."
          },
          "veggieLike": {
            "type": "boolean",
            "description": "Do I like this vegetable?"
          }
        }
      }
    }
  }
}

这在一般意义上是不可能的,因为:

  • $ref 可能是递归的(即再次引用自身)
  • $ref 中的关键字可能会复制包含模式中的某些关键字,这会导致某些逻辑被覆盖。

为什么需要以这种方式更改架构? 通常,JSON 模式实现将在根据提供的数据评估模式时自动解析 $refs。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM