lib/json_matchers/matcher.rb in json_matchers-0.6.3 vs lib/json_matchers/matcher.rb in json_matchers-0.7.0
- old
+ new
@@ -1,36 +1,45 @@
require "json-schema"
-require "json_matchers/payload"
+require "json_matchers/validator"
module JsonMatchers
class Matcher
def initialize(schema_path, options = {})
@schema_path = schema_path
@options = default_options.merge(options)
end
def matches?(response)
- JSON::Validator.validate!(
- schema_path.to_s,
- Payload.new(response).to_s,
- options,
- )
- 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