Skip to content
Closed
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
8 changes: 6 additions & 2 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions test/fragment_validation_with_ref_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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