Module: Paramore::Validate

Defined in:
lib/paramore/validate.rb

Class Method Summary collapse

Class Method Details

.hash_types(hash) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/paramore/validate.rb', line 28

def hash_types(hash)
  validate_wildly_keyed_hash!(hash)

  hash.flat_map do |param_name, field|
    raise Paramore::NonField.new(param_name, field) unless field.is_a?(Paramore::Field)

    field.type.is_a?(Hash) ? types(field.type) : field.type
  end.uniq
end

.run(root_field) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/paramore/validate.rb', line 5

def run(root_field)
  types(root_field.type).uniq.each do |type|
    unless type.respond_to?(Paramore.configuration.type_method_name)
      raise NoMethodError,
        "Paramore: type `#{type}` does not respond to " +
        "`#{Paramore.configuration.type_method_name}`!"
    end
  end

  root_field
end

.types(type) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/paramore/validate.rb', line 17

def types(type)
  case type
  when Hash
    hash_types(type)
  when Array
    type.flat_map { |subtype| types(subtype) }
  else
    [type]
  end
end

.validate_wildly_keyed_hash!(hash) ⇒ Object



38
39
40
41
42
# File 'lib/paramore/validate.rb', line 38

def validate_wildly_keyed_hash!(hash)
  if Paramore::Field.wildly_keyed_hash?(hash) && hash.keys.map(&:class).count > 1
    raise Paramore::HashTooWild.new(hash)
  end
end

.wildly_keyed_hash?(hash) ⇒ Boolean

Returns:



44
45
46
# File 'lib/paramore/validate.rb', line 44

def wildly_keyed_hash?(hash)
  [Class, Module].include?(hash.keys.first.class)
end