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.008-6-g8ae6d75
2             # ABSTRACT: Email address validation type constraints for Moose.
3             # KEYWORDS: moose type constraint email address message abstract
4              
5             our $VERSION = '0.009';
6              
7             use MooseX::Types
8 5     5   940772 -declare => [qw/EmailAddress EmailMessage EmailAddresses EmailMessages/];
  5         1644198  
  5         49  
9              
10 5     5   32157 use MooseX::Types::Moose qw/Object ArrayRef Str/;
  5         25493  
  5         56  
11 5     5   29534 use Email::Valid;
  5         778168  
  5         733  
12 5     5   2787 use Email::Abstract;
  5         219906  
  5         432  
13 5     5   55 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  5         11  
  5         486  
14              
15             subtype EmailAddress,
16             as Str,
17             where { Email::Valid->address($_) },
18             message { "Must be a valid e-mail address" },
19             inline_as {
20             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
21             . qq{ Email::Valid->address($_[1]) };
22             };
23              
24             subtype EmailMessage,
25             as Object, where { Email::Abstract->new($_) },
26             message { "Must be something Email::Abstract recognizes" },
27             inline_as {
28             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
29             . qq{ Email::Abstract->new($_[1]) };
30             };
31              
32             coerce EmailMessage,
33             from Object,
34             via { Email::Abstract->new($_) };
35              
36              
37             subtype EmailAddresses,
38             as ArrayRef[EmailAddress],
39             message { 'Must be an arrayref of valid e-mail addresses' };
40              
41             coerce EmailAddresses,
42             from Str,
43             via { [ $_ ] };
44              
45             subtype EmailMessages,
46             as ArrayRef[Object],
47             where { not grep { not Email::Abstract->new($_) } @$_ },
48             message { 'Must be an arrayref of something Email::Abstract recognizes' },
49             inline_as {
50             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
51             . qq{ not grep { not Email::Abstract->new(\$_) } \@{ $_[1] } };
52             };
53              
54             # no coercion from Object, as that would also catch existing Email::Abstract
55             # objects and its subtypes.
56              
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             MooseX::Types::Email - Email address validation type constraints for Moose.
68              
69             =head1 VERSION
70              
71             version 0.009
72              
73             =head1 SYNOPSIS
74              
75             package MyClass;
76             use Moose;
77             use MooseX::Types::Email qw/EmailAddress EmailMessage EmailAddresses EmailMessages/;
78             use namespace::autoclean;
79              
80             has email => ( isa => EmailAddress, required => 1, is => 'ro' );
81             has message => ( isa => EmailMessage, required => 1, is => 'ro' );
82              
83             has emails => ( isa => EmailAddresses, required => 1, is => 'ro' );
84             has messages => ( isa => EmailMessages, required => 1, is => 'ro' );
85              
86             =head1 DESCRIPTION
87              
88             Moose type constraints which uses L<Email::Valid> and L<Email::Abstract> to check
89             for valid email addresses and messages. Types that support both single items
90             and an arrayref of items are available.
91              
92             Note that C<EmailMessage> must be an object that can be passed to
93             L<Email::Abstract>. Currently, constraining strings is not supported due to the
94             leniency of Email::Abstract.
95              
96             =head1 SEE ALSO
97              
98             =over
99              
100             =item L<Moose::Util::TypeConstraints>
101              
102             =item L<MooseX::Types>
103              
104             =item L<Email::Valid>
105              
106             =item L<Email::Abstract>
107              
108             =back
109              
110             =head1 ORIGIN
111              
112             Shamelessly extracted from L<Reaction::Types::Email>.
113              
114             =head1 ACKNOWLEDGEMENTS
115              
116             Chris Nehren C<< <apeiron@cpan.org> >> added support for validating email
117             messages.
118              
119             Karen Etheridge C<< <ether@cpan.org> >> added support for lists of email
120             addresses and messages.
121              
122             =head1 SUPPORT
123              
124             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Email>
125             (or L<bug-MooseX-Types-Email@rt.cpan.org|mailto:bug-MooseX-Types-Email@rt.cpan.org>).
126              
127             There is also a mailing list available for users of this distribution, at
128             L<http://lists.perl.org/list/moose.html>.
129              
130             There is also an irc channel available for users of this distribution, at
131             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
132              
133             =head1 AUTHOR
134              
135             Tomas Doran (t0m) <bobtfish@bobtfish.net
136              
137             =head1 CONTRIBUTORS
138              
139             =for stopwords Karen Etheridge Tomas Doran (t0m) Alexander Hartmaier Chris Nehren Gregory Oschwald
140              
141             =over 4
142              
143             =item *
144              
145             Karen Etheridge <ether@cpan.org>
146              
147             =item *
148              
149             Tomas Doran (t0m) <bobtfish@bobtfish.net>
150              
151             =item *
152              
153             Alexander Hartmaier <abraxxa@cpan.org>
154              
155             =item *
156              
157             Chris Nehren <apeiron@cpan.org>
158              
159             =item *
160              
161             Gregory Oschwald <goschwald@maxmind.com>
162              
163             =back
164              
165             =head1 COPYRIGHT AND LICENCE
166              
167             This software is copyright (c) 2009 by Tomas Doran (t0m).
168              
169             This is free software; you can redistribute it and/or modify it under
170             the same terms as the Perl 5 programming language system itself.
171              
172             =cut