blib/lib/JSON/Validator/Schema/Troglodyne.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 95.4% 21 / 22 Conditioncond 71.4% 10 / 14 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 91.9% 114 / 124 CC 18 SCAR 17.3
CC
18
Cov
94%
_validate
10.1
_validate_type_callback
7.0
_validate_type_email
3.0
CRAP 5.6
↑ Summary
1 package JSON::Validator::Schema::Troglodyne;
2 2 $JSON::Validator::Schema::Troglodyne::VERSION = '1.001';
3 # ABSTRACT: Troglodyne extensions to the OpenAPIv3 schema validator.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use parent qw{JSON::Validator::Schema::OpenAPIv3};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Data::Validate::Email();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::Util qw{any};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 7 ▶sub _validate_type_email {
Subroutine _validate_type_email: called CC=3 SCAR=11.0
19 7 my ( $self, $input, $info ) = @_;
20 7 my $path = $self->_troglodyne_path($info);
21 7 ▶ return "wrong type, not email" if ref $input;
Branch: 1 branch
BranchTrueFalseTotal
if ref $input 1 6 7
22 6 ▶ return "$path ain't no email I never heard of pardner" unless Data::Validate::Email::is_email($input);
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Validate::Email::is_email($input) 1 5 6
23 5 return;
24 }
25  
26  
27 7 ▶sub _validate_type_callback {
Subroutine _validate_type_callback: called CC=7 SCAR=19.5
28 7 my ( $self, $input, $info ) = @_;
29  
30 7 my $path = $self->_troglodyne_path($info);
31  
32 7 ▶ return "$path is not a fully qualified sub name" unless defined $input && !ref $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input and not ref $input 1 6 7
Condition66%2/3
defined $input and not ref $input
!ll&&!rl&&r
016
MC/DC50%1/2
defined $input and not ref $input
defined $input not ref $input
Truth table66%2/3
A and not(B)
A= defined $inputB= not ref $input
ABresult
0X0
100
111
33 6 my ($modname) = $input =~ m/^([\w:]+)::\w+$/;
34 6 ▶ return "$path is not a fully qualified sub name" unless $modname;
Branch: 1 branch
BranchTrueFalseTotal
unless $modname 1 5 6
35  
36 5 my $modpath = $modname;
37 5 $modpath =~ s{::}{/}g;
38 5 $modpath .= '.pm';
39  
40 5 local $@;
41 # Require so we can check the sub exists, but don't dobule-require
42 5 my @available = keys(%INC);
43 5 ▶ my $loaded = any { m/\Q$modpath\E/ } @available;
Subroutine __ANON__: called CC=1 SCAR=0.0
44 5 ▶ if (!$loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 1 4 5
45 0 ▶ eval { require $modpath; 1 } or return "$path refers to a module ($modname) which cannot be loaded";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $modpath; 1 } } or return "$path refers to a module ($modname) which cannot be loaded" 0 1 1
46 }
47  
48 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶ return "$path refers to a sub which does not exist" unless defined &{$input};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$input;} 1 3 4
50 3 return;
51 }
52  
53  
54 51 ▶sub _validate {
Subroutine _validate: called CC=10 SCAR=23.1
55 51 my ( $self, $data, $state ) = @_;
56 51 my @errors = $self->SUPER::_validate( $_[1], $state );
57 51 my $schema = $state->{schema};
58 51 ▶ return @errors if !@errors || ref $schema ne 'HASH' || ref $schema->{'x-errorMessage'} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not @errors or ref $schema ne "HASH" or ref $schema->{"x-errorMessage"} ne "HASH" 47 4 51
Condition83%5/6
not @errors or ref $schema ne "HASH" or ref $schema->{"x-errorMessage"} ne "HASH"
l!l&&r!l&&!r
25224
not @errors or ref $schema ne "HASH"
l!l&&r!l&&!r
25026
MC/DC66%2/3
not @errors or ref $schema ne "HASH" or ref $schema->{"x-errorMessage"} ne "HASH"
not @errors ref $schema ne "HASH" ref $schema->{"x-errorMessage"} ne "HASH"
Truth table75%3/4
not(A) or B or C
A= not @errorsB= ref $schema ne "HASH"C= ref $schema->{"x-errorMessage"} ne "HASH"
ABCresult
1XX1
01X1
0011
0000
59  
60 4 my $messages = $schema->{'x-errorMessage'};
61 4 my $here = JSON::Validator::Error->new( $state->{path} )->path;
62 4 for my $error (@errors) {
63 5 my $keyword = $error->details->[1];
64 5 ▶ next if !defined $keyword || !defined $messages->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyword and defined $messages->{$keyword} 1 4 5
Condition66%2/3
defined $keyword and defined $messages->{$keyword}
!ll&&!rl&&r
014
MC/DC50%1/2
defined $keyword and defined $messages->{$keyword}
defined $keyword defined $messages->{$keyword}
Truth table66%2/3
A and B
A= defined $keywordB= defined $messages->{$keyword}
ABresult
0X0
100
111
65 4 my $at = $error->path;
66 4 ▶ if ( $keyword eq 'required' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword eq "required") 1 3 4
67 1 $at =~ s{/[^/]*\z}{};
68 1 ▶ $at ||= '/';
Condition50%1/2
$at ||= "/"
l!l
10
MC/DC0%0/1
$at ||= "/"
$at
Truth table50%1/2
A
A= $at
Aresult
11
00
69 }
70 4 ▶ $error->message( $messages->{$keyword} ) if $at eq $here;
Branch: 1 branch
BranchTrueFalseTotal
if $at eq $here 3 1 4
71 }
72 4 return @errors;
73 }
74  
75 # Build the path the module usually does for errors
76 14 ▶sub _troglodyne_path {
Subroutine _troglodyne_path: called CC=1 SCAR=0.0
77 14 my ($self, $input) = @_;
78 14 return "$input->{base_url}/".join('/', @{$input->{path}});
79 }
80  
81 2 1;
82  
83 __END__
↑ Summary