JSONSchema - 解析模式引用时出错

时间:2015-12-27 02:26:13

标签: jsonschema

尝试使用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属性,认为它与引用混淆,但这也没有帮助。请帮忙!

1 个答案:

答案 0 :(得分:1)

您的问题是id对象中定义的rootThe documentation声明了以下内容:id

  

但请注意id属性的第二个目的:它声明文件中其他位置的相对$ ref URL的基本URL。

因此,您必须从id对象中删除root,以便$ref引用当前架构(如下所示),或确保id中引用的架构{1}}确实包含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"]
}

......这是有效的。