Skip to content

Commit 07c38e5

Browse files
author
Morgan Haskel
committed
Merge pull request puppetlabs#482 from DavidS/document_validate_slength_3rd_arg
Add validate_slength's optional 3rd arg to README
2 parents b2aed66 + c64ecfb commit 07c38e5

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

README.markdown

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,20 +938,22 @@ test, and the second argument should be a stringified regular expression (withou
938938

939939
#### `validate_slength`
940940

941-
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.
941+
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.
942942

943943
The following values pass:
944944

945945
~~~
946946
validate_slength("discombobulate",17)
947947
validate_slength(["discombobulate","moo"],17)
948+
validate_slength(["discombobulate","moo"],17,3)
948949
~~~
949950

950951
The following values fail:
951952

952953
~~~
953954
validate_slength("discombobulate",1)
954955
validate_slength(["discombobulate","thermometer"],5)
956+
validate_slength(["discombobulate","moo"],17,10)
955957
~~~
956958

957959
*Type*: statement.

lib/puppet/parser/functions/validate_slength.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Puppet::Parser::Functions
33
newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args|
44
Validate that the first argument is a string (or an array of strings), and
55
less/equal to than the length of the second argument. An optional third
6-
parameter can be given a the minimum length. It fails if the first
6+
parameter can be given the minimum length. It fails if the first
77
argument is not a string or array of strings, and if arg 2 and arg 3 are
88
not convertable to a number.
99
@@ -43,9 +43,7 @@ module Puppet::Parser::Functions
4343
min_length = 0
4444
end
4545

46-
if min_length > max_length
47-
raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
48-
end
46+
raise Puppet::ParseError, "validate_slength(): Expected second argument to be equal to or larger than third argument" unless max_length >= min_length
4947

5048
validator = lambda do |str|
5149
unless str.length <= max_length and str.length >= min_length

spec/functions/validate_slength_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
1111
it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
1212
it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
13-
it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be larger than third argument/) }
13+
it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be equal to or larger than third argument/) }
1414
end
1515

1616
context "with a maximum length of 10" do

0 commit comments

Comments
 (0)