user1452030
user1452030

Reputation: 1021

JSONSchema - Error when resolving schema reference

When trying to validate the following schema using 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"]
}

I'm getting the following error:

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

I'm not sure what's causing this error. I tried scanning the documentation several times, but for the life of me, I'm not able to figure out what's wrong. I even tried removing the id attributes thinking it messes up with the reference, but that didn't help either. Please help!

Upvotes: 2

Views: 2899

Answers (1)

user513418
user513418

Reputation:

Your problem is the id defined in your root object. The documentation states the following, about 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.

So, you either have to remove id from your root object so your $ref refers to the current schema (as below), or make sure that the schema referenced in the id does contain the test-params definition.

Your schema then becomes:

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

... which is valid.

Upvotes: 2

Related Questions