Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Local $ref doesn't work #53

Open
Krzysztof-FF opened this Issue · 4 comments

3 participants

Krzysztof-FF Andrew Bradley Philipp Kewisch
Krzysztof-FF

When supplying a chema containing internal references, e.g. simplest one taken from example posted by "fge" in https://groups.google.com/forum/#!topic/json-schema/xiAEZG6fKT4:

{
"even-numbers": {
"type": "integer",
"divisibleBy": 2
},
"type": "array",
"additionalItems": {
"$ref": "#/even-numbers"
}
}

trying to validate simplest example:

[2,4]

returns following error:

/additionalItems Unknown schema reference urn:uuid:fb503557-341d-4f70-a3c1-fa6791338b6b#/even-numbers

Replacing JSON Pointer addressing by relative URI referring to "id" attribute doesn't work, either:

{
"even-numbers": {
"id": "even",
"type": "integer",
"divisibleBy": 2
},
"id": "root",
"type": "array",
"additionalItems": {
"$ref": "#even"
}
}

returns following error:

/additionalItems Unknown schema reference urn:root#even

Please explain if I am doing something wrong or whether it is a JSV error.

Andrew Bradley

I'm running into the exact same issue.

I have a few common types used in various places within a schema. I would like to write schemas for these common types in one place, then reference them throughout the schema.

A contrived example (I know Javascript comments aren't allowed in JSON):

// Schema for a child, describing his/her name and parents
{

  // set of schemas for types that are referenced from the root schema
  "otherSchemas":
  {
    "parent":
    {
      "id": "parent"
    , "properties": {
        "name": {"type": "string"}
      , "age": {"type": "integer"}
      }
    }
  }


, "title": "child"
, "properties":
  {
    "name": {"type": "string"}
    "mother": {"$ref": "parent"}
  , "father": {"$ref": "parent"}
  }

}

Using "$ref": "#/otherSchemas/parent" doesn't seem to work either.

Philipp Kewisch

The problem here is that JSV doesn't traverse properties of the schema that don't belong to the json schema draft, that are only used as containers for other schema elements.

From the newsgroup question I asked, it seems like the general notion is that this should be possible though. I have a patch I can send as a PR that will at least cover searching for random elements using json pointer, i.e based on cspotcode's example using:

{
  ...
  "properties" : {
     "name": { "type": "string" },
     "mother": { "$ref": "#/otherSchemas/parent" }
  }
}

This patch doesn't cover explicitly setting IDs, as this will require traversing each unknown property to its deepest element to find out if there is an "id" member somewhere.

Krzysztof-FF

This is some kind of drawback that the only way to define subschemas is to use some property with name not being a keyword. Ignoring such parts of schema using rule "this is outside of schema, so let's skip it" is contradiction to first sentence.

In the same time, it is ridiculous that if one wants to define some subschema, he has to use some name not being a keyword. I can't define my own subschemas named "title" or "description", for example.

Even after avoiding some keywords there is no guarantee that in future releases, set of keywords being JSON Schema vocabulary may change and will get in conflict with my own vocabulary which I want to define.

Concept of namespaces used in XML schemas is abandoned in JSON Schema deliberately, but I don't know how to address such name conflict problem.

Philipp Kewisch

Well, what you can do is put the schemas in multiple files and give each its own URI. Then you can reference them without issues using the full URI, as long as you register each schema with JSV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.