line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
37
|
|
|
37
|
|
32973
|
use strict; |
|
37
|
|
|
|
|
76
|
|
|
37
|
|
|
|
|
2106
|
|
3
|
37
|
|
|
37
|
|
325
|
use warnings; |
|
37
|
|
|
|
|
70
|
|
|
37
|
|
|
|
|
2744
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Validator::Declarative::Rules::ParametrizedType; |
6
|
|
|
|
|
|
|
{ |
7
|
|
|
|
|
|
|
$Validator::Declarative::Rules::ParametrizedType::VERSION = '1.20130722.2105'; |
8
|
|
|
|
|
|
|
} |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: Declarative parameters validation - default simple types rules |
11
|
|
|
|
|
|
|
|
12
|
37
|
|
|
37
|
|
204
|
use Error qw/ :try /; |
|
37
|
|
|
|
|
78
|
|
|
37
|
|
|
|
|
304
|
|
13
|
37
|
|
|
37
|
|
8655
|
use Scalar::Util qw/ blessed looks_like_number /; |
|
37
|
|
|
|
|
76
|
|
|
37
|
|
|
|
|
4023
|
|
14
|
|
|
|
|
|
|
require Validator::Declarative; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# INTERNALS |
18
|
|
|
|
|
|
|
# |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _validate_min { |
21
|
13
|
|
|
13
|
|
18
|
my ( $input, $param ) = @_; |
22
|
37
|
|
|
37
|
|
215
|
no warnings; |
|
37
|
|
|
|
|
75
|
|
|
37
|
|
|
|
|
4879
|
|
23
|
13
|
100
|
66
|
|
|
144
|
throw Error::Simple('does not satisfy MIN') |
|
|
|
100
|
|
|
|
|
24
|
|
|
|
|
|
|
if !looks_like_number($input) || !looks_like_number($param) || $input < $param; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _validate_max { |
28
|
13
|
|
|
13
|
|
21
|
my ( $input, $param ) = @_; |
29
|
37
|
|
|
37
|
|
224
|
no warnings; |
|
37
|
|
|
|
|
74
|
|
|
37
|
|
|
|
|
56570
|
|
30
|
13
|
100
|
66
|
|
|
145
|
throw Error::Simple('does not satisfy MAX') |
|
|
|
100
|
|
|
|
|
31
|
|
|
|
|
|
|
if !looks_like_number($input) || !looks_like_number($param) || $input > $param; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _validate_ref { |
35
|
31
|
|
|
31
|
|
49
|
my ( $input, $param ) = @_; |
36
|
31
|
100
|
|
|
|
131
|
throw Error::Simple('does not satisfy REF') if !ref($input); |
37
|
16
|
100
|
|
|
|
48
|
return if !$param; |
38
|
11
|
100
|
|
|
|
33
|
$param = [$param] if ref($param) ne 'ARRAY'; |
39
|
11
|
|
100
|
|
|
63
|
ref($input) eq $_ && return for @$param; |
40
|
7
|
|
|
|
|
26
|
throw Error::Simple('does not satisfy REF'); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub _validate_class { |
44
|
42
|
|
|
42
|
|
73
|
my ( $input, $param ) = @_; |
45
|
42
|
100
|
|
|
|
247
|
throw Error::Simple('does not satisfy CLASS') if !blessed($input); |
46
|
15
|
100
|
|
|
|
45
|
return if !$param; |
47
|
10
|
100
|
|
|
|
32
|
$param = [$param] if ref($param) ne 'ARRAY'; |
48
|
10
|
|
100
|
|
|
143
|
$input->isa($_) && return for @$param; |
49
|
5
|
|
|
|
|
23
|
throw Error::Simple('does not satisfy CLASS'); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _validate_can { |
53
|
56
|
|
|
56
|
|
124
|
my ( $input, $param ) = @_; |
54
|
56
|
100
|
66
|
|
|
384
|
throw Error::Simple('does not satisfy CAN') if !blessed($input) || !$param; |
55
|
20
|
100
|
|
|
|
169
|
$param = [$param] if ref($param) ne 'ARRAY'; |
56
|
20
|
|
66
|
|
|
318
|
$input->can($_) || throw Error::Simple('does not satisfy CAN') for @$param; |
57
|
6
|
|
|
|
|
23
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _validate_can_any { |
61
|
14
|
|
|
14
|
|
22
|
my ( $input, $param ) = @_; |
62
|
14
|
100
|
66
|
|
|
89
|
throw Error::Simple('does not satisfy CAN_ANY') if !blessed($input) || !$param; |
63
|
5
|
50
|
|
|
|
13
|
$param = [$param] if ref($param) ne 'ARRAY'; |
64
|
5
|
|
100
|
|
|
84
|
$input->can($_) && return for @$param; |
65
|
3
|
|
|
|
|
11
|
throw Error::Simple('does not satisfy CAN_ANY'); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _validate_any_of { |
69
|
50
|
|
|
50
|
|
62
|
my ( $input, $param ) = @_; |
70
|
50
|
50
|
|
|
|
93
|
throw Error::Simple('does not satisfy ANY_OF') if !$param; |
71
|
50
|
50
|
|
|
|
106
|
$param = [$param] if ref($param) ne 'ARRAY'; |
72
|
50
|
|
100
|
|
|
153
|
_smart_match( $input, $_ ) && return for @$param; |
73
|
30
|
|
|
|
|
111
|
throw Error::Simple('does not satisfy ANY_OF'); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _validate_list_of { |
77
|
0
|
|
|
0
|
|
0
|
my ( $input, $param ) = @_; |
78
|
|
|
|
|
|
|
## TBD in next release |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub _validate_hash_of { |
82
|
0
|
|
|
0
|
|
0
|
my ( $input, $param ) = @_; |
83
|
|
|
|
|
|
|
## TBD in next release |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _validate_hash { |
87
|
0
|
|
|
0
|
|
0
|
my ( $input, $param ) = @_; |
88
|
|
|
|
|
|
|
## TBD in next release |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _validate_date { |
92
|
0
|
|
|
0
|
|
0
|
my ( $input, $param ) = @_; |
93
|
|
|
|
|
|
|
## TBD in next release |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# this is limited subset of smart-match operator (~~) from Perl 5.10+ |
97
|
|
|
|
|
|
|
sub _smart_match { |
98
|
248
|
|
|
248
|
|
564
|
my ( $x, $y ) = @_; |
99
|
248
|
100
|
100
|
|
|
770
|
looks_like_number($x) && looks_like_number($y) && return $x == $y; |
100
|
232
|
100
|
|
|
|
511
|
ref($y) eq 'CODE' && return $y->($x); |
101
|
188
|
100
|
|
|
|
510
|
ref($y) eq 'Regexp' && return $x =~ m/$y/; |
102
|
146
|
|
|
|
|
522
|
return $x eq $y; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _register_default_parametrized_types { |
106
|
37
|
|
|
37
|
|
309
|
Validator::Declarative::register_type( |
107
|
|
|
|
|
|
|
## simple parametrized types |
108
|
|
|
|
|
|
|
min => \&_validate_min, |
109
|
|
|
|
|
|
|
max => \&_validate_max, |
110
|
|
|
|
|
|
|
ref => \&_validate_ref, |
111
|
|
|
|
|
|
|
class => \&_validate_class, |
112
|
|
|
|
|
|
|
ducktype => \&_validate_can, |
113
|
|
|
|
|
|
|
can => \&_validate_can, |
114
|
|
|
|
|
|
|
can_any => \&_validate_can_any, |
115
|
|
|
|
|
|
|
any_of => \&_validate_any_of, |
116
|
|
|
|
|
|
|
enum => \&_validate_any_of, |
117
|
|
|
|
|
|
|
## complex/recursive parametrized types |
118
|
|
|
|
|
|
|
list_of => \&Validator::Declarative::_validate_pass, |
119
|
|
|
|
|
|
|
hash_of => \&Validator::Declarative::_validate_pass, |
120
|
|
|
|
|
|
|
hash => \&Validator::Declarative::_validate_pass, |
121
|
|
|
|
|
|
|
date => \&Validator::Declarative::_validate_pass, |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
_register_default_parametrized_types(); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; # End of Validator::Declarative::Rules::ParametrizedType |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |