Class: Ree::Contracts::HashValidator
- Inherits:
-
BaseValidator
- Object
- BaseValidator
- Ree::Contracts::HashValidator
- Defined in:
- lib/ree/contracts/validators/hash_validator.rb
Instance Attribute Summary collapse
-
#opt_dict ⇒ Object
readonly
Returns the value of attribute opt_dict.
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
Attributes inherited from BaseValidator
Instance Method Summary collapse
- #call(value) ⇒ Object
-
#initialize(contract) ⇒ HashValidator
constructor
A new instance of HashValidator.
- #message(value, name, lvl = 1) ⇒ Object
- #to_s ⇒ Object
Methods included from Truncatable
Constructor Details
#initialize(contract) ⇒ HashValidator
Returns a new instance of HashValidator.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 9 def initialize(contract) super(contract) @opt_dict = Set.new @validators = contract .transform_values { |cont| Validators.fetch_for(cont) } .transform_keys { |key| next key unless key.is_a?(String) || key.is_a?(Symbol) key_str = key.to_s next key unless key_str.end_with?('?') && key_str.length > 1 opt_key = key_str[0..-2] opt_key = opt_key.to_sym if key.is_a? Symbol @opt_dict << opt_key opt_key } end |
Instance Attribute Details
#opt_dict ⇒ Object (readonly)
Returns the value of attribute opt_dict.
7 8 9 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 7 def opt_dict @opt_dict end |
#validators ⇒ Object (readonly)
Returns the value of attribute validators.
7 8 9 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 7 def validators @validators end |
Instance Method Details
#call(value) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 30 def call(value) return false unless value.is_a?(Hash) return false unless value.all? { |key, _| validators.has_key?(key) } validators.all? do |key, validator| value.has_key?(key) ? validator.call(value[key]) : optional?(key) end end |
#message(value, name, lvl = 1) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 43 def (value, name, lvl = 1) unless value.is_a?(Hash) return "expected Hash, got #{value.class} => #{truncate(value.inspect)}" end errors = [] sps = " " * lvl validators.each do |key, validator| if errors.size > 3 errors << "\n\t#{sps} - ..." break end unless value.key?(key) errors << "\n\t#{sps} - #{name}[#{key.inspect}]: missing" unless optional?(key) next end val = value[key] next if validator.call(val) msg = validator.(val, "#{name}[#{key.inspect}]", lvl + 1) errors << "\n\t#{sps} - #{name}[#{key.inspect}]: #{msg}" end value.each do |key, val| if errors.size > 3 errors << "\n\t#{sps} - ..." break end next if validators.key?(key) errors << "\n\t#{sps} - #{name}[#{key.inspect}]: unexpected" end errors.join end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/ree/contracts/validators/hash_validator.rb', line 39 def to_s "{#{validators.map { |k, v| "#{key_to_s(k)} => #{v.to_s}" }.join(', ')}}" end |