From c12f6000795cc0fdf8d17d97d5bf9c1b383037e4 Mon Sep 17 00:00:00 2001 From: Takehiro Adachi Date: Fri, 16 Jan 2015 18:46:43 +0900 Subject: [PATCH] Fix validator when the specified fragment includes array Fixes #265 --- CHANGELOG.md | 4 +- lib/json-schema/validator.rb | 2 + test/fragment_resolution_test.rb | 53 +++++++++++++++++++++++ test/fragment_validation_with_ref_test.rb | 33 ++++++++++++++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae2a0751..1be4860a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index 2c462969..c439b613 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -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 diff --git a/test/fragment_resolution_test.rb b/test/fragment_resolution_test.rb index 36e9aee2..5fe90113 100644 --- a/test/fragment_resolution_test.rb +++ b/test/fragment_resolution_test.rb @@ -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 diff --git a/test/fragment_validation_with_ref_test.rb b/test/fragment_validation_with_ref_test.rb index dbcbd882..9ed3801a 100644 --- a/test/fragment_validation_with_ref_test.rb +++ b/test/fragment_validation_with_ref_test.rb @@ -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