Commit | Line | Data |
ac73ab52 |
1 | package MooseX::Types::Common::String; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
c89e0261 |
6 | our $VERSION = '0.001003'; |
ac73ab52 |
7 | |
8 | use MooseX::Types -declare => [ |
a5c9a433 |
9 | qw(SimpleStr |
10 | NonEmptySimpleStr |
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 | |
ac73ab52 |
46 | subtype Password, |
47 | as NonEmptySimpleStr, |
48 | where { length($_) > 3 }, |
d2461fda |
49 | message { "Must be between 4 and 255 chars" }, |
50 | ( $Moose::VERSION >= 2.0200 |
51 | ? inline_as { |
52 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
53 | . qq{ (length($_[1]) > 3) }; |
54 | } |
55 | : () |
56 | ); |
ac73ab52 |
57 | |
58 | subtype StrongPassword, |
59 | as Password, |
60 | where { (length($_) > 7) && (m/[^a-zA-Z]/) }, |
d2461fda |
61 | message {"Must be between 8 and 255 chars, and contain a non-alpha char" }, |
62 | ( $Moose::VERSION >= 2.0200 |
63 | ? inline_as { |
64 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
65 | . qq{ ( (length($_[1]) > 7) && ($_[1] =~ m/[^a-zA-Z]/) ) }; |
66 | } |
67 | : () |
68 | ); |
ac73ab52 |
69 | |
70 | subtype NonEmptyStr, |
71 | as Str, |
72 | where { length($_) > 0 }, |
d2461fda |
73 | message { "Must not be empty" }, |
74 | ( $Moose::VERSION >= 2.0200 |
75 | ? inline_as { |
76 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
77 | . qq{ (length($_[1]) > 0) }; |
78 | } |
79 | : () |
80 | ); |
ac73ab52 |
81 | |
a5c9a433 |
82 | subtype LowerCaseStr, |
83 | as NonEmptyStr, |
84 | where { /^[a-z]+$/xms }, |
85 | message { "Must only contain lower case letters" }, |
86 | ( $Moose::VERSION >= 2.0200 |
87 | ? inline_as { |
88 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
89 | . qq{ ( $_[1] =~ m/^[a-z]+\$/xms ) }; |
90 | } |
91 | : () |
92 | ); |
93 | |
94 | coerce LowerCaseStr, |
95 | from NonEmptyStr, |
96 | via { lc }; |
97 | |
98 | subtype UpperCaseStr, |
99 | as NonEmptyStr, |
100 | where { /^[A-Z]+$/xms }, |
101 | message { "Must only contain upper case letters" }, |
102 | ( $Moose::VERSION >= 2.0200 |
103 | ? inline_as { |
104 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
105 | . qq{ ( $_[1] =~ m/^[A-Z]+\$/xms ) }; |
106 | } |
107 | : () |
108 | ); |
109 | |
110 | coerce UpperCaseStr, |
111 | from NonEmptyStr, |
112 | via { uc }; |
113 | |
114 | subtype LowerCaseSimpleStr, |
115 | as NonEmptySimpleStr, |
116 | where { /^[a-z]+$/x }, |
117 | message { "Must only contain lower case letters" }, |
118 | ( $Moose::VERSION >= 2.0200 |
119 | ? inline_as { |
120 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
121 | . qq{ ( $_[1] =~ m/^[a-z]+\$/x ) }; |
122 | } |
123 | : () |
124 | ); |
125 | |
126 | coerce LowerCaseSimpleStr, |
127 | from NonEmptySimpleStr, |
128 | via { lc }; |
129 | |
130 | subtype UpperCaseSimpleStr, |
131 | as NonEmptySimpleStr, |
132 | where { /^[A-Z]+$/x }, |
133 | message { "Must only contain upper case letters" }, |
134 | ( $Moose::VERSION >= 2.0200 |
135 | ? inline_as { |
136 | $_[0]->parent()->_inline_check( $_[1] ) . ' && ' |
137 | . qq{ ( $_[1] =~ m/^[A-Z]+\$/x ) }; |
138 | } |
139 | : () |
140 | ); |
141 | |
142 | coerce UpperCaseSimpleStr, |
143 | from NonEmptySimpleStr, |
144 | via { uc }; |
ac73ab52 |
145 | |
146 | 1; |
147 | |
148 | =head1 NAME |
149 | |
6820e939 |
150 | MooseX::Types::Common::String - Commonly used string types |
ac73ab52 |
151 | |
152 | =head1 SYNOPSIS |
153 | |
154 | use MooseX::Types::Common::String qw/SimpleStr/; |
155 | has short_str => (is => 'rw', isa => SimpleStr); |
156 | |
157 | ... |
158 | #this will fail |
159 | $object->short_str("string\nwith\nbreaks"); |
160 | |
161 | =head1 DESCRIPTION |
162 | |
163 | A set of commonly-used string type constraints that do not ship with Moose by |
164 | default. |
165 | |
166 | =over |
167 | |
168 | =item * SimpleStr |
169 | |
170 | A Str with no new-line characters. |
171 | |
172 | =item * NonEmptySimpleStr |
173 | |
a5c9a433 |
174 | A Str with no new-line characters and length > 0 |
175 | |
176 | =item * LowerCaseSimpleStr |
177 | |
178 | A Str with no new-line characters, length > 0 and all lowercase characters |
179 | A coercion exists via C<lc> from NonEmptySimpleStr |
180 | |
181 | =item * UpperCaseSimpleStr |
182 | |
183 | A Str with no new-line characters, length > 0 and all uppercase characters |
184 | A coercion exists via C<uc> from NonEmptySimpleStr |
ac73ab52 |
185 | |
186 | =item * Password |
187 | |
188 | =item * StrongPassword |
189 | |
190 | =item * NonEmptyStr |
191 | |
a5c9a433 |
192 | A Str with length > 0 |
193 | |
194 | =item * LowerCaseStr |
195 | |
196 | A Str with length > 0 and all lowercase characters. |
197 | A coercion exists via C<lc> from NonEmptyStr |
198 | |
199 | =item * UpperCaseStr |
200 | |
201 | A Str with length > 0 and all uppercase characters. |
202 | A coercion exists via C<uc> from NonEmptyStr |
203 | |
ac73ab52 |
204 | =back |
205 | |
206 | =head1 SEE ALSO |
207 | |
208 | =over |
209 | |
210 | =item * L<MooseX::Types::Common::Numeric> |
211 | |
212 | =back |
213 | |
214 | =head1 AUTHORS |
215 | |
216 | Please see:: L<MooseX::Types::Common> |
217 | |
218 | =cut |