Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@ 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
Expand Down
22 changes: 22 additions & 0 deletions spec/json_matchers/match_response_schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@
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)
Expand Down