Puppet Function: simplib::validate_re_array

Defined in:
lib/puppet/functions/simplib/validate_re_array.rb
Function type:
Ruby 4.x API

Overview

Perform simple validation of a ‘String`, or `Array` of `Strings`, against one or more regular expressions.

  • Derived from the Puppet Labs stdlib validate_re.

  • Terminates catalog compilation if validation fails.

Signatures:

  • simplib::validate_re_array(String $input, String $regex, Optional[String] $err_msg)Nil

    Examples:

    Passing

    simplib::validate_re_array('one', '^one$')

    Failing

    validate_re_array('one', '^two')

    Custom Error Message

    validate_re_array($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')

    Parameters:

    • input (String)

      String to be validated

    • regex (String)

      Stringified regex expression (regex without the ‘//` delimiters)

    • err_msg (Optional[String])

      Optional error message to emit upon failure

    Returns:

    • (Nil)

    Raises:

    • (RuntimeError)

      if validation fails

  • simplib::validate_re_array(String $input, Array $regex_list, Optional[String] $err_msg)Nil

    Examples:

    Passing

    simplib::validate_re_array('one', [ '^one', '^two' ])

    Failing

    validate_re_array('one', [ '^two', '^three' ])

    Custom Error Message

    $myvar = 'baz'
    validate_re_array($myvar, ['^foo', '^bar'], 'myvar does not begin with foo or bar')

    Parameters:

    • input (String)

      String to be validated

    • regex_list (Array)

      Array of stringified regex expressions ( regexes without the ‘//` delimiters)

    • err_msg (Optional[String])

      Optional error message to emit upon failure

    Returns:

    • (Nil)

    Raises:

    • (RuntimeError)

      if validation fails

  • simplib::validate_re_array(Array $inputs, String $regex, Optional[String] $err_msg)Nil

    Examples:

    Passing

    simplib::validate_re_array(['one-a', 'one-b'], [ '^one', '^two' ])

    Failing

    validate_re_array(['one-a', 'one-b'], [ '^two', '^three' ])

    Custom Error Message

    $myvar = ['hello', 'world']
    validate_re_array($myvar, '^foo', 'myvar elements do not begin with foo')

    Parameters:

    • inputs (Array)

      Array of strings to be validated

    • regex (String)

      Stringified regex expression (regex without the ‘//` delimiters)

    • err_msg (Optional[String])

      Optional error message to emit upon failure

    Returns:

    • (Nil)

    Raises:

    • (RuntimeError)

      if validation fails

  • simplib::validate_re_array(Array $inputs, Array $regex_list, Optional[String] $err_msg)Nil

    Examples:

    Passing

    simplib::validate_re_array(['one-a', 'one-b'], [ '^one', '^two' ])

    Failing

    validate_re_array(['one-a', 'one-b'], [ '^two', '^three' ])

    Custom Error Message

    $myvar = ['hello', 'world']
    validate_re_array($myvar, ['^foo', '^bar'], 'myvar elements do not begin with foo or bar')

    Parameters:

    • inputs (Array)

      Array of strings to be validated

    • regex_list (Array)

      Array of stringified regex expressions ( regexes without the ‘//` delimiters)

    • err_msg (Optional[String])

      Optional error message to emit upon failure

    Returns:

    • (Nil)

    Raises:

    • (RuntimeError)

      if validation fails



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/puppet/functions/simplib/validate_re_array.rb', line 7

Puppet::Functions.create_function(:'simplib::validate_re_array') do

  # @param input String to be validated
  # @param regex Stringified regex expression (regex without the `//`
  #    delimiters)
  #
  # @param err_msg Optional error message to emit upon failure
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   simplib::validate_re_array('one', '^one$')
  #
  # @example Failing
  #   validate_re_array('one', '^two')
  #
  # @example Custom Error Message
  #   validate_re_array($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
  #
  dispatch :validate_re_array_1_to_1 do
    required_param 'String', :input
    required_param 'String', :regex
    optional_param 'String', :err_msg
  end

  # @param input String to be validated
  # @param regex_list Array of stringified regex expressions (
  #    regexes without the `//` delimiters)
  #
  # @param err_msg Optional error message to emit upon failure
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   simplib::validate_re_array('one', [ '^one', '^two' ])
  #
  # @example Failing
  #   validate_re_array('one', [ '^two', '^three' ])
  #
  # @example Custom Error Message
  #   $myvar = 'baz'
  #   validate_re_array($myvar, ['^foo', '^bar'], 'myvar does not begin with foo or bar')
  #
  dispatch :validate_re_array_1_to_n do
    required_param 'String', :input
    required_param 'Array', :regex_list
    optional_param 'String', :err_msg
  end

  # @param inputs Array of strings to be validated
  # @param regex Stringified regex expression (regex without the `//`
  #    delimiters)
  #
  # @param err_msg Optional error message to emit upon failure
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   simplib::validate_re_array(['one-a', 'one-b'], [ '^one', '^two' ])
  #
  # @example Failing
  #   validate_re_array(['one-a', 'one-b'], [ '^two', '^three' ])
  #
  # @example Custom Error Message
  #   $myvar = ['hello', 'world']
  #   validate_re_array($myvar, '^foo', 'myvar elements do not begin with foo')
  #
  dispatch :validate_re_array_n_to_1 do
    required_param 'Array', :inputs
    required_param 'String', :regex
    optional_param 'String', :err_msg
  end

  # @param inputs Array of strings to be validated
  # @param regex_list Array of stringified regex expressions (
  #    regexes without the `//` delimiters)
  # @param err_msg Optional error message to emit upon failure
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   simplib::validate_re_array(['one-a', 'one-b'], [ '^one', '^two' ])
  #
  # @example Failing
  #   validate_re_array(['one-a', 'one-b'], [ '^two', '^three' ])
  #
  # @example Custom Error Message
  #   $myvar = ['hello', 'world']
  #   validate_re_array($myvar, ['^foo', '^bar'], 'myvar elements do not begin with foo or bar')
  #
  dispatch :validate_re_array do
    required_param 'Array', :inputs
    required_param 'Array', :regex_list
    optional_param 'String', :err_msg
  end

  def validate_re_array_1_to_1(input, regex, err_msg=nil)
    validate_re_array([ input ], [ regex ], err_msg)
  end

  def validate_re_array_n_to_1(inputs, regex, err_msg=nil)
    validate_re_array(inputs, [ regex ], err_msg)
  end

  def validate_re_array_1_to_n(input, regex_list, err_msg=nil)
    validate_re_array([ input ], regex_list, err_msg)
  end

  def validate_re_array(inputs, regex_list, err_msg=nil)

    inputs.each do |to_check|
      valid = false
      regex_list.each do |re_str|
        if "#{to_check}" =~ Regexp.compile(re_str)
          valid = true
          break
        end
      end

      # Bail at the first failure.
      unless valid
        msg = err_msg || "simplib::validate_re_array(): #{to_check.inspect} does not match #{regex_list.inspect}"
        fail(msg)
      end
    end

  end
end