| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package APISchema::Validator; |
|
2
|
3
|
|
|
3
|
|
2864
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
82
|
|
|
3
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
72
|
|
|
4
|
3
|
|
|
3
|
|
60
|
use 5.014; |
|
|
3
|
|
|
|
|
18
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# cpan |
|
7
|
3
|
|
|
3
|
|
523
|
use Class::Load qw(load_class); |
|
|
3
|
|
|
|
|
16858
|
|
|
|
3
|
|
|
|
|
192
|
|
|
8
|
|
|
|
|
|
|
use Class::Accessor::Lite::Lazy ( |
|
9
|
3
|
|
|
|
|
23
|
ro => [qw(fetch_resource_method)], |
|
10
|
|
|
|
|
|
|
ro_lazy => [qw(validator_class)], |
|
11
|
3
|
|
|
3
|
|
1427
|
); |
|
|
3
|
|
|
|
|
3360
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# lib |
|
14
|
3
|
|
|
3
|
|
381
|
use APISchema::Resource; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
86
|
|
|
15
|
3
|
|
|
3
|
|
1007
|
use APISchema::Validator::Decoder; |
|
|
3
|
|
|
|
|
8
|
|
|
|
3
|
|
|
|
|
103
|
|
|
16
|
3
|
|
|
3
|
|
997
|
use APISchema::Validator::Result; |
|
|
3
|
|
|
|
|
9
|
|
|
|
3
|
|
|
|
|
146
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use constant +{ |
|
19
|
3
|
|
|
|
|
2587
|
DEFAULT_VALIDATOR_CLASS => 'Valiemon', |
|
20
|
|
|
|
|
|
|
TARGETS => [qw(header parameter body)], |
|
21
|
|
|
|
|
|
|
DEFAULT_ENCODING_SPEC => { |
|
22
|
|
|
|
|
|
|
'application/json' => 'json', |
|
23
|
|
|
|
|
|
|
'application/x-www-form-urlencoded' => 'url_parameter', |
|
24
|
|
|
|
|
|
|
# TODO yaml, xml |
|
25
|
|
|
|
|
|
|
}, |
|
26
|
3
|
|
|
3
|
|
19
|
}; |
|
|
3
|
|
|
|
|
4
|
|
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _build_validator_class { |
|
29
|
30
|
|
|
30
|
|
1473
|
return DEFAULT_VALIDATOR_CLASS; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _new { |
|
33
|
65
|
|
|
65
|
|
93
|
my $class = shift; |
|
34
|
65
|
50
|
33
|
|
|
396
|
return bless { @_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_ }, $class; |
|
|
0
|
|
|
|
|
0
|
|
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub for_request { |
|
38
|
34
|
|
|
34
|
0
|
3538
|
my $class = shift; |
|
39
|
34
|
|
|
|
|
99
|
return $class->_new(@_, fetch_resource_method => 'canonical_request_resource'); |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub for_response { |
|
43
|
31
|
|
|
31
|
0
|
3813
|
my $class = shift; |
|
44
|
31
|
|
|
|
|
117
|
return $class->_new(@_, fetch_resource_method => 'canonical_response_resource'); |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
94
|
|
|
94
|
|
519
|
sub _valid_result { APISchema::Validator::Result->new_valid(@_) } |
|
48
|
40
|
|
|
40
|
|
165
|
sub _error_result { APISchema::Validator::Result->new_error(@_) } |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub _resolve_encoding { |
|
51
|
46
|
|
|
46
|
|
134
|
my ($content_type, $encoding_spec) = @_; |
|
52
|
|
|
|
|
|
|
# TODO handle charset? |
|
53
|
46
|
|
|
|
|
122
|
$content_type = $content_type =~ s/\s*;.*$//r; |
|
54
|
46
|
|
100
|
|
|
152
|
$encoding_spec //= DEFAULT_ENCODING_SPEC; |
|
55
|
|
|
|
|
|
|
|
|
56
|
46
|
100
|
|
|
|
108
|
if (ref $encoding_spec) { |
|
57
|
33
|
|
|
|
|
76
|
$encoding_spec = $encoding_spec->{$content_type}; |
|
58
|
33
|
100
|
|
|
|
108
|
return ( undef, { message => "Wrong content-type: $content_type" } ) |
|
59
|
|
|
|
|
|
|
unless $encoding_spec; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
38
|
|
|
|
|
56
|
my $method = $encoding_spec; |
|
63
|
|
|
|
|
|
|
return ( undef, { |
|
64
|
38
|
100
|
|
|
|
226
|
message => "Unknown decoding method: $method", |
|
65
|
|
|
|
|
|
|
content_type => $content_type, |
|
66
|
|
|
|
|
|
|
} ) |
|
67
|
|
|
|
|
|
|
unless APISchema::Validator::Decoder->new->can($method); |
|
68
|
|
|
|
|
|
|
|
|
69
|
36
|
|
|
|
|
375
|
return ($method, undef); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub _validate { |
|
73
|
57
|
|
|
57
|
|
139
|
my ($validator_class, $decode, $target, $spec) = @_; |
|
74
|
|
|
|
|
|
|
|
|
75
|
57
|
|
|
|
|
90
|
my $obj = eval { APISchema::Validator::Decoder->new->$decode($target) }; |
|
|
57
|
|
|
|
|
213
|
|
|
76
|
57
|
100
|
|
|
|
1112
|
return { message => "Failed to parse $decode" } if $@; |
|
77
|
|
|
|
|
|
|
|
|
78
|
53
|
|
|
|
|
145
|
my $validator = $validator_class->new($spec->definition); |
|
79
|
53
|
|
|
|
|
984
|
my ($valid, $err) = $validator->validate($obj); |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return { |
|
82
|
53
|
100
|
|
|
|
169260
|
attribute => $err->attribute, |
|
83
|
|
|
|
|
|
|
position => $err->position, |
|
84
|
|
|
|
|
|
|
expected => $err->expected, |
|
85
|
|
|
|
|
|
|
actual => $err->actual, |
|
86
|
26
|
|
|
|
|
543
|
message => "Contents do not match resource '@{[$spec->title]}'", |
|
87
|
|
|
|
|
|
|
} unless $valid; |
|
88
|
|
|
|
|
|
|
|
|
89
|
27
|
|
|
|
|
159
|
return; # avoid returning the last conditional value |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub validate { |
|
93
|
67
|
|
|
67
|
0
|
19212
|
my ($self, $route_name, $target, $schema) = @_; |
|
94
|
|
|
|
|
|
|
|
|
95
|
67
|
|
|
|
|
109
|
my @target_keys = @{+TARGETS}; |
|
|
67
|
|
|
|
|
190
|
|
|
96
|
67
|
|
|
|
|
166
|
my $valid = _valid_result(@target_keys); |
|
97
|
|
|
|
|
|
|
|
|
98
|
67
|
100
|
|
|
|
657
|
my $route = $schema->get_route_by_name($route_name) |
|
99
|
|
|
|
|
|
|
or return $valid; |
|
100
|
65
|
|
|
|
|
208
|
my $method = $self->fetch_resource_method; |
|
101
|
65
|
|
|
|
|
533
|
my $resource_root = $schema->get_resource_root; |
|
102
|
|
|
|
|
|
|
my $resource_spec = $route->$method( |
|
103
|
|
|
|
|
|
|
$resource_root, |
|
104
|
65
|
100
|
|
|
|
784
|
$target->{status_code} ? [ $target->{status_code} ] : [], |
|
105
|
|
|
|
|
|
|
[ @target_keys ], |
|
106
|
|
|
|
|
|
|
); |
|
107
|
65
|
|
|
|
|
745
|
@target_keys = grep { $resource_spec->{$_} } @target_keys; |
|
|
195
|
|
|
|
|
370
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
65
|
|
100
|
|
|
203
|
my $body_encoding = $resource_spec->{body} && do { |
|
110
|
|
|
|
|
|
|
my ($enc, $err) = _resolve_encoding( |
|
111
|
|
|
|
|
|
|
$target->{content_type} // '', |
|
112
|
|
|
|
|
|
|
$resource_spec->{encoding}, |
|
113
|
|
|
|
|
|
|
); |
|
114
|
|
|
|
|
|
|
if ($err) { |
|
115
|
|
|
|
|
|
|
return _error_result(body => $err); |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
$enc; |
|
118
|
|
|
|
|
|
|
}; |
|
119
|
|
|
|
|
|
|
|
|
120
|
55
|
|
|
|
|
181
|
my $encoding = { |
|
121
|
|
|
|
|
|
|
body => $body_encoding, |
|
122
|
|
|
|
|
|
|
parameter => 'url_parameter', |
|
123
|
|
|
|
|
|
|
header => 'perl', |
|
124
|
|
|
|
|
|
|
}; |
|
125
|
|
|
|
|
|
|
|
|
126
|
55
|
|
|
|
|
223
|
my $validator_class = $self->validator_class; |
|
127
|
55
|
|
|
|
|
379
|
load_class $validator_class; |
|
128
|
55
|
|
|
|
|
32336
|
my $result = APISchema::Validator::Result->new; |
|
129
|
55
|
|
|
|
|
381
|
$result->merge($_) for map { |
|
130
|
57
|
|
|
|
|
136
|
my $field = $_; |
|
131
|
57
|
|
|
|
|
115
|
my $err = _validate($validator_class, map { $_->{$field} } ( |
|
|
171
|
|
|
|
|
373
|
|
|
132
|
|
|
|
|
|
|
$encoding, $target, $resource_spec, |
|
133
|
|
|
|
|
|
|
)); |
|
134
|
|
|
|
|
|
|
$err ? _error_result($field => { |
|
135
|
|
|
|
|
|
|
%$err, |
|
136
|
57
|
100
|
|
|
|
650
|
encoding => $encoding->{$_}, |
|
137
|
|
|
|
|
|
|
}) : _valid_result($field); |
|
138
|
|
|
|
|
|
|
} @target_keys; |
|
139
|
|
|
|
|
|
|
|
|
140
|
55
|
|
|
|
|
530
|
return $result; |
|
141
|
|
|
|
|
|
|
} |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
|
144
|
|
|
|
|
|
|
__END__ |