File Coverage

blib/lib/MooseX/Types/Email.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Email;
2             BEGIN {
3 5     5   86024 $MooseX::Types::Email::AUTHORITY = 'cpan:BOBTFISH';
4             }
5             {
6             $MooseX::Types::Email::VERSION = '0.006';
7             }
8             # git description: 2a8e05c
9              
10             # ABSTRACT: Email address validation type constraints for Moose.
11              
12             use MooseX::Types
13 5     5   2642 -declare => [qw/EmailAddress EmailMessage EmailAddresses EmailMessages/];
  5         2010557  
  5         43  
14              
15 5     5   24242 use MooseX::Types::Moose qw/Object ArrayRef Str/;
  5         60077  
  5         58  
16 5     5   24669 use Email::Valid;
  5         455501  
  5         199  
17 5     5   2272 use Email::Abstract;
  5         143604  
  5         1739  
18              
19             subtype EmailAddress,
20             as Str,
21             where { Email::Valid->address($_) },
22             message { "Must be a valid e-mail address" };
23              
24             subtype EmailMessage,
25             as Object, where { Email::Abstract->new($_) },
26             message { "Must be something Email::Abstract recognizes" };
27              
28             coerce EmailMessage,
29             from Object,
30             via { Email::Abstract->new($_) };
31              
32              
33             subtype EmailAddresses,
34             as ArrayRef[EmailAddress],
35             message { 'Must be an arrayref of valid e-mail addresses' };
36              
37             coerce EmailAddresses,
38             from Str,
39             via { [ $_ ] };
40              
41             subtype EmailMessages,
42             as ArrayRef[Object],
43             where { not grep { not Email::Abstract->new($_) } @$_ },
44             message { 'Must be an arrayref of something Email::Abstract recognizes' };
45              
46             # no coercion from Object, as that would also catch existing Email::Abstract
47             # objects and its subtypes.
48              
49             1;
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             MooseX::Types::Email - Email address validation type constraints for Moose.
60              
61             =head1 VERSION
62              
63             version 0.006
64              
65             =head1 SYNOPSIS
66              
67             package MyClass;
68             use Moose;
69             use MooseX::Types::Email qw/EmailAddress EmailMessage EmailAddresses EmailMessages/;
70             use namespace::autoclean;
71              
72             has email => ( isa => EmailAddress, required => 1, is => 'ro' );
73             has message => ( isa => EmailMessage, required => 1, is => 'ro' );
74              
75             has emails => ( isa => EmailAddresses, required => 1, is => 'ro' );
76             has messages => ( isa => EmailMessages, required => 1, is => 'ro' );
77              
78             =head1 DESCRIPTION
79              
80             Moose type constraints which uses L<Email::Valid> and L<Email::Abstract> to check
81             for valid email addresses and messages. Types that support both single items
82             and an arrayref of items are available.
83              
84             Note that C<EmailMessage> must be an object that can be passed to
85             L<Email::Abstract>. Currently, constraining strings is not supported due to the
86             leniency of Email::Abstract.
87              
88             =head1 SEE ALSO
89              
90             =over
91              
92             =item L<Moose::Util::TypeConstraints>
93              
94             =item L<MooseX::Types>
95              
96             =item L<Email::Valid>
97              
98             =item L<Email::Abstract>
99              
100             =back
101              
102             =head1 ORIGIN
103              
104             Shamelessly extracted from L<Reaction::Types::Email>.
105              
106             =head1 ACKNOWLEDGEMENTS
107              
108             Chris Nehren C<< <apeiron@cpan.org> >> added support for validating email
109             messages.
110              
111             Karen Etheridge C<< <ether@cpan.org> >> added support for lists of email
112             addresses and messages.
113              
114             =head1 AUTHOR
115              
116             Tomas Doran (t0m) <bobtfish@bobtfish.net
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2009 by Tomas Doran (t0m).
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut