Skip to content

Commit

Permalink
Generate absolute URIs as fake schema URIs.
Browse files Browse the repository at this point in the history
This generates absolute URN URIs for schemas as necessary, rather than
relative ones. Using relative URIs causes errors given schemas that
should actually be valid.

BadSchemaRefTest has been altered to use a reference to an invalid
URI, rather than a valid relative URI.
  • Loading branch information
gabrielg committed Nov 7, 2014
1 parent 482521d commit 0c63566
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,14 @@ def serialize schema
end
end

def fake_uuid schema
def fake_uuid(schema)
@@fake_uuid_generator.call(schema)
end

def fake_uri_for_schema(schema)
URI::Generic.build(:scheme => "urn", :opaque => "uuid:#{fake_uuid(schema)}")
end

def schema_to_list(schema)
new_schema = {"type" => "array", "items" => schema}
if !schema["$schema"].nil?
Expand All @@ -516,7 +520,7 @@ def initialize_schema(schema)
if schema.is_a?(String)
begin
# Build a fake URI for this
schema_uri = URI.parse(fake_uuid(schema))
schema_uri = fake_uri_for_schema(schema)
schema = JSON::Validator.parse(schema)
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema)
Expand All @@ -537,7 +541,7 @@ def initialize_schema(schema)
schema = Validator.schemas[schema_uri.to_s]
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema.schema)
schema_uri = URI.parse(fake_uuid(serialize(schema)))
schema_uri = fake_uri_for_schema(serialize(schema))
schema = JSON::Schema.new(schema, schema_uri, @options[:version])
Validator.add_schema(schema)
end
Expand All @@ -548,7 +552,7 @@ def initialize_schema(schema)
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema)
end
schema_uri = URI.parse(fake_uuid(serialize(schema)))
schema_uri = fake_uri_for_schema(serialize(schema))
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
Validator.add_schema(schema)
else
Expand Down
6 changes: 3 additions & 3 deletions test/test_bad_schema_ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def test_bad_uri_ref
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "array",
"items" => { "$ref" => "../google.json"}
"items" => { "$ref" => "{bad-scheme}://foo.com"}
}

data = [1,2,3]
assert_raises(URI::BadURIError) do
JSON::Validator.validate(schema,data)
assert_raises(URI::InvalidURIError) do
JSON::Validator.validate(schema, data)
end
end

Expand Down

0 comments on commit 0c63566

Please sign in to comment.