Class: T::Types::FixedHash
Overview
Takes a hash of types. Validates each item in a hash using the type in the same position in the list.
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Instance Method Summary collapse
-
#describe_obj(obj) ⇒ Object
This gives us better errors, e.g.: ‘Expected String, got TrueClass` instead of `Expected String, got Hash`.
-
#initialize(types) ⇒ FixedHash
constructor
A new instance of FixedHash.
-
#name ⇒ Object
overrides Base.
-
#recursively_valid?(obj) ⇒ Boolean
overrides Base.
-
#valid?(obj) ⇒ Boolean
overrides Base.
Methods inherited from Base
#==, #error_message_for_obj, #error_message_for_obj_recursive, #hash, method_added, #subtype_of?, #to_s, #validate!
Constructor Details
Instance Attribute Details
#types ⇒ Object (readonly)
Returns the value of attribute types.
8 9 10 |
# File 'lib/types/types/fixed_hash.rb', line 8 def types @types end |
Instance Method Details
#describe_obj(obj) ⇒ Object
This gives us better errors, e.g.: ‘Expected String, got TrueClass` instead of `Expected String, got Hash`.
overrides Base
74 75 76 77 78 79 80 |
# File 'lib/types/types/fixed_hash.rb', line 74 def describe_obj(obj) if obj.is_a?(Hash) "type #{serialize_hash(obj.transform_values(&:class))}" else super end end |
#name ⇒ Object
overrides Base
15 16 17 |
# File 'lib/types/types/fixed_hash.rb', line 15 def name serialize_hash(@types) end |
#recursively_valid?(obj) ⇒ Boolean
overrides Base
20 21 22 23 24 25 |
# File 'lib/types/types/fixed_hash.rb', line 20 def recursively_valid?(obj) return false unless obj.is_a?(Hash) return false if @types.any? {|key, type| !type.recursively_valid?(obj[key])} return false if obj.any? {|key, _| !@types[key]} true end |