File Coverage

blib/lib/MooseX/Types/Common/String.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Common::String;
2             # ABSTRACT: Commonly used string types
3              
4             our $VERSION = '0.001015';
5              
6 4     4   451660 use strict;
  4         9  
  4         207  
7 4     4   22 use warnings;
  4         8  
  4         335  
8              
9 4         45 use MooseX::Types -declare => [
10             qw(SimpleStr
11             NonEmptySimpleStr
12             NumericCode
13             LowerCaseSimpleStr
14             UpperCaseSimpleStr
15             Password
16             StrongPassword
17             NonEmptyStr
18             LowerCaseStr
19             UpperCaseStr)
20 4     4   2631 ];
  4         1620575  
21              
22 4     4   44049 use Moose ();
  4         879499  
  4         212  
23 4     4   2850 use MooseX::Types::Moose qw/Str/;
  4         25796  
  4         45  
24 4     4   26327 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  4         16  
  4         247  
25              
26             subtype SimpleStr,
27             as Str,
28             where { (length($_) <= 255) && ($_ !~ m/\n/) },
29             message { "Must be a single line of no more than 255 chars" },
30             ( $Moose::VERSION >= 2.0200
31             ? inline_as {
32             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
33             . qq{ ( (length($_[1]) <= 255) && ($_[1] !~ m/\n/) ) };
34             }
35             : ()
36             );
37              
38             subtype NonEmptySimpleStr,
39             as SimpleStr,
40             where { length($_) > 0 },
41             message { "Must be a non-empty single line of no more than 255 chars" },
42             ( $Moose::VERSION >= 2.0200
43             ? inline_as {
44             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
45             . qq{ (length($_[1]) > 0) };
46             }
47             : ()
48             );
49              
50             subtype NumericCode,
51             as NonEmptySimpleStr,
52             where { $_ =~ m/^[0-9]+$/ },
53             message {
54             'Must be a non-empty single line of no more than 255 chars that consists '
55             . 'of numeric characters only'
56             },
57             ( $Moose::VERSION >= 2.0200
58             ? inline_as {
59             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
60             . qq{ $_[1] =~ m/^[0-9]+\$/ };
61             }
62             : ()
63             );
64              
65             coerce NumericCode,
66             from NonEmptySimpleStr,
67             via { my $code = $_; $code =~ s/[[:punct:][:space:]]//g; return $code };
68              
69             subtype Password,
70             as NonEmptySimpleStr,
71             where { length($_) > 3 },
72             message { "Must be between 4 and 255 chars" },
73             ( $Moose::VERSION >= 2.0200
74             ? inline_as {
75             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
76             . qq{ (length($_[1]) > 3) };
77             }
78             : ()
79             );
80              
81             subtype StrongPassword,
82             as Password,
83             where { (length($_) > 7) && (m/[^a-zA-Z]/) },
84             message {"Must be between 8 and 255 chars, and contain a non-alpha char" },
85             ( $Moose::VERSION >= 2.0200
86             ? inline_as {
87             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
88             . qq{ ( (length($_[1]) > 7) && ($_[1] =~ m/[^a-zA-Z]/) ) };
89             }
90             : ()
91             );
92              
93             subtype NonEmptyStr,
94             as Str,
95             where { length($_) > 0 },
96             message { "Must not be empty" },
97             ( $Moose::VERSION >= 2.0200
98             ? inline_as {
99             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
100             . qq{ (length($_[1]) > 0) };
101             }
102             : ()
103             );
104              
105             subtype LowerCaseStr,
106             as NonEmptyStr,
107             where { !/\p{Upper}/ms },
108             message { "Must not contain upper case letters" },
109             ( $Moose::VERSION >= 2.0200
110             ? inline_as {
111             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
112             . qq{ ( $_[1] !~ /\\p{Upper}/ms ) };
113             }
114             : ()
115             );
116              
117             coerce LowerCaseStr,
118             from NonEmptyStr,
119             via { lc };
120              
121             subtype UpperCaseStr,
122             as NonEmptyStr,
123             where { !/\p{Lower}/ms },
124             message { "Must not contain lower case letters" },
125             ( $Moose::VERSION >= 2.0200
126             ? inline_as {
127             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
128             . qq{ ( $_[1] !~ /\\p{Lower}/ms ) };
129             }
130             : ()
131             );
132              
133             coerce UpperCaseStr,
134             from NonEmptyStr,
135             via { uc };
136              
137             subtype LowerCaseSimpleStr,
138             as NonEmptySimpleStr,
139             where { !/\p{Upper}/ },
140             message { "Must not contain upper case letters" },
141             ( $Moose::VERSION >= 2.0200
142             ? inline_as {
143             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
144             . qq{ ( $_[1] !~ /\\p{Upper}/ ) };
145             }
146             : ()
147             );
148              
149             coerce LowerCaseSimpleStr,
150             from NonEmptySimpleStr,
151             via { lc };
152              
153             subtype UpperCaseSimpleStr,
154             as NonEmptySimpleStr,
155             where { !/\p{Lower}/ },
156             message { "Must not contain lower case letters" },
157             ( $Moose::VERSION >= 2.0200
158             ? inline_as {
159             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
160             . qq{ ( $_[1] !~ /\\p{Lower}/ ) };
161             }
162             : ()
163             );
164              
165             coerce UpperCaseSimpleStr,
166             from NonEmptySimpleStr,
167             via { uc };
168              
169             1;
170              
171             __END__
172              
173             =pod
174              
175             =encoding UTF-8
176              
177             =head1 NAME
178              
179             MooseX::Types::Common::String - Commonly used string types
180              
181             =head1 VERSION
182              
183             version 0.001015
184              
185             =head1 SYNOPSIS
186              
187             use MooseX::Types::Common::String qw/SimpleStr/;
188             has short_str => (is => 'rw', isa => SimpleStr);
189              
190             ...
191             #this will fail
192             $object->short_str("string\nwith\nbreaks");
193              
194             =head1 DESCRIPTION
195              
196             A set of commonly-used string type constraints that do not ship with Moose by
197             default.
198              
199             =over
200              
201             =item * C<SimpleStr>
202              
203             A C<Str> with no new-line characters and length <= 255.
204              
205             =item * C<NonEmptySimpleStr>
206              
207             A C<SimpleStr> with length > 0.
208              
209             =item * C<LowerCaseSimpleStr>
210              
211             A C<NonEmptySimpleStr> with no uppercase characters. A coercion exists via
212             C<lc> from C<NonEmptySimpleStr>.
213              
214             =item * C<UpperCaseSimpleStr>
215              
216             A C<NonEmptySimpleStr> with no lowercase characters. A coercion exists via
217             C<uc> from C<NonEmptySimpleStr>.
218              
219             =item * C<Password>
220              
221             A C<NonEmptySimpleStr> with length > 3.
222              
223             =item * C<StrongPassword>
224              
225             A C<NonEmptySimpleStr> with length > 7 containing at least one non-alpha
226             character.
227              
228             =item * C<NonEmptyStr>
229              
230             A C<Str> with length > 0.
231              
232             =item * C<LowerCaseStr>
233              
234             A C<Str> with length > 0 and no uppercase characters.
235             A coercion exists via C<lc> from C<NonEmptyStr>.
236              
237             =item * C<UpperCaseStr>
238              
239             A C<Str> with length > 0 and no lowercase characters.
240             A coercion exists via C<uc> from C<NonEmptyStr>.
241              
242             =item * C<NumericCode>
243              
244             A C<Str> with no new-line characters that consists of only Numeric characters.
245             Examples include, Social Security Numbers, Personal Identification Numbers, Postal Codes, HTTP Status
246             Codes, etc. Supports attempting to coerce from a string that has punctuation
247             or whitespaces in it ( e.g credit card number 4111-1111-1111-1111 ).
248              
249             =back
250              
251             =head1 SEE ALSO
252              
253             =over
254              
255             =item * L<MooseX::Types::Common::Numeric>
256              
257             =back
258              
259             =head1 SUPPORT
260              
261             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Common>
262             (or L<bug-MooseX-Types-Common@rt.cpan.org|mailto:bug-MooseX-Types-Common@rt.cpan.org>).
263              
264             There is also a mailing list available for users of this distribution, at
265             L<http://lists.perl.org/list/moose.html>.
266              
267             There is also an irc channel available for users of this distribution, at
268             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
269              
270             =head1 AUTHORS
271              
272             =over 4
273              
274             =item *
275              
276             Matt S Trout - mst (at) shadowcat.co.uk (L<https://www.shadowcat.co.uk/>)
277              
278             =item *
279              
280             K. James Cheetham <jamie@shadowcat.co.uk>
281              
282             =item *
283              
284             Guillermo Roditi <groditi@gmail.com>
285              
286             =back
287              
288             =head1 COPYRIGHT AND LICENSE
289              
290             This software is copyright (c) 2009 by Matt S Trout - mst (at) shadowcat.co.uk (L<https://www.shadowcat.co.uk/>).
291              
292             This is free software; you can redistribute it and/or modify it under
293             the same terms as the Perl 5 programming language system itself.
294              
295             =cut