Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix id handling #178

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions lib/json-schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,11 @@

module JSON
class Schema

attr_accessor :schema, :uri, :validator

def initialize(schema,uri,parent_validator=nil)
def initialize(schema, uri, parent_validator = nil)
@schema = self.class.stringify(schema)
@uri = uri

# If there is an ID on this schema, use it to generate the URI
if @schema['id'] && @schema['id'].kind_of?(String)
temp_uri = URI.parse(@schema['id'])
if temp_uri.relative?
uri = uri.merge(@schema['id'])
temp_uri = uri
end
@uri = temp_uri
end
@uri.fragment = ''
@uri = generate_schema_uri(uri)

# If there is a $schema on this schema, use it to determine which validator to use
if @schema['$schema']
Expand Down Expand Up @@ -50,14 +38,23 @@ def self.stringify(schema)
end

def base_uri
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you introduced a new usage I haven't noticed, I think this method has been entirely unused since the first commit of this project. I propose just deleting it unless anyone has a reason to keep it. =)

parts = @uri.to_s.split('/')
parts.pop
parts.join('/') + '/'
@uri.merge(File.dirname(@uri.path)).to_s
end

def to_s
@schema.to_json
end

private

def generate_schema_uri(uri)
schema_uri = uri.merge('#') # Drop fragment if present
return schema_uri unless @schema['id'] && @schema['id'].kind_of?(String)

id_uri = URI.parse(@schema['id'])
return id_uri unless id_uri.relative?

schema_uri.merge(id_uri.path)
end
end
end

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
2 changes: 1 addition & 1 deletion test/test_list_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_list_option_reusing_schemas
"properties" => { "a" => { "type" => "integer" } }
}

uri = URI.parse('http://example.com/item')
uri = URI.parse('http://example.com/item#')
schema = JSON::Schema.new(schema_hash, uri)
JSON::Validator.add_schema(schema)

Expand Down
10 changes: 10 additions & 0 deletions test/test_schema_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def valid_schema_v4
}
end

def valid_schema_v4_with_fragment_id
valid_schema_v4.merge("id" => "#/foo")
end

def valid_schema_v4_with_absolute_uri_id
valid_schema_v4.merge("id" => "http://www.example.invalid/#/foo")
end

def invalid_schema_v4
{
"$schema" => "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -142,6 +150,8 @@ def test_draft04_validation
data = {"b" => {"a" => 5}}
assert(JSON::Validator.validate(valid_schema_v4,data,:validate_schema => true, :version => :draft4))
assert(!JSON::Validator.validate(invalid_schema_v4,data,:validate_schema => true, :version => :draft4))
assert(JSON::Validator.validate(valid_schema_v4_with_fragment_id,data,:validate_schema => true, :version => :draft4))
assert(JSON::Validator.validate(valid_schema_v4_with_absolute_uri_id,data,:validate_schema => true, :version => :draft4))
end

def test_validate_just_schema_draft04
Expand Down