line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
37
|
|
|
37
|
|
34321
|
use strict; |
|
37
|
|
|
|
|
81
|
|
|
37
|
|
|
|
|
2772
|
|
3
|
37
|
|
|
37
|
|
231
|
use warnings; |
|
37
|
|
|
|
|
78
|
|
|
37
|
|
|
|
|
3270
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Validator::Declarative::Rules::SimpleType; |
6
|
|
|
|
|
|
|
{ |
7
|
|
|
|
|
|
|
$Validator::Declarative::Rules::SimpleType::VERSION = '1.20130722.2105'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: Declarative parameters validation - default simple types rules |
11
|
|
|
|
|
|
|
|
12
|
37
|
|
|
37
|
|
317
|
use Error qw/ :try /; |
|
37
|
|
|
|
|
101
|
|
|
37
|
|
|
|
|
276
|
|
13
|
37
|
|
|
37
|
|
51424
|
use Email::Valid; |
|
37
|
|
|
|
|
6839819
|
|
|
37
|
|
|
|
|
14532
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# INTERNALS |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
sub _validate_bool { |
19
|
23
|
|
|
23
|
|
30
|
my ($input) = @_; |
20
|
23
|
100
|
100
|
|
|
187
|
throw Error::Simple('does not satisfy BOOL') |
21
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^(1|true|t|yes|y|0|false|f|no|n|)$/i; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _validate_float { |
25
|
16
|
|
|
16
|
|
20
|
my ($input) = @_; |
26
|
16
|
100
|
100
|
|
|
227
|
throw Error::Simple('does not satisfy FLOAT') |
27
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^[+-]?\d+(:?\.\d*)?$/; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _validate_int { |
31
|
3992
|
|
|
3992
|
|
5236
|
my ($input) = @_; |
32
|
3992
|
100
|
100
|
|
|
53344
|
throw Error::Simple('does not satisfy INT') |
33
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^[+-]?\d+$/; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _validate_positive { |
37
|
20
|
|
|
20
|
|
32
|
my ($input) = @_; |
38
|
37
|
|
|
37
|
|
501
|
no warnings; |
|
37
|
|
|
|
|
98
|
|
|
37
|
|
|
|
|
6656
|
|
39
|
20
|
100
|
100
|
|
|
160
|
throw Error::Simple('does not satisfy POSITIVE') |
40
|
|
|
|
|
|
|
if ref($input) || $input <= 0; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _validate_negative { |
44
|
14
|
|
|
14
|
|
24
|
my ($input) = @_; |
45
|
37
|
|
|
37
|
|
239
|
no warnings; |
|
37
|
|
|
|
|
137
|
|
|
37
|
|
|
|
|
11674
|
|
46
|
14
|
100
|
100
|
|
|
108
|
throw Error::Simple('does not satisfy NEGATIVE') |
47
|
|
|
|
|
|
|
if ref($input) || $input >= 0; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _validate_id { |
51
|
14
|
|
|
14
|
|
23
|
my ($input) = @_; |
52
|
|
|
|
|
|
|
try { |
53
|
14
|
|
|
14
|
|
326
|
_validate_int($input); |
54
|
6
|
|
|
|
|
12
|
_validate_positive($input); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
catch Error with { |
57
|
12
|
|
|
12
|
|
1441
|
throw Error::Simple('does not satisfy ID'); |
58
|
14
|
|
|
|
|
79
|
}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _validate_email { |
62
|
18
|
|
|
18
|
|
28
|
my ($input) = @_; |
63
|
18
|
100
|
100
|
|
|
96
|
throw Error::Simple('does not satisfy EMAIL') |
64
|
|
|
|
|
|
|
if ref($input) || !Email::Valid->address($input); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _validate_year { |
68
|
3213
|
|
|
3213
|
|
4779
|
my ($input) = @_; |
69
|
|
|
|
|
|
|
try { |
70
|
3213
|
|
|
3213
|
|
81898
|
_validate_int($input); |
71
|
37
|
|
|
37
|
|
224
|
no warnings; |
|
37
|
|
|
|
|
75
|
|
|
37
|
|
|
|
|
14737
|
|
72
|
3205
|
100
|
66
|
|
|
31551
|
die('bad') if ref($input) || $input < 1970 || $input > 3000; |
|
|
|
100
|
|
|
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
catch Error with { |
75
|
2182
|
|
|
2182
|
|
251355
|
throw Error::Simple('does not satisfy YEAR'); |
76
|
3213
|
|
|
|
|
26341
|
}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _validate_week { |
80
|
266
|
|
|
266
|
|
912
|
my ($input) = @_; |
81
|
|
|
|
|
|
|
try { |
82
|
266
|
|
|
266
|
|
8798
|
_validate_int($input); |
83
|
37
|
|
|
37
|
|
224
|
no warnings; |
|
37
|
|
|
|
|
88
|
|
|
37
|
|
|
|
|
9205
|
|
84
|
258
|
100
|
66
|
|
|
2900
|
die('bad') if ref($input) || $input < 1 || $input > 53; |
|
|
|
100
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
catch Error with { |
87
|
213
|
|
|
213
|
|
41716
|
throw Error::Simple('does not satisfy WEEK'); |
88
|
266
|
|
|
|
|
1621
|
}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _validate_month { |
92
|
225
|
|
|
225
|
|
293
|
my ($input) = @_; |
93
|
|
|
|
|
|
|
try { |
94
|
225
|
|
|
225
|
|
4042
|
_validate_int($input); |
95
|
37
|
|
|
37
|
|
213
|
no warnings; |
|
37
|
|
|
|
|
77
|
|
|
37
|
|
|
|
|
6539
|
|
96
|
217
|
100
|
66
|
|
|
2200
|
die('bad') if ref($input) || $input < 1 || $input > 12; |
|
|
|
100
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
catch Error with { |
99
|
213
|
|
|
213
|
|
20819
|
throw Error::Simple('does not satisfy MONTH'); |
100
|
225
|
|
|
|
|
1114
|
}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _validate_day { |
104
|
244
|
|
|
244
|
|
420
|
my ($input) = @_; |
105
|
|
|
|
|
|
|
try { |
106
|
244
|
|
|
244
|
|
5082
|
_validate_int($input); |
107
|
37
|
|
|
37
|
|
281
|
no warnings; |
|
37
|
|
|
|
|
177
|
|
|
37
|
|
|
|
|
14204
|
|
108
|
236
|
100
|
66
|
|
|
2300
|
die('bad') if ref($input) || $input < 1 || $input > 31; |
|
|
|
100
|
|
|
|
|
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
catch Error with { |
111
|
213
|
|
|
213
|
|
25670
|
throw Error::Simple('does not satisfy DAY'); |
112
|
244
|
|
|
|
|
1260
|
}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _validate_ymd { |
116
|
7102
|
|
|
7102
|
|
9669
|
my ($input) = @_; |
117
|
37
|
|
|
37
|
|
247
|
no warnings; |
|
37
|
|
|
|
|
104
|
|
|
37
|
|
|
|
|
9123
|
|
118
|
7102
|
100
|
100
|
|
|
157228
|
throw Error::Simple('does not satisfy YMD') |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
119
|
|
|
|
|
|
|
if ref($input) |
120
|
|
|
|
|
|
|
|| $input !~ m/^(\d{4})-(\d{2})-(\d{2})$/ |
121
|
|
|
|
|
|
|
|| $1 < 1970 |
122
|
|
|
|
|
|
|
|| $1 > 3000 |
123
|
|
|
|
|
|
|
|| $2 < 1 |
124
|
|
|
|
|
|
|
|| $2 > 12 |
125
|
|
|
|
|
|
|
|| $3 < 1 |
126
|
|
|
|
|
|
|
|| $3 > 31; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _validate_mdy { |
130
|
43930
|
|
|
43930
|
|
63200
|
my ($input) = @_; |
131
|
37
|
|
|
37
|
|
283
|
no warnings; |
|
37
|
|
|
|
|
98
|
|
|
37
|
|
|
|
|
7033
|
|
132
|
43930
|
100
|
100
|
|
|
1148277
|
throw Error::Simple('does not satisfy MDY') |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
133
|
|
|
|
|
|
|
if ref($input) |
134
|
|
|
|
|
|
|
|| $input !~ m|^(\d\d?)/(\d\d?)/(\d\d(?:\d\d)?)$| |
135
|
|
|
|
|
|
|
|| $1 < 1 |
136
|
|
|
|
|
|
|
|| $1 > 12 |
137
|
|
|
|
|
|
|
|| $2 < 1 |
138
|
|
|
|
|
|
|
|| $2 > 31 |
139
|
|
|
|
|
|
|
|| !( $3 > 0 && $3 < 100 || $3 >= 1970 && $3 <= 3000 ); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _validate_time { |
143
|
86416
|
|
|
86416
|
|
113213
|
my ($input) = @_; |
144
|
37
|
|
|
37
|
|
242
|
no warnings; |
|
37
|
|
|
|
|
223
|
|
|
37
|
|
|
|
|
5358
|
|
145
|
86416
|
100
|
100
|
|
|
1326519
|
throw Error::Simple('does not satisfy TIME') |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
146
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^(\d\d):(\d\d):(\d\d)$/ || $1 > 23 || $2 > 59 || $3 > 59; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub _validate_hhmm { |
150
|
1455
|
|
|
1455
|
|
2408
|
my ($input) = @_; |
151
|
37
|
|
|
37
|
|
225
|
no warnings; |
|
37
|
|
|
|
|
965
|
|
|
37
|
|
|
|
|
5680
|
|
152
|
1455
|
100
|
100
|
|
|
25338
|
throw Error::Simple('does not satisfy HHMM') |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
153
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^(\d\d):(\d\d)$/ || $1 > 23 || $2 > 59; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _validate_timestamp { |
157
|
32
|
|
|
32
|
|
48
|
my ($input) = @_; |
158
|
|
|
|
|
|
|
## almost same as float, but can't have sign |
159
|
|
|
|
|
|
|
## we can't use _validate_float && _validate_positive because input can be zero |
160
|
37
|
|
|
37
|
|
217
|
no warnings; |
|
37
|
|
|
|
|
61
|
|
|
37
|
|
|
|
|
11023
|
|
161
|
32
|
100
|
100
|
|
|
357
|
throw Error::Simple('does not satisfy TIMESTAMP') |
162
|
|
|
|
|
|
|
if ref($input) || $input !~ m/^\d+(:?\.\d*)?$/; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub _register_default_simple_types { |
166
|
37
|
|
|
37
|
|
229
|
require Validator::Declarative; |
167
|
37
|
|
|
|
|
488
|
Validator::Declarative::register_type( |
168
|
|
|
|
|
|
|
## generic types |
169
|
|
|
|
|
|
|
bool => \&_validate_bool, |
170
|
|
|
|
|
|
|
float => \&_validate_float, |
171
|
|
|
|
|
|
|
int => \&_validate_int, |
172
|
|
|
|
|
|
|
integer => \&_validate_int, |
173
|
|
|
|
|
|
|
positive => \&_validate_positive, |
174
|
|
|
|
|
|
|
negative => \&_validate_negative, |
175
|
|
|
|
|
|
|
id => \&_validate_id, |
176
|
|
|
|
|
|
|
email => \&_validate_email, |
177
|
|
|
|
|
|
|
## datetime-like types |
178
|
|
|
|
|
|
|
year => \&_validate_year, |
179
|
|
|
|
|
|
|
week => \&_validate_week, |
180
|
|
|
|
|
|
|
month => \&_validate_month, |
181
|
|
|
|
|
|
|
day => \&_validate_day, |
182
|
|
|
|
|
|
|
ymd => \&_validate_ymd, |
183
|
|
|
|
|
|
|
mdy => \&_validate_mdy, |
184
|
|
|
|
|
|
|
time => \&_validate_time, |
185
|
|
|
|
|
|
|
hhmm => \&_validate_hhmm, |
186
|
|
|
|
|
|
|
timestamp => \&_validate_timestamp, |
187
|
|
|
|
|
|
|
msec => \&_validate_timestamp, |
188
|
|
|
|
|
|
|
); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
_register_default_simple_types(); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
1; # End of Validator::Declarative::Rules::SimpleType |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
__END__ |