0cee6f93ffb4dfae4499fd12813d24a5410b18cc
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / String.pm
1 package MooseX::Types::Common::String;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.001008';
7
8 use MooseX::Types -declare => [
9   qw(SimpleStr
10      NonEmptySimpleStr
11      NumericCode
12      LowerCaseSimpleStr
13      UpperCaseSimpleStr
14      Password
15      StrongPassword
16      NonEmptyStr
17      LowerCaseStr
18      UpperCaseStr)
19 ];
20
21 use MooseX::Types::Moose qw/Str/;
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:]]//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   where { !/[A-Z]/ms },
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] !~ /[A-Z]/ms ) };
103         }
104         : ()
105     );
106
107 coerce LowerCaseStr,
108   from NonEmptyStr,
109   via { lc };
110
111 subtype UpperCaseStr,
112   as NonEmptyStr,
113   where { !/[a-z]/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] !~ m/[a-z]/ms ) };
119         }
120         : ()
121     );
122
123 coerce UpperCaseStr,
124   from NonEmptyStr,
125   via { uc };
126
127 subtype LowerCaseSimpleStr,
128   as NonEmptySimpleStr,
129   where { !/[A-Z]/ },
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] !~ m/[A-Z]/ ) };
135         }
136         : ()
137     );
138   
139 coerce LowerCaseSimpleStr,
140   from NonEmptySimpleStr,
141   via { lc };
142
143 subtype UpperCaseSimpleStr,
144   as NonEmptySimpleStr,
145   where { !/[a-z]/ },
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] !~ m/[a-z]/ ) };
151         }
152         : ()
153     );
154
155 coerce UpperCaseSimpleStr,
156   from NonEmptySimpleStr,
157   via { uc };
158
159 1;
160
161 =head1 NAME
162
163 MooseX::Types::Common::String - Commonly used string types
164
165 =head1 SYNOPSIS
166
167     use MooseX::Types::Common::String qw/SimpleStr/;
168     has short_str => (is => 'rw', isa => SimpleStr);
169
170     ...
171     #this will fail
172     $object->short_str("string\nwith\nbreaks");
173
174 =head1 DESCRIPTION
175
176 A set of commonly-used string type constraints that do not ship with Moose by
177 default.
178
179 =over
180
181 =item * SimpleStr
182
183 A Str with no new-line characters.
184
185 =item * NonEmptySimpleStr
186
187 A Str with no new-line characters and length > 0
188
189 =item * LowerCaseSimpleStr
190
191 A Str with no new-line characters, length > 0 and no uppercase characters
192 A coercion exists via C<lc> from NonEmptySimpleStr
193
194 =item * UpperCaseSimpleStr
195
196 A Str with no new-line characters, length > 0 and no lowercase characters
197 A coercion exists via C<uc> from NonEmptySimpleStr
198
199 =item * Password
200
201 =item * StrongPassword
202
203 =item * NonEmptyStr
204
205 A Str with length > 0
206
207 =item * LowerCaseStr
208
209 A Str with length > 0 and no uppercase characters.
210 A coercion exists via C<lc> from NonEmptyStr
211
212 =item * UpperCaseStr
213
214 A Str with length > 0 and no lowercase characters.
215 A coercion exists via C<uc> from NonEmptyStr
216
217 =item * NumericCode
218
219 A Str with no new-line characters that consists of only Numeric characters.
220 Examples include, Social Security Numbers, PINs, Postal Codes, HTTP Status
221 Codes, etc. Supports attempting to coerce from a string that has punctuation
222 in it ( e.g credit card number 4111-1111-1111-1111 ).
223
224 =back
225
226 =head1 SEE ALSO
227
228 =over
229
230 =item * L<MooseX::Types::Common::Numeric>
231
232 =back
233
234 =head1 AUTHORS
235
236 Please see:: L<MooseX::Types::Common>
237
238 =cut