]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Add validate_slength's optional 3rd arg to README
authorAlexander Fisher <alex@linfratech.co.uk>
Mon, 6 Jul 2015 16:03:49 +0000 (17:03 +0100)
committerDavid Schmitt <david.schmitt@puppetlabs.com>
Thu, 9 Jul 2015 17:13:19 +0000 (18:13 +0100)
README.markdown
lib/puppet/parser/functions/validate_slength.rb
spec/functions/validate_slength_spec.rb

index 8ed3d9b2492500ee9fc6bd74d7f0e840a100b471..4dcdc2e2c3fafef03b91250e6d63e319d18e80f9 100644 (file)
@@ -938,13 +938,14 @@ test, and the second argument should be a stringified regular expression (withou
 
 #### `validate_slength`
 
-Validates that the first argument is a string (or an array of strings), and is less than or equal to the length of the second argument. It fails if the first argument is not a string or array of strings, or if arg 2 is not convertable to a number.
+Validates that the first argument is a string (or an array of strings), and is less than or equal to the length of the second argument. It fails if the first argument is not a string or array of strings, or if arg 2 is not convertable to a number.  Optionally, a minimum string length can be given as the third argument.
 
   The following values pass:
 
   ~~~
   validate_slength("discombobulate",17)
   validate_slength(["discombobulate","moo"],17)
+  validate_slength(["discombobulate","moo"],17,3)
   ~~~
 
   The following values fail:
@@ -952,6 +953,7 @@ Validates that the first argument is a string (or an array of strings), and is l
   ~~~
   validate_slength("discombobulate",1)
   validate_slength(["discombobulate","thermometer"],5)
+  validate_slength(["discombobulate","moo"],17,10)
   ~~~
 
 *Type*: statement.
index 7d534f3703e623a1224a35d74259a127d95fc325..47c7d4a6c272bab0c4d7415f6bf9695bcff33b66 100644 (file)
@@ -3,7 +3,7 @@ module Puppet::Parser::Functions
   newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args|
     Validate that the first argument is a string (or an array of strings), and
     less/equal to than the length of the second argument. An optional third
-    parameter can be given the minimum length. It fails if the first
+    parameter can be given the minimum length. It fails if the first
     argument is not a string or array of strings, and if arg 2 and arg 3 are
     not convertable to a number.
 
@@ -43,9 +43,7 @@ module Puppet::Parser::Functions
       min_length = 0
     end
 
-    if min_length > max_length
-      raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
-    end
+    raise Puppet::ParseError, "validate_slength(): Expected second argument to be equal to or larger than third argument" unless max_length >= min_length
 
     validator = lambda do |str|
       unless str.length <= max_length and str.length >= min_length
index 391f83a2ab411d0917af3afb1afd5866ae6c3b82..5a8fa6a840269bbb8b0d23693467afe0e91c0b77 100755 (executable)
@@ -10,7 +10,7 @@ describe 'validate_slength' do
     it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
     it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
     it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
-    it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be larger than third argument/) }
+    it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be equal to or larger than third argument/) }
   end
 
   context "with a maximum length of 10" do