Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle record_errors scenario #51

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 23 additions & 5 deletions lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [91/80]

# to record errors, so we must instead inspect fully_validate's errors response

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [85/80]

if options[:record_errors]
errors = JSON::Validator.fully_validate(
schema_path.to_s,
Payload.new(response).to_s,
options

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put a comma after the last parameter of a multiline method call.

)

# 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put a comma after the last parameter of a multiline method call.

)
end
rescue JSON::Schema::ValidationError => ex
@validation_failure_message = ex.message
false
Expand Down
23 changes: 23 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,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", {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant curly braces around a hash parameter.

"type" => "object",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis.

"required" => ["foo"],
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the first position after the preceding left parenthesis.


expect(response_for({})).not_to match_response_schema("foo_schema")
end
end
end

def raise_formatted_error(error_message)
Expand Down