Skip to content

Commit 1ec4cc5

Browse files
committed
Merge pull request voxpupuli#217 from take/master
2 parents 3c50306 + ba40360 commit 1ec4cc5

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77

88
### Fixed
99
- Corrected the draft6 schema id to `http://json-schema.org/draft/schema#`
10-
- Rescue URI error when initializing a data string that contains a colon
10+
- Rescue URI error when initializing a data string that contains a colon
11+
- Fragments with an odd number of components no longer raise an `undefined method `validate'`
12+
error
1113

1214
## [2.8.0] - 2017-02-07
1315

lib/json-schema/validator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ def schema_from_fragment(base_schema, fragment)
103103

104104
if @options[:list]
105105
base_schema.to_array_schema
106+
elsif base_schema.is_a?(Hash)
107+
JSON::Schema.new(base_schema, schema_uri, @options[:version])
106108
else
107109
base_schema
108110
end

test/fragment_resolution_test.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,57 @@ def test_fragment_resolution
2727
JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
2828
end
2929
end
30+
31+
def test_odd_level_fragment_resolution
32+
schema = {
33+
"foo" => {
34+
"type" => "object",
35+
"required" => ["a"],
36+
"properties" => {
37+
"a" => {"type" => "integer"}
38+
}
39+
}
40+
}
41+
42+
assert_valid schema, {"a" => 1}, :fragment => "#/foo"
43+
refute_valid schema, {}, :fragment => "#/foo"
44+
end
45+
46+
def test_even_level_fragment_resolution
47+
schema = {
48+
"foo" => {
49+
"bar" => {
50+
"type" => "object",
51+
"required" => ["a"],
52+
"properties" => {
53+
"a" => {"type" => "integer"}
54+
}
55+
}
56+
}
57+
}
58+
59+
assert_valid schema, {"a" => 1}, :fragment => "#/foo/bar"
60+
refute_valid schema, {}, :fragment => "#/foo/bar"
61+
end
62+
63+
def test_array_fragment_resolution
64+
schema = {
65+
"type" => "object",
66+
"required" => ["a"],
67+
"properties" => {
68+
"a" => {
69+
"anyOf" => [
70+
{"type" => "integer"},
71+
{"type" => "string"}
72+
]
73+
}
74+
}
75+
}
76+
77+
refute_valid schema, "foo", :fragment => "#/properties/a/anyOf/0"
78+
assert_valid schema, "foo", :fragment => "#/properties/a/anyOf/1"
79+
80+
assert_valid schema, 5, :fragment => "#/properties/a/anyOf/0"
81+
refute_valid schema, 5, :fragment => "#/properties/a/anyOf/1"
82+
end
3083
end

test/fragment_validation_with_ref_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,41 @@ 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+
"omg" => {
36+
"links" => [
37+
{
38+
"type" => "object",
39+
"schema" => {
40+
"properties" => {
41+
"content" => {
42+
"type" => "string"
43+
},
44+
"author" => {
45+
"type" => "string"
46+
}
47+
},
48+
"required" => ["content", "author"]
49+
}
50+
}
51+
]
52+
}
53+
}
54+
}
55+
end
56+
3057
def test_validation_of_fragment
3158
data = [{"content" => "ohai", "author" => "Bob"}]
3259
assert_valid whole_schema, data, :fragment => "#/definitions/posts"
3360
end
61+
62+
def test_validation_of_fragment_with_array
63+
data = {"content" => "ohai", "author" => "Bob"}
64+
assert_valid(whole_schema_with_array, data,
65+
:fragment => "#/definitions/omg/links/0/schema")
66+
end
3467
end

0 commit comments

Comments
 (0)