Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_slength.rb
1 #
2 # validate_slength.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:validate_slength, :doc => <<-DOC
6     @summary
7       Validate that the first argument is a string (or an array of strings), and less/equal to than the length of the second argument.
8       An optional third parameter can be given the minimum length. It fails if the first argument is not a string or array of strings,
9       and if arg 2 and arg 3 are not convertable to a number.
10
11     @return
12       validate that the first argument is a string (or an array of strings), and less/equal to than the length of the second argument. Fail compilation if any of the checks fail.
13
14     @example **Usage**
15       The following values will pass:
16
17         validate_slength("discombobulate",17)
18         validate_slength(["discombobulate","moo"],17)
19         validate_slength(["discombobulate","moo"],17,3)
20
21       The following valueis will not:
22
23         validate_slength("discombobulate",1)
24         validate_slength(["discombobulate","thermometer"],5)
25         validate_slength(["discombobulate","moo"],17,10)
26     DOC
27              ) do |args|
28     function_deprecation([:validate_slength, 'This method is deprecated, please use the stdlib validate_legacy function,
29                             with String[]. There is further documentation for validate_legacy function in the README.'])
30
31     raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 || args.length == 3
32
33     input, max_length, min_length = *args
34
35     begin
36       max_length = Integer(max_length)
37       raise ArgumentError if max_length <= 0
38     rescue ArgumentError, TypeError
39       raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got #{max_length}:#{max_length.class}"
40     end
41
42     if min_length
43       begin
44         min_length = Integer(min_length)
45         raise ArgumentError if min_length < 0
46       rescue ArgumentError, TypeError
47         raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got #{min_length}:#{min_length.class}"
48       end
49     else
50       min_length = 0
51     end
52
53     raise Puppet::ParseError, 'validate_slength(): Expected second argument to be equal to or larger than third argument' unless max_length >= min_length
54
55     validator = ->(str) do
56       unless str.length <= max_length && str.length >= min_length
57         raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}"
58       end
59     end
60
61     case input
62     when String
63       validator.call(input)
64     when Array
65       input.each_with_index do |arg, pos|
66         raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got #{arg.class}" unless arg.is_a? String
67         validator.call(arg)
68       end
69     else
70       raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got #{input.class}"
71     end
72   end
73 end