File Coverage

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


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