-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes [#51]. Extract `JsonMatchers::Validator` to encapsulate error reporting behavior. `JSON::Validator` exposes a `#validate!` method that will raise on validation failures, and a `#fully_validate!` method that won't raise, but will return an array of error messages instead. [#51]: #51
- Loading branch information
1 parent
871bb40
commit 2c686ee
Showing
2 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |