From a1de68caa61b917c84c33b929c66a6731c271fab Mon Sep 17 00:00:00 2001 From: Bob Bonifield Date: Mon, 30 Jan 2017 12:18:44 -0700 Subject: [PATCH 1/4] Correctly handle record_errors scenario --- lib/json_matchers/matcher.rb | 28 +++++++++++++++---- .../match_response_schema_spec.rb | 23 +++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index 940fdac..6ce5370 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -9,11 +9,29 @@ def initialize(schema_path, options = {}) end def matches?(response) - JSON::Validator.validate!( - schema_path.to_s, - Payload.new(response).to_s, - options, - ) + # validate! will not raise and will always return true if you configure the validator + # to record errors, so we must instead inspect fully_validate's errors response + if options[:record_errors] + errors = JSON::Validator.fully_validate( + schema_path.to_s, + Payload.new(response).to_s, + options + ) + + # errors is an array, but it will always only return a single item + if errors.any? + @validation_failure_message = errors.first + false + else + true + end + else + JSON::Validator.validate!( + schema_path.to_s, + Payload.new(response).to_s, + options + ) + end rescue JSON::Schema::ValidationError => ex @validation_failure_message = ex.message false diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index 74201c2..de232a2 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -165,6 +165,29 @@ config.options.delete(:strict) end end + + context "when options specify to record errors" do + around do |example| + JsonMatchers.configure do |config| + config.options[:record_errors] = true + end + + example.run + + JsonMatchers.configure do |config| + config.options.delete(:record_errors) + end + end + + it "fails when the body is missing a required property" do + create_schema("foo_schema", { + "type" => "object", + "required" => ["foo"], + }) + + expect(response_for({})).not_to match_response_schema("foo_schema") + end + end end def raise_formatted_error(error_message) From 0fb02858fc184b82765ed926594a35f325799db6 Mon Sep 17 00:00:00 2001 From: Bob Bonifield Date: Mon, 30 Jan 2017 12:39:43 -0700 Subject: [PATCH 2/4] Hound adjustments --- lib/json_matchers/matcher.rb | 9 +++++---- spec/json_matchers/match_response_schema_spec.rb | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/json_matchers/matcher.rb b/lib/json_matchers/matcher.rb index 6ce5370..1e5fc15 100644 --- a/lib/json_matchers/matcher.rb +++ b/lib/json_matchers/matcher.rb @@ -9,13 +9,14 @@ def initialize(schema_path, options = {}) end def matches?(response) - # validate! will not raise and will always return true if you configure the validator - # to record errors, so we must instead inspect fully_validate's errors response + # validate! will not raise and will always return true if you configure + # the validator to record errors, so we must instead inspect + # fully_validate's errors response if options[:record_errors] errors = JSON::Validator.fully_validate( schema_path.to_s, Payload.new(response).to_s, - options + options, ) # errors is an array, but it will always only return a single item @@ -29,7 +30,7 @@ def matches?(response) JSON::Validator.validate!( schema_path.to_s, Payload.new(response).to_s, - options + options, ) end rescue JSON::Schema::ValidationError => ex diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index de232a2..65a33be 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -180,10 +180,10 @@ end it "fails when the body is missing a required property" do - create_schema("foo_schema", { - "type" => "object", - "required" => ["foo"], - }) + create_schema("foo_schema", + "type" => "object", + "required" => ["foo"], + ) expect(response_for({})).not_to match_response_schema("foo_schema") end From 68d37d5f4de93e8355b56168ed7815a7a1a6ab56 Mon Sep 17 00:00:00 2001 From: Bob Bonifield Date: Mon, 30 Jan 2017 12:40:36 -0700 Subject: [PATCH 3/4] Hound again --- spec/json_matchers/match_response_schema_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index 65a33be..d3a1c6a 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -182,8 +182,7 @@ it "fails when the body is missing a required property" do create_schema("foo_schema", "type" => "object", - "required" => ["foo"], - ) + "required" => ["foo"],) expect(response_for({})).not_to match_response_schema("foo_schema") end From 22b72deaf7e7a4a69bb924ce17414cb065c5e2fe Mon Sep 17 00:00:00 2001 From: Bob Bonifield Date: Mon, 30 Jan 2017 12:42:16 -0700 Subject: [PATCH 4/4] Another adjustment --- spec/json_matchers/match_response_schema_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/json_matchers/match_response_schema_spec.rb b/spec/json_matchers/match_response_schema_spec.rb index d3a1c6a..5f6c427 100644 --- a/spec/json_matchers/match_response_schema_spec.rb +++ b/spec/json_matchers/match_response_schema_spec.rb @@ -182,7 +182,7 @@ it "fails when the body is missing a required property" do create_schema("foo_schema", "type" => "object", - "required" => ["foo"],) + "required" => ["foo"]) expect(response_for({})).not_to match_response_schema("foo_schema") end