Skip to content

Rewrite validate_email_address() as a Puppet 4.x function #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/puppet/functions/validate_email_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# @summary
# Validate that all values passed are valid email addresses.
# Fail compilation if any value fails this check.
Puppet::Functions.create_function(:validate_email_address) do
# @param values An e-mail address or an array of e-mail addresses to check
#
# @return [Undef]
# Fail compilation if any value fails this check.
#
# @example Passing examples
# $my_email = "waldo@gmail.com"
# validate_email_address($my_email)
# validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email)
#
# @example Failing examples (causing compilation to abort)
# $some_array = [ 'bad_email@/d/efdf.com' ]
# validate_email_address($some_array)
dispatch :validate_email_address do
repeated_param 'Stdlib::Email', :values
end

def validate_email_address(*args)
assert_arg_count(args)
end

def assert_arg_count(args)
raise(ArgumentError, 'validate_email_address(): Wrong number of arguments need at least one') if args.empty?
end
end
45 changes: 0 additions & 45 deletions lib/puppet/parser/functions/validate_email_address.rb

This file was deleted.

16 changes: 8 additions & 8 deletions spec/functions/validate_email_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'validate_email_address' do
describe 'signature validation' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
end

describe 'valid inputs' do
Expand All @@ -14,12 +14,12 @@
end

describe 'invalid inputs' do
it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) }
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) }
it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) }
it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) }
it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) }
it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{got Hash}) }
it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{got Integer}) }
it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{got Boolean}) }
it { is_expected.to run.with_params('one').and_raise_error(ArgumentError, %r{got 'one'}) }
it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(ArgumentError, %r{got Hash}) }
it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(ArgumentError, %r{got Boolean}) }
it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(ArgumentError, %r{got 'one'}) }
end
end