Skip to content

Commit

Permalink
Configure matcher to record_errors
Browse files Browse the repository at this point in the history
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
seanpdoyle committed Feb 24, 2017
1 parent 871bb40 commit 2c686ee
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
46 changes: 18 additions & 28 deletions lib/json_matchers/matcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "json-schema"
require "json_matchers/payload"
require "json_matchers/validator"

module JsonMatchers
class Matcher
Expand All @@ -9,47 +9,37 @@ def initialize(schema_path, options = {})
end

def matches?(response)
# 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
validator = build_validator(response)

self.errors = validator.validate!

errors.empty?
rescue JSON::Schema::ValidationError => error
self.errors = [error.message]
false
rescue JSON::Schema::JsonParseError
raise InvalidSchemaError
end

def validation_failure_message
@validation_failure_message.to_s
errors.first.to_s
end

private

attr_reader :schema_path, :options
attr_accessor :errors

def default_options
JsonMatchers.configuration.options || {}
end

def build_validator(response)
Validator.new(
options: options,
response: response,
schema_path: schema_path,
)
end
end
end
38 changes: 38 additions & 0 deletions lib/json_matchers/validator.rb
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

0 comments on commit 2c686ee

Please sign in to comment.