Commit | Line | Data |
ac73ab52 |
1 | package MooseX::Types::Common::String; |
3ebb788b |
2 | # ABSTRACT: Commonly used string types |
ac73ab52 |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
ac73ab52 |
7 | use 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 | |
20 | use MooseX::Types::Moose qw/Str/; |
21 | |
22 | subtype 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 | |
34 | subtype 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 |
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 | |
ac73ab52 |
58 | subtype 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 | |
70 | subtype 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 | |
82 | subtype 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 |
94 | subtype 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 | |
106 | coerce LowerCaseStr, |
107 | from NonEmptyStr, |
108 | via { lc }; |
109 | |
110 | subtype 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 | |
122 | coerce UpperCaseStr, |
123 | from NonEmptyStr, |
124 | via { uc }; |
125 | |
126 | subtype 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 | ); |
d174f768 |
137 | |
a5c9a433 |
138 | coerce LowerCaseSimpleStr, |
139 | from NonEmptySimpleStr, |
140 | via { lc }; |
141 | |
142 | subtype 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 | |
154 | coerce UpperCaseSimpleStr, |
155 | from NonEmptySimpleStr, |
156 | via { uc }; |
ac73ab52 |
157 | |
158 | 1; |
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 | |
174 | A set of commonly-used string type constraints that do not ship with Moose by |
175 | default. |
176 | |
177 | =over |
178 | |
d174f768 |
179 | =item * C<SimpleStr> |
ac73ab52 |
180 | |
d174f768 |
181 | A C<Str> with no new-line characters. |
ac73ab52 |
182 | |
d174f768 |
183 | =item * C<NonEmptySimpleStr> |
ac73ab52 |
184 | |
d174f768 |
185 | A C<Str> with no new-line characters and length > 0 |
a5c9a433 |
186 | |
d174f768 |
187 | =item * C<LowerCaseSimpleStr> |
a5c9a433 |
188 | |
d174f768 |
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> |
a5c9a433 |
191 | |
d174f768 |
192 | =item * C<UpperCaseSimpleStr> |
a5c9a433 |
193 | |
d174f768 |
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> |
ac73ab52 |
196 | |
d174f768 |
197 | =item * C<Password> |
ac73ab52 |
198 | |
d174f768 |
199 | =item * C<StrongPassword> |
ac73ab52 |
200 | |
d174f768 |
201 | =item * C<NonEmptyStr> |
ac73ab52 |
202 | |
d174f768 |
203 | A C<Str> with length > 0 |
a5c9a433 |
204 | |
d174f768 |
205 | =item * C<LowerCaseStr> |
a5c9a433 |
206 | |
d174f768 |
207 | A C<Str> with length > 0 and no uppercase characters. |
208 | A coercion exists via C<lc> from C<NonEmptyStr> |
a5c9a433 |
209 | |
d174f768 |
210 | =item * C<UpperCaseStr> |
a5c9a433 |
211 | |
d174f768 |
212 | A C<Str> with length > 0 and no lowercase characters. |
213 | A coercion exists via C<uc> from C<NonEmptyStr> |
a5c9a433 |
214 | |
d174f768 |
215 | =item * C<NumericCode> |
559b5d74 |
216 | |
d174f768 |
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 |
559b5d74 |
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 | |
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 |