class EasyJSONMatcher::ValidatorSet

Attributes

strict[RW]
validators[RW]

Public Class Methods

new(validators:, strict: false) click to toggle source
# File lib/easy_json_matcher/validator_set.rb, line 8
def initialize(validators:, strict: false)
  @validators = validators
  @strict = strict
end

Public Instance Methods

call(value:) click to toggle source
# File lib/easy_json_matcher/validator_set.rb, line 13
def call(value:)
  error_hash = validators.each_with_object({}) do |k_v, errors_found|
    key = k_v[0]
    val = value[key.to_s]
    validator = k_v[1]
    results = validator.call(value: val)
    errors_found[key] = results unless results.empty?
  end
  validate_strict_keyset(keys: validators.keys, candidates: value.keys, errors: error_hash) if strict
  if error_hash.empty? then [] else [error_hash] end
end
missing_keys(keys:, errors:, candidates:) click to toggle source
# File lib/easy_json_matcher/validator_set.rb, line 30
def missing_keys(keys:, errors:, candidates:)
  missing = keys - candidates
  errors[:missing_keys] = "Missing keys: #{missing}" unless missing.empty?
end
unexpected_keys(keys:, errors:, candidates:) click to toggle source
# File lib/easy_json_matcher/validator_set.rb, line 35
def unexpected_keys(keys:, errors:, candidates:)
  rogue_keys = candidates - keys
  errors[:unexpected_keys] = "Unexpected keys: #{rogue_keys}" unless rogue_keys.empty?
end
validate_strict_keyset(keys:, errors:, candidates:) click to toggle source
# File lib/easy_json_matcher/validator_set.rb, line 25
def validate_strict_keyset(keys:, errors:, candidates:)
  missing_keys(keys: keys, errors: errors, candidates: candidates)
  unexpected_keys(keys: keys, errors: errors, candidates: candidates)
end