Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_numeric.rb
1 #
2 # validate_numeric.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:validate_numeric, :doc => <<-'DOC') do |args|
6     Validate that the first argument is a numeric value (or an array of numeric values). Abort catalog compilation if any of the checks fail.
7
8     The second argument is optional and passes a maximum. (All elements of) the first argument has to be less or equal to this max.
9
10     The third argument is optional and passes a minimum.  (All elements of) the first argument has to be greater or equal to this min.
11     If, and only if, a minimum is given, the second argument may be an empty string or undef, which will be handled to just check
12     if (all elements of) the first argument are greater or equal to the given minimum.
13
14     It will fail if the first argument is not a numeric (Integer or Float) or array of numerics, and if arg 2 and arg 3 are not convertable to a numeric.
15
16     For passing and failing usage, see `validate_integer()`. It is all the same for validate_numeric, yet now floating point values are allowed, too.
17
18     DOC
19
20     function_deprecation([:validate_numeric, 'This method is deprecated, please use the stdlib validate_legacy function,
21                             with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.'])
22
23     # tell the user we need at least one, and optionally up to two other parameters
24     raise Puppet::ParseError, "validate_numeric(): Wrong number of arguments; must be 1, 2 or 3, got #{args.length}" unless !args.empty? && args.length < 4
25
26     input, max, min = *args
27
28     # check maximum parameter
29     if args.length > 1
30       max = max.to_s
31       # allow max to be empty (or undefined) if we have a minimum set
32       if args.length > 2 && max == ''
33         max = nil
34       else
35         begin
36           max = Float(max)
37         rescue TypeError, ArgumentError
38           raise Puppet::ParseError, "validate_numeric(): Expected second argument to be unset or a Numeric, got #{max}:#{max.class}"
39         end
40       end
41     else
42       max = nil
43     end
44
45     # check minimum parameter
46     if args.length > 2
47       begin
48         min = Float(min.to_s)
49       rescue TypeError, ArgumentError
50         raise Puppet::ParseError, "validate_numeric(): Expected third argument to be unset or a Numeric, got #{min}:#{min.class}"
51       end
52     else
53       min = nil
54     end
55
56     # ensure that min < max
57     if min && max && min > max
58       raise Puppet::ParseError, "validate_numeric(): Expected second argument to be larger than third argument, got #{max} < #{min}"
59     end
60
61     # create lamba validator function
62     validator = ->(num) do
63       # check input < max
64       if max && num > max
65         raise Puppet::ParseError, "validate_numeric(): Expected #{input.inspect} to be smaller or equal to #{max}, got #{input.inspect}."
66       end
67       # check input > min (this will only be checked if no exception has been raised before)
68       if min && num < min
69         raise Puppet::ParseError, "validate_numeric(): Expected #{input.inspect} to be greater or equal to #{min}, got #{input.inspect}."
70       end
71     end
72
73     # if this is an array, handle it.
74     case input
75     when Array
76       # check every element of the array
77       input.each_with_index do |arg, pos|
78         begin
79           raise TypeError if arg.is_a?(Hash)
80           arg = Float(arg.to_s)
81           validator.call(arg)
82         rescue TypeError, ArgumentError
83           raise Puppet::ParseError, "validate_numeric(): Expected element at array position #{pos} to be a Numeric, got #{arg.class}"
84         end
85       end
86     # for the sake of compatibility with ruby 1.8, we need extra handling of hashes
87     when Hash
88       raise Puppet::ParseError, "validate_integer(): Expected first argument to be a Numeric or Array, got #{input.class}"
89     # check the input. this will also fail any stuff other than pure, shiny integers
90     else
91       begin
92         input = Float(input.to_s)
93         validator.call(input)
94       rescue TypeError, ArgumentError
95         raise Puppet::ParseError, "validate_numeric(): Expected first argument to be a Numeric or Array, got #{input.class}"
96       end
97     end
98   end
99 end