Validator/Draft4.pm

filter: Statementstmt 96.3% 106 / 110 Branchbran 81.2% 26 / 32 Conditioncond 76.3% 29 / 38 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 95.8% 23 / 24 Podpod 40.0% 4 / 10 totaltotal 84.9% 198 / 233 CC 33 SCAR 22.1
CC
33
Cov
89%
_validate_schema
19.4
validate_schema
9.0
create
7.0
CRAP 9.1
« Validator/URIResolver.pm ↑ Summary Validator/Error.pm »
1 package JSONSchema::Validator::Draft4;
2  
3 # ABSTRACT: Validator for JSON Schema Draft4
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 11 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use JSONSchema::Validator::Error 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use JSONSchema::Validator::JSONPointer 'json_pointer';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use JSONSchema::Validator::Constraints::Draft4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use JSONSchema::Validator::URIResolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use JSONSchema::Validator::Util qw(is_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use constant SPECIFICATION => 'Draft4';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use constant ID => 'http://json-schema.org/draft-04/schema#';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use constant ID_FIELD => 'id';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 321 ▶sub create {
Subroutine create: called CC=7 SCAR=19.5
Pod undocumented
21 321 my ($class, %params) = @_;
22  
23 321 ▶ croak 'schema is required' unless exists $params{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"schema"} 0 321 321
24  
25 321 my $schema = $params{schema};
26 321 ▶ my $using_id_with_ref = $params{using_id_with_ref} // 1;
Condition100%2/2
$params{"using_id_with_ref"} // 1
l!l
39282
MC/DC100%1/1
$params{"using_id_with_ref"} // 1
$params{"using_id_with_ref"}
Truth table100%2/2
A
A= $params{"using_id_with_ref"}
Aresult
11
00
27  
28 321 ▶ my $scheme_handlers = $params{scheme_handlers} // {};
Condition50%1/2
$params{"scheme_handlers"} // {}
l!l
0321
MC/DC0%0/1
$params{"scheme_handlers"} // {}
$params{"scheme_handlers"}
Truth table50%1/2
A
A= $params{"scheme_handlers"}
Aresult
11
00
29  
30 321 my $self = {
31 schema => $schema,
32 errors => [],
33 scopes => [],
34 using_id_with_ref => $using_id_with_ref
35 };
36  
37 321 bless $self, $class;
38  
39 # schema may be boolean value according to json schema draft6
40 321 my $base_uri = $params{base_uri};
41 321 ▶ $base_uri //= $schema->{$self->ID_FIELD} if ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $schema eq "HASH" 319 2 321
Condition100%3/3
$base_uri //= $$schema{$self->ID_FIELD}
l!l&&r!l&&!r
931279
MC/DC100%2/2
$base_uri //= $$schema{$self->ID_FIELD}
$base_uri $$schema{$self->ID_FIELD}
Truth table100%3/3
A //= B
A= $base_uriB= $$schema{$self->ID_FIELD}
ABresult
1X1
011
000
42 321 ▶ $base_uri //= '';
Condition100%2/2
$base_uri //= ""
l!l
40281
MC/DC100%1/1
$base_uri //= ""
$base_uri
Truth table100%2/2
A
A= $base_uri
Aresult
11
00
43 321 $self->{base_uri} = $base_uri;
44  
45 321 my $resolver = JSONSchema::Validator::URIResolver->new(
46 validator => $self,
47 base_uri => $base_uri,
48 schema => $schema,
49 scheme_handlers => $scheme_handlers
50 );
51 321 $self->{resolver} = $resolver;
52  
53 321 push @{$self->scopes}, $base_uri;
54  
55 321 return $self;
56 }
57  
58 239 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
59 239 my ($class, %params) = @_;
60  
61 239 my $self = $class->create(%params);
62  
63 239 ▶ my $constraints = JSONSchema::Validator::Constraints::Draft4->new(validator => $self, strict => $params{strict} // 1);
Condition100%2/2
$params{"strict"} // 1
l!l
38201
MC/DC100%1/1
$params{"strict"} // 1
$params{"strict"}
Truth table100%2/2
A
A= $params{"strict"}
Aresult
11
00
64 239 $self->{constraints} = $constraints;
65  
66 239 return $self;
67 }
68  
69 373 ▶sub schema { shift->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
70 7540 ▶sub constraints { shift->{constraints} }
Subroutine constraints: called CC=1 SCAR=0.0
Pod undocumented
71 454 ▶sub resolver { shift->{resolver} }
Subroutine resolver: called CC=1 SCAR=0.0
Pod undocumented
72 1299 ▶sub scopes { shift->{scopes} }
Subroutine scopes: called CC=1 SCAR=0.0
Pod undocumented
73 538 ▶sub scope { shift->{scopes}[-1] }
Subroutine scope: called CC=1 SCAR=0.0
Pod undocumented
74 0 ▶sub base_uri { shift->{base_uri} }
Subroutine base_uri: not called CC=1 SCAR=6.9
Pod undocumented
75 2196 ▶sub using_id_with_ref { shift->{using_id_with_ref} }
Subroutine using_id_with_ref: called CC=1 SCAR=0.0
Pod documented
76  
77 294 ▶sub validate_schema {
Subroutine validate_schema: called CC=7 SCAR=22.0
Pod documented
78 294 my ($self, $instance, %params) = @_;
79  
80 294 ▶ my $schema = $params{schema} // $self->schema;
Condition33%1/3
$params{"schema"} // $self->schema
l!l&&r!l&&!r
02940
MC/DC0%0/2
$params{"schema"} // $self->schema
$params{"schema"} $self->schema
Truth table33%1/3
A // B
A= $params{"schema"}B= $self->schema
ABresult
1X1
011
000
81 294 ▶ my $instance_path = $params{instance_path} // '/';
Condition50%1/2
$params{"instance_path"} // "/"
l!l
0294
MC/DC0%0/1
$params{"instance_path"} // "/"
$params{"instance_path"}
Truth table50%1/2
A
A= $params{"instance_path"}
Aresult
11
00
82 294 ▶ my $schema_path = $params{schema_path} // '/';
Condition50%1/2
$params{"schema_path"} // "/"
l!l
0294
MC/DC0%0/1
$params{"schema_path"} // "/"
$params{"schema_path"}
Truth table50%1/2
A
A= $params{"schema_path"}
Aresult
11
00
83 294 my $scope = $params{scope};
84  
85 294 ▶ croak 'No schema specified' unless defined $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 0 294 294
86  
87 0 ▶ push @{$self->scopes}, $scope if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 0 294 294
88  
89 294 my $errors = [];
90 294 my $result = $self->_validate_schema($instance, $schema, $instance_path, $schema_path, {errors => $errors});
91  
92 0 ▶ pop @{$self->scopes} if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 0 294 294
93  
94 294 return $result, $errors;
95 }
96  
97 2232 ▶sub _validate_schema {
Subroutine _validate_schema: called CC=19 SCAR=29.6
98 2232 my ($self, $instance, $schema, $instance_path, $schema_path, $data, %params) = @_;
99  
100 # for json schema draft 6 which allow boolean value for schema
101 2232 ▶ if (is_type($schema, 'boolean', 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_type($schema, "boolean", 1)) 14 2218 2232
102 14 ▶ return 1 if $schema;
Branch: 1 branch
BranchTrueFalseTotal
if $schema 13 1 14
103 1 push @{$data->{errors}}, error(
104 message => 'Schema with value "false" does not allow anything',
105 instance_path => $instance_path,
106 schema_path => $schema_path
107 );
108 1 return 0;
109 }
110  
111 2218 ▶ my $apply_scope = $params{apply_scope} // 1;
Condition100%2/2
$params{"apply_scope"} // 1
l!l
4161802
MC/DC100%1/1
$params{"apply_scope"} // 1
$params{"apply_scope"}
Truth table100%2/2
A
A= $params{"apply_scope"}
Aresult
11
00
112  
113 2218 my $is_exists_ref = exists $schema->{'$ref'};
114  
115 2218 my $id = $schema->{$self->ID_FIELD};
116 2218 ▶ if ($id && $apply_scope && $self->using_id_with_ref && !$is_exists_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref) 39 2179 2218
Condition77%7/9
$id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref
!ll&&!rl&&r
2179039
$id and $apply_scope and $self->using_id_with_ref
!ll&&!rl&&r
2179039
$id and $apply_scope
!ll&&!rl&&r
21136639
MC/DC50%2/4
$id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref
$id $apply_scope $self->using_id_with_ref not $is_exists_ref
Truth table60%3/5
A and B and C and not(D)
A= $idB= $apply_scopeC= $self->using_id_with_refD= not $is_exists_ref
ABCDresult
0XXX0
10XX0
110X0
11100
11111
117 39 my $uri = $id;
118 39 ▶ $uri = URI->new($id)->abs($self->scope)->as_string if $self->scope;
Branch: 1 branch
BranchTrueFalseTotal
if $self->scope 39 0 39
119 39 push @{$self->scopes}, $uri;
120 }
121  
122 2218 my @schema_keys = $self->_schema_keys($schema, $instance_path, $data);
123  
124 2218 my $result = 1;
125 2218 for my $k (@schema_keys) {
126 4114 my $v = $schema->{$k};
127  
128 4114 ▶ my $method = $k eq '$ref' ? 'ref' : $k;
Branch: 1 branch
BranchTrueFalseTotal
$k eq '$ref' ? : 388 3726 4114
129 4114 ▶ next unless my $constraint = $self->constraints->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless my $constraint = $self->constraints->can($method) 688 3426 4114
130  
131 3426 ▶ my $spath = json_pointer->append($schema_path, $k eq '$ref' ? "$k($v)" : $k);
Branch: 1 branch
BranchTrueFalseTotal
$k eq '$ref' ? : 388 3038 3426
132  
133 3426 my $r = eval {
134 3426 $self->constraints->$constraint($instance, $v, $schema, $instance_path, $spath, $data);
135 };
136 0 ▶ push @{$data->{errors}}, error(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3426 3426
137 message => "exception: $@",
138 instance_path => $instance_path,
139 schema_path => $spath
140 ) if $@;
141 3426 ▶ $result = 0 unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 558 2868 3426
142 }
143  
144 2218 ▶ pop @{$self->scopes} if $id && $apply_scope && $self->using_id_with_ref && !$is_exists_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref 39 2179 2218
Condition77%7/9
$id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref
!ll&&!rl&&r
2179039
$id and $apply_scope and $self->using_id_with_ref
!ll&&!rl&&r
2179039
$id and $apply_scope
!ll&&!rl&&r
21136639
MC/DC50%2/4
$id and $apply_scope and $self->using_id_with_ref and not $is_exists_ref
$id $apply_scope $self->using_id_with_ref not $is_exists_ref
Truth table60%3/5
A and B and C and not(D)
A= $idB= $apply_scopeC= $self->using_id_with_refD= not $is_exists_ref
ABCDresult
0XXX0
10XX0
110X0
11100
11111
145 2218 return $result;
146 }
147  
148 1694 ▶sub _schema_keys {
Subroutine _schema_keys: called CC=2 SCAR=6.9
149 1694 my ($self, $schema, $instance_path, $data) = @_;
150 # if ref exists other preperties MUST be ignored
151 1694 ▶ return '$ref' if $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $schema->{"\$ref"} 326 1368 1694
152 1368 return keys %$schema;
153 }
154  
155 11 1;
156  
157 __END__
« Validator/URIResolver.pm ↑ Summary Validator/Error.pm »