Skip to content

Commit e2eb07f

Browse files
Fix vavalidator
add test fix schema_from_fragment method Remove the waste of schema_from_fragment method fix schema_from_fragment method
1 parent ffdbf2d commit e2eb07f

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/json-schema/validator.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,21 @@ def schema_from_fragment(base_schema, fragment)
9090
if !base_schema.has_key?(f)
9191
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
9292
end
93-
base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version])
93+
base_schema = base_schema[f]
9494
elsif base_schema.is_a?(Array)
9595
if base_schema[f.to_i].nil?
9696
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
9797
end
98-
base_schema = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version])
98+
base_schema = base_schema[f.to_i]
9999
else
100100
raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
101101
end
102102
end
103103

104+
if !base_schema.is_a?(JSON::Schema)
105+
base_schema = JSON::Schema.new(base_schema, schema_uri, @options[:version])
106+
end
107+
104108
if @options[:list]
105109
base_schema.to_array_schema
106110
else

test/fragment_validation_with_ref_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,47 @@ def whole_schema
2727
}
2828
end
2929

30+
def whole_schema_with_array
31+
{
32+
"$schema" => "http://json-schema.org/draft-04/schema#",
33+
"type" => "object",
34+
"definitions" => {
35+
"post" => {
36+
"links"=> [{
37+
"type" => "object",
38+
"properties" => {
39+
"content" => {
40+
"type" => "string"
41+
},
42+
"author" => {
43+
"type" => "string"
44+
}
45+
}
46+
},
47+
{
48+
"type" => "object",
49+
"properties" => {
50+
"content" => {
51+
"type" => "string"
52+
},
53+
"author" => {
54+
"type" => "integer"
55+
}
56+
}
57+
}]
58+
}
59+
}
60+
}
61+
62+
end
63+
3064
def test_validation_of_fragment
3165
data = [{"content" => "ohai", "author" => "Bob"}]
3266
assert_valid whole_schema, data, :fragment => "#/definitions/posts"
3367
end
68+
69+
def test_validation_of_fragment_with_array
70+
data = {"content" => "ohai", "author" => 1}
71+
assert_valid whole_schema_with_array, data, :fragment => "#/definitions/post/links/1/"
72+
end
3473
end

0 commit comments

Comments
 (0)