diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 9aac21c2..0631d5ca 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -90,17 +90,21 @@ def schema_from_fragment(base_schema, fragment) if !base_schema.has_key?(f) raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option") end - base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version]) + base_schema = base_schema[f] 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 = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version]) + base_schema = base_schema[f.to_i] else raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option") end end + if !base_schema.is_a?(JSON::Schema) + base_schema = JSON::Schema.new(base_schema, schema_uri, @options[:version]) + end + if @options[:list] base_schema.to_array_schema else diff --git a/test/fragment_validation_with_ref_test.rb b/test/fragment_validation_with_ref_test.rb index dbcbd882..9ba07d44 100644 --- a/test/fragment_validation_with_ref_test.rb +++ b/test/fragment_validation_with_ref_test.rb @@ -27,8 +27,47 @@ def whole_schema } end + def whole_schema_with_array + { + "$schema" => "http://json-schema.org/draft-04/schema#", + "type" => "object", + "definitions" => { + "post" => { + "links"=> [{ + "type" => "object", + "properties" => { + "content" => { + "type" => "string" + }, + "author" => { + "type" => "string" + } + } + }, + { + "type" => "object", + "properties" => { + "content" => { + "type" => "string" + }, + "author" => { + "type" => "integer" + } + } + }] + } + } + } + + end + def test_validation_of_fragment data = [{"content" => "ohai", "author" => "Bob"}] assert_valid whole_schema, data, :fragment => "#/definitions/posts" end + + def test_validation_of_fragment_with_array + data = {"content" => "ohai", "author" => 1} + assert_valid whole_schema_with_array, data, :fragment => "#/definitions/post/links/1/" + end end