Sha256: 4d4f0882a446a55203e61227a09a6e6bd822dcd41d91e55eca15e2bc07254452

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require "json_schema"
require "json_matchers/parser"
require "json_matchers/validator"

module JsonMatchers
  class Matcher
    def initialize(schema_path)
      @schema_path = schema_path
      @document_store = build_and_populate_document_store
    end

    def matches?(payload)
      self.errors = validator.validate(payload)

      errors.empty?
    end

    def validation_failure_message
      errors.first.to_s
    end

    private

    attr_accessor :errors
    attr_reader :document_store, :schema_path

    def validator
      Validator.new(schema_path: schema_path, document_store: document_store)
    end

    def build_and_populate_document_store
      document_store = JsonSchema::DocumentStore.new

      Dir.glob("#{JsonMatchers.schema_root}/**/*.json").
        map { |path| Pathname.new(path) }.
        map { |schema_path| Parser.new(schema_path).parse }.
        map { |schema| document_store.add_schema(schema) }.
        each { |schema| schema.expand_references!(store: document_store) }

      document_store
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_matchers-0.11.0 lib/json_matchers/matcher.rb