Puppet Function: simplib::validate_between

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

Overview

simplib::validate_between(Variant[String[1],Numeric] $value, Numeric $min_value, Numeric $max_value)Nil

Validate that the first value is between the second and third values numerically. The range is inclusive.

Terminates catalog compilation if validation fails.

Examples:

Passing

simplib::validate_between('-1', -3, 0)
simplib::validate_between(7, 0, 60)
simplib::validate_between(7.6, 7.1, 8.4)

Parameters:

  • value (Variant[String[1],Numeric])

    Value to validate

  • min_value (Numeric)

    Minimum value that is valid

  • max_value (Numeric)

    Maximum value that is valid

Returns:

  • (Nil)

Raises:

  • (RuntimeError)

    if validation fails



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
# File 'lib/puppet/functions/simplib/validate_between.rb', line 6

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

  # @param value Value to validate
  # @param min_value Minimum value that is valid
  # @param max_value Maximum value that is valid
  # @return [Nil]
  # @raise [RuntimeError] if validation fails
  #
  # @example Passing
  #   simplib::validate_between('-1', -3, 0)
  #   simplib::validate_between(7, 0, 60)
  #   simplib::validate_between(7.6, 7.1, 8.4)
  #
  # #example Failing
  #   simplib::validate_between('-1', 0, 3)
  #   simplib::validate_between(0, 1, 60)
  #   simplib::validate_between(7.6, 7.7, 8.4)
  #
  dispatch :validate_between do
    required_param 'Variant[String[1],Numeric]', :value
    required_param 'Numeric', :min_value
    required_param 'Numeric', :max_value
  end

  def validate_between(value, min_value, max_value)
    numeric_value = value.to_f
    unless numeric_value >= min_value and numeric_value <= max_value
      # The original method was used in SIMP modules as if it raised an
      # exception, so this implementation will work as expected.
      err_msg = "simplib::validate_between: '#{value}' is not between" +
        " '#{min_value}' and '#{max_value}'"
      fail(err_msg)
    end
  end
end