lib/json_matchers/matcher.rb in json_matchers-0.9.0 vs lib/json_matchers/matcher.rb in json_matchers-0.10.0

- old
+ new

@@ -1,45 +1,42 @@ -require "json-schema" +require "json_schema" +require "json_matchers/parser" require "json_matchers/validator" module JsonMatchers class Matcher - def initialize(schema_path, options = {}) + def initialize(schema_path) @schema_path = schema_path - @options = default_options.merge(options) + @document_store = build_and_populate_document_store end def matches?(payload) - validator = build_validator(payload) + self.errors = validator.validate(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 + attr_reader :document_store, :schema_path - def default_options - JsonMatchers.configuration.options || {} + def validator + Validator.new(schema_path: schema_path, document_store: document_store) end - def build_validator(payload) - Validator.new( - options: options, - payload: payload, - schema_path: schema_path, - ) + 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 }. + each { |schema| document_store.add_schema(schema) } + + document_store end end end