convert to Dist::Zilla
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / String.pm
CommitLineData
ac73ab52 1package MooseX::Types::Common::String;
3ebb788b 2# ABSTRACT: Commonly used string types
ac73ab52 3
4use strict;
5use warnings;
6
ac73ab52 7use MooseX::Types -declare => [
a5c9a433 8 qw(SimpleStr
9 NonEmptySimpleStr
559b5d74 10 NumericCode
a5c9a433 11 LowerCaseSimpleStr
12 UpperCaseSimpleStr
13 Password
14 StrongPassword
15 NonEmptyStr
16 LowerCaseStr
17 UpperCaseStr)
ac73ab52 18];
19
20use MooseX::Types::Moose qw/Str/;
21
22subtype SimpleStr,
23 as Str,
24 where { (length($_) <= 255) && ($_ !~ m/\n/) },
d2461fda 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 );
ac73ab52 33
34subtype NonEmptySimpleStr,
35 as SimpleStr,
36 where { length($_) > 0 },
d2461fda 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 );
ac73ab52 45
559b5d74 46subtype 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
54coerce NumericCode,
55 from NonEmptySimpleStr,
56 via { my $code = $_; $code =~ s/[[:punct:]]//g; return $code };
57
ac73ab52 58subtype Password,
59 as NonEmptySimpleStr,
60 where { length($_) > 3 },
d2461fda 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 );
ac73ab52 69
70subtype StrongPassword,
71 as Password,
72 where { (length($_) > 7) && (m/[^a-zA-Z]/) },
d2461fda 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 );
ac73ab52 81
82subtype NonEmptyStr,
83 as Str,
84 where { length($_) > 0 },
d2461fda 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 );
ac73ab52 93
a5c9a433 94subtype LowerCaseStr,
95 as NonEmptyStr,
19ef3f5a 96 where { !/[A-Z]/ms },
97 message { "Must not contain upper case letters" },
a5c9a433 98 ( $Moose::VERSION >= 2.0200
99 ? inline_as {
100 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
19ef3f5a 101 . qq{ ( $_[1] !~ /[A-Z]/ms ) };
a5c9a433 102 }
103 : ()
104 );
105
106coerce LowerCaseStr,
107 from NonEmptyStr,
108 via { lc };
109
110subtype UpperCaseStr,
111 as NonEmptyStr,
19ef3f5a 112 where { !/[a-z]/ms },
113 message { "Must not contain lower case letters" },
a5c9a433 114 ( $Moose::VERSION >= 2.0200
115 ? inline_as {
116 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
19ef3f5a 117 . qq{ ( $_[1] !~ m/[a-z]/ms ) };
a5c9a433 118 }
119 : ()
120 );
121
122coerce UpperCaseStr,
123 from NonEmptyStr,
124 via { uc };
125
126subtype LowerCaseSimpleStr,
127 as NonEmptySimpleStr,
19ef3f5a 128 where { !/[A-Z]/ },
129 message { "Must not contain upper case letters" },
a5c9a433 130 ( $Moose::VERSION >= 2.0200
131 ? inline_as {
132 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
19ef3f5a 133 . qq{ ( $_[1] !~ m/[A-Z]/ ) };
a5c9a433 134 }
135 : ()
136 );
137
138coerce LowerCaseSimpleStr,
139 from NonEmptySimpleStr,
140 via { lc };
141
142subtype UpperCaseSimpleStr,
143 as NonEmptySimpleStr,
19ef3f5a 144 where { !/[a-z]/ },
145 message { "Must not contain lower case letters" },
a5c9a433 146 ( $Moose::VERSION >= 2.0200
147 ? inline_as {
148 $_[0]->parent()->_inline_check( $_[1] ) . ' && '
19ef3f5a 149 . qq{ ( $_[1] !~ m/[a-z]/ ) };
a5c9a433 150 }
151 : ()
152 );
153
154coerce UpperCaseSimpleStr,
155 from NonEmptySimpleStr,
156 via { uc };
ac73ab52 157
1581;
3ebb788b 159__END__
ac73ab52 160
3ebb788b 161=pod
ac73ab52 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
174A set of commonly-used string type constraints that do not ship with Moose by
175default.
176
177=over
178
179=item * SimpleStr
180
181A Str with no new-line characters.
182
183=item * NonEmptySimpleStr
184
a5c9a433 185A Str with no new-line characters and length > 0
186
187=item * LowerCaseSimpleStr
188
19ef3f5a 189A Str with no new-line characters, length > 0 and no uppercase characters
a5c9a433 190A coercion exists via C<lc> from NonEmptySimpleStr
191
192=item * UpperCaseSimpleStr
193
19ef3f5a 194A Str with no new-line characters, length > 0 and no lowercase characters
a5c9a433 195A coercion exists via C<uc> from NonEmptySimpleStr
ac73ab52 196
197=item * Password
198
199=item * StrongPassword
200
201=item * NonEmptyStr
202
a5c9a433 203A Str with length > 0
204
205=item * LowerCaseStr
206
19ef3f5a 207A Str with length > 0 and no uppercase characters.
a5c9a433 208A coercion exists via C<lc> from NonEmptyStr
209
210=item * UpperCaseStr
211
19ef3f5a 212A Str with length > 0 and no lowercase characters.
a5c9a433 213A coercion exists via C<uc> from NonEmptyStr
214
559b5d74 215=item * NumericCode
216
217A Str with no new-line characters that consists of only Numeric characters.
218Examples include, Social Security Numbers, PINs, Postal Codes, HTTP Status
219Codes, etc. Supports attempting to coerce from a string that has punctuation
220in it ( e.g credit card number 4111-1111-1111-1111 ).
221
c7cac4c0 222=back
223
ac73ab52 224=head1 SEE ALSO
225
226=over
227
228=item * L<MooseX::Types::Common::Numeric>
229
230=back
231
ac73ab52 232=cut