簡體   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