Module: CC::Config::Validation::HashValidations

Included in:
CheckValidator, EngineValidator, FetchValidator, FileValidator, PrepareValidator
Defined in:
lib/cc/config/validation/hash_validations.rb

Instance Method Summary collapse

Instance Method Details

#key_type_error_message(key, types) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cc/config/validation/hash_validations.rb', line 25

def key_type_error_message(key, types)
  if types.one?
    klass_name = types[0].to_s.downcase
    article =
      if klass_name[0] == "a"
        "an"
      else
        "a"
      end
    "'#{key}' must be #{article} #{klass_name}"
  elsif types == [TrueClass, FalseClass]
    "'#{key}' must be a boolean"
  else
    type_names = types.map(&:to_s).map(&:downcase)
    "'#{key}' must be one of #{type_names.join(", ")}"
  end
end

#validate_hash_dataObject



5
6
7
8
9
10
11
# File 'lib/cc/config/validation/hash_validations.rb', line 5

def validate_hash_data
  unless data.is_a?(Hash)
    errors << "Config file should contain a hash, not a #{data.class.to_s.downcase}"
    return false
  end
  true
end

#validate_key_type(key, types) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cc/config/validation/hash_validations.rb', line 13

def validate_key_type(key, types)
  if types.is_a?(Class)
    return validate_key_type(key, [types])
  elsif data.key?(key)
    unless types.include?(data[key].class)
      errors << key_type_error_message(key, types)
      return false
    end
  end
  true
end

#warn_unrecognized_keys(recognized_keys) ⇒ Object



43
44
45
46
47
48
# File 'lib/cc/config/validation/hash_validations.rb', line 43

def warn_unrecognized_keys(recognized_keys)
  unknown_keys = data.keys.reject { |k| recognized_keys.include?(k) }
  unknown_keys.each do |key|
    warnings << "unrecognized key '#{key}'"
  end
end