Skip to content

Commit b418839

Browse files
committed
(maint) collapse String/Array validation into shared lambda
1 parent 6df05cb commit b418839

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/puppet/parser/functions/validate_slength.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ module Puppet::Parser::Functions
5151
raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
5252
end
5353

54+
validator = lambda do |str|
55+
unless str.length <= max_length and str.length >= min_length
56+
raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}"
57+
end
58+
end
59+
5460
case input
5561
when String
56-
raise Puppet::ParseError, "validate_slength(): #{input.inspect} is #{input.length} characters. It should have been between #{min_length} and #{max_length} characters" unless input.length <= max_length and min_length <= input.length
62+
validator.call(input)
5763
when Array
58-
input.each do |arg|
64+
input.each_with_index do |arg, pos|
5965
if arg.is_a?(String)
60-
unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length)
61-
raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters"
62-
end
66+
validator.call(arg)
6367
else
64-
raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}"
68+
raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}"
6569
end
6670
end
67-
else
68-
raise Puppet::ParseError, "validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{input.class}"
6971
end
7072
end
7173
end

0 commit comments

Comments
 (0)