Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make intra-doc refs work when validating with :fragment
Here's a weird corner-case for you: when calling JSON::Validator.validate and using the :fragment option, any references to other parts of the same schema would fail. Why? Because the handling of :fragment involved creating a *new* schema, with a new URI, with a subset of the original schema. Internal refs (as handled by JSON::Schema::RefAttribute) would try to resolve the ref within this sub-schema, and go *fwackoom* because the structure of the sub-schema wouldn't have the right path in it. I've adjusted the code for schema_from_fragment to not call initialize_schema (and hence not create those new sub-schemas), and instead just create a new JSON::Schema object (which, according to the comments, is the only reason that initialize_schema was being called). This causes no new schemas to be created, and RefAttribute can go looking up its ref in the right place.
- Loading branch information
Showing
3 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
require 'json-schema' | ||
|
||
class FragmentValidationWithRef < Test::Unit::TestCase | ||
def whole_schema | ||
{ | ||
"$schema" => "http://json-schema.org/draft-04/schema#", | ||
"type" => "object", | ||
"definitions" => { | ||
"post" => { | ||
"type" => "object", | ||
"properties" => { | ||
"content" => { | ||
"type" => "string" | ||
}, | ||
"author" => { | ||
"type" => "string" | ||
} | ||
} | ||
}, | ||
"posts" => { | ||
"type" => "array", | ||
"items" => { | ||
"$ref" => "#/definitions/post" | ||
} | ||
} | ||
} | ||
} | ||
end | ||
|
||
def test_validation_of_fragment | ||
data = [{"content" => "ohai", "author" => "Bob"}] | ||
v = nil | ||
assert_nothing_raised do | ||
v = JSON::Validator.fully_validate(whole_schema,data,:fragment => "#/definitions/posts") | ||
end | ||
|
||
assert(v.empty?, v.join("\n")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'test/unit' | ||
$:.unshift(File.expand_path('../../lib', __FILE__)) |