Puppet Function: validate_array_of_hashes

Defined in:
lib/puppet/parser/functions/validate_array_of_hashes.rb
Function type:
Ruby 3.x API

Overview

validate_array_of_hashes()Boolean

Validate that the passed argument is either an empty Array or an Array that only contains Hashes.

Examples:


validate_array_of_hashes([{'foo' => 'bar'}])  => OK
validate_array_of_hashes([])                  => OK
validate_array_of_hashes(['FOO','BAR'])       => BAD

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
10
11
12
13
14
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/puppet/parser/functions/validate_array_of_hashes.rb', line 2

newfunction(:validate_array_of_hashes, :doc => <<-'ENDHEREDOC') do |args|

  unless args.length == 1
  Validate that the passed argument is either an empty `Array` or an
  `Array` that only contains `Hashes`.

  @example

    validate_array_of_hashes([{'foo' => 'bar'}])  => OK
    validate_array_of_hashes([])                  => OK
    validate_array_of_hashes(['FOO','BAR'])       => BAD

  @return [Boolean]
  ENDHEREDOC
    raise Puppet::ParseError, ("validate_array_of_hashes(): expects exactly one argument. Got '#{args.length}'")
  end

  valid = true
  to_check = args[0]

  if not to_check.is_a?(Array) then
    valid = false
  else
    to_check.each do |entry|
      next if entry.is_a?(Hash)

      valid = false
      break
    end
  end

  unless valid
    require 'pp'
    raise Puppet::ParseError, ("validate_array_of_hashes(): '#{PP.singleline_pp(to_check,"")}' is not an array of hashes.")
  end
end