fix spelling, whitespace, unused vars release test failures
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / String.pm
1 package MooseX::Types::Common::String;
2 # ABSTRACT:  Commonly used string types
3
4 use strict;
5 use warnings;
6
7 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 ];
19
20 use MooseX::Types::Moose qw/Str/;
21
22 subtype SimpleStr,
23   as Str,
24   where { (length($_) <= 255) && ($_ !~ m/\n/) },
25   message { "Must be a single line of no more than 255 chars" },
26     ( $Moose::VERSION >= 2.0200
27         ? inline_as {
28             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
29                 . qq{ ( (length($_[1]) <= 255) && ($_[1] !~ m/\n/) ) };
30         }
31         : ()
32     );
33
34 subtype NonEmptySimpleStr,
35   as SimpleStr,
36   where { length($_) > 0 },
37   message { "Must be a non-empty single line of no more than 255 chars" },
38     ( $Moose::VERSION >= 2.0200
39         ? inline_as {
40             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
41                 . qq{ (length($_[1]) > 0) };
42         }
43         : ()
44     );
45
46 subtype NumericCode,
47   as NonEmptySimpleStr,
48   where { $_ =~ m/^[0-9]+$/ },
49   message {
50     'Must be a non-empty single line of no more than 255 chars that consists '
51         . 'of numeric characters only'
52   };
53
54 coerce NumericCode,
55   from NonEmptySimpleStr,
56   via { my $code = $_; $code =~ s/[[:punct:]]//g; return $code };
57
58 subtype Password,
59   as NonEmptySimpleStr,
60   where { length($_) > 3 },
61   message { "Must be between 4 and 255 chars" },
62     ( $Moose::VERSION >= 2.0200
63         ? inline_as {
64             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
65                 . qq{ (length($_[1]) > 3) };
66         }
67         : ()
68     );
69
70 subtype StrongPassword,
71   as Password,
72   where { (length($_) > 7) && (m/[^a-zA-Z]/) },
73   message {"Must be between 8 and 255 chars, and contain a non-alpha char" },
74     ( $Moose::VERSION >= 2.0200
75         ? inline_as {
76             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
77                 . qq{ ( (length($_[1]) > 7) && ($_[1] =~ m/[^a-zA-Z]/) ) };
78         }
79         : ()
80     );
81
82 subtype NonEmptyStr,
83   as Str,
84   where { length($_) > 0 },
85   message { "Must not be empty" },
86     ( $Moose::VERSION >= 2.0200
87         ? inline_as {
88             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
89                 . qq{ (length($_[1]) > 0) };
90         }
91         : ()
92     );
93
94 subtype LowerCaseStr,
95   as NonEmptyStr,
96   where { !/[A-Z]/ms },
97   message { "Must not contain upper case letters" },
98     ( $Moose::VERSION >= 2.0200
99         ? inline_as {
100             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
101                 . qq{ ( $_[1] !~ /[A-Z]/ms ) };
102         }
103         : ()
104     );
105
106 coerce LowerCaseStr,
107   from NonEmptyStr,
108   via { lc };
109
110 subtype UpperCaseStr,
111   as NonEmptyStr,
112   where { !/[a-z]/ms },
113   message { "Must not contain lower case letters" },
114     ( $Moose::VERSION >= 2.0200
115         ? inline_as {
116             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
117                 . qq{ ( $_[1] !~ m/[a-z]/ms ) };
118         }
119         : ()
120     );
121
122 coerce UpperCaseStr,
123   from NonEmptyStr,
124   via { uc };
125
126 subtype LowerCaseSimpleStr,
127   as NonEmptySimpleStr,
128   where { !/[A-Z]/ },
129   message { "Must not contain upper case letters" },
130     ( $Moose::VERSION >= 2.0200
131         ? inline_as {
132             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
133                 . qq{ ( $_[1] !~ m/[A-Z]/ ) };
134         }
135         : ()
136     );
137
138 coerce LowerCaseSimpleStr,
139   from NonEmptySimpleStr,
140   via { lc };
141
142 subtype UpperCaseSimpleStr,
143   as NonEmptySimpleStr,
144   where { !/[a-z]/ },
145   message { "Must not contain lower case letters" },
146     ( $Moose::VERSION >= 2.0200
147         ? inline_as {
148             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
149                 . qq{ ( $_[1] !~ m/[a-z]/ ) };
150         }
151         : ()
152     );
153
154 coerce UpperCaseSimpleStr,
155   from NonEmptySimpleStr,
156   via { uc };
157
158 1;
159 __END__
160
161 =pod
162
163 =head1 SYNOPSIS
164
165     use MooseX::Types::Common::String qw/SimpleStr/;
166     has short_str => (is => 'rw', isa => SimpleStr);
167
168     ...
169     #this will fail
170     $object->short_str("string\nwith\nbreaks");
171
172 =head1 DESCRIPTION
173
174 A set of commonly-used string type constraints that do not ship with Moose by
175 default.
176
177 =over
178
179 =item * C<SimpleStr>
180
181 A C<Str> with no new-line characters.
182
183 =item * C<NonEmptySimpleStr>
184
185 A C<Str> with no new-line characters and length > 0
186
187 =item * C<LowerCaseSimpleStr>
188
189 A C<Str> with no new-line characters, length > 0 and no uppercase characters
190 A coercion exists via C<lc> from C<NonEmptySimpleStr>
191
192 =item * C<UpperCaseSimpleStr>
193
194 A C<Str> with no new-line characters, length > 0 and no lowercase characters
195 A coercion exists via C<uc> from C<NonEmptySimpleStr>
196
197 =item * C<Password>
198
199 =item * C<StrongPassword>
200
201 =item * C<NonEmptyStr>
202
203 A C<Str> with length > 0
204
205 =item * C<LowerCaseStr>
206
207 A C<Str> with length > 0 and no uppercase characters.
208 A coercion exists via C<lc> from C<NonEmptyStr>
209
210 =item * C<UpperCaseStr>
211
212 A C<Str> with length > 0 and no lowercase characters.
213 A coercion exists via C<uc> from C<NonEmptyStr>
214
215 =item * C<NumericCode>
216
217 A C<Str> with no new-line characters that consists of only Numeric characters.
218 Examples include, Social Security Numbers, Personal Identification Numbers, Postal Codes, HTTP Status
219 Codes, etc. Supports attempting to coerce from a string that has punctuation
220 in it ( e.g credit card number 4111-1111-1111-1111 ).
221
222 =back
223
224 =head1 SEE ALSO
225
226 =over
227
228 =item * L<MooseX::Types::Common::Numeric>
229
230 =back
231
232 =cut