diff options
author | Adrien Thebo <git@somethingsinistral.net> | 2013-08-12 11:50:55 -0700 |
---|---|---|
committer | Adrien Thebo <git@somethingsinistral.net> | 2013-08-12 12:55:46 -0700 |
commit | b41883933c1dbf1a3c9fa7fce65e273246b474ee (patch) | |
tree | a9bbae639a5f403d34205d72f16b1f6884433fb9 | |
parent | 6df05cbc2d6d4d85774de7ca16f1fc557f69516d (diff) | |
download | puppet-stdlib-b41883933c1dbf1a3c9fa7fce65e273246b474ee.tar.gz puppet-stdlib-b41883933c1dbf1a3c9fa7fce65e273246b474ee.tar.bz2 |
(maint) collapse String/Array validation into shared lambda
-rw-r--r-- | lib/puppet/parser/functions/validate_slength.rb | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb index 83c7ed0..259df5a 100644 --- a/lib/puppet/parser/functions/validate_slength.rb +++ b/lib/puppet/parser/functions/validate_slength.rb @@ -51,21 +51,23 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument" end + validator = lambda do |str| + unless str.length <= max_length and str.length >= min_length + raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}" + end + end + case input when String - 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 + validator.call(input) when Array - input.each do |arg| + input.each_with_index do |arg, pos| if arg.is_a?(String) - unless ( arg.is_a?(String) and arg.length <= max_length and min_length <= arg.length) - raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been between #{min_length} and #{max_length} characters" - end + validator.call(arg) else - raise Puppet::ParseError, "validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}" + raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got a #{arg.class}" end end - else - 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}" end end end |