Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_email_address.rb
1 #
2 # validate_email_address.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:validate_email_address, :doc => <<-DOC
6     Validate that all values passed are valid email addresses.
7     Fail compilation if any value fails this check.
8     The following values will pass:
9     $my_email = "waldo@gmail.com"
10     validate_email_address($my_email)
11     validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email)
12
13     The following values will fail, causing compilation to abort:
14     $some_array = [ 'bad_email@/d/efdf.com' ]
15     validate_email_address($some_array)
16     DOC
17              ) do |args|
18     rescuable_exceptions = [ArgumentError]
19
20     if args.empty?
21       raise Puppet::ParseError, "validate_email_address(): wrong number of arguments (#{args.length}; must be > 0)"
22     end
23
24     args.each do |arg|
25       raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
26
27       begin
28         raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" unless function_is_email_address([arg])
29       rescue *rescuable_exceptions
30         raise Puppet::ParseError, "#{arg.inspect} is not a valid email address"
31       end
32     end
33   end
34 end