Skip to content

Commit

Permalink
Fix validator when the specified fragment includes array
Browse files Browse the repository at this point in the history
Fixes #265
  • Loading branch information
take authored and iainbeeston committed Jul 5, 2017
1 parent e51e1ff commit c12f600
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).

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

## [2.8.0] - 2017-02-07

Expand Down
2 changes: 2 additions & 0 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def schema_from_fragment(base_schema, fragment)

if @options[:list]
base_schema.to_array_schema
elsif base_schema.is_a?(Hash)
JSON::Schema.new(base_schema, schema_uri, @options[:version])
else
base_schema
end
Expand Down
53 changes: 53 additions & 0 deletions test/fragment_resolution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,57 @@ def test_fragment_resolution
JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
end
end

def test_odd_level_fragment_resolution
schema = {
"foo" => {
"type" => "object",
"required" => ["a"],
"properties" => {
"a" => {"type" => "integer"}
}
}
}

assert_valid schema, {"a" => 1}, :fragment => "#/foo"
refute_valid schema, {}, :fragment => "#/foo"
end

def test_even_level_fragment_resolution
schema = {
"foo" => {
"bar" => {
"type" => "object",
"required" => ["a"],
"properties" => {
"a" => {"type" => "integer"}
}
}
}
}

assert_valid schema, {"a" => 1}, :fragment => "#/foo/bar"
refute_valid schema, {}, :fragment => "#/foo/bar"
end

def test_array_fragment_resolution
schema = {
"type" => "object",
"required" => ["a"],
"properties" => {
"a" => {
"anyOf" => [
{"type" => "integer"},
{"type" => "string"}
]
}
}
}

refute_valid schema, "foo", :fragment => "#/properties/a/anyOf/0"
assert_valid schema, "foo", :fragment => "#/properties/a/anyOf/1"

assert_valid schema, 5, :fragment => "#/properties/a/anyOf/0"
refute_valid schema, 5, :fragment => "#/properties/a/anyOf/1"
end
end
33 changes: 33 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,41 @@ def whole_schema
}
end

def whole_schema_with_array
{
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "object",
"definitions" => {
"omg" => {
"links" => [
{
"type" => "object",
"schema" => {
"properties" => {
"content" => {
"type" => "string"
},
"author" => {
"type" => "string"
}
},
"required" => ["content", "author"]
}
}
]
}
}
}
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" => "Bob"}
assert_valid(whole_schema_with_array, data,
:fragment => "#/definitions/omg/links/0/schema")
end
end

0 comments on commit c12f600

Please sign in to comment.