Skip to content

Commit b4602ac

Browse files
committed
Rewrite validate_email_address() as a Puppet 4.x function
The 3.x function rely on is_email_address() which is deprecated. Rewrite it using the more modern puppet 4.x function to rely on data types for better parameters validation.
1 parent a3abe3d commit b4602ac

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# Validate that all values passed are valid email addresses.
5+
# Fail compilation if any value fails this check.
6+
Puppet::Functions.create_function(:validate_email_address) do
7+
# @param values An e-mail address or an array of e-mail addresses to check
8+
#
9+
# @return [Undef]
10+
# Fail compilation if any value fails this check.
11+
#
12+
# @example Passing examples
13+
# $my_email = "waldo@gmail.com"
14+
# validate_email_address($my_email)
15+
# validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email)
16+
#
17+
# @example Failing examples (causing compilation to abort)
18+
# $some_array = [ 'bad_email@/d/efdf.com' ]
19+
# validate_email_address($some_array)
20+
dispatch :validate_email_address do
21+
repeated_param 'Stdlib::Email', :values
22+
end
23+
24+
def validate_email_address(*args)
25+
assert_arg_count(args)
26+
end
27+
28+
def assert_arg_count(args)
29+
raise(ArgumentError, 'validate_email_address(): Wrong number of arguments need at least one') if args.empty?
30+
end
31+
end

lib/puppet/parser/functions/validate_email_address.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

spec/functions/validate_email_address_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
describe 'validate_email_address' do
66
describe 'signature validation' do
77
it { is_expected.not_to eq(nil) }
8-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
8+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
99
end
1010

1111
describe 'valid inputs' do
@@ -14,12 +14,12 @@
1414
end
1515

1616
describe 'invalid inputs' do
17-
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
18-
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) }
19-
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
20-
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) }
21-
it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
22-
it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
23-
it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) }
17+
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{got Hash}) }
18+
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{got Integer}) }
19+
it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{got Boolean}) }
20+
it { is_expected.to run.with_params('one').and_raise_error(ArgumentError, %r{got 'one'}) }
21+
it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(ArgumentError, %r{got Hash}) }
22+
it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(ArgumentError, %r{got Boolean}) }
23+
it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(ArgumentError, %r{got 'one'}) }
2424
end
2525
end

0 commit comments

Comments
 (0)