简体   繁体   English

JSON Schema 引用解析

[英]JSON Schema reference resolution

I have a JSON schema that contains "$ref" tags and I am trying to get a version of the JSON schema that have the "$ref" tags resolved.我有一个包含“$ref”标签的 JSON 模式,我正在尝试获取已解析“$ref”标签的 JSON 模式版本。 I am only looking to resolve "$ref" from definition (tags) within the JSON Schema string (ie. not external resolution needed).我只想从 JSON 模式字符串中的定义(标签)中解析“$ref”(即不需要外部解析)。

Is there a library that performs the resolution of the JSON Schema?是否有执行 JSON Schema 解析的库? (I am currently using org.everit.json.schema library, which is great, but I can't find how to do what I need). (我目前正在使用 org.everit.json.schema 库,这很棒,但我找不到如何做我需要的)。

For example, my original schema is:例如,我的原始架构是:

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

Which would resolve to something like this (notice that the "#defs/veggie" resolves to its definition inserted inline in the schema):这将解析为这样的事情(请注意,“#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?"
          }
        }
      }
    }
  }
}

This isn't possible in the general sense, because:这在一般意义上是不可能的,因为:

  • the $ref might be recursive (ie reference itself again) $ref 可能是递归的(即再次引用自身)
  • the keywords in the $ref might duplicate some of the keywords in the containing schema, which would cause some logic to be overwritten. $ref 中的关键字可能会复制包含模式中的某些关键字,这会导致某些逻辑被覆盖。

Why do you need to alter the schema in this way?为什么需要以这种方式更改架构? Generally, a JSON Schema implementation will resolve the $refs automatically while evaluating the schema against provided data.通常,JSON 模式实现将在根据提供的数据评估模式时自动解析 $refs。

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

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