Class: Restspec::Schema::Types::HashType

Inherits:
BasicType
  • Object
show all
Defined in:
lib/restspec/schema/types/hash_type.rb

Instance Method Summary collapse

Methods inherited from BasicType

#initialize, #of, #totally_valid?, #|

Constructor Details

This class inherits a constructor from Restspec::Schema::Types::BasicType

Instance Method Details

#example_for(attribute) ⇒ Object



3
4
5
# File 'lib/restspec/schema/types/hash_type.rb', line 3

def example_for(attribute)
  {}
end

#valid?(attribute, value) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/restspec/schema/types/hash_type.rb', line 7

def valid?(attribute, value)
  is_hash = value.is_a?(Hash)
  keys = schema_options.fetch(:keys, [])

  if keys.empty?
    is_hash
  else
    value_type = schema_options.fetch(:value_type, nil)

    is_hash && keys.all? do |key|
      has_key = value.has_key?(key)
      if has_key && value_type
        has_key && value_type.totally_valid?(attribute, value[key])
      end
    end
  end
end