1package MooseX::Types::Common::String; 2# ABSTRACT: Commonly used string types 3 4our $VERSION = '0.001014'; 5 6use strict; 7use warnings; 8 9use MooseX::Types -declare => [ 10 qw(SimpleStr 11 NonEmptySimpleStr 12 NumericCode 13 LowerCaseSimpleStr 14 UpperCaseSimpleStr 15 Password 16 StrongPassword 17 NonEmptyStr 18 LowerCaseStr 19 UpperCaseStr) 20]; 21 22use MooseX::Types::Moose qw/Str/; 23use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean'; 24 25subtype SimpleStr, 26 as Str, 27 where { (length($_) <= 255) && ($_ !~ m/\n/) }, 28 message { "Must be a single line of no more than 255 chars" }, 29 ( $Moose::VERSION >= 2.0200 30 ? inline_as { 31 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 32 . qq{ ( (length($_[1]) <= 255) && ($_[1] !~ m/\n/) ) }; 33 } 34 : () 35 ); 36 37subtype NonEmptySimpleStr, 38 as SimpleStr, 39 where { length($_) > 0 }, 40 message { "Must be a non-empty single line of no more than 255 chars" }, 41 ( $Moose::VERSION >= 2.0200 42 ? inline_as { 43 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 44 . qq{ (length($_[1]) > 0) }; 45 } 46 : () 47 ); 48 49subtype NumericCode, 50 as NonEmptySimpleStr, 51 where { $_ =~ m/^[0-9]+$/ }, 52 message { 53 'Must be a non-empty single line of no more than 255 chars that consists ' 54 . 'of numeric characters only' 55 }, 56 ( $Moose::VERSION >= 2.0200 57 ? inline_as { 58 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 59 . qq{ $_[1] =~ m/^[0-9]+\$/ }; 60 } 61 : () 62 ); 63 64coerce NumericCode, 65 from NonEmptySimpleStr, 66 via { my $code = $_; $code =~ s/[[:punct:][:space:]]//g; return $code }; 67 68subtype Password, 69 as NonEmptySimpleStr, 70 where { length($_) > 3 }, 71 message { "Must be between 4 and 255 chars" }, 72 ( $Moose::VERSION >= 2.0200 73 ? inline_as { 74 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 75 . qq{ (length($_[1]) > 3) }; 76 } 77 : () 78 ); 79 80subtype StrongPassword, 81 as Password, 82 where { (length($_) > 7) && (m/[^a-zA-Z]/) }, 83 message {"Must be between 8 and 255 chars, and contain a non-alpha char" }, 84 ( $Moose::VERSION >= 2.0200 85 ? inline_as { 86 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 87 . qq{ ( (length($_[1]) > 7) && ($_[1] =~ m/[^a-zA-Z]/) ) }; 88 } 89 : () 90 ); 91 92subtype NonEmptyStr, 93 as Str, 94 where { length($_) > 0 }, 95 message { "Must not be empty" }, 96 ( $Moose::VERSION >= 2.0200 97 ? inline_as { 98 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 99 . qq{ (length($_[1]) > 0) }; 100 } 101 : () 102 ); 103 104subtype LowerCaseStr, 105 as NonEmptyStr, 106 where { !/\p{Upper}/ms }, 107 message { "Must not contain upper case letters" }, 108 ( $Moose::VERSION >= 2.0200 109 ? inline_as { 110 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 111 . qq{ ( $_[1] !~ /\\p{Upper}/ms ) }; 112 } 113 : () 114 ); 115 116coerce LowerCaseStr, 117 from NonEmptyStr, 118 via { lc }; 119 120subtype UpperCaseStr, 121 as NonEmptyStr, 122 where { !/\p{Lower}/ms }, 123 message { "Must not contain lower case letters" }, 124 ( $Moose::VERSION >= 2.0200 125 ? inline_as { 126 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 127 . qq{ ( $_[1] !~ /\\p{Lower}/ms ) }; 128 } 129 : () 130 ); 131 132coerce UpperCaseStr, 133 from NonEmptyStr, 134 via { uc }; 135 136subtype LowerCaseSimpleStr, 137 as NonEmptySimpleStr, 138 where { !/\p{Upper}/ }, 139 message { "Must not contain upper case letters" }, 140 ( $Moose::VERSION >= 2.0200 141 ? inline_as { 142 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 143 . qq{ ( $_[1] !~ /\\p{Upper}/ ) }; 144 } 145 : () 146 ); 147 148coerce LowerCaseSimpleStr, 149 from NonEmptySimpleStr, 150 via { lc }; 151 152subtype UpperCaseSimpleStr, 153 as NonEmptySimpleStr, 154 where { !/\p{Lower}/ }, 155 message { "Must not contain lower case letters" }, 156 ( $Moose::VERSION >= 2.0200 157 ? inline_as { 158 $_[0]->parent()->_inline_check( $_[1] ) . ' && ' 159 . qq{ ( $_[1] !~ /\\p{Lower}/ ) }; 160 } 161 : () 162 ); 163 164coerce UpperCaseSimpleStr, 165 from NonEmptySimpleStr, 166 via { uc }; 167 1681; 169 170__END__ 171 172=pod 173 174=encoding UTF-8 175 176=head1 NAME 177 178MooseX::Types::Common::String - Commonly used string types 179 180=head1 VERSION 181 182version 0.001014 183 184=head1 SYNOPSIS 185 186 use MooseX::Types::Common::String qw/SimpleStr/; 187 has short_str => (is => 'rw', isa => SimpleStr); 188 189 ... 190 #this will fail 191 $object->short_str("string\nwith\nbreaks"); 192 193=head1 DESCRIPTION 194 195A set of commonly-used string type constraints that do not ship with Moose by 196default. 197 198=over 199 200=item * C<SimpleStr> 201 202A C<Str> with no new-line characters and length <= 255. 203 204=item * C<NonEmptySimpleStr> 205 206A C<SimpleStr> with length > 0. 207 208=item * C<LowerCaseSimpleStr> 209 210A C<NonEmptySimpleStr> with no uppercase characters. A coercion exists via 211C<lc> from C<NonEmptySimpleStr>. 212 213=item * C<UpperCaseSimpleStr> 214 215A C<NonEmptySimpleStr> with no lowercase characters. A coercion exists via 216C<uc> from C<NonEmptySimpleStr>. 217 218=item * C<Password> 219 220A C<NonEmptySimpleStr> with length > 3. 221 222=item * C<StrongPassword> 223 224A C<NonEmptySimpleStr> with length > 7 containing at least one non-alpha 225character. 226 227=item * C<NonEmptyStr> 228 229A C<Str> with length > 0. 230 231=item * C<LowerCaseStr> 232 233A C<Str> with length > 0 and no uppercase characters. 234A coercion exists via C<lc> from C<NonEmptyStr>. 235 236=item * C<UpperCaseStr> 237 238A C<Str> with length > 0 and no lowercase characters. 239A coercion exists via C<uc> from C<NonEmptyStr>. 240 241=item * C<NumericCode> 242 243A C<Str> with no new-line characters that consists of only Numeric characters. 244Examples include, Social Security Numbers, Personal Identification Numbers, Postal Codes, HTTP Status 245Codes, etc. Supports attempting to coerce from a string that has punctuation 246or whitespaces in it ( e.g credit card number 4111-1111-1111-1111 ). 247 248=back 249 250=head1 SEE ALSO 251 252=over 253 254=item * L<MooseX::Types::Common::Numeric> 255 256=back 257 258=head1 SUPPORT 259 260Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Common> 261(or L<bug-MooseX-Types-Common@rt.cpan.org|mailto:bug-MooseX-Types-Common@rt.cpan.org>). 262 263There is also a mailing list available for users of this distribution, at 264L<http://lists.perl.org/list/moose.html>. 265 266There is also an irc channel available for users of this distribution, at 267L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>. 268 269=head1 AUTHORS 270 271=over 4 272 273=item * 274 275Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>) 276 277=item * 278 279K. James Cheetham <jamie@shadowcatsystems.co.uk> 280 281=item * 282 283Guillermo Roditi <groditi@gmail.com> 284 285=back 286 287=head1 COPYRIGHT AND LICENSE 288 289This software is copyright (c) 2009 by Matt S Trout - mst (at) shadowcatsystems.co.uk (L<http://www.shadowcatsystems.co.uk/>). 290 291This is free software; you can redistribute it and/or modify it under 292the same terms as the Perl 5 programming language system itself. 293 294=cut 295