Update stdlib and concat to 6.1.0 both
[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     @summary
7       Validate that all values passed are valid email addresses.
8       Fail compilation if any value fails this check.
9
10     @return
11       Fail compilation if any value fails this check.
12
13     @example **Usage**
14
15       The following values will pass:
16
17         $my_email = "waldo@gmail.com"
18         validate_email_address($my_email)
19         validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email)
20
21       The following values will fail, causing compilation to abort:
22
23         $some_array = [ 'bad_email@/d/efdf.com' ]
24         validate_email_address($some_array)
25     DOC
26              ) do |args|
27     rescuable_exceptions = [ArgumentError]
28
29     if args.empty?
30       raise Puppet::ParseError, "validate_email_address(): wrong number of arguments (#{args.length}; must be > 0)"
31     end
32
33     args.each do |arg|
34       raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String)
35
36       begin
37         raise Puppet::ParseError, "#{arg.inspect} is not a valid email address" unless function_is_email_address([arg])
38       rescue *rescuable_exceptions
39         raise Puppet::ParseError, "#{arg.inspect} is not a valid email address"
40       end
41     end
42   end
43 end