JSONSchema - 解析模式引用时出错

JSONSchema - Error when resolving schema reference

尝试使用 http://www.jsonschemavalidator.net/

验证以下架构时
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://test.com/unified-ingest",
  "type": "object",
  "definitions" : {

    "test-params" : {
      "id": "http://test.com/test-params",
      "type": "object",
      "properties" : {

        "operation-type": {
          "id" : "http://test.com/test-params/operation-type",
          "enum": [ "create", "update" ]
        },

        "required" : {
          "id" : "http://test.com/test-params/required",
          "type" : "array",
          "items" : {
            "type" : "string",
            "pattern" : "^[\w,\s-\p{L}\p{M}]+\.(jpg|png|xml)$"
          }
        }

      },
      "required" : ["operation-type"]
    }

  },

  "properties": {

    "root" : {
      "id": "http://test.com/unified-ingest/root",
      "type": "object",
      "properties" : {

        "$" : {
          "id" : "http://test.com/unified-ingest/root/attributes",
          "type" : "object",
          "properties" : {
            "xmlns:xsi" : {
              "type" : "string"
            },
            "xmlns" : {
              "type" : "string"
            },
            "xsi:schemaLocation" : {
              "type" : "string",
              "pattern" : "^[a-z]*$"
            }
          }
        },

        "test-params" : {
          "$ref" : "#/definitions/test-params"
        }

      },
      "required" : ["test-params"]
    }

  },
  "required" : ["root"]
}

我收到以下错误:

Error when resolving schema reference '#/definitions/test-params'. Path 'properties.root.properties.test-params', line 56, position 25.

我不确定是什么导致了这个错误。我尝试多次扫描文档,但对于我来说,我无法找出问题所在。我什至尝试删除 id 属性,认为它会混淆引用,但这也无济于事。请帮忙!

您的问题是 root 对象中定义的 idThe documentation 关于 id 的陈述如下:

But be aware of the second purpose of the id property: that it declares a base URL for relative $ref URLs elsewhere in the file.

因此,您要么必须从 root 对象中删除 id,以便您的 $ref 引用当前架构(如下所示),要么确保引用的架构id 确实包含 test-params 定义。

您的架构将变为:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://test.com/unified-ingest",
  "type": "object",
  "definitions" : {

    "test-params" : {
      "id": "http://test.com/test-params",
      "type": "object",
      "properties" : {

        "operation-type": {
          "id" : "http://test.com/test-params/operation-type",
          "enum": [ "create", "update" ]
        },

        "required" : {
          "id" : "http://test.com/test-params/required",
          "type" : "array",
          "items" : {
            "type" : "string",
            "pattern" : "^[\w,\s-\p{L}\p{M}]+\.(jpg|png|xml)$"
          }
        }

      },
      "required" : ["operation-type"]
    }

  },

  "properties": {

    "root" : {
      "type": "object",
      "properties" : {

        "$" : {
          "id" : "http://test.com/unified-ingest/root/attributes",
          "type" : "object",
          "properties" : {
            "xmlns:xsi" : {
              "type" : "string"
            },
            "xmlns" : {
              "type" : "string"
            },
            "xsi:schemaLocation" : {
              "type" : "string",
              "pattern" : "^[a-z]*$"
            }
          }
        },

        "test-params" : {
          "$ref" : "#/definitions/test-params"
        }

      },
      "required" : ["test-params"]
    }

  },
  "required" : ["root"]
}

...这是有效的。