Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_email_address.rb
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 = "waldo@gmail.com"
7     validate_email_address($my_email)
8     validate_email_address("bob@gmail.com", "alice@gmail.com", $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