json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

Fix id handling

Open gabrielg opened this issue 10 years ago • 1 comments

This introduces a few refactors and changes which fixes handling of what validates as a valid Draft 4 schema:

{
  "id": "#/example",
  "type": "integer",
  "oneOf": [
    {"$ref": "#/definitions/foo"},
    {"$ref": "#/definitions/bar"}],
  "definitions": {
    "foo": {
      "type": "integer",
      "maximum": 1000
    },
    "bar": {
      "type": "integer",
      "minimum": 2000,
      "maximum": 3000
    }
  }
}

json-schema validation to show it's a valid schema:

irb(main):005:0> require 'json-schema'
=> true
irb(main):006:0> puts valid_schema
{
  "id": "#/example",
  "type": "integer",
  "oneOf": [
    {"$ref": "#/definitions/foo"},
    {"$ref": "#/definitions/bar"}],
  "definitions": {
    "foo": {
      "type": "integer",
      "maximum": 1000
    },
    "bar": {
      "type": "integer",
      "minimum": 2000,
      "maximum": 3000
    }
  }
}
=> nil
irb(main):007:0> JSON::Validator.fully_validate_schema(valid_schema, version: :draft4)
=> []

It also validates against the Java json-schema-validator.

Before these changes, trying to use the provided example schema would result in an exception:

/usr/local/opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/uri/common.rb:176:in `split': bad URI(is not URI?): {"id"=>"#/example", "type"=>"integer", "oneOf"=>[{"$ref"=>"#/definitions/foo"}, {"$ref"=>"#/definitions/bar"}], "definitions"=>{"foo"=>{"type"=>"integer", "maximum"=>1000}, "bar"=>{"type"=>"integer", "minimum"=>2000, "maximum"=>3000}}} (URI::InvalidURIError)
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/uri/common.rb:211:in `parse'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/2.1.0/uri/common.rb:747:in `parse'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:584:in `normalized_uri'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:528:in `rescue in initialize_schema'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:517:in `initialize_schema'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:47:in `block in initialize'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:47:in `synchronize'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:47:in `initialize'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:294:in `new'
    from /usr/local/opt/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/json-schema-2.4.1/lib/json-schema/validator.rb:294:in `fully_validate'
    from ./bin/json-validator:19:in `<main>'

This PR refactors some things and fixes the issue.

gabrielg avatar Nov 07 '14 00:11 gabrielg

Seems okay. I don't really grok why a schema would ever define a fragment as its top-level id, tho. I find it very difficult to reason about the Correct Behavior :tm: of the id property, especially since it seems to be largely redefined from draft3->draft4.

pd avatar Nov 07 '14 02:11 pd