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