initial checkin
[gitmo/MooseX-Types-Common.git] / lib / MooseX / Types / Common.pm
1 package MooseX::Types::Common;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = '0.001000';
7
8 use MooseX::Types -declare => [
9   qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr
10      PositiveNum PositiveInt NegativeNum NegativeInt SingleDigit)
11 ];
12
13 use MooseX::Types::Moose qw/Str Num Int Object/;
14
15 subtype SimpleStr,
16   as Str,
17   where { (length($_) <= 255) && ($_ !~ m/\n/) },
18   message { "Must be a single line of no more than 255 chars" };
19
20 subtype NonEmptySimpleStr,
21   as SimpleStr,
22   where { length($_) > 0 },
23   message { "Must be a non-empty single line of no more than 255 chars" };
24
25 # XXX duplicating constraint msges since moose only uses last message
26 subtype Password,
27   as NonEmptySimpleStr,
28   where { length($_) > 3 },
29   message { "Must be between 4 and 255 chars" };
30
31 subtype StrongPassword,
32   as Password,
33   where { (length($_) > 7) && (m/[^a-zA-Z]/) },
34   message {"Must be between 8 and 255 chars, and contain a non-alpha char" };
35
36 subtype NonEmptyStr,
37   as Str,
38   where { length($_) > 0 },
39   message { "Must not be empty" };
40
41 subtype PositiveNum,
42   as Num,
43   where { $_ >= 0 },
44   message { "Must be a positive number" };
45
46 subtype PositiveInt,
47   as Int,
48   where { $_ >= 0 },
49   message { "Must be a positive integer" };
50
51 subtype NegativeNum,
52   as Num,
53   where { $_ <= 0 },
54   message { "Must be a negative number" };
55
56 subtype NegativeInt,
57   as Int,
58   where { $_ <= 0 },
59   message { "Must be a negative integer" };
60
61 subtype SingleDigit,
62   as PositiveInt,
63   where { $_ <= 9 },
64   message { "Must be a single digit" };
65
66 1;
67
68 =head1 NAME
69
70 MooseX::Types::Common
71
72 =head1 SYNOPSIS
73
74     use MooseX::Types::Common qw/SimpleStr/;
75     has short_str => (is => 'rw', isa => SimpleStr);
76
77     ...
78     #this will fail
79     $object->short_str("string\nwith\nbreaks");
80
81 =head1 DESCRIPTION
82
83 A set of commonly-used type constraints that do not ship with Moose by default.
84
85 =over
86
87 =item * SimpleStr
88
89 A Str with no new-line characters.
90
91 =item * NonEmptySimpleStr
92
93 Does what it says on the tin.
94
95 =item * Password
96
97 =item * StrongPassword
98
99 =item * NonEmptyStr
100
101 =item * PositiveNum
102
103 =item * PositiveInt
104
105 =item * NegativeNum
106
107 =item * Int
108
109 =item * SingleDigit
110
111 =back
112
113 =head1 SEE ALSO
114
115 =over
116
117 =item * L<MooseX::Types>
118
119 =item * L<Moose::Util::TypeConstraints>
120
121 =back
122
123 =head1 AUTHORS
124
125 This distribution was extracted from the L<Reaction> code base by Guillermo
126 Roditi (groditi).
127
128 The original authors of this library are:
129
130 =over 4
131
132 =item * Matt S. Trout
133
134 =item * K. J. Cheetham
135
136 =item * Guillermo Roditi
137
138 =back
139
140 =head1 LICENSE
141
142 This library is free software, you can redistribute it and/or modify
143 it under the same terms as Perl itself.
144
145 =cut