Class: Chef::Attribute::Validator::Check::Required

Inherits:
Chef::Attribute::Validator::Check show all
Defined in:
lib/chef-attribute-validator/checks/required.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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chef-attribute-validator/checks/required.rb', line 15

def check(attrset)
  violations = []
  if check_arg # If we are not required, we're effectively a no-op
    if attrset.empty?
      violations.push Chef::Attribute::Validator::Violation.new(rule_name, path_spec, "No attributes exist for path '#{path_spec}', but this rule says it is required.")
    else
      attrset.each do |path, value|
        if val_scalar?(value)
          if value.nil? 
            violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attributes value is nil, but is required.")
          elsif value == ""
            violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attributes value is '', but is required.")
          end
        else
          if value.size == 0
            violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Attribute has no children, but is required.")       
          end
        end
      end
    end
  end
  violations
end

#validate_check_argObject



9
10
11
12
13
# File 'lib/chef-attribute-validator/checks/required.rb', line 9

def validate_check_arg
  unless check_arg.kind_of?(TrueClass) || check_arg.kind_of?(FalseClass)
    raise "Bad 'required' check argument '#{check_arg}' for rule '#{rule_name}' - expected one of true,false"
  end
end