brandur/json_schema

Path must begin with a leading "/"

glasserc opened this issue · 0 comments

Hi! I'm using json_schema indirectly through https://github.com/thoughtbot/json_matchers so this might not be the right place to report this.

I have a question about reference URLs. The following code fails:

# frozen_string_literal: true

require "json_schema"

store = JsonSchema::DocumentStore.new

foo_schema = JsonSchema.parse!(
  {
    "id" => "file:/foo.json",
    "type" => "object",
    "properties" => {
      "a" => {
        "type" => "string"
      }
    }
  }
)

store.add_schema(foo_schema)

bar_schema = JsonSchema.parse!(
  {
    "id" => "file:/bar.json",
    "type" => "object",
    "properties" => {
      "foo" => {
        "$ref" => "file:/foo.json",
      }
    }
  }
)

bar_schema.expand_references!(store: store)

bar_schema.validate!(
  {
    foo: {
      a: 1
    }
  }
)

However, as a workaround, my team discovered that putting # at the end of foo id and the $ref seems to work.

Tracing through the code I found this reference:

# given a simple fragment without '#', resolve as a JSON Pointer only as
# per spec

This seems to indicate that the behavior is intentional. However, I can't figure out what spec is being referred to here. I assumed it was Draft 4, but I didn't see anything about this behavior in https://datatracker.ietf.org/doc/html/draft-zyp-json-schema-04 or the corresponding validation spec (https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00). Am I missing something obvious?