Class: EasyJSONMatcher::ValidatorSet

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_json_matcher/validator_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validators:, strict: false) ⇒ ValidatorSet

Returns a new instance of ValidatorSet.



8
9
10
11
# File 'lib/easy_json_matcher/validator_set.rb', line 8

def initialize(validators:, strict: false)
  @validators = validators
  @strict = strict
end

Instance Attribute Details

#strictObject

Returns the value of attribute strict.



6
7
8
# File 'lib/easy_json_matcher/validator_set.rb', line 6

def strict
  @strict
end

#validatorsObject

Returns the value of attribute validators.



6
7
8
# File 'lib/easy_json_matcher/validator_set.rb', line 6

def validators
  @validators
end

Instance Method Details

#call(value:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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]
    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
  errors = []
  errors << error_hash unless error_hash.empty?
  errors
end

#validate_strict_keyset(keys:, errors:, candidates:) ⇒ Object



27
28
29
30
# File 'lib/easy_json_matcher/validator_set.rb', line 27

def validate_strict_keyset(keys:, errors:, candidates:)
  rogue_keys = candidates - keys
  errors[:unexpected_keys] = "Unexpected keys: #{rogue_keys}" unless rogue_keys.empty?
end