Skip to content

Commit

Permalink
Merge pull request #461 from DavidS/validate-hashes
Browse files Browse the repository at this point in the history
validate_integer, validate_numeric: explicitely reject hashes in arrays
  • Loading branch information
tphoney committed May 28, 2015
2 parents b409018 + cf9f7a6 commit c9b810c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/puppet/parser/functions/validate_integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ module Puppet::Parser::Functions
# check every element of the array
input.each_with_index do |arg, pos|
begin
raise TypeError if arg.is_a?(Hash)
arg = Integer(arg.to_s)
validator.call(arg)
rescue TypeError, ArgumentError
Expand Down
1 change: 1 addition & 0 deletions lib/puppet/parser/functions/validate_numeric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module Puppet::Parser::Functions
# check every element of the array
input.each_with_index do |arg, pos|
begin
raise TypeError if arg.is_a?(Hash)
arg = Float(arg.to_s)
validator.call(arg)
rescue TypeError, ArgumentError
Expand Down
5 changes: 5 additions & 0 deletions spec/functions/validate_integer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer or Array/)
end

it "should not compile when a Hash is passed as Array" do
Puppet[:code] = "validate_integer([{ 1 => 2 }])"
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be an Integer/)
end

it "should not compile when an explicitly undef variable is passed" do
Puppet[:code] = <<-'ENDofPUPPETcode'
$foo = undef
Expand Down
5 changes: 5 additions & 0 deletions spec/functions/validate_numeric_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric or Array/)
end

it "should not compile when a Hash is passed in an Array" do
Puppet[:code] = "validate_numeric([{ 1 => 2 }])"
expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /to be a Numeric/)
end

it "should not compile when an explicitly undef variable is passed" do
Puppet[:code] = <<-'ENDofPUPPETcode'
$foo = undef
Expand Down

0 comments on commit c9b810c

Please sign in to comment.