blib/lib/APISchema/Validator.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 153 CC 11 SCAR 23.9
CC
11
Cov
0%
validate
30.0
_resolve_encoding
20.0
_validate
12.0
CRAP 10.9
1 package APISchema::Validator;
2 use strict;
3 use warnings;
4 use 5.014;
5  
6 # cpan
7 use Class::Load qw(load_class);
8 use Class::Accessor::Lite::Lazy (
9 ro => [qw(fetch_resource_method)],
10 ro_lazy => [qw(validator_class)],
11 );
12  
13 # lib
14 use APISchema::Resource;
15 use APISchema::Validator::Decoder;
16 use APISchema::Validator::Result;
17  
18 use constant +{
19 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 };
27  
28 sub _build_validator_class {
29 return DEFAULT_VALIDATOR_CLASS;
30 }
31  
32 sub _new {
33 my $class = shift;
34 return bless { @_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_ }, $class;
35 }
36  
37 sub for_request {
38 my $class = shift;
39 return $class->_new(@_, fetch_resource_method => 'canonical_request_resource');
40 }
41  
42 sub for_response {
43 my $class = shift;
44 return $class->_new(@_, fetch_resource_method => 'canonical_response_resource');
45 }
46  
47 sub _valid_result { APISchema::Validator::Result->new_valid(@_) }
48 sub _error_result { APISchema::Validator::Result->new_error(@_) }
49  
50 sub _resolve_encoding {
51 my ($content_type, $encoding_spec) = @_;
52 # TODO handle charset?
53 $content_type = $content_type =~ s/\s*;.*$//r;
54 $encoding_spec //= DEFAULT_ENCODING_SPEC;
55  
56 if (ref $encoding_spec) {
57 $encoding_spec = $encoding_spec->{$content_type};
58 return ( undef, { message => "Wrong content-type: $content_type" } )
59 unless $encoding_spec;
60 }
61  
62 my $method = $encoding_spec;
63 return ( undef, {
64 message => "Unknown decoding method: $method",
65 content_type => $content_type,
66 } )
67 unless APISchema::Validator::Decoder->new->can($method);
68  
69 return ($method, undef);
70 }
71  
72 sub _validate {
73 my ($validator_class, $decode, $target, $spec) = @_;
74  
75 my $obj = eval { APISchema::Validator::Decoder->new->$decode($target) };
76 return { message => "Failed to parse $decode" } if $@;
77  
78 my $validator = $validator_class->new($spec->definition);
79 my ($valid, $err) = $validator->validate($obj);
80  
81 return {
82 attribute => $err->attribute,
83 position => $err->position,
84 expected => $err->expected,
85 actual => $err->actual,
86 message => "Contents do not match resource '@{[$spec->title]}'",
87 } unless $valid;
88  
89 return; # avoid returning the last conditional value
90 }
91  
92 sub validate {
93 my ($self, $route_name, $target, $schema) = @_;
94  
95 my @target_keys = grep {
96 $target->{$_};
97 } @{+TARGETS};
98 my $valid = _valid_result(@target_keys);
99  
100 return $valid unless scalar @target_keys;
101  
102 my $route = $schema->get_route_by_name($route_name)
103 or return $valid;
104 my $method = $self->fetch_resource_method;
105 my $resource_root = $schema->get_resource_root;
106 my $resource_spec = $route->$method(
107 $resource_root,
108 $target->{status_code} ? [ $target->{status_code} ] : [],
109 [ @target_keys ],
110 );
111 @target_keys = grep { $resource_spec->{$_} } @target_keys;
112  
113 my $body_encoding = $target->{body} && do {
114 my ($enc, $err) = _resolve_encoding(
115 $target->{content_type} // '',
116 $resource_spec->{encoding},
117 );
118 if ($err && $resource_spec->{body}) {
119 return _error_result(body => $err);
120 }
121 $enc;
122 };
123  
124 my $encoding = {
125 body => $body_encoding,
126 parameter => 'url_parameter',
127 header => 'perl',
128 };
129  
130 my $validator_class = $self->validator_class;
131 load_class $validator_class;
132 my $result = APISchema::Validator::Result->new;
133 $result->merge($_) for map {
134 my $field = $_;
135 my $err = _validate($validator_class, map { $_->{$field} } (
136 $encoding, $target, $resource_spec,
137 ));
138 $err ? _error_result($field => {
139 %$err,
140 encoding => $encoding->{$_},
141 }) : _valid_result($field);
142 } @target_keys;
143  
144 return $result;
145 }
146  
147 1;
148 __END__