brandur / json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.
MIT License
230 stars 45 forks source link

Path must begin with a leading "/" #123

Open glasserc opened 3 years ago

glasserc commented 3 years ago

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:

https://github.com/brandur/json_schema/blob/2c0d59bf6f0ff6fe6e7c805a7c829999b31182a9/lib/json_reference.rb#L19-L20

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?