0b2775063e5274ec6fd8aa7eab6edf41af07630f
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common / String.pm
1 package MooseX::Types::Common::String;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.001003';
7
8 use MooseX::Types -declare => [
9   qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr)
10 ];
11
12 use MooseX::Types::Moose qw/Str/;
13
14 subtype SimpleStr,
15   as Str,
16   where { (length($_) <= 255) && ($_ !~ m/\n/) },
17   message { "Must be a single line of no more than 255 chars" },
18     ( $Moose::VERSION >= 2.0200
19         ? inline_as {
20             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
21                 . qq{ ( (length($_[1]) <= 255) && ($_[1] !~ m/\n/) ) };
22         }
23         : ()
24     );
25
26 subtype NonEmptySimpleStr,
27   as SimpleStr,
28   where { length($_) > 0 },
29   message { "Must be a non-empty single line of no more than 255 chars" },
30     ( $Moose::VERSION >= 2.0200
31         ? inline_as {
32             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
33                 . qq{ (length($_[1]) > 0) };
34         }
35         : ()
36     );
37
38 # XXX duplicating constraint msges since moose only uses last message
39 subtype Password,
40   as NonEmptySimpleStr,
41   where { length($_) > 3 },
42   message { "Must be between 4 and 255 chars" },
43     ( $Moose::VERSION >= 2.0200
44         ? inline_as {
45             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
46                 . qq{ (length($_[1]) > 3) };
47         }
48         : ()
49     );
50
51 subtype StrongPassword,
52   as Password,
53   where { (length($_) > 7) && (m/[^a-zA-Z]/) },
54   message {"Must be between 8 and 255 chars, and contain a non-alpha char" },
55     ( $Moose::VERSION >= 2.0200
56         ? inline_as {
57             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
58                 . qq{ ( (length($_[1]) > 7) && ($_[1] =~ m/[^a-zA-Z]/) ) };
59         }
60         : ()
61     );
62
63 subtype NonEmptyStr,
64   as Str,
65   where { length($_) > 0 },
66   message { "Must not be empty" },
67     ( $Moose::VERSION >= 2.0200
68         ? inline_as {
69             $_[0]->parent()->_inline_check( $_[1] ) . ' && '
70                 . qq{ (length($_[1]) > 0) };
71         }
72         : ()
73     );
74
75
76 1;
77
78 =head1 NAME
79
80 MooseX::Types::Common::String - Commonly used string types
81
82 =head1 SYNOPSIS
83
84     use MooseX::Types::Common::String qw/SimpleStr/;
85     has short_str => (is => 'rw', isa => SimpleStr);
86
87     ...
88     #this will fail
89     $object->short_str("string\nwith\nbreaks");
90
91 =head1 DESCRIPTION
92
93 A set of commonly-used string type constraints that do not ship with Moose by
94 default.
95
96 =over
97
98 =item * SimpleStr
99
100 A Str with no new-line characters.
101
102 =item * NonEmptySimpleStr
103
104 Does what it says on the tin.
105
106 =item * Password
107
108 =item * StrongPassword
109
110 =item * NonEmptyStr
111
112 =back
113
114 =head1 SEE ALSO
115
116 =over
117
118 =item * L<MooseX::Types::Common::Numeric>
119
120 =back
121
122 =head1 AUTHORS
123
124 Please see:: L<MooseX::Types::Common>
125
126 =cut