Puppet Function: validate_array_member

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

Overview

validate_array_member()Boolean

Validate that the first String (or Array) passed is a member of the second Array passed. An optional third argument of i can be passed, which ignores the case of the objects inside the Array.

Examples:


validate_array_member('foo',['foo','bar'])     # => true
validate_array_member('foo',['FOO','BAR'])     # => false

#Optional 'i' as third object, ignoring case of FOO and BAR#

validate_array_member('foo',['FOO','BAR'],'i') # => true

Returns:

  • (Boolean)


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/parser/functions/validate_array_member.rb', line 3

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

  unless args.length == 2 or args.length == 3
  Validate that the first `String` (or `Array`) passed is a member of the
  second `Array` passed. An optional third argument of i can be passed,
  which ignores the case of the objects inside the `Array`.

  @example

    validate_array_member('foo',['foo','bar'])     # => true
    validate_array_member('foo',['FOO','BAR'])     # => false

    #Optional 'i' as third object, ignoring case of FOO and BAR#

    validate_array_member('foo',['FOO','BAR'],'i') # => true

  @return [Boolean]
  ENDHEREDOC
    raise Puppet::ParseError, ("validate_array_member(): expects two or three arguments. Got '#{args.length}'")
  end

  to_compare = Array(args[0]).dup
  target_array = Array(args[1]).dup
  modifier = args[2]

  if modifier then
    if modifier == 'i' then
      to_compare.map!{|x|
        if x.is_a?(String) then
          x = x.downcase
        else
          x = x
        end
        x
      }
      target_array.map!{|x|
        if x.is_a?(String) then
          x = x.downcase
        else
          x = x
        end
        x
      }
    end
  end

  unless (to_compare - target_array).empty?
    raise Puppet::ParseError, ("validate_array_member(): '#{Array(args[1]).join(',')}' does not contain '#{Array(args[0]).join(',')}'.")
  end

end