Commit | Line | Data |
ac73ab52 |
1 | package MooseX::Types::Common::String; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
31633b26 |
6 | our $VERSION = '0.001005'; |
ac73ab52 |
7 | |
8 | use MooseX::Types -declare => [ |
a5c9a433 |
9 | qw(SimpleStr |
10 | NonEmptySimpleStr |
559b5d74 |
11 | NumericCode |
a5c9a433 |
12 | LowerCaseSimpleStr |
13 | UpperCaseSimpleStr |
14 | Password |
15 | StrongPassword |
16 | NonEmptyStr |
17 | LowerCaseStr |
18 | UpperCaseStr) |
ac73ab52 |
19 | ]; |
20 | |
21 | use MooseX::Types::Moose qw/Str/; |
22 | |
23 | subtype SimpleStr, |
24 | as Str, |
25 | where { (length($_) <= 255) && ($_ !~ m/\n/) }, |
d2461fda |
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 | ); |
ac73ab52 |
34 | |
35 | subtype NonEmptySimpleStr, |
36 | as SimpleStr, |
37 | where { length($_) > 0 }, |
d2461fda |
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 | ); |
ac73ab52 |
46 | |
559b5d74 |
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 | |
ac73ab52 |
59 | subtype Password, |
60 | as NonEmptySimpleStr, |
61 | where { length($_) > 3 }, |
d2461fda |
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 | ); |
ac73ab52 |
70 | |
71 | subtype StrongPassword, |
72 | as Password, |
73 | where { (length($_) > 7) && (m/[^a-zA-Z]/) }, |
d2461fda |
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 | ); |
ac73ab52 |
82 | |
83 | subtype NonEmptyStr, |
84 | as Str, |
85 | where { length($_) > 0 }, |
d2461fda |
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 | ); |
ac73ab52 |
94 | |
a5c9a433 |
95 | subtype LowerCaseStr, |
96 | as NonEmptyStr, |
97 | where { /^[a-z]+$/xms }, |
98 | message { "Must only contain lower case letters" }, |
99 | ( $Moose::VERSION >= 2.0200 |
100 | ? inline_as { |
101 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
102 | . qq{ ( $_[1] =~ m/^[a-z]+\$/xms ) }; |
103 | } |
104 | : () |
105 | ); |
106 | |
107 | coerce LowerCaseStr, |
108 | from NonEmptyStr, |
109 | via { lc }; |
110 | |
111 | subtype UpperCaseStr, |
112 | as NonEmptyStr, |
113 | where { /^[A-Z]+$/xms }, |
114 | message { "Must only contain upper case letters" }, |
115 | ( $Moose::VERSION >= 2.0200 |
116 | ? inline_as { |
117 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
118 | . qq{ ( $_[1] =~ m/^[A-Z]+\$/xms ) }; |
119 | } |
120 | : () |
121 | ); |
122 | |
123 | coerce UpperCaseStr, |
124 | from NonEmptyStr, |
125 | via { uc }; |
126 | |
127 | subtype LowerCaseSimpleStr, |
128 | as NonEmptySimpleStr, |
129 | where { /^[a-z]+$/x }, |
130 | message { "Must only contain lower case letters" }, |
131 | ( $Moose::VERSION >= 2.0200 |
132 | ? inline_as { |
133 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
134 | . qq{ ( $_[1] =~ m/^[a-z]+\$/x ) }; |
135 | } |
136 | : () |
137 | ); |
138 | |
139 | coerce LowerCaseSimpleStr, |
140 | from NonEmptySimpleStr, |
141 | via { lc }; |
142 | |
143 | subtype UpperCaseSimpleStr, |
144 | as NonEmptySimpleStr, |
145 | where { /^[A-Z]+$/x }, |
146 | message { "Must only contain upper case letters" }, |
147 | ( $Moose::VERSION >= 2.0200 |
148 | ? inline_as { |
149 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
150 | . qq{ ( $_[1] =~ m/^[A-Z]+\$/x ) }; |
151 | } |
152 | : () |
153 | ); |
154 | |
155 | coerce UpperCaseSimpleStr, |
156 | from NonEmptySimpleStr, |
157 | via { uc }; |
ac73ab52 |
158 | |
159 | 1; |
160 | |
161 | =head1 NAME |
162 | |
6820e939 |
163 | MooseX::Types::Common::String - Commonly used string types |
ac73ab52 |
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 | |
a5c9a433 |
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 all lowercase 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 all uppercase characters |
197 | A coercion exists via C<uc> from NonEmptySimpleStr |
ac73ab52 |
198 | |
199 | =item * Password |
200 | |
201 | =item * StrongPassword |
202 | |
203 | =item * NonEmptyStr |
204 | |
a5c9a433 |
205 | A Str with length > 0 |
206 | |
207 | =item * LowerCaseStr |
208 | |
209 | A Str with length > 0 and all lowercase characters. |
210 | A coercion exists via C<lc> from NonEmptyStr |
211 | |
212 | =item * UpperCaseStr |
213 | |
214 | A Str with length > 0 and all uppercase characters. |
215 | A coercion exists via C<uc> from NonEmptyStr |
216 | |
ac73ab52 |
217 | =back |
218 | |
559b5d74 |
219 | =item * NumericCode |
220 | |
221 | A Str with no new-line characters that consists of only Numeric characters. |
222 | Examples include, Social Security Numbers, PINs, Postal Codes, HTTP Status |
223 | Codes, etc. Supports attempting to coerce from a string that has punctuation |
224 | in it ( e.g credit card number 4111-1111-1111-1111 ). |
225 | |
ac73ab52 |
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 |