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

Fixed fragment resolution. Issue #64 #75

Merged
merged 4 commits into from
Dec 31, 2013
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.project
.*.swp
pkg
*.gem
2 changes: 1 addition & 1 deletion lib/json-schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
require 'schema'
require 'validator'
Dir[File.join(File.dirname(__FILE__), "json-schema/attributes/*.rb")].each {|file| require file }
Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*.rb")].each {|file| require file }
Dir[File.join(File.dirname(__FILE__), "json-schema/validators/*.rb")].sort!.each {|file| require file }
require 'uri/file'
48 changes: 26 additions & 22 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,28 @@ def schema_from_fragment(base_schema, fragment)
end

fragments.each do |f|
if base_schema.is_a?(Hash)
if base_schema.is_a?(JSON::Schema) #test if fragment is a JSON:Schema instance
if !base_schema.schema.has_key?(f)
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = base_schema.schema[f]
elsif base_schema.is_a?(Hash)
if !base_schema.has_key?(f)
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = base_schema[f]
base_schema = initialize_schema(base_schema[f]) #need to return a Schema instance for validation to work
elsif base_schema.is_a?(Array)
if base_schema[f.to_i].nil?
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = base_schema[f.to_i]
base_schema = initialize_schema(base_schema[f.to_i])
else
raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
end
end

if @options[:list] #check if the schema is validating a list
base_schema.schema = schema_to_list(base_schema.schema)
end
base_schema
end

Expand Down Expand Up @@ -553,18 +560,23 @@ def fake_uri schema
@@fake_uri_generator.call(schema)
end

def schema_to_list(schema)
new_schema = {"type" => "array", "items" => schema}
if !schema["$schema"].nil?
new_schema["$schema"] = schema["$schema"]
end

new_schema
end

def initialize_schema(schema)
if schema.is_a?(String)
begin
# Build a fake URI for this
schema_uri = URI.parse(fake_uri(schema))
schema = JSON::Validator.parse(schema)
if @options[:list]
new_schema = {"type" => "array", "items" => schema}
if !schema["$schema"].nil?
new_schema["$schema"] = schema["$schema"]
end
schema = new_schema
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema)
end
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
Validator.add_schema(schema)
Expand All @@ -581,12 +593,8 @@ def initialize_schema(schema)
end
if Validator.schemas[schema_uri.to_s].nil?
schema = JSON::Validator.parse(open(schema_uri.to_s).read)
if @options[:list]
new_schema = {"type" => "array", "items" => schema}
if !schema["$schema"].nil?
new_schema["$schema"] = schema["$schema"]
end
schema = new_schema
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema)
end
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
Validator.add_schema(schema)
Expand All @@ -595,12 +603,8 @@ def initialize_schema(schema)
end
end
elsif schema.is_a?(Hash)
if @options[:list]
new_schema = {"type" => "array", "items" => schema}
if !schema["$schema"].nil?
new_schema["$schema"] = schema["$schema"]
end
schema = new_schema
if @options[:list] && @options[:fragment].nil?
schema = schema_to_list(schema)
end
schema_uri = URI.parse(fake_uri(serialize(schema)))
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FragmentResolution < Test::Unit::TestCase
def test_fragment_resolution
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"required" => ["a"],
"properties" => {
"a" => {
"type" => "object",
Expand Down