Sha256: adb96e24df1a175163e275eade1868317dd21f1627a020f27c8ef1026679fc13

Contents?: true

Size: 931 Bytes

Versions: 2

Compression:

Stored size: 931 Bytes

Contents

require "json-schema"
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?(payload)
      validator = build_validator(payload)

      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
      errors.first.to_s
    end

    private

    attr_reader :schema_path, :options
    attr_accessor :errors

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

    def build_validator(payload)
      Validator.new(
        options: options,
        payload: payload,
        schema_path: schema_path,
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_matchers-0.9.0 lib/json_matchers/matcher.rb
json_matchers-0.8.0 lib/json_matchers/matcher.rb