Validator/Constraints/Draft6.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 70.0% 28 / 40 Conditioncond 42.8% 9 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 73.4% 122 / 166 CC 30 SCAR 25.3
CC
30
Cov
81%
const
29.4
contains
6.0
check_type
5.6
CRAP 12.6
« Validator/JSONPointer.pm ↑ Summary Validator/Util.pm »
1 package JSONSchema::Validator::Constraints::Draft6;
2  
3 # ABSTRACT: JSON Schema Draft6 specification constraints
4  
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use JSONSchema::Validator::JSONPointer 'json_pointer';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use JSONSchema::Validator::Error 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use JSONSchema::Validator::Util qw(is_type serialize unbool);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use parent 'JSONSchema::Validator::Constraints::Draft4';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # params: $self, $value, $type, $strict
15 1219 ▶sub check_type {
Subroutine check_type: called CC=5 SCAR=17.2
Pod undocumented
16 1219 ▶ if ($_[2] eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] eq "integer") 11 1208 1219
17 11 ▶ return is_type($_[1], 'number', $_[3] // $_[0]->strict) && int($_[1]) == $_[1];
Condition50%3/6
is_type($_[1], 'number', $_[3] // $_[0]->strict) && int $_[1] == $_[1]
!ll&&!rl&&r
0110
$_[3] // $_[0]->strict
l!l&&r!l&&!r
0110
MC/DC50%1/2
is_type($_[1], 'number', $_[3] // $_[0]->strict) && int $_[1] == $_[1]
is_type($_[1], 'number', $_[3] // $_[0]->strict) int $_[1] == $_[1]
MC/DC0%0/2
$_[3] // $_[0]->strict
$_[3] $_[0]->strict
Truth table66%2/3
A && B
A= is_type($_[1], 'number', $_[3] // $_[0]->strict)B= int $_[1] == $_[1]
ABresult
0X0
100
111
Truth table33%1/3
A // B
A= $_[3]B= $_[0]->strict
ABresult
1X1
011
000
18 }
19 1208 ▶ return is_type($_[1], $_[2], $_[3] // $_[0]->strict);
Condition66%2/3
$_[3] // $_[0]->strict
l!l&&r!l&&!r
7884200
MC/DC0%0/2
$_[3] // $_[0]->strict
$_[3] $_[0]->strict
Truth table66%2/3
A // B
A= $_[3]B= $_[0]->strict
ABresult
1X1
011
000
20 }
21  
22 7 ▶sub exclusiveMaximum {
Subroutine exclusiveMaximum: called CC=3 SCAR=11.0
Pod undocumented
23 7 my ($self, $instance, $exclusiveMaximum, $schema, $instance_path, $schema_path, $data) = @_;
24 7 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 7 7
25  
26 7 ▶ return 1 if $instance < $exclusiveMaximum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance < $exclusiveMaximum 2 5 7
27  
28 5 push @{$data->{errors}}, error(
29 message => "${instance} is equal or greater than ${exclusiveMaximum}",
30 instance_path => $instance_path,
31 schema_path => $schema_path
32 );
33 5 return 0;
34 }
35  
36 3 ▶sub exclusiveMinimum {
Subroutine exclusiveMinimum: called CC=3 SCAR=11.0
Pod undocumented
37 3 my ($self, $instance, $exclusiveMinimum, $schema, $instance_path, $schema_path, $data) = @_;
38 3 ▶ return 1 unless $self->check_type($instance, 'number');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "number") 0 3 3
39  
40 3 ▶ return 1 if $instance > $exclusiveMinimum;
Branch: 1 branch
BranchTrueFalseTotal
if $instance > $exclusiveMinimum 1 2 3
41  
42 2 push @{$data->{errors}}, error(
43 message => "${instance} is equal or less than ${exclusiveMinimum}",
44 instance_path => $instance_path,
45 schema_path => $schema_path
46 );
47 2 return 0;
48 }
49  
50 2 ▶sub propertyNames {
Subroutine propertyNames: called CC=4 SCAR=13.9
Pod undocumented
51 2 my ($self, $instance, $propertyNames, $schema, $instance_path, $schema_path, $data) = @_;
52 2 ▶ return 1 unless $self->check_type($instance, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "object") 0 2 2
53  
54 2 my $result = 1;
55 2 for my $p (keys %$instance) {
56 4 my $ipath = json_pointer->append($instance_path, $p);
57 4 my $r = $self->validator->_validate_schema($p, $propertyNames, $ipath, $schema_path, $data);
58 4 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 1 3 4
59 }
60 2 return $result;
61 }
62  
63 6 ▶sub contains {
Subroutine contains: called CC=6 SCAR=17.9
Pod undocumented
64 6 my ($self, $instance, $contains, $schema, $instance_path, $schema_path, $data) = @_;
65 6 ▶ return 1 unless $self->check_type($instance, 'array');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_type($instance, "array") 0 6 6
66  
67 6 my $errors = $data->{errors};
68 6 my $local_errors = [];
69  
70 6 my $result = 0;
71 6 for my $idx (0 .. $#{$instance}) {
72 20 $data->{errors} = [];
73 20 my $ipath = json_pointer->append($instance_path, $idx);
74 20 $result = $self->validator->_validate_schema($instance->[$idx], $contains, $ipath, $schema_path, $data);
75 20 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 17 3 20
76 17 push @{$local_errors}, error(
77 message => qq'${idx} part of "contains" has errors',
78 context => $data->{errors},
79 17 instance_path => $ipath,
80 schema_path => $schema_path
81 );
82 }
83 20 ▶ last if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 17 20
84 }
85 6 $data->{errors} = $errors;
86 6 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 3 3 6
87  
88 3 push @{$data->{errors}}, error(
89 message => 'No elems of instance satisfy schema of "contains"',
90 context => $local_errors,
91 instance_path => $instance_path,
92 schema_path => $schema_path
93 );
94 3 return 0;
95 }
96  
97 10 ▶sub const {
Subroutine const: called CC=14 SCAR=33.8
Pod undocumented
98 10 my ($self, $instance, $const, $schema, $instance_path, $schema_path, $data) = @_;
99  
100 10 my $result = 0;
101  
102 # schema must have strict check
103 10 ▶ if ($self->check_type($const, 'boolean', 1)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->check_type($const, 'boolean', 1)) { } 0 10 10
elsif ($self->check_type($const, 'object', 1) or $self->check_type($const, 'array', 1)) { } 2 8 10
elsif ($self->check_type($const, 'number', 1)) { } 4 4 8
elsif (defined $const and defined $instance) { } 4 0 4
elsif (not defined $const || defined $instance) { } 0 0 0
104 0 ▶ $result = $self->check_type($instance, 'boolean')
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'boolean') ? : 0 0 0
105 ? unbool($instance) eq unbool($const)
106 : 0
107 ▶ } elsif ($self->check_type($const, 'object', 1) || $self->check_type($const, 'array', 1)) {
Condition66%2/3
$self->check_type($const, "object", 1) or $self->check_type($const, "array", 1)
l!l&&r!l&&!r
028
MC/DC50%1/2
$self->check_type($const, "object", 1) or $self->check_type($const, "array", 1)
$self->check_type($const, "object", 1) $self->check_type($const, "array", 1)
Truth table66%2/3
A or B
A= $self->check_type($const, "object", 1)B= $self->check_type($const, "array", 1)
ABresult
1X1
011
000
108 2 ▶ $result = $self->check_type($instance, 'object') ||
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'object') || $self->check_type($instance, 'array') ? : 2 0 2
Condition33%1/3
$self->check_type($instance, "object") or $self->check_type($instance, "array")
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->check_type($instance, "object") or $self->check_type($instance, "array")
$self->check_type($instance, "object") $self->check_type($instance, "array")
Truth table33%1/3
A or B
A= $self->check_type($instance, "object")B= $self->check_type($instance, "array")
ABresult
1X1
011
000
109 $self->check_type($instance, 'array')
110 ? serialize($instance) eq serialize($const)
111 : 0;
112 } elsif ($self->check_type($const, 'number', 1)) {
113 4 ▶ $result = $self->check_type($instance, 'number')
Branch: 1 branch
BranchTrueFalseTotal
$self->check_type($instance, 'number') ? : 4 0 4
114 ? $const == $instance
115 : 0;
116 ▶ } elsif (defined $const && defined $instance) {
Condition33%1/3
defined $const and defined $instance
!ll&&!rl&&r
004
MC/DC0%0/2
defined $const and defined $instance
defined $const defined $instance
Truth table33%1/3
A and B
A= defined $constB= defined $instance
ABresult
0X0
100
111
117 4 $result = $const eq $instance;
118 ▶ } elsif (!defined $const && !defined $instance) {
Condition0%0/3
defined $const || defined $instance
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $const || defined $instance
defined $const defined $instance
Truth table0%0/3
A || B
A= defined $constB= defined $instance
ABresult
1X1
011
000
119 0 $result = 1;
120 }
121  
122 10 ▶ return 1 if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 5 5 10
123  
124 5 push @{$data->{errors}}, error(
125 message => "instance is not equal const",
126 instance_path => $instance_path,
127 schema_path => $schema_path
128 );
129 5 return 0;
130 }
131  
132 11 1;
133  
134 __END__
« Validator/JSONPointer.pm ↑ Summary Validator/Util.pm »