From b41883933c1dbf1a3c9fa7fce65e273246b474ee Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Mon, 12 Aug 2013 11:50:55 -0700 Subject: (maint) collapse String/Array validation into shared lambda --- lib/puppet/parser/functions/validate_slength.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'lib/puppet/parser/functions/validate_slength.rb') 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 -- cgit v1.2.3