Class: Chef::Attribute::Validator::Check::ChildKeys

Inherits:
Chef::Attribute::Validator::Check show all
Defined in:
lib/chef-attribute-validator/checks/child_keys.rb

Instance Attribute Summary

Attributes inherited from Chef::Attribute::Validator::Check

#check_arg, #path_spec, #rule_name

Instance Method Summary collapse

Methods inherited from Chef::Attribute::Validator::Check

#initialize, list_check_types, make

Constructor Details

This class inherits a constructor from Chef::Attribute::Validator::Check

Instance Method Details

#check(attrset) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chef-attribute-validator/checks/child_keys.rb', line 19

def check(attrset)
  violations = []
  attrset.each do |path, value|
    # Must be hashlike
    if [ Mash, Hash ].any? { |k| value.kind_of?(k) }
      value.keys.each do |key|
        if check_arg.kind_of?(Array)
          unless check_arg.include?(key)
            violations.push Chef::Attribute::Validator::Violation.new(rule_name, path + '/' + key, "Child Key #{key} must be one of #{check_arg.join(', ')}.")                      
          end
        elsif check_arg.kind_of?(Regexp)
          unless check_arg.match(key)
            violations.push Chef::Attribute::Validator::Violation.new(rule_name, path + '/' + key, "Child Key #{key} does not match regex '#{check_arg}'")
          end
        end
      end
    else
      violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute's value is '#{value}', but must be a Hash-like thing to use child_keys")
    end
  end
  violations
end

#validate_check_argObject



9
10
11
12
13
14
15
16
17
# File 'lib/chef-attribute-validator/checks/child_keys.rb', line 9

def validate_check_arg
  if check_arg.kind_of?(Array)
    unless check_arg.all? { |e| e.kind_of?(String) } 
      raise "Bad 'child_keys' check argument '#{check_arg}' for rule '#{rule_name}' - expected a Regexp or an Array of Strings."
    end
  elsif ! check_arg.kind_of?(Regexp)
    raise "Bad 'child_keys' check argument '#{check_arg}' for rule '#{rule_name}' - expected a Regexp or an Array of Strings."
  end
end