Skip to content

Commit 60864fd

Browse files
committed
Merge pull request #583 from jyaworski/validate_email_address
Add validate_email_address function
2 parents be1ff3f + bfe6cf6 commit 60864fd

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# is_email_address.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:is_email_address, type: :rvalue, doc: <<-EOS
7+
Returns true if the string passed to this function is a valid email address.
8+
EOS
9+
) do |arguments|
10+
if arguments.size != 1
11+
raise(Puppet::ParseError, 'is_email_address(): Wrong number of arguments '\
12+
"given #{arguments.size} for 1")
13+
end
14+
15+
# Taken from http://emailregex.com/ (simpler regex)
16+
valid_email_regex = %r{\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z}
17+
return (arguments[0] =~ valid_email_regex) == 0
18+
end
19+
end
20+
21+
# vim: set ts=2 sw=2 et :
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:validate_email_address, doc: <<-ENDHEREDOC
3+
Validate that all values passed are valid email addresses.
4+
Fail compilation if any value fails this check.
5+
The following values will pass:
6+
$my_email = "[email protected]"
7+
validate_email_address($my_email)
8+
validate_email_address("[email protected]", "[email protected]", $my_email)
9+
10+
The following values will fail, causing compilation to abort:
11+
$some_array = [ 'bad_email@/d/efdf.com' ]
12+
validate_email_address($some_array)
13+
ENDHEREDOC
14+
) do |args|
15+
rescuable_exceptions = [ArgumentError]
16+
17+
if args.empty?
18+
raise Puppet::ParseError, "validate_email_address(): wrong number of arguments (#{args.length}; must be > 0)"
19+
end
20+
21+
args.each do |arg|
22+
raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
23+
24+
begin
25+
raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" unless function_is_email_address([arg])
26+
rescue *rescuable_exceptions
27+
raise Puppet::ParseError, "#{arg.inspect} is not a valid email address"
28+
end
29+
end
30+
end
31+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
3+
describe 'is_email_address' do
4+
it { is_expected.not_to eq(nil) }
5+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
6+
it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
it { is_expected.to run.with_params('[email protected]').and_return(true) }
8+
it { is_expected.to run.with_params('[email protected]').and_return(true) }
9+
it { is_expected.to run.with_params('[email protected]').and_return(true) }
10+
it { is_expected.to run.with_params('1.2.3@domain').and_return(false) }
11+
it { is_expected.to run.with_params('1.2.3.4.5@').and_return(false) }
12+
it { is_expected.to run.with_params({}).and_return(false) }
13+
it { is_expected.to run.with_params([]).and_return(false) }
14+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe 'validate_email_address' do
4+
describe 'signature validation' do
5+
it { is_expected.not_to eq(nil) }
6+
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
7+
end
8+
9+
describe 'valid inputs' do
10+
it { is_expected.to run.with_params('[email protected]') }
11+
it { is_expected.to run.with_params('[email protected]') }
12+
end
13+
14+
describe 'invalid inputs' do
15+
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
16+
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
17+
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
18+
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid email/) }
19+
it { is_expected.to run.with_params('[email protected]', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
20+
it { is_expected.to run.with_params('[email protected]', true).and_raise_error(Puppet::ParseError, /is not a string/) }
21+
it { is_expected.to run.with_params('[email protected]', 'one').and_raise_error(Puppet::ParseError, /is not a valid email/) }
22+
end
23+
end

0 commit comments

Comments
 (0)