Module: Chef::Validation::Validator

Defined in:
lib/chef/validation/validator.rb

Class Method Summary collapse

Class Method Details

.run(node, metadata) ⇒ Hash

Validates that the given node object satisfies all of the attribute constraints found in the given metadata.

Returns a hash containing key/value pairs where the keys are the name of an attribute which was not properly set on the node object and the values are errors that were generated for that attribute.

Parameters:

  • node (Chef::Node)
  • metadata (Chef::Metadata)

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/chef/validation/validator.rb', line 15

def run(node, )
  errors = {}
  .attributes.each do |attribute, rules|
    unless (err = validate(node, attribute, rules)).empty?
      errors[attribute] = err
    end
  end
  errors
end

.validate(node, name, rules) ⇒ Array<String>

Validates that the given node object passes the given validation rules for the given attribute name.

Parameters:

  • node (Chef::Node)

    node to validate

  • name (String)

    name of the attribute to validate

  • rules (Hash)

    a hash of rules (defined by the metadata of a cookbook)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chef/validation/validator.rb', line 36

def validate(node, name, rules)
  value  = HashExt.dig(node.attributes, name, ATTR_SEPARATOR)
  errors = []

  if rules["recipes"].present?
    if rules["recipes"].select { |recipe| recipe_present?(node, recipe) }.empty?
      return errors
    end
  end
  if rules["required"].present?
    errors += validate_required(rules["required"], value)
  end
  if rules["type"].present?
    errors += validate_type(value, rules["type"], name)
  end
  if rules["choice"].present?
    errors += validate_choice(value, rules["choice"], name)
  end

  errors
end