-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant curly braces around a hash parameter. |
||
"type" => "object", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"], | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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]