-
-
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
Configure matcher to record_errors
#52
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "json-schema" | ||
require "json_matchers/payload" | ||
|
||
module JsonMatchers | ||
class Validator | ||
def initialize(options:, response:, schema_path:) | ||
@options = options.dup | ||
@payload = Payload.new(response).to_s | ||
@schema_path = schema_path.to_s | ||
end | ||
|
||
def validate! | ||
if recording_errors? | ||
validate_recording_errors | ||
else | ||
validate | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :options, :payload, :schema_path | ||
|
||
def recording_errors? | ||
options.fetch(:record_errors, false) | ||
end | ||
|
||
def validate_recording_errors | ||
JSON::Validator.fully_validate(schema_path, payload, options) | ||
end | ||
|
||
def validate | ||
JSON::Validator.validate!(schema_path, payload, options) | ||
|
||
[] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,13 +159,24 @@ | |
end | ||
|
||
context "when configured to record errors" do | ||
it "fails when the body is missing a required property" do | ||
it "includes the reasons for failure in the exception's message" do | ||
with_options(record_errors: true) do | ||
create_schema("foo_schema", | ||
"type" => "object", | ||
"required" => ["foo"]) | ||
|
||
expect(response_for({})).not_to match_response_schema("foo_schema") | ||
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. 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. |
||
"properties" => { | ||
"username" => { | ||
"allOf": [ | ||
{ "type": "string" }, | ||
{ "minLength": 5 } | ||
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 item of a multiline array. 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 item of a multiline array. |
||
] | ||
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 item of a multiline hash. 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 item of a multiline hash. |
||
} | ||
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 item of a multiline hash. 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 item of a multiline hash. |
||
} | ||
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 item of a multiline hash. 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 item of a multiline hash. |
||
}) | ||
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. 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. |
||
invalid_payload = response_for({ "username" => "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. Redundant curly braces around a hash parameter. 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. |
||
|
||
expect { | ||
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. Avoid using {...} for multi-line blocks. 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. Avoid using {...} for multi-line blocks. |
||
expect(invalid_payload).to match_response_schema("foo_schema") | ||
}.to raise_error(/minimum/) | ||
end | ||
end | ||
end | ||
|
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.
Redundant curly braces around a hash parameter.