lib/JSON/Structure/SchemaValidator.pm

filter: Statementstmt 61.3% 435 / 709 Branchbran 48.8% 244 / 500 Conditioncond 30.1% 105 / 348 MC/DCmcdc 9.3% 18 / 193 Subroutinesub 83.0% 44 / 53 Podpod 50.0% 1 / 2 totaltotal 46.9% 847 / 1805 CC 407 SCAR 44.8
CC
407
Cov
50%
_validate_extended_keywords
332.7
_validate_extends
272.0
_validate_schema
212.5
CRAP 87.8
« lib/JSON/Structure/InstanceValidator.pm ↑ Summary lib/JSON/Structure/JsonSourceLocator.pm »
1 package JSON::Structure::SchemaValidator;
2  
3 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 our $VERSION = '0.8.2';
8  
9 17 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use JSON::Structure::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use JSON::Structure::ErrorCodes qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use JSON::Structure::JsonSourceLocator;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 JSON::Structure::SchemaValidator - Validate JSON Structure schema documents
18  
19 =head1 SYNOPSIS
20  
21 use JSON::Structure::SchemaValidator;
22 use JSON::PP;
23
24 my $validator = JSON::Structure::SchemaValidator->new(
25 extended => 1, # Enable extended validation
26 allow_import => 1, # Enable $import/$importdefs
27 );
28
29 my $schema = decode_json($schema_json);
30 my $result = $validator->validate($schema, $schema_json);
31
32 if ($result->is_valid) {
33 say "Schema is valid!";
34 } else {
35 for my $error (@{$result->errors}) {
36 say $error->to_string;
37 }
38 }
39  
40 =head1 DESCRIPTION
41  
42 Validates JSON Structure Core documents for conformance with the specification.
43 Provides error messages annotated with line and column numbers.
44  
45 =cut
46  
47 # Regular expressions
48 17 my $ABSOLUTE_URI_REGEX = qr/^[a-zA-Z][a-zA-Z0-9+\-.]*:\/\//;
49 17 my $IDENTIFIER_REGEX = qr/^[A-Za-z_][A-Za-z0-9_]*$/;
50 17 my $IDENTIFIER_DOLLAR_REGEX = qr/^[A-Za-z_\$][A-Za-z0-9_\$]*$/;
51 17 my $MAP_KEY_REGEX = qr/^[A-Za-z0-9._-]+$/;
52  
53 # Type definitions
54 my %PRIMITIVE_TYPES =
55 17 map { $_ => 1 } @{ JSON::Structure::Types::PRIMITIVE_TYPES() };
56 my %COMPOUND_TYPES =
57 17 map { $_ => 1 } @{ JSON::Structure::Types::COMPOUND_TYPES() };
58 my %NUMERIC_TYPES =
59 17 map { $_ => 1 } @{ JSON::Structure::Types::NUMERIC_TYPES() };
60  
61 # Reserved keywords
62 17 my %RESERVED_KEYWORDS = map { $_ => 1 } qw(
63 definitions $extends $id $ref $root $schema $uses
64 $offers abstract additionalProperties const default
65 description enum examples format items maxLength
66 name precision properties required scale type
67 values choices selector tuple
68 unit ucumUnit currency symbols identity relations
69 );
70  
71 # Extended keywords for conditional composition
72 my %COMPOSITION_KEYWORDS =
73 17 map { $_ => 1 } qw(allOf anyOf oneOf not if then else);
74  
75 # Extended keywords for validation - combined for warning generation
76 17 my %VALIDATION_EXTENSION_KEYWORDS = map { $_ => 1 } qw(
77 pattern format minLength maxLength
78 minimum maximum exclusiveMinimum exclusiveMaximum multipleOf
79 minItems maxItems uniqueItems contains minContains maxContains
80 minProperties maxProperties dependentRequired patternProperties
81 propertyNames default contentEncoding contentMediaType
82 minEntries maxEntries patternKeys keyNames has
83 );
84  
85 # Extended keywords for validation - categorized for constraint validation
86 17 my %NUMERIC_VALIDATION_KEYWORDS = map { $_ => 1 } qw(
87 minimum maximum exclusiveMinimum exclusiveMaximum multipleOf
88 );
89 17 my %STRING_VALIDATION_KEYWORDS = map { $_ => 1 } qw(
90 minLength maxLength pattern format contentEncoding contentMediaType
91 );
92 17 my %ARRAY_VALIDATION_KEYWORDS = map { $_ => 1 } qw(
93 minItems maxItems uniqueItems contains minContains maxContains
94 );
95 17 my %OBJECT_VALIDATION_KEYWORDS = map { $_ => 1 } qw(
96 minProperties maxProperties minEntries maxEntries
97 dependentRequired patternProperties patternKeys
98 propertyNames keyNames has default
99 );
100  
101 # Combined validation keywords for warning detection
102 17 my %ALL_VALIDATION_KEYWORDS = (
103 %NUMERIC_VALIDATION_KEYWORDS, %STRING_VALIDATION_KEYWORDS,
104 %ARRAY_VALIDATION_KEYWORDS, %OBJECT_VALIDATION_KEYWORDS,
105 );
106  
107 # Valid format values
108 17 my %VALID_FORMATS = map { $_ => 1 } qw(
109 ipv4 ipv6 email idn-email hostname idn-hostname
110 iri iri-reference uri-template relative-json-pointer regex
111 );
112  
113 # Known extensions
114 17 my %KNOWN_EXTENSIONS = map { $_ => 1 } qw(
115 JSONStructureImport JSONStructureAlternateNames JSONStructureUnits
116 JSONStructureConditionalComposition JSONStructureValidation
117 JSONStructureRelations
118 );
119  
120 30 ▶sub new {
Subroutine new: called CC=11 SCAR=34.6
Pod undocumented
121 30 my ( $class, %args ) = @_;
122  
123 my $self = bless {
124 allow_dollar => $args{allow_dollar} // 0,
125 allow_import => $args{allow_import} // 0,
126 import_map => $args{import_map} // {},
127 extended => $args{extended} // 0,
128 external_schemas => {},
129 warn_on_unused_extensions => $args{warn_on_unused_extension_keywords}
130 // 1,
131 30 ▶ max_validation_depth => $args{max_validation_depth} // 64,
Condition58%7/12
$args{"allow_dollar"} // 0
l!l
030
$args{"allow_import"} // 0
l!l
030
$args{"import_map"} // {}
l!l
030
$args{"extended"} // 0
l!l
426
$args{"warn_on_unused_extension_keywords"} // 1
l!l
030
$args{"max_validation_depth"} // 64
l!l
030
MC/DC0%0/1
$args{"allow_dollar"} // 0
$args{"allow_dollar"}
MC/DC0%0/1
$args{"allow_import"} // 0
$args{"allow_import"}
MC/DC0%0/1
$args{"import_map"} // {}
$args{"import_map"}
MC/DC100%1/1
$args{"extended"} // 0
$args{"extended"}
MC/DC0%0/1
$args{"warn_on_unused_extension_keywords"} // 1
$args{"warn_on_unused_extension_keywords"}
MC/DC0%0/1
$args{"max_validation_depth"} // 64
$args{"max_validation_depth"}
Truth table50%1/2
A
A= $args{"allow_dollar"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"allow_import"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"import_map"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"extended"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"warn_on_unused_extension_keywords"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"max_validation_depth"}
Aresult
11
00
132 enabled_extensions => {},
133 errors => [],
134 warnings => [],
135 doc => undef,
136 source_text => undef,
137 source_locator => undef,
138 seen_extends => {},
139 seen_refs => {},
140 current_depth => 0,
141 }, $class;
142  
143 # Build lookup for external schemas by $id
144 30 ▶ if ( $args{external_schemas} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"external_schemas"}) 0 30 30
145 0 for my $schema ( @{ $args{external_schemas} } ) {
146 0 ▶ if ( ref($schema) eq 'HASH' && exists $schema->{'$id'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and exists $schema->{"\$id"}) 0 0 0
Condition0%0/3
ref $schema eq "HASH" and exists $schema->{"\$id"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $schema eq "HASH" and exists $schema->{"\$id"}
ref $schema eq "HASH" exists $schema->{"\$id"}
Truth table0%0/3
A and B
A= ref $schema eq "HASH"B= exists $schema->{"\$id"}
ABresult
0X0
100
111
147 0 $self->{external_schemas}{ $schema->{'$id'} } = $schema;
148 }
149 }
150 }
151  
152 30 return $self;
153 }
154  
155 =head2 validate($doc, $source_text)
156  
157 Validates a JSON Structure schema document.
158  
159 Returns a ValidationResult object with errors and warnings.
160  
161 =cut
162  
163 85 ▶sub validate {
Subroutine validate: called CC=23 SCAR=33.7
Pod documented
164 85 my ( $self, $doc, $source_text ) = @_;
165  
166 # Reset state
167 85 $self->{errors} = [];
168 85 $self->{warnings} = [];
169 85 $self->{doc} = $doc;
170 85 $self->{source_text} = $source_text;
171 85 $self->{seen_extends} = {};
172 85 $self->{seen_refs} = {};
173 85 $self->{enabled_extensions} = {};
174 85 $self->{current_depth} = 0;
175  
176 # Initialize source locator
177 85 ▶ if ( defined $source_text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_text) { } 1 84 85
178 $self->{source_locator} =
179 1 JSON::Structure::JsonSourceLocator->new($source_text);
180 }
181 else {
182 84 $self->{source_locator} = undef;
183 }
184  
185 # Check for null/undefined
186 85 ▶ if ( !defined $doc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc) 0 85 85
187 0 $self->_add_error( SCHEMA_NULL, 'Schema cannot be null', '#' );
188 0 return $self->_make_result();
189 }
190  
191 # Check document is an object
192 85 ▶ if ( ref($doc) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc ne "HASH") 0 85 85
193 0 $self->_add_error( SCHEMA_INVALID_TYPE,
194 'Root of the document must be a JSON object', '#' );
195 0 return $self->_make_result();
196 }
197  
198 # Process $import and $importdefs
199 85 ▶ $self->_process_imports( $doc, '#' ) if $self->{allow_import};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"allow_import"} 0 85 85
200  
201 # Check enabled extensions
202 85 ▶ $self->_check_enabled_extensions($doc) if $self->{extended};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"extended"} 22 63 85
203  
204 # Validate required top-level keywords
205 85 $self->_check_required_top_level_keywords( $doc, '#' );
206  
207 # Validate $schema
208 85 ▶ if ( exists $doc->{'$schema'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"\$schema"}) 75 10 85
209 75 $self->_check_is_absolute_uri( $doc->{'$schema'}, '$schema',
210 '#/$schema' );
211 }
212  
213 # Validate $id
214 85 ▶ if ( exists $doc->{'$id'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"\$id"}) 83 2 85
215 83 $self->_check_is_absolute_uri( $doc->{'$id'}, '$id', '#/$id' );
216 }
217  
218 # Validate $uses
219 85 ▶ if ( exists $doc->{'$uses'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"\$uses"}) 22 63 85
220 22 $self->_check_uses( $doc->{'$uses'}, '#/$uses' );
221 }
222  
223 # Check for conflicting type and $root
224 85 ▶ if ( exists $doc->{type} && exists $doc->{'$root'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"type"} and exists $doc->{"\$root"}) 0 85 85
Condition66%2/3
exists $doc->{"type"} and exists $doc->{"\$root"}
!ll&&!rl&&r
4810
MC/DC0%0/2
exists $doc->{"type"} and exists $doc->{"\$root"}
exists $doc->{"type"} exists $doc->{"\$root"}
Truth table66%2/3
A and B
A= exists $doc->{"type"}B= exists $doc->{"\$root"}
ABresult
0X0
100
111
225 0 $self->_add_error(
226 SCHEMA_ROOT_CONFLICT,
227 "Document cannot have both 'type' at root and '\$root' at the same time",
228 '#'
229 );
230 }
231  
232 # Validate type if present
233 85 ▶ if ( exists $doc->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"type"}) 81 4 85
234 81 $self->_validate_schema( $doc, 1, '#', undef );
235 }
236  
237 # Validate $root if present
238 85 ▶ if ( exists $doc->{'$root'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"\$root"}) 0 85 85
239 0 $self->_check_json_pointer( $doc->{'$root'}, $self->{doc}, '#/$root' );
240 }
241  
242 # Validate definitions
243 85 ▶ if ( exists $doc->{definitions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"definitions"}) 4 81 85
244 4 ▶ if ( ref( $doc->{definitions} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc->{'definitions'} ne 'HASH') { } 0 4 4
245 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
246 'definitions must be an object',
247 '#/definitions' );
248 }
249 else {
250 4 $self->_validate_namespace( $doc->{definitions}, '#/definitions' );
251 }
252 }
253  
254 # Validate $offers
255 85 ▶ if ( exists $doc->{'$offers'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $doc->{"\$offers"}) 0 85 85
256 0 $self->_check_offers( $doc->{'$offers'}, '#/$offers' );
257 }
258  
259 # Check composition keywords at root if no type
260 85 ▶ if ( $self->{extended} && !exists $doc->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"} and not exists $doc->{"type"}) 0 85 85
Condition66%2/3
$self->{"extended"} and not exists $doc->{"type"}
!ll&&!rl&&r
63220
MC/DC0%0/2
$self->{"extended"} and not exists $doc->{"type"}
$self->{"extended"} not exists $doc->{"type"}
Truth table66%2/3
A and not(B)
A= $self->{"extended"}B= not exists $doc->{"type"}
ABresult
0X0
100
111
261 0 $self->_check_composition_keywords( $doc, '#' );
262 }
263  
264 # Ensure document has type, $root, or composition keywords
265 85 my $has_type = exists $doc->{type};
266 85 my $has_root = exists $doc->{'$root'};
267 my $has_composition =
268 85 ▶ $self->{extended} && $self->_has_composition_keywords($doc);
Condition66%2/3
$self->{'extended'} && $self->_has_composition_keywords($doc)
!ll&&!rl&&r
63220
MC/DC0%0/2
$self->{'extended'} && $self->_has_composition_keywords($doc)
$self->{'extended'} $self->_has_composition_keywords($doc)
Truth table66%2/3
A && B
A= $self->{'extended'}B= $self->_has_composition_keywords($doc)
ABresult
0X0
100
111
269  
270 85 ▶ if ( !$has_type && !$has_root && !$has_composition ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_type or $has_root or $has_composition) 4 81 85
Condition66%4/6
$has_type or $has_root or $has_composition
l!l&&r!l&&!r
8104
$has_type or $has_root
l!l&&r!l&&!r
8104
MC/DC33%1/3
$has_type or $has_root or $has_composition
$has_type $has_root $has_composition
Truth table50%2/4
A or B or C
A= $has_typeB= $has_rootC= $has_composition
ABCresult
1XX1
01X1
0011
0000
271 4 $self->_add_error(
272 SCHEMA_ROOT_MISSING_TYPE,
273 "Document must have 'type', '\$root', or composition keywords at root",
274 '#'
275 );
276 }
277  
278 # Check for validation extension keywords without $uses (warnings)
279 85 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 22 63 85
280 22 $self->_check_validation_keyword_warnings( $doc, '#' );
281 }
282  
283 85 return $self->_make_result();
284 }
285  
286 85 ▶sub _make_result {
Subroutine _make_result: called CC=1 SCAR=0.0
287 85 my ($self) = @_;
288  
289 return JSON::Structure::Types::ValidationResult->new(
290 85 is_valid => scalar( @{ $self->{errors} } ) == 0,
291 errors => $self->{errors},
292 warnings => $self->{warnings},
293 85 );
294 }
295  
296 52 ▶sub _add_error {
Subroutine _add_error: called CC=2 SCAR=6.9
297 52 my ( $self, $code, $message, $path, $schema_path ) = @_;
298  
299 my $location =
300 $self->{source_locator}
301 52 ▶ ? $self->{source_locator}->get_location($path)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'source_locator'} ? : 1 51 52
302 : JSON::Structure::Types::JsonLocation->unknown();
303  
304 52 push @{ $self->{errors} },
305 JSON::Structure::Types::ValidationError->new(
306 code => $code,
307 message => $message,
308 path => $path,
309 severity => JSON::Structure::Types::ValidationSeverity::ERROR,
310 location => $location,
311 schema_path => $schema_path,
312 );
313 }
314  
315 0 ▶sub _add_warning {
Subroutine _add_warning: not called CC=2 SCAR=17.9
316 0 my ( $self, $code, $message, $path ) = @_;
317  
318 my $location =
319 $self->{source_locator}
320 0 ▶ ? $self->{source_locator}->get_location($path)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'source_locator'} ? : 0 0 0
321 : JSON::Structure::Types::JsonLocation->unknown();
322  
323 0 push @{ $self->{warnings} },
324 JSON::Structure::Types::ValidationError->new(
325 code => $code,
326 message => $message,
327 path => $path,
328 severity => JSON::Structure::Types::ValidationSeverity::WARNING,
329 location => $location,
330 );
331 }
332  
333 43 ▶sub _check_validation_keyword_warnings {
Subroutine _check_validation_keyword_warnings: called CC=30 SCAR=48.5
334 43 my ( $self, $schema, $path ) = @_;
335  
336 43 ▶ return unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 43 43
337  
338 # Check if $uses is present at this level
339 43 my $has_uses = exists $schema->{'$uses'};
340  
341 # Check for validation keywords without $uses
342 43 for my $key ( keys %$schema ) {
343 175 ▶ if ( exists $ALL_VALIDATION_KEYWORDS{$key} && !$has_uses ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ALL_VALIDATION_KEYWORDS{$key} and not $has_uses) 0 175 175
Condition66%2/3
exists $ALL_VALIDATION_KEYWORDS{$key} and not $has_uses
!ll&&!rl&&r
17140
MC/DC0%0/2
exists $ALL_VALIDATION_KEYWORDS{$key} and not $has_uses
exists $ALL_VALIDATION_KEYWORDS{$key} not $has_uses
Truth table66%2/3
A and not(B)
A= exists $ALL_VALIDATION_KEYWORDS{$key}B= not $has_uses
ABresult
0X0
100
111
344 0 $self->_add_warning(
345 SCHEMA_EXTENSION_KEYWORD_NOT_ENABLED,
346 "Validation keyword '$key' found but no '\$uses' declaration in scope. Add '\$uses' with an appropriate validation extension to enable this keyword.",
347 "$path/$key"
348 );
349 }
350 }
351  
352 # Recurse into nested schemas
353 43 ▶ if ( exists $schema->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"type"}) 43 0 43
354 43 my $type = $schema->{type};
355  
356 43 ▶ if ( $type eq 'array' && exists $schema->{items} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'array' and exists $schema->{'items'}) { } 1 42 43
elsif ($type eq 'map' and exists $schema->{'values'}) { } 0 42 42
elsif ($type eq 'object') { } 11 31 42
elsif ($type eq 'tuple' and exists $schema->{'items'} and ref $schema->{'items'} eq 'ARRAY') { } 0 62 62
Condition66%2/3
$type eq "array" and exists $schema->{"items"}
!ll&&!rl&&r
4201
MC/DC50%1/2
$type eq "array" and exists $schema->{"items"}
$type eq "array" exists $schema->{"items"}
Truth table66%2/3
A and B
A= $type eq "array"B= exists $schema->{"items"}
ABresult
0X0
100
111
357 $self->_check_validation_keyword_warnings( $schema->{items},
358 1 "$path/items" );
359 }
360 ▶ elsif ( $type eq 'map' && exists $schema->{values} ) {
Condition33%1/3
$type eq "map" and exists $schema->{"values"}
!ll&&!rl&&r
4200
MC/DC0%0/2
$type eq "map" and exists $schema->{"values"}
$type eq "map" exists $schema->{"values"}
Truth table33%1/3
A and B
A= $type eq "map"B= exists $schema->{"values"}
ABresult
0X0
100
111
361 $self->_check_validation_keyword_warnings( $schema->{values},
362 0 "$path/values" );
363 }
364 elsif ( $type eq 'object' ) {
365 11 ▶ if ( exists $schema->{properties}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"properties"} and ref $schema->{"properties"} eq "HASH") 11 0 11
Condition33%1/3
exists $schema->{"properties"} and ref $schema->{"properties"} eq "HASH"
!ll&&!rl&&r
0011
MC/DC0%0/2
exists $schema->{"properties"} and ref $schema->{"properties"} eq "HASH"
exists $schema->{"properties"} ref $schema->{"properties"} eq "HASH"
Truth table33%1/3
A and B
A= exists $schema->{"properties"}B= ref $schema->{"properties"} eq "HASH"
ABresult
0X0
100
111
366 && ref( $schema->{properties} ) eq 'HASH' )
367 {
368 11 for my $prop ( keys %{ $schema->{properties} } ) {
369 $self->_check_validation_keyword_warnings(
370 15 $schema->{properties}{$prop},
371 "$path/properties/$prop"
372 );
373 }
374 }
375 11 ▶ if ( exists $schema->{optionalProperties}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"optionalProperties"} and ref $schema->{"optionalProperties"} eq "HASH") 0 11 11
Condition33%1/3
exists $schema->{"optionalProperties"} and ref $schema->{"optionalProperties"} eq "HASH"
!ll&&!rl&&r
1100
MC/DC0%0/2
exists $schema->{"optionalProperties"} and ref $schema->{"optionalProperties"} eq "HASH"
exists $schema->{"optionalProperties"} ref $schema->{"optionalProperties"} eq "HASH"
Truth table33%1/3
A and B
A= exists $schema->{"optionalProperties"}B= ref $schema->{"optionalProperties"} eq "HASH"
ABresult
0X0
100
111
376 && ref( $schema->{optionalProperties} ) eq 'HASH' )
377 {
378 0 for my $prop ( keys %{ $schema->{optionalProperties} } ) {
379 $self->_check_validation_keyword_warnings(
380 0 $schema->{optionalProperties}{$prop},
381 "$path/optionalProperties/$prop"
382 );
383 }
384 }
385 }
386 ▶ elsif ( $type eq 'tuple'
Condition33%2/6
$type eq "tuple" and exists $schema->{"items"} and ref $schema->{"items"} eq "ARRAY"
!ll&&!rl&&r
3100
$type eq "tuple" and exists $schema->{"items"}
!ll&&!rl&&r
3100
MC/DC0%0/3
$type eq "tuple" and exists $schema->{"items"} and ref $schema->{"items"} eq "ARRAY"
$type eq "tuple" exists $schema->{"items"} ref $schema->{"items"} eq "ARRAY"
Truth table25%1/4
A and B and C
A= $type eq "tuple"B= exists $schema->{"items"}C= ref $schema->{"items"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
387 && exists $schema->{items}
388 && ref( $schema->{items} ) eq 'ARRAY' )
389 {
390 0 for my $i ( 0 .. $#{ $schema->{items} } ) {
391 0 $self->_check_validation_keyword_warnings( $schema->{items}[$i],
392 "$path/items/$i" );
393 }
394 }
395 }
396  
397 # Check composition keywords
398 43 for my $comp_key (qw(allOf anyOf oneOf)) {
399 129 ▶ if ( exists $schema->{$comp_key}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{$comp_key} and ref $schema->{$comp_key} eq "ARRAY") 0 129 129
Condition33%1/3
exists $schema->{$comp_key} and ref $schema->{$comp_key} eq "ARRAY"
!ll&&!rl&&r
12900
MC/DC0%0/2
exists $schema->{$comp_key} and ref $schema->{$comp_key} eq "ARRAY"
exists $schema->{$comp_key} ref $schema->{$comp_key} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $schema->{$comp_key}B= ref $schema->{$comp_key} eq "ARRAY"
ABresult
0X0
100
111
400 && ref( $schema->{$comp_key} ) eq 'ARRAY' )
401 {
402 0 for my $i ( 0 .. $#{ $schema->{$comp_key} } ) {
403 $self->_check_validation_keyword_warnings(
404 0 $schema->{$comp_key}[$i],
405 "$path/$comp_key/$i" );
406 }
407 }
408 }
409  
410 # Check if keyword
411 43 ▶ if ( exists $schema->{if} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"if"}) 0 43 43
412 0 $self->_check_validation_keyword_warnings( $schema->{if}, "$path/if" );
413 $self->_check_validation_keyword_warnings( $schema->{then},
414 "$path/then" )
415 0 ▶ if exists $schema->{then};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"then"} 0 0 0
416 $self->_check_validation_keyword_warnings( $schema->{else},
417 "$path/else" )
418 0 ▶ if exists $schema->{else};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"else"} 0 0 0
419 }
420  
421 # Check definitions
422 43 ▶ if ( exists $schema->{definitions}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"definitions"} and ref $schema->{"definitions"} eq "HASH") 3 40 43
Condition66%2/3
exists $schema->{"definitions"} and ref $schema->{"definitions"} eq "HASH"
!ll&&!rl&&r
4003
MC/DC50%1/2
exists $schema->{"definitions"} and ref $schema->{"definitions"} eq "HASH"
exists $schema->{"definitions"} ref $schema->{"definitions"} eq "HASH"
Truth table66%2/3
A and B
A= exists $schema->{"definitions"}B= ref $schema->{"definitions"} eq "HASH"
ABresult
0X0
100
111
423 && ref( $schema->{definitions} ) eq 'HASH' )
424 {
425 $self->_check_definitions_warnings( $schema->{definitions},
426 3 "$path/definitions" );
427 }
428 }
429  
430 3 ▶sub _check_definitions_warnings {
Subroutine _check_definitions_warnings: called CC=5 SCAR=17.2
431 3 my ( $self, $defs, $path ) = @_;
432  
433 3 ▶ return unless ref($defs) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $defs eq "HASH" 0 3 3
434  
435 3 for my $key ( keys %$defs ) {
436 5 my $val = $defs->{$key};
437 5 ▶ if ( ref($val) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "HASH") 5 0 5
438 5 ▶ if ( exists $val->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $val->{'type'}) { } 5 0 5
439  
440 # This is a schema
441 5 $self->_check_validation_keyword_warnings( $val, "$path/$key" );
442 }
443 else {
444 # This might be a namespace
445 0 $self->_check_definitions_warnings( $val, "$path/$key" );
446 }
447 }
448 }
449 }
450  
451 85 ▶sub _check_required_top_level_keywords {
Subroutine _check_required_top_level_keywords: called CC=8 SCAR=21.5
452 85 my ( $self, $obj, $location ) = @_;
453  
454 # $id is required at root
455 85 ▶ if ( !exists $obj->{'$id'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $obj->{'$id'}) { } 2 83 85
elsif (defined $obj->{'$id'} and not ref $obj->{'$id'}) { } 83 0 83
456 2 $self->_add_error( SCHEMA_ROOT_MISSING_ID,
457 "Missing required '\$id' keyword at root", $location );
458 }
459 ▶ elsif ( defined $obj->{'$id'} && !ref( $obj->{'$id'} ) ) {
Condition33%1/3
defined $obj->{"\$id"} and not ref $obj->{"\$id"}
!ll&&!rl&&r
0083
MC/DC0%0/2
defined $obj->{"\$id"} and not ref $obj->{"\$id"}
defined $obj->{"\$id"} not ref $obj->{"\$id"}
Truth table33%1/3
A and not(B)
A= defined $obj->{"\$id"}B= not ref $obj->{"\$id"}
ABresult
0X0
100
111
460 83 ▶ if ( $obj->{'$id'} eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->{'$id'} eq '') { } 0 83 83
elsif (not $obj->{'$id'} =~ /^[a-zA-Z][a-zA-Z0-9+\-.]*:/u) { } 9 74 83
461 0 $self->_add_error( SCHEMA_KEYWORD_EMPTY,
462 '\$id must not be empty', '#/$id' );
463 }
464 elsif ( $obj->{'$id'} !~ /^[a-zA-Z][a-zA-Z0-9+\-.]*:/ ) {
465 9 $self->_add_error(
466 SCHEMA_CONSTRAINT_VALUE_INVALID,
467 '\$id must be a URI with a scheme',
468 '#/$id'
469 );
470 }
471 }
472  
473 # Root schema with 'type' must have 'name'
474 85 ▶ if ( exists $obj->{type} && !exists $obj->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"type"} and not exists $obj->{"name"}) 2 83 85
Condition100%3/3
exists $obj->{"type"} and not exists $obj->{"name"}
!ll&&!rl&&r
4792
MC/DC100%2/2
exists $obj->{"type"} and not exists $obj->{"name"}
exists $obj->{"type"} not exists $obj->{"name"}
Truth table100%3/3
A and not(B)
A= exists $obj->{"type"}B= not exists $obj->{"name"}
ABresult
0X0
100
111
475 2 $self->_add_error( SCHEMA_ROOT_MISSING_NAME,
476 "Root schema with 'type' must have a 'name' property", $location );
477 }
478 }
479  
480 158 ▶sub _check_is_absolute_uri {
Subroutine _check_is_absolute_uri: called CC=4 SCAR=15.9
481 158 my ( $self, $value, $keyword, $location ) = @_;
482  
483 158 ▶ if ( !defined $value || ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 0 158 158
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
00158
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
484 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
485 "'$keyword' must be a string", $location );
486 0 return;
487 }
488  
489 158 ▶ if ( $value !~ $ABSOLUTE_URI_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value =~ /$ABSOLUTE_URI_REGEX/u) 9 149 158
490 9 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
491 "'$keyword' must be an absolute URI", $location );
492 }
493 }
494  
495 22 ▶sub _check_enabled_extensions {
Subroutine _check_enabled_extensions: called CC=8 SCAR=22.8
496 22 my ( $self, $doc ) = @_;
497  
498 22 ▶ my $schema_uri = $doc->{'$schema'} // '';
Condition50%1/2
$doc->{"\$schema"} // ""
l!l
220
MC/DC0%0/1
$doc->{"\$schema"} // ""
$doc->{"\$schema"}
Truth table50%1/2
A
A= $doc->{"\$schema"}
Aresult
11
00
499 22 ▶ my $uses = $doc->{'$uses'} // [];
Condition50%1/2
$doc->{"\$uses"} // []
l!l
220
MC/DC0%0/1
$doc->{"\$uses"} // []
$doc->{"\$uses"}
Truth table50%1/2
A
A= $doc->{"\$uses"}
Aresult
11
00
500  
501 # Check if using extended or validation meta-schema
502 22 ▶ if ( $schema_uri =~ /extended|validation/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_uri =~ /extended|validation/u) 19 3 22
503 19 ▶ if ( $schema_uri =~ /validation/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema_uri =~ /validation/u) 0 19 19
504 $self->{enabled_extensions}{JSONStructureConditionalComposition} =
505 0 1;
506 0 $self->{enabled_extensions}{JSONStructureValidation} = 1;
507 }
508 }
509  
510 # Check $uses array
511 22 ▶ if ( ref($uses) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uses eq "ARRAY") 22 0 22
512 22 for my $ext (@$uses) {
513 22 ▶ if ( exists $KNOWN_EXTENSIONS{$ext} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $KNOWN_EXTENSIONS{$ext}) 22 0 22
514 22 $self->{enabled_extensions}{$ext} = 1;
515 }
516 }
517 }
518 }
519  
520 22 ▶sub _check_uses {
Subroutine _check_uses: called CC=7 SCAR=26.4
521 22 my ( $self, $uses, $path ) = @_;
522  
523 22 ▶ if ( ref($uses) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uses ne "ARRAY") 0 22 22
524 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
525 '$uses must be an array', $path );
526 0 return;
527 }
528  
529 22 for my $i ( 0 .. $#$uses ) {
530 22 my $ext = $uses->[$i];
531 22 ▶ if ( !defined $ext || ref($ext) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ext or ref $ext) { } 0 22 22
elsif ($self->{'extended'} and not exists $KNOWN_EXTENSIONS{$ext}) { } 0 22 22
Condition33%1/3
not defined $ext or ref $ext
l!l&&r!l&&!r
0022
MC/DC0%0/2
not defined $ext or ref $ext
not defined $ext ref $ext
Truth table33%1/3
not(A) or B
A= not defined $extB= ref $ext
ABresult
1X1
011
000
532 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
533 "\$uses[$i] must be a string", "$path\[$i]" );
534 }
535 ▶ elsif ( $self->{extended} && !exists $KNOWN_EXTENSIONS{$ext} ) {
Condition33%1/3
$self->{"extended"} and not exists $KNOWN_EXTENSIONS{$ext}
!ll&&!rl&&r
0220
MC/DC0%0/2
$self->{"extended"} and not exists $KNOWN_EXTENSIONS{$ext}
$self->{"extended"} not exists $KNOWN_EXTENSIONS{$ext}
Truth table33%1/3
A and not(B)
A= $self->{"extended"}B= not exists $KNOWN_EXTENSIONS{$ext}
ABresult
0X0
100
111
536 0 $self->_add_error(
537 SCHEMA_USES_UNKNOWN_EXTENSION,
538 "Unknown extension '$ext' in \$uses",
539 "$path\[$i]"
540 );
541 }
542 }
543 }
544  
545 0 ▶sub _check_offers {
Subroutine _check_offers: not called CC=5 SCAR=34.0
546 0 my ( $self, $offers, $path ) = @_;
547  
548 0 ▶ if ( ref($offers) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $offers ne "ARRAY") 0 0 0
549 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
550 '$offers must be an array', $path );
551 0 return;
552 }
553  
554 0 for my $i ( 0 .. $#$offers ) {
555 0 my $ext = $offers->[$i];
556 0 ▶ if ( !defined $ext || ref($ext) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ext or ref $ext) 0 0 0
Condition0%0/3
not defined $ext or ref $ext
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ext or ref $ext
not defined $ext ref $ext
Truth table0%0/3
not(A) or B
A= not defined $extB= ref $ext
ABresult
1X1
011
000
557 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
558 "\$offers[$i] must be a string", "$path\[$i]" );
559 }
560 }
561 }
562  
563 22 ▶sub _has_composition_keywords {
Subroutine _has_composition_keywords: called CC=4 SCAR=14.3
564 22 my ( $self, $obj ) = @_;
565  
566 22 ▶ return 0 unless ref($obj) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "HASH" 0 22 22
567  
568 22 for my $key ( keys %COMPOSITION_KEYWORDS ) {
569 154 ▶ return 1 if exists $obj->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{$key} 0 154 154
570 }
571  
572 22 return 0;
573 }
574  
575 52 ▶sub _check_composition_keywords {
Subroutine _check_composition_keywords: called CC=11 SCAR=39.1
576 52 my ( $self, $obj, $path ) = @_;
577  
578 52 for my $keyword (qw(allOf anyOf oneOf)) {
579 156 ▶ if ( exists $obj->{$keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{$keyword}) 0 156 156
580 0 $self->_validate_composition_array( $obj->{$keyword}, $keyword,
581 "$path/$keyword" );
582 }
583 }
584  
585 52 ▶ if ( exists $obj->{not} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"not"}) 0 52 52
586 0 ▶ if ( ref( $obj->{not} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{'not'} ne 'HASH') { } 0 0 0
587 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
588 "'not' must be a schema object", "$path/not" );
589 }
590 else {
591 0 $self->_validate_schema( $obj->{not}, 0, "$path/not", undef );
592 }
593 }
594  
595 # if/then/else
596 52 ▶ if ( exists $obj->{if} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"if"}) 0 52 52
597 0 ▶ if ( ref( $obj->{if} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{'if'} ne 'HASH') { } 0 0 0
598 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
599 "'if' must be a schema object", "$path/if" );
600 }
601 else {
602 0 $self->_validate_schema( $obj->{if}, 0, "$path/if", undef );
603 }
604 }
605  
606 52 ▶ if ( exists $obj->{then} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"then"}) 0 52 52
607 0 ▶ if ( ref( $obj->{then} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{'then'} ne 'HASH') { } 0 0 0
608 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
609 "'then' must be a schema object", "$path/then" );
610 }
611 else {
612 0 $self->_validate_schema( $obj->{then}, 0, "$path/then", undef );
613 }
614 }
615  
616 52 ▶ if ( exists $obj->{else} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{"else"}) 0 52 52
617 0 ▶ if ( ref( $obj->{else} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{'else'} ne 'HASH') { } 0 0 0
618 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
619 "'else' must be a schema object", "$path/else" );
620 }
621 else {
622 0 $self->_validate_schema( $obj->{else}, 0, "$path/else", undef );
623 }
624 }
625 }
626  
627 0 ▶sub _validate_composition_array {
Subroutine _validate_composition_array: not called CC=5 SCAR=34.0
628 0 my ( $self, $arr, $keyword, $path ) = @_;
629  
630 0 ▶ if ( ref($arr) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arr ne "ARRAY") 0 0 0
631 0 $self->_add_error( SCHEMA_COMPOSITION_NOT_ARRAY,
632 "$keyword must be an array", $path );
633 0 return;
634 }
635  
636 0 ▶ if ( @$arr == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$arr == 0) 0 0 0
637 0 $self->_add_error( SCHEMA_COMPOSITION_EMPTY,
638 "$keyword array cannot be empty", $path );
639 0 return;
640 }
641  
642 0 for my $i ( 0 .. $#$arr ) {
643 0 my $schema = $arr->[$i];
644 0 ▶ if ( ref($schema) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema ne 'HASH') { } 0 0 0
645 0 $self->_add_error(
646 SCHEMA_KEYWORD_INVALID_TYPE,
647 "$keyword\[$i] must be a schema object",
648 "$path\[$i]"
649 );
650 }
651 else {
652 0 $self->_validate_schema( $schema, 0, "$path\[$i]", undef );
653 }
654 }
655 }
656  
657 0 ▶sub _check_json_pointer {
Subroutine _check_json_pointer: not called CC=4 SCAR=30.0
658 0 my ( $self, $pointer, $doc, $path ) = @_;
659  
660 0 ▶ if ( !defined $pointer || ref($pointer) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pointer or ref $pointer) 0 0 0
Condition0%0/3
not defined $pointer or ref $pointer
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pointer or ref $pointer
not defined $pointer ref $pointer
Truth table0%0/3
not(A) or B
A= not defined $pointerB= ref $pointer
ABresult
1X1
011
000
661 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
662 '$root must be a string', $path );
663 0 return;
664 }
665  
666 # Validate the pointer resolves
667 0 my $target = $self->_resolve_json_pointer( $pointer, $doc );
668 0 ▶ if ( !defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $target) 0 0 0
669 0 $self->_add_error( SCHEMA_REF_NOT_FOUND,
670 "\$root target does not exist: $pointer", $path );
671 }
672 }
673  
674 8 ▶sub _resolve_json_pointer {
Subroutine _resolve_json_pointer: called CC=11 SCAR=31.7
675 8 my ( $self, $pointer, $doc ) = @_;
676  
677 # Handle # prefix
678 8 $pointer =~ s/^#//;
679  
680 8 ▶ return $doc if $pointer eq '' || $pointer eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $pointer eq "" or $pointer eq "/" 0 8 8
Condition33%1/3
$pointer eq "" or $pointer eq "/"
l!l&&r!l&&!r
008
MC/DC0%0/2
$pointer eq "" or $pointer eq "/"
$pointer eq "" $pointer eq "/"
Truth table33%1/3
A or B
A= $pointer eq ""B= $pointer eq "/"
ABresult
1X1
011
000
681  
682 8 my @segments = split m{/}, $pointer;
683 8 ▶ shift @segments if @segments && $segments[0] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @segments and $segments[0] eq "" 8 0 8
Condition33%1/3
@segments and $segments[0] eq ""
!ll&&!rl&&r
008
MC/DC0%0/2
@segments and $segments[0] eq ""
@segments $segments[0] eq ""
Truth table33%1/3
A and B
A= @segmentsB= $segments[0] eq ""
ABresult
0X0
100
111
684  
685 8 my $current = $doc;
686  
687 8 for my $segment (@segments) {
688  
689 # Unescape JSON Pointer tokens
690 15 $segment =~ s/~1/\//g;
691 15 $segment =~ s/~0/~/g;
692  
693 15 ▶ if ( ref($current) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $current eq 'HASH') { } 15 0 15
elsif (ref $current eq 'ARRAY') { } 0 0 0
694 15 ▶ return undef unless exists $current->{$segment};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$segment} 1 14 15
695 14 $current = $current->{$segment};
696 }
697 elsif ( ref($current) eq 'ARRAY' ) {
698 0 ▶ return undef unless $segment =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment =~ /^\d+$/u 0 0 0
699 0 my $idx = int($segment);
700 0 ▶ return undef if $idx >= @$current;
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= @$current 0 0 0
701 0 $current = $current->[$idx];
702 }
703 else {
704 0 return undef;
705 }
706 }
707  
708 7 return $current;
709 }
710  
711 8 ▶sub _validate_namespace {
Subroutine _validate_namespace: called CC=13 SCAR=41.7
712 8 my ( $self, $definitions, $path ) = @_;
713  
714 8 for my $name ( keys %$definitions ) {
715 12 my $def = $definitions->{$name};
716 12 my $def_path = "$path/$name";
717  
718 # Validate identifier
719 my $id_regex =
720 12 ▶ $self->{allow_dollar} ? $IDENTIFIER_DOLLAR_REGEX : $IDENTIFIER_REGEX;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'allow_dollar'} ? : 0 12 12
721 12 ▶ if ( $name !~ $id_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /$id_regex/u) 0 12 12
722 0 $self->_add_error( SCHEMA_NAME_INVALID,
723 "Definition name '$name' must be a valid identifier",
724 $def_path );
725 }
726  
727 12 ▶ if ( ref($def) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def ne "HASH") 0 12 12
728 0 $self->_add_error( SCHEMA_INVALID_TYPE,
729 'Definition must be an object', $def_path );
730 0 next;
731 }
732  
733 # Check for nested definitions (namespace)
734 12 ▶ if ( !exists $def->{type}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $def->{"type"} or exists $def->{"\$ref"} or $self->_has_composition_keywords($def)) 0 12 12
Condition33%2/6
exists $def->{"type"} or exists $def->{"\$ref"} or $self->_has_composition_keywords($def)
l!l&&r!l&&!r
1200
exists $def->{"type"} or exists $def->{"\$ref"}
l!l&&r!l&&!r
1200
MC/DC0%0/3
exists $def->{"type"} or exists $def->{"\$ref"} or $self->_has_composition_keywords($def)
exists $def->{"type"} exists $def->{"\$ref"} $self->_has_composition_keywords($def)
Truth table25%1/4
A or B or C
A= exists $def->{"type"}B= exists $def->{"\$ref"}C= $self->_has_composition_keywords($def)
ABCresult
1XX1
01X1
0011
0000
735 && !exists $def->{'$ref'}
736 && !$self->_has_composition_keywords($def) )
737 {
738 # Could be a namespace with nested definitions
739 0 my $has_nested = 0;
740 0 for my $key ( keys %$def ) {
741 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def->{$key} eq "HASH" and exists $def->{$key}{"type"} || exists $def->{$key}{"\$ref"}) 0 0 0
Condition0%0/6
ref $def->{$key} eq "HASH" and exists $def->{$key}{"type"} || exists $def->{$key}{"\$ref"}
!ll&&!rl&&r
000
exists $def->{$key}{"type"} or exists $def->{$key}{"\$ref"}
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $def->{$key} eq "HASH" and exists $def->{$key}{"type"} || exists $def->{$key}{"\$ref"}
ref $def->{$key} eq "HASH" exists $def->{$key}{"type"} exists $def->{$key}{"\$ref"}
Truth table0%0/4
A and B or C
A= ref $def->{$key} eq "HASH"B= exists $def->{$key}{"type"}C= exists $def->{$key}{"\$ref"}
ABCresult
0XX0
1000
11X1
1011
742 ref( $def->{$key} ) eq 'HASH'
743 && ( exists $def->{$key}{type}
744 || exists $def->{$key}{'$ref'} )
745 )
746 {
747 0 $has_nested = 1;
748 0 last;
749 }
750 }
751  
752 0 ▶ if ($has_nested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_nested) 0 0 0
753 0 $self->_validate_namespace( $def, $def_path );
754 0 next;
755 }
756  
757 # Not a namespace - must have type or $ref
758 0 $self->_add_error( SCHEMA_MISSING_TYPE,
759 "Definition must have 'type' or '\$ref'", $def_path );
760 0 next;
761 }
762  
763 12 $self->_validate_schema( $def, 0, $def_path, $name );
764 }
765 }
766  
767 147 ▶sub _validate_schema {
Subroutine _validate_schema: called CC=36 SCAR=53.6
768 147 my ( $self, $schema, $is_root, $path, $name_in_namespace ) = @_;
769  
770 # Check depth
771 147 $self->{current_depth}++;
772 147 ▶ if ( $self->{current_depth} > $self->{max_validation_depth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_depth"} > $self->{"max_validation_depth"}) 0 147 147
773 0 $self->_add_error(
774 SCHEMA_MAX_DEPTH_EXCEEDED,
775 "Maximum validation depth ($self->{max_validation_depth}) exceeded",
776 $path
777 );
778 0 $self->{current_depth}--;
779 0 return;
780 }
781  
782 # Handle boolean schemas
783 147 ▶ if ( !ref($schema) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema) 0 147 147
784 0 ▶ if ( $schema eq '1'
Branch: 1 branch
BranchTrueFalseTotal
if ($schema eq 1 or $schema eq 0 or _is_json_bool($schema) or $schema eq "true" or $schema eq "false") 0 0 0
Condition0%0/12
$schema eq 1 or $schema eq 0 or _is_json_bool($schema) or $schema eq "true" or $schema eq "false"
l!l&&r!l&&!r
000
$schema eq 1 or $schema eq 0 or _is_json_bool($schema) or $schema eq "true"
l!l&&r!l&&!r
000
$schema eq 1 or $schema eq 0 or _is_json_bool($schema)
l!l&&r!l&&!r
000
$schema eq 1 or $schema eq 0
l!l&&r!l&&!r
000
MC/DC0%0/5
$schema eq 1 or $schema eq 0 or _is_json_bool($schema) or $schema eq "true" or $schema eq "false"
$schema eq 1 $schema eq 0 _is_json_bool($schema) $schema eq "true" $schema eq "false"
Truth table0%0/6
A or B or C or D or E
A= $schema eq 1B= $schema eq 0C= _is_json_bool($schema)D= $schema eq "true"E= $schema eq "false"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
785 || $schema eq '0'
786 || _is_json_bool($schema)
787 || $schema eq 'true'
788 || $schema eq 'false' )
789 {
790 0 $self->{current_depth}--;
791 0 return; # Boolean schema is valid
792 }
793 }
794  
795 147 ▶ if ( ref($schema) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema ne "HASH") 0 147 147
796 0 $self->_add_error( SCHEMA_INVALID_TYPE,
797 'Schema must be a boolean or object', $path );
798 0 $self->{current_depth}--;
799 0 return;
800 }
801  
802 # Validate name if present
803 147 ▶ if ( exists $schema->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"name"}) 89 58 147
804 89 ▶ if ( !defined $schema->{name}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $schema->{"name"} or ref $schema->{"name"} or not $schema->{"name"} =~ /$IDENTIFIER_DOLLAR_REGEX/u) 0 89 89
Condition33%2/6
not defined $schema->{"name"} or ref $schema->{"name"} or not $schema->{"name"} =~ /$IDENTIFIER_DOLLAR_REGEX/u
l!l&&r!l&&!r
0089
not defined $schema->{"name"} or ref $schema->{"name"}
l!l&&r!l&&!r
0089
MC/DC0%0/3
not defined $schema->{"name"} or ref $schema->{"name"} or not $schema->{"name"} =~ /$IDENTIFIER_DOLLAR_REGEX/u
not defined $schema->{"name"} ref $schema->{"name"} not $schema->{"name"} =~ /$IDENTIFIER_DOLLAR_REGEX/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $schema->{"name"}B= ref $schema->{"name"}C= not $schema->{"name"} =~ /$IDENTIFIER_DOLLAR_REGEX/u
ABCresult
1XX1
01X1
0011
0000
805 || ref( $schema->{name} )
806 || $schema->{name} !~ $IDENTIFIER_DOLLAR_REGEX )
807 {
808 0 $self->_add_error( SCHEMA_NAME_INVALID,
809 'name must be a valid identifier', "$path/name" );
810 }
811 }
812  
813 # Validate type
814 147 my $type = $schema->{type};
815  
816 147 ▶ if ( !defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type) { } 0 147 147
817  
818 # Check for composition keywords in extended mode
819 0 ▶ if ( $self->{extended} && $self->_has_composition_keywords($schema) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"} and $self->_has_composition_keywords($schema)) 0 0 0
Condition0%0/3
$self->{"extended"} and $self->_has_composition_keywords($schema)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"extended"} and $self->_has_composition_keywords($schema)
$self->{"extended"} $self->_has_composition_keywords($schema)
Truth table0%0/3
A and B
A= $self->{"extended"}B= $self->_has_composition_keywords($schema)
ABresult
0X0
100
111
820 0 $self->_check_composition_keywords( $schema, $path );
821 0 $self->{current_depth}--;
822 0 return;
823 }
824  
825 # Check for $ref in type object form - this is allowed
826 0 ▶ if ( exists $schema->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"\$ref"}) 0 0 0
827 0 $self->_add_error( SCHEMA_REF_NOT_IN_TYPE,
828 "\$ref is only permitted inside the 'type' attribute", $path );
829 }
830  
831 0 ▶ if ( !$is_root ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_root) 0 0 0
832  
833 # Non-root schemas without type are OK if they have other defining keywords
834 0 my $has_defining = 0;
835 0 for my $kw (qw(properties items values choices const enum)) {
836 0 ▶ if ( exists $schema->{$kw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{$kw}) 0 0 0
837 0 $has_defining = 1;
838 0 last;
839 }
840 }
841  
842 0 ▶ if ( !$has_defining && !exists $schema->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_defining or exists $schema->{"\$ref"}) 0 0 0
Condition0%0/3
$has_defining or exists $schema->{"\$ref"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$has_defining or exists $schema->{"\$ref"}
$has_defining exists $schema->{"\$ref"}
Truth table0%0/3
A or B
A= $has_definingB= exists $schema->{"\$ref"}
ABresult
1X1
011
000
843  
844 # Could be just metadata - that's OK in some contexts
845 }
846 }
847 }
848 else {
849 147 $self->_validate_type( $type, $schema, $path );
850 }
851  
852 # Validate properties
853 147 ▶ if ( exists $schema->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"properties"}) 35 112 147
854 35 $self->_validate_properties( $schema->{properties}, $path );
855 }
856  
857 # Validate required
858 147 ▶ if ( exists $schema->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"required"}) 9 138 147
859 $self->_validate_required( $schema->{required}, $schema->{properties},
860 9 $path );
861 }
862  
863 # Validate items
864 147 ▶ if ( exists $schema->{items} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"items"}) 3 144 147
865 3 $self->_validate_items( $schema->{items}, "$path/items" );
866 }
867  
868 # Validate values (for map type)
869 147 ▶ if ( exists $schema->{values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"values"}) 1 146 147
870 1 $self->_validate_values( $schema->{values}, "$path/values" );
871 }
872  
873 # Validate choices (for choice type)
874 147 ▶ if ( exists $schema->{choices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"choices"}) 1 146 147
875 $self->_validate_choices( $schema->{choices}, $schema->{selector},
876 1 $path );
877 }
878  
879 # Validate tuple
880 147 ▶ if ( exists $schema->{tuple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"tuple"}) 1 146 147
881 $self->_validate_tuple( $schema->{tuple}, $schema->{properties},
882 1 $path );
883 }
884  
885 # Validate enum
886 147 ▶ if ( exists $schema->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"enum"}) 3 144 147
887 3 $self->_validate_enum( $schema->{enum}, "$path/enum", $schema->{type} );
888 }
889  
890 # Validate const
891 147 ▶ if ( exists $schema->{const} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"const"}) 0 147 147
892  
893 # const can be any value, no specific validation needed
894 }
895  
896 # Validate additionalProperties
897 147 ▶ if ( exists $schema->{additionalProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"additionalProperties"}) 0 147 147
898 $self->_validate_additional_properties( $schema->{additionalProperties},
899 0 "$path/additionalProperties" );
900 }
901  
902 # Validate $extends
903 147 ▶ if ( exists $schema->{'$extends'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"\$extends"}) 0 147 147
904 0 $self->_validate_extends( $schema->{'$extends'}, $path );
905 }
906  
907 # Validate definitions within schema
908 147 ▶ if ( exists $schema->{definitions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"definitions"}) 4 143 147
909 4 ▶ if ( ref( $schema->{definitions} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{'definitions'} ne 'HASH') { } 0 4 4
910 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
911 'definitions must be an object',
912 "$path/definitions" );
913 }
914 else {
915 $self->_validate_namespace( $schema->{definitions},
916 4 "$path/definitions" );
917 }
918 }
919  
920 # Validate extended keywords if enabled
921 147 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 52 95 147
922 52 $self->_validate_extended_keywords( $schema, $type, $path );
923  
924 # Also check composition keywords within schemas with type
925 52 $self->_check_composition_keywords( $schema, $path );
926 }
927  
928 # Validate altnames
929 147 ▶ if ( exists $schema->{altnames} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"altnames"}) 0 147 147
930 0 $self->_validate_altnames( $schema->{altnames}, "$path/altnames" );
931 }
932  
933 147 $self->{current_depth}--;
934 }
935  
936 147 ▶sub _validate_type {
Subroutine _validate_type: called CC=10 SCAR=34.9
937 147 my ( $self, $type, $schema, $path ) = @_;
938  
939 147 my $type_path = "$path/type";
940  
941 # Type can be a string, array of strings, or object with $ref
942 147 ▶ if ( !defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 147 147
943 0 return; # No type is OK in some contexts
944 }
945  
946 147 ▶ if ( !ref($type) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $type) { } 145 2 147
elsif (ref $type eq 'ARRAY') { } 0 2 2
elsif (ref $type eq 'HASH') { } 2 0 2
947  
948 # Simple string type
949 145 $self->_validate_type_name( $type, $type_path );
950 145 $self->_validate_type_constraints( $type, $schema, $path );
951 }
952 elsif ( ref($type) eq 'ARRAY' ) {
953  
954 # Array of types (union)
955 0 ▶ if ( @$type == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$type == 0) 0 0 0
956 0 $self->_add_error( SCHEMA_TYPE_ARRAY_EMPTY,
957 'type array cannot be empty', $type_path );
958 0 return;
959 }
960  
961 0 for my $i ( 0 .. $#$type ) {
962 0 my $t = $type->[$i];
963 0 ▶ if ( !defined $t || ref($t) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $t or ref $t) { } 0 0 0
Condition0%0/3
not defined $t or ref $t
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $t or ref $t
not defined $t ref $t
Truth table0%0/3
not(A) or B
A= not defined $tB= ref $t
ABresult
1X1
011
000
964 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
965 "type[$i] must be a string",
966 "$type_path\[$i]" );
967 }
968 else {
969 0 $self->_validate_type_name( $t, "$type_path\[$i]" );
970 }
971 }
972 }
973 elsif ( ref($type) eq 'HASH' ) {
974  
975 # Object type with $ref
976 2 ▶ if ( !exists $type->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $type->{'$ref'}) { } 0 2 2
977 0 $self->_add_error( SCHEMA_TYPE_OBJECT_MISSING_REF,
978 'type object must contain $ref', $type_path );
979 }
980 else {
981 2 $self->_validate_ref( $type->{'$ref'}, "$type_path/\$ref" );
982 }
983 }
984 else {
985 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
986 'type must be a string, array, or object with $ref', $type_path );
987 }
988 }
989  
990 145 ▶sub _validate_type_name {
Subroutine _validate_type_name: called CC=3 SCAR=11.0
991 145 my ( $self, $type_name, $path ) = @_;
992  
993 145 ▶ return if exists $PRIMITIVE_TYPES{$type_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $PRIMITIVE_TYPES{$type_name} 97 48 145
994 48 ▶ return if exists $COMPOUND_TYPES{$type_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $COMPOUND_TYPES{$type_name} 44 4 48
995  
996 4 $self->_add_error( SCHEMA_TYPE_INVALID, "Invalid type: '$type_name'",
997 $path );
998 }
999  
1000 145 ▶sub _validate_type_constraints {
Subroutine _validate_type_constraints: called CC=12 SCAR=24.9
1001 145 my ( $self, $type, $schema, $path ) = @_;
1002  
1003 # Validate compound type requirements
1004 145 ▶ if ( $type eq 'array' || $type eq 'set' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'array' or $type eq 'set') { } 4 141 145
elsif ($type eq 'map') { } 2 139 141
elsif ($type eq 'tuple') { } 2 137 139
elsif ($type eq 'choice') { } 2 135 137
Condition66%2/3
$type eq "array" or $type eq "set"
l!l&&r!l&&!r
40141
MC/DC50%1/2
$type eq "array" or $type eq "set"
$type eq "array" $type eq "set"
Truth table66%2/3
A or B
A= $type eq "array"B= $type eq "set"
ABresult
1X1
011
000
1005 4 ▶ if ( !exists $schema->{items} && !exists $schema->{contains} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $schema->{"items"} || exists $schema->{"contains"}) 1 3 4
Condition66%2/3
exists $schema->{'items'} || exists $schema->{'contains'}
l!l&&r!l&&!r
301
MC/DC50%1/2
exists $schema->{'items'} || exists $schema->{'contains'}
exists $schema->{'items'} exists $schema->{'contains'}
Truth table66%2/3
A || B
A= exists $schema->{'items'}B= exists $schema->{'contains'}
ABresult
1X1
011
000
1006 1 $self->_add_error( SCHEMA_ARRAY_MISSING_ITEMS,
1007 "$type type requires 'items' or 'contains' schema", $path );
1008 }
1009 }
1010 elsif ( $type eq 'map' ) {
1011 2 ▶ if ( !exists $schema->{values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $schema->{"values"}) 1 1 2
1012 1 $self->_add_error( SCHEMA_MAP_MISSING_VALUES,
1013 "map type requires 'values' schema", $path );
1014 }
1015 }
1016 elsif ( $type eq 'tuple' ) {
1017 2 ▶ if ( !exists $schema->{properties} || !exists $schema->{tuple} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $schema->{"properties"} && exists $schema->{"tuple"}) 1 1 2
Condition66%2/3
exists $schema->{'properties'} && exists $schema->{'tuple'}
!ll&&!rl&&r
101
MC/DC50%1/2
exists $schema->{'properties'} && exists $schema->{'tuple'}
exists $schema->{'properties'} exists $schema->{'tuple'}
Truth table66%2/3
A && B
A= exists $schema->{'properties'}B= exists $schema->{'tuple'}
ABresult
0X0
100
111
1018 1 $self->_add_error( SCHEMA_TUPLE_MISSING_DEFINITION,
1019 "tuple type requires 'properties' and 'tuple' keywords",
1020 $path );
1021 }
1022 }
1023 elsif ( $type eq 'choice' ) {
1024 2 ▶ if ( !exists $schema->{choices} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $schema->{"choices"}) 1 1 2
1025 1 $self->_add_error( SCHEMA_CHOICE_MISSING_CHOICES,
1026 "choice type requires 'choices' keyword", $path );
1027 }
1028 }
1029 }
1030  
1031 8 ▶sub _validate_ref {
Subroutine _validate_ref: called CC=10 SCAR=29.5
1032 8 my ( $self, $ref, $path ) = @_;
1033  
1034 8 ▶ if ( !defined $ref || ref($ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ref or ref $ref) 0 8 8
Condition33%1/3
not defined $ref or ref $ref
l!l&&r!l&&!r
008
MC/DC0%0/2
not defined $ref or ref $ref
not defined $ref ref $ref
Truth table33%1/3
not(A) or B
A= not defined $refB= ref $ref
ABresult
1X1
011
000
1035 0 $self->_add_error( SCHEMA_REF_INVALID, '$ref must be a string', $path );
1036 0 return;
1037 }
1038  
1039 # Check for circular references
1040 8 ▶ if ( exists $self->{seen_refs}{$ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen_refs"}{$ref}) 0 8 8
1041 0 $self->_add_error( SCHEMA_CIRCULAR_REF,
1042 "Circular reference detected: $ref", $path );
1043 0 return;
1044 }
1045  
1046 # Mark this ref as being processed
1047 8 $self->{seen_refs}{$ref} = 1;
1048  
1049 # Check if reference resolves
1050 8 my $target = $self->_resolve_json_pointer( $ref, $self->{doc} );
1051 8 ▶ if ( !defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $target) { } 1 7 8
1052  
1053 # Check external schemas
1054 1 my $found = 0;
1055 1 for my $id ( keys %{ $self->{external_schemas} } ) {
1056 0 my $ext = $self->{external_schemas}{$id};
1057 0 ▶ if ( $ref =~ /^$id/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /^$id/u) 0 0 0
1058 0 $found = 1;
1059 0 last;
1060 }
1061 }
1062  
1063 1 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 1 0 1
1064 1 $self->_add_error( SCHEMA_REF_NOT_FOUND,
1065 "\$ref target does not exist: $ref", $path );
1066 }
1067 }
1068 else {
1069 # Validate the target schema, checking for circular refs
1070 7 ▶ if ( ref($target) eq 'HASH' && exists $target->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target eq "HASH" and exists $target->{"type"}) 7 0 7
Condition33%1/3
ref $target eq "HASH" and exists $target->{"type"}
!ll&&!rl&&r
007
MC/DC0%0/2
ref $target eq "HASH" and exists $target->{"type"}
ref $target eq "HASH" exists $target->{"type"}
Truth table33%1/3
A and B
A= ref $target eq "HASH"B= exists $target->{"type"}
ABresult
0X0
100
111
1071 7 $self->_check_type_for_circular_ref( $target, $ref, $path );
1072 }
1073 }
1074  
1075 # Clear the seen ref after processing
1076 8 delete $self->{seen_refs}{$ref};
1077 }
1078  
1079 7 ▶sub _check_type_for_circular_ref {
Subroutine _check_type_for_circular_ref: called CC=7 SCAR=28.4
1080 7 my ( $self, $schema, $original_ref, $path ) = @_;
1081  
1082 7 ▶ return unless ref($schema) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 7 7
1083  
1084 7 my $type = $schema->{type};
1085 7 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 7 7
1086  
1087 7 ▶ if ( ref($type) eq 'HASH' && exists $type->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH" and exists $type->{"\$ref"}) 0 7 7
Condition33%1/3
ref $type eq "HASH" and exists $type->{"\$ref"}
!ll&&!rl&&r
700
MC/DC0%0/2
ref $type eq "HASH" and exists $type->{"\$ref"}
ref $type eq "HASH" exists $type->{"\$ref"}
Truth table33%1/3
A and B
A= ref $type eq "HASH"B= exists $type->{"\$ref"}
ABresult
0X0
100
111
1088 0 my $nested_ref = $type->{'$ref'};
1089 0 ▶ if ( $nested_ref eq $original_ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nested_ref eq $original_ref) { } 0 0 0
elsif (exists $self->{'seen_refs'}{$nested_ref}) { } 0 0 0
1090 0 $self->_add_error(
1091 SCHEMA_CIRCULAR_REF,
1092 "Direct circular reference detected: type references $nested_ref which references itself",
1093 $path
1094 );
1095 }
1096 elsif ( exists $self->{seen_refs}{$nested_ref} ) {
1097 0 $self->_add_error( SCHEMA_CIRCULAR_REF,
1098 "Circular reference chain detected involving: $nested_ref",
1099 $path );
1100 }
1101 }
1102 }
1103  
1104 35 ▶sub _validate_properties {
Subroutine _validate_properties: called CC=5 SCAR=19.6
1105 35 my ( $self, $properties, $path ) = @_;
1106  
1107 35 ▶ if ( ref($properties) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $properties ne "HASH") 0 35 35
1108 0 $self->_add_error( SCHEMA_PROPERTIES_NOT_OBJECT,
1109 'properties must be an object',
1110 "$path/properties" );
1111 0 return;
1112 }
1113  
1114 35 for my $prop_name ( keys %$properties ) {
1115 48 my $prop_schema = $properties->{$prop_name};
1116 48 my $prop_path = "$path/properties/$prop_name";
1117  
1118 48 ▶ if ( ref($prop_schema) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $prop_schema eq 'HASH') { } 48 0 48
elsif (not ref $prop_schema) { } 0 0 0
1119 48 $self->_validate_schema( $prop_schema, 0, $prop_path, undef );
1120 }
1121 elsif ( !ref($prop_schema) ) {
1122  
1123 # Boolean schema is valid
1124 }
1125 else {
1126 0 $self->_add_error( SCHEMA_INVALID_TYPE,
1127 'Property schema must be a boolean or object', $prop_path );
1128 }
1129 }
1130 }
1131  
1132 9 ▶sub _validate_required {
Subroutine _validate_required: called CC=8 SCAR=25.5
1133 9 my ( $self, $required, $properties, $path ) = @_;
1134  
1135 9 ▶ if ( ref($required) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $required ne "ARRAY") 0 9 9
1136 0 $self->_add_error( SCHEMA_REQUIRED_NOT_ARRAY,
1137 'required must be an array',
1138 "$path/required" );
1139 0 return;
1140 }
1141  
1142 9 for my $i ( 0 .. $#$required ) {
1143 10 my $prop = $required->[$i];
1144  
1145 10 ▶ if ( !defined $prop || ref($prop) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prop or ref $prop) 0 10 10
Condition33%1/3
not defined $prop or ref $prop
l!l&&r!l&&!r
0010
MC/DC0%0/2
not defined $prop or ref $prop
not defined $prop ref $prop
Truth table33%1/3
not(A) or B
A= not defined $propB= ref $prop
ABresult
1X1
011
000
1146 0 $self->_add_error(
1147 SCHEMA_REQUIRED_ITEM_NOT_STRING,
1148 'required array items must be strings',
1149 "$path/required[$i]"
1150 );
1151 0 next;
1152 }
1153  
1154 # Check if property exists in properties
1155 10 ▶ if ( defined $properties && ref($properties) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties and ref $properties eq "HASH") 10 0 10
Condition33%1/3
defined $properties and ref $properties eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $properties and ref $properties eq "HASH"
defined $properties ref $properties eq "HASH"
Truth table33%1/3
A and B
A= defined $propertiesB= ref $properties eq "HASH"
ABresult
0X0
100
111
1156 10 ▶ if ( !exists $properties->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $properties->{$prop}) 1 9 10
1157 1 $self->_add_error(
1158 SCHEMA_REQUIRED_PROPERTY_NOT_DEFINED,
1159 "Required property '$prop' is not defined in properties",
1160 "$path/required[$i]"
1161 );
1162 }
1163 }
1164 }
1165 }
1166  
1167 3 ▶sub _validate_items {
Subroutine _validate_items: called CC=3 SCAR=14.2
1168 3 my ( $self, $items, $path ) = @_;
1169  
1170 3 ▶ if ( ref($items) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $items eq 'HASH') { } 3 0 3
elsif (not ref $items) { } 0 0 0
1171 3 $self->_validate_schema( $items, 0, $path, undef );
1172 }
1173 elsif ( !ref($items) ) {
1174  
1175 # Boolean schema is valid
1176 }
1177 else {
1178 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
1179 'items must be a boolean or schema object', $path );
1180 }
1181 }
1182  
1183 1 ▶sub _validate_values {
Subroutine _validate_values: called CC=3 SCAR=14.2
1184 1 my ( $self, $values, $path ) = @_;
1185  
1186 1 ▶ if ( ref($values) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $values eq 'HASH') { } 1 0 1
elsif (not ref $values) { } 0 0 0
1187 1 $self->_validate_schema( $values, 0, $path, undef );
1188 }
1189 elsif ( !ref($values) ) {
1190  
1191 # Boolean schema is valid
1192 }
1193 else {
1194 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
1195 'values must be a boolean or schema object', $path );
1196 }
1197 }
1198  
1199 1 ▶sub _validate_choices {
Subroutine _validate_choices: called CC=6 SCAR=23.5
1200 1 my ( $self, $choices, $selector, $path ) = @_;
1201  
1202 1 ▶ if ( ref($choices) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $choices ne "HASH") 0 1 1
1203 0 $self->_add_error( SCHEMA_CHOICES_NOT_OBJECT,
1204 'choices must be an object',
1205 "$path/choices" );
1206 0 return;
1207 }
1208  
1209 1 ▶ if ( keys %$choices == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$choices == 0) 0 1 1
1210 0 $self->_add_error( SCHEMA_KEYWORD_EMPTY, 'choices cannot be empty',
1211 "$path/choices" );
1212 0 return;
1213 }
1214  
1215 1 for my $choice_name ( keys %$choices ) {
1216 2 my $choice_schema = $choices->{$choice_name};
1217 2 ▶ if ( ref($choice_schema) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $choice_schema eq 'HASH') { } 2 0 2
elsif (not ref $choice_schema) { } 0 0 0
1218 2 $self->_validate_schema( $choice_schema, 0,
1219 "$path/choices/$choice_name", undef );
1220 }
1221 elsif ( !ref($choice_schema) ) {
1222  
1223 # Boolean schema is valid
1224 }
1225 else {
1226 0 $self->_add_error(
1227 SCHEMA_INVALID_TYPE,
1228 'Choice schema must be a boolean or object',
1229 "$path/choices/$choice_name"
1230 );
1231 }
1232 }
1233 }
1234  
1235 1 ▶sub _validate_tuple {
Subroutine _validate_tuple: called CC=13 SCAR=40.4
1236 1 my ( $self, $tuple, $properties, $path ) = @_;
1237  
1238 1 ▶ if ( ref($tuple) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tuple ne "ARRAY") 0 1 1
1239 0 $self->_add_error(
1240 SCHEMA_TUPLE_ORDER_NOT_ARRAY,
1241 "'tuple' keyword must be an array of property names",
1242 "$path/tuple"
1243 );
1244 0 return;
1245 }
1246  
1247 # Validate each tuple entry exists in properties or resolves via $ref
1248 1 ▶ if ( defined $properties && ref($properties) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $properties and ref $properties eq "HASH") 1 0 1
Condition33%1/3
defined $properties and ref $properties eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $properties and ref $properties eq "HASH"
defined $properties ref $properties eq "HASH"
Truth table33%1/3
A and B
A= defined $propertiesB= ref $properties eq "HASH"
ABresult
0X0
100
111
1249 1 for my $i ( 0 .. $#$tuple ) {
1250 2 my $prop = $tuple->[$i];
1251  
1252 2 ▶ if ( ref($prop) eq 'HASH' && exists $prop->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prop eq "HASH" and exists $prop->{"\$ref"}) 0 2 2
Condition33%1/3
ref $prop eq "HASH" and exists $prop->{"\$ref"}
!ll&&!rl&&r
200
MC/DC0%0/2
ref $prop eq "HASH" and exists $prop->{"\$ref"}
ref $prop eq "HASH" exists $prop->{"\$ref"}
Truth table33%1/3
A and B
A= ref $prop eq "HASH"B= exists $prop->{"\$ref"}
ABresult
0X0
100
111
1253 0 my $ref = $prop->{'$ref'};
1254 my $target =
1255 defined $ref && !ref($ref)
1256 ? $self->_resolve_json_pointer( $ref, $self->{doc} )
1257 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && !ref($ref) ? : 0 0 0
Condition0%0/3
defined $ref and not ref $ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ref and not ref $ref
defined $ref not ref $ref
Truth table0%0/3
A and not(B)
A= defined $refB= not ref $ref
ABresult
0X0
100
111
1258 0 ▶ if ( !defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target) 0 0 0
1259 0 $self->_add_error( SCHEMA_REF_NOT_FOUND,
1260 "\$ref target does not exist: $ref",
1261 "$path/tuple[$i]/\$ref" );
1262 }
1263 0 next;
1264 }
1265  
1266 2 ▶ if ( !defined $prop || ref($prop) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $prop or ref $prop) 0 2 2
Condition33%1/3
not defined $prop or ref $prop
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $prop or ref $prop
not defined $prop ref $prop
Truth table33%1/3
not(A) or B
A= not defined $propB= ref $prop
ABresult
1X1
011
000
1267 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
1268 "tuple[$i] must be a string",
1269 "$path/tuple[$i]" );
1270 0 next;
1271 }
1272  
1273 2 ▶ if ( !exists $properties->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $properties->{$prop}) 0 2 2
1274 0 $self->_add_error(
1275 SCHEMA_REQUIRED_PROPERTY_NOT_DEFINED,
1276 "Tuple property '$prop' is not defined in properties",
1277 "$path/tuple[$i]"
1278 );
1279 }
1280 }
1281 }
1282 }
1283  
1284 3 ▶sub _validate_enum {
Subroutine _validate_enum: called CC=10 SCAR=25.1
1285 3 my ( $self, $enum, $path, $type ) = @_;
1286  
1287 3 ▶ if ( ref($enum) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $enum ne "ARRAY") 0 3 3
1288 0 $self->_add_error( SCHEMA_ENUM_NOT_ARRAY, 'enum must be an array',
1289 $path );
1290 0 return;
1291 }
1292  
1293 3 ▶ if ( @$enum == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$enum == 0) 1 2 3
1294 1 $self->_add_error( SCHEMA_ENUM_EMPTY, 'enum array cannot be empty',
1295 $path );
1296 1 return;
1297 }
1298  
1299 # Check for duplicates
1300 2 my %seen;
1301 2 for my $i ( 0 .. $#$enum ) {
1302 6 my $value = $enum->[$i];
1303 6 my $key = _value_to_key($value);
1304  
1305 6 ▶ if ( exists $seen{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) 1 5 6
1306 1 $self->_add_error( SCHEMA_ENUM_DUPLICATES,
1307 'enum array contains duplicate values', $path );
1308 1 last;
1309 }
1310 5 $seen{$key} = 1;
1311 }
1312  
1313 2 ▶ if ( defined $type && !ref($type) && !exists $COMPOUND_TYPES{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and not ref $type and not exists $COMPOUND_TYPES{$type}) 2 0 2
Condition33%2/6
defined $type and not ref $type and not exists $COMPOUND_TYPES{$type}
!ll&&!rl&&r
002
defined $type and not ref $type
!ll&&!rl&&r
002
MC/DC0%0/3
defined $type and not ref $type and not exists $COMPOUND_TYPES{$type}
defined $type not ref $type not exists $COMPOUND_TYPES{$type}
Truth table25%1/4
A and not(B) and not(C)
A= defined $typeB= not ref $typeC= not exists $COMPOUND_TYPES{$type}
ABCresult
0XX0
10X0
1100
1111
1314 2 for my $i ( 0 .. $#$enum ) {
1315 6 ▶ next if _enum_value_matches_type( $enum->[$i], $type );
Branch: 1 branch
BranchTrueFalseTotal
if _enum_value_matches_type($enum->[$i], $type) 6 0 6
1316 0 $self->_add_error(
1317 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1318 "enum value at index $i does not match declared type '$type'",
1319 "$path\[$i]"
1320 );
1321 }
1322 }
1323 }
1324  
1325 6 ▶sub _enum_value_matches_type {
Subroutine _enum_value_matches_type: called CC=11 SCAR=37.0
1326 6 my ( $value, $type ) = @_;
1327  
1328 6 ▶ return !defined $value if $type eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "null" 0 6 6
1329 6 ▶ return _is_json_bool($value) if $type eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" 0 6 6
1330 return defined $value
1331 && !ref($value)
1332 && !_is_json_bool($value)
1333 && looks_like_number($value)
1334 6 ▶ if exists $NUMERIC_TYPES{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $NUMERIC_TYPES{$type} 0 6 6
Condition0%0/9
defined $value && !ref($value) && !_is_json_bool($value) && &looks_like_number($value)
!ll&&!rl&&r
000
defined $value and not ref $value and not _is_json_bool($value)
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $value && !ref($value) && !_is_json_bool($value) && &looks_like_number($value)
defined $value not ref $value not _is_json_bool($value) &looks_like_number($value)
Truth table0%0/5
A and not(B) and not(C) && D
A= defined $valueB= not ref $valueC= not _is_json_bool($value)D= &looks_like_number($value)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1335 6 ▶ return defined $value
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 6 0 6
Condition33%3/9
defined $value && !ref($value) && !_is_json_bool($value) && !&looks_like_number($value)
!ll&&!rl&&r
006
defined $value and not ref $value and not _is_json_bool($value)
!ll&&!rl&&r
006
defined $value and not ref $value
!ll&&!rl&&r
006
MC/DC0%0/4
defined $value && !ref($value) && !_is_json_bool($value) && !&looks_like_number($value)
defined $value not ref $value not _is_json_bool($value) !&looks_like_number($value)
Truth table20%1/5
A and not(B) and not(C) && not(D)
A= defined $valueB= not ref $valueC= not _is_json_bool($value)D= !&looks_like_number($value)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1336 && !ref($value)
1337 && !_is_json_bool($value)
1338 && !looks_like_number($value)
1339 if $type eq 'string';
1340  
1341 0 return 1;
1342 }
1343  
1344 6 ▶sub _value_to_key {
Subroutine _value_to_key: called CC=5 SCAR=26.2
1345 6 my ($value) = @_;
1346  
1347 # Create a string key for comparison
1348 6 ▶ if ( !defined $value ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 6 6
elsif (not ref $value) { } 6 0 6
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
1349 0 return 'null';
1350 }
1351 elsif ( !ref($value) ) {
1352 6 return "s:$value";
1353 }
1354 elsif ( ref($value) eq 'ARRAY' ) {
1355 0 return 'a:' . join( ',', map { _value_to_key($_) } @$value );
1356 }
1357 elsif ( ref($value) eq 'HASH' ) {
1358 return 'o:'
1359 . join( ',',
1360 0 map { "$_:" . _value_to_key( $value->{$_} ) } sort keys %$value );
1361 }
1362 else {
1363 0 return "?:$value";
1364 }
1365 }
1366  
1367 0 ▶sub _validate_additional_properties {
Subroutine _validate_additional_properties: not called CC=7 SCAR=40.3
1368 0 my ( $self, $additional, $path ) = @_;
1369  
1370 0 ▶ if ( ref($additional) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $additional eq 'HASH') { } 0 0 0
elsif (not ref $additional) { } 0 0 0
1371 0 $self->_validate_schema( $additional, 0, $path, undef );
1372 }
1373 elsif ( !ref($additional) ) {
1374  
1375 # Boolean is valid
1376 0 ▶ if ( $additional !~ /^[01]$/
Branch: 1 branch
BranchTrueFalseTotal
if (not $additional =~ /^[01]$/u || _is_json_bool($additional) and $additional ne "true" and $additional ne "false") 0 0 0
Condition0%0/9
not $additional =~ /^[01]$/u || _is_json_bool($additional) and $additional ne "true" and $additional ne "false"
!ll&&!rl&&r
000
not $additional =~ /^[01]$/u || _is_json_bool($additional) and $additional ne "true"
!ll&&!rl&&r
000
$additional =~ /^[01]$/u || _is_json_bool($additional)
l!l&&r!l&&!r
000
MC/DC0%0/4
not $additional =~ /^[01]$/u || _is_json_bool($additional) and $additional ne "true" and $additional ne "false"
$additional =~ /^[01]$/u _is_json_bool($additional) $additional ne "true" $additional ne "false"
Truth table0%0/5
not(A || B) and C and D
A= $additional =~ /^[01]$/uB= _is_json_bool($additional)C= $additional ne "true"D= $additional ne "false"
ABCDresult
1XXX0
01XX0
000X0
00100
00111
1377 && !_is_json_bool($additional)
1378 && $additional ne 'true'
1379 && $additional ne 'false' )
1380 {
1381 # Plain strings that aren't booleans aren't valid
1382 0 $self->_add_error( SCHEMA_ADDITIONAL_PROPERTIES_INVALID,
1383 'additionalProperties must be a boolean or schema', $path );
1384 }
1385 }
1386 else {
1387 0 $self->_add_error( SCHEMA_ADDITIONAL_PROPERTIES_INVALID,
1388 'additionalProperties must be a boolean or schema', $path );
1389 }
1390 }
1391  
1392 0 ▶sub _validate_extends {
Subroutine _validate_extends: not called CC=16 SCAR=56.1
1393 0 my ( $self, $extends, $path ) = @_;
1394  
1395 0 ▶ if ( !defined $extends || ref($extends) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $extends or ref $extends) 0 0 0
Condition0%0/3
not defined $extends or ref $extends
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $extends or ref $extends
not defined $extends ref $extends
Truth table0%0/3
not(A) or B
A= not defined $extendsB= ref $extends
ABresult
1X1
011
000
1396 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
1397 '$extends must be a string',
1398 "$path/\$extends" );
1399 0 return;
1400 }
1401  
1402 # Check for circular extends
1403 0 ▶ if ( exists $self->{seen_extends}{$extends} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen_extends"}{$extends}) 0 0 0
1404 0 $self->_add_error(
1405 SCHEMA_EXTENDS_CIRCULAR,
1406 "Circular \$extends reference detected: $extends",
1407 "$path/\$extends"
1408 );
1409 0 return;
1410 }
1411  
1412 0 $self->{seen_extends}{$extends} = 1;
1413  
1414 # Check if reference resolves
1415 0 my $target = $self->_resolve_json_pointer( $extends, $self->{doc} );
1416 0 ▶ if ( !defined $target ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $target) { } 0 0 0
elsif (ref $target ne 'HASH' or defined $target->{'type'} and not ref $target->{'type'} and $target->{'type'} ne 'object' and $target->{'type'} ne 'tuple' and $target->{'type'} ne 'map' and $target->{'type'} ne 'array' and $target->{'type'} ne 'set' and $target->{'type'} ne 'choice') { } 0 0 0
elsif (ref $target eq 'HASH' and defined $target->{'$extends'}) { } 0 0 0
1417 0 $self->_add_error(
1418 SCHEMA_EXTENDS_NOT_FOUND,
1419 "\$extends reference not found: $extends",
1420 "$path/\$extends"
1421 );
1422 }
1423 ▶ elsif (
Condition0%0/24
ref $target ne "HASH" or defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map" and $target->{"type"} ne "array" and $target->{"type"} ne "set" and $target->{"type"} ne "choice"
l!l&&r!l&&!r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map" and $target->{"type"} ne "array" and $target->{"type"} ne "set" and $target->{"type"} ne "choice"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map" and $target->{"type"} ne "array" and $target->{"type"} ne "set"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map" and $target->{"type"} ne "array"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object"
!ll&&!rl&&r
000
defined $target->{"type"} and not ref $target->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/9
ref $target ne "HASH" or defined $target->{"type"} and not ref $target->{"type"} and $target->{"type"} ne "object" and $target->{"type"} ne "tuple" and $target->{"type"} ne "map" and $target->{"type"} ne "array" and $target->{"type"} ne "set" and $target->{"type"} ne "choice"
ref $target ne "HASH" defined $target->{"type"} not ref $target->{"type"} $target->{"type"} ne "object" $target->{"type"} ne "tuple" $target->{"type"} ne "map" $target->{"type"} ne "array" $target->{"type"} ne "set" $target->{"type"} ne "choice"
Truth table0%0/10
A or B and not(C) and D and E and F and G and H and I
A= ref $target ne "HASH"B= defined $target->{"type"}C= not ref $target->{"type"}D= $target->{"type"} ne "object"E= $target->{"type"} ne "tuple"F= $target->{"type"} ne "map"G= $target->{"type"} ne "array"H= $target->{"type"} ne "set"I= $target->{"type"} ne "choice"
ABCDEFGHIresult
1XXXXXXXX1
0111111111
00XXXXXXX0
010XXXXXX0
0110XXXXX0
01110XXXX0
011110XXX0
0111110XX0
01111110X0
0111111100
1424 ref($target) ne 'HASH'
1425 || ( defined $target->{type}
1426 && !ref( $target->{type} )
1427 && $target->{type} ne 'object'
1428 && $target->{type} ne 'tuple'
1429 && $target->{type} ne 'map'
1430 && $target->{type} ne 'array'
1431 && $target->{type} ne 'set'
1432 && $target->{type} ne 'choice' )
1433 )
1434 {
1435 0 $self->_add_error(
1436 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1437 "\$extends target '$extends' must not resolve to a primitive type",
1438 "$path/\$extends"
1439 );
1440 }
1441 ▶ elsif ( ref($target) eq 'HASH' && defined $target->{'$extends'} ) {
Condition0%0/3
ref $target eq "HASH" and defined $target->{"\$extends"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $target eq "HASH" and defined $target->{"\$extends"}
ref $target eq "HASH" defined $target->{"\$extends"}
Truth table0%0/3
A and B
A= ref $target eq "HASH"B= defined $target->{"\$extends"}
ABresult
0X0
100
111
1442 # Recursively validate the extended schema's $extends
1443 0 $self->_validate_extends( $target->{'$extends'}, "$path/\$extends" );
1444 }
1445  
1446 0 delete $self->{seen_extends}{$extends};
1447 }
1448  
1449 20 ▶sub _has_enabled_extension {
Subroutine _has_enabled_extension: called CC=1 SCAR=0.0
1450 20 my ( $self, $extension ) = @_;
1451 20 return !!$self->{enabled_extensions}{$extension};
1452 }
1453  
1454 52 ▶sub _validate_units_keywords {
Subroutine _validate_units_keywords: called CC=11 SCAR=29.7
1455 52 my ( $self, $schema, $type, $path ) = @_;
1456  
1457 52 for my $keyword (qw(unit currency symbols)) {
1458 156 ▶ next if !exists $schema->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{$keyword} 155 1 156
1459  
1460 1 ▶ if ( !$self->_has_enabled_extension('JSONStructureUnits') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_enabled_extension("JSONStructureUnits")) 0 1 1
1461 0 $self->_add_error(
1462 SCHEMA_EXTENSION_KEYWORD_NOT_ENABLED,
1463 "'$keyword' requires JSONStructureUnits extension.",
1464 "$path/$keyword"
1465 );
1466 }
1467 }
1468  
1469 52 ▶ return if !exists $schema->{unit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"unit"} 51 1 52
1470  
1471 1 ▶ if ( !defined $schema->{unit} || ref( $schema->{unit} ) || looks_like_number( $schema->{unit} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $schema->{"unit"} or ref $schema->{"unit"} or &looks_like_number($schema->{"unit"})) 0 1 1
Condition33%2/6
not defined $schema->{"unit"} or ref $schema->{"unit"} or &looks_like_number($schema->{"unit"})
l!l&&r!l&&!r
001
not defined $schema->{"unit"} or ref $schema->{"unit"}
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $schema->{"unit"} or ref $schema->{"unit"} or &looks_like_number($schema->{"unit"})
not defined $schema->{"unit"} ref $schema->{"unit"} &looks_like_number($schema->{"unit"})
Truth table25%1/4
not(A) or B or C
A= not defined $schema->{"unit"}B= ref $schema->{"unit"}C= &looks_like_number($schema->{"unit"})
ABCresult
1XX1
01X1
0011
0000
1472 0 $self->_add_error(
1473 SCHEMA_KEYWORD_INVALID_TYPE,
1474 "'unit' must be a string.",
1475 "$path/unit"
1476 );
1477 }
1478  
1479 1 my %numeric_types = map { $_ => 1 }
1480 qw(number integer float double decimal int32 uint32 int64 uint64 int128 uint128);
1481 1 ▶ if ( !defined $type || ref($type) || !$numeric_types{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or ref $type or not $numeric_types{$type}) 0 1 1
Condition33%2/6
not defined $type or ref $type or not $numeric_types{$type}
l!l&&r!l&&!r
001
not defined $type or ref $type
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $type or ref $type or not $numeric_types{$type}
not defined $type ref $type not $numeric_types{$type}
Truth table25%1/4
not(A) or B or not(C)
A= not defined $typeB= ref $typeC= not $numeric_types{$type}
ABCresult
1XX1
01X1
0011
0000
1482 0 $self->_add_error(
1483 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1484 "'unit' can only appear in numeric schemas.",
1485 "$path/unit"
1486 );
1487 }
1488 }
1489  
1490 52 ▶sub _validate_ucum_unit_keyword {
Subroutine _validate_ucum_unit_keyword: called CC=9 SCAR=22.7
1491 52 my ( $self, $schema, $type, $path ) = @_;
1492  
1493 52 ▶ return if !exists $schema->{ucumUnit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{"ucumUnit"} 42 10 52
1494  
1495 10 ▶ if ( !$self->_has_enabled_extension('JSONStructureUnits') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_enabled_extension("JSONStructureUnits")) 0 10 10
1496 0 $self->_add_error(
1497 SCHEMA_EXTENSION_KEYWORD_NOT_ENABLED,
1498 "'ucumUnit' requires JSONStructureUnits extension.",
1499 "$path/ucumUnit"
1500 );
1501 }
1502  
1503 10 ▶ if ( !defined $schema->{ucumUnit} || ref( $schema->{ucumUnit} ) || looks_like_number( $schema->{ucumUnit} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $schema->{"ucumUnit"} or ref $schema->{"ucumUnit"} or &looks_like_number($schema->{"ucumUnit"})) 3 7 10
Condition83%5/6
not defined $schema->{"ucumUnit"} or ref $schema->{"ucumUnit"} or &looks_like_number($schema->{"ucumUnit"})
l!l&&r!l&&!r
217
not defined $schema->{"ucumUnit"} or ref $schema->{"ucumUnit"}
l!l&&r!l&&!r
028
MC/DC66%2/3
not defined $schema->{"ucumUnit"} or ref $schema->{"ucumUnit"} or &looks_like_number($schema->{"ucumUnit"})
not defined $schema->{"ucumUnit"} ref $schema->{"ucumUnit"} &looks_like_number($schema->{"ucumUnit"})
Truth table75%3/4
not(A) or B or C
A= not defined $schema->{"ucumUnit"}B= ref $schema->{"ucumUnit"}C= &looks_like_number($schema->{"ucumUnit"})
ABCresult
1XX1
01X1
0011
0000
1504 3 $self->_add_error(
1505 SCHEMA_KEYWORD_INVALID_TYPE,
1506 "'ucumUnit' must be a string.",
1507 "$path/ucumUnit"
1508 );
1509 }
1510  
1511 10 my %numeric_types = map { $_ => 1 }
1512 qw(number integer float double decimal int32 uint32 int64 uint64 int128 uint128);
1513 10 ▶ if ( !defined $type || ref($type) || !$numeric_types{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or ref $type or not $numeric_types{$type}) 1 9 10
Condition50%3/6
not defined $type or ref $type or not $numeric_types{$type}
l!l&&r!l&&!r
019
not defined $type or ref $type
l!l&&r!l&&!r
0010
MC/DC33%1/3
not defined $type or ref $type or not $numeric_types{$type}
not defined $type ref $type not $numeric_types{$type}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $typeB= ref $typeC= not $numeric_types{$type}
ABCresult
1XX1
01X1
0011
0000
1514 1 $self->_add_error(
1515 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1516 "'ucumUnit' can only appear in numeric schemas.",
1517 "$path/ucumUnit"
1518 );
1519 }
1520 }
1521  
1522 52 ▶sub _validate_relations_keywords {
Subroutine _validate_relations_keywords: called CC=39 SCAR=45.4
1523 52 my ( $self, $schema, $type, $path ) = @_;
1524  
1525 52 my $has_identity = exists $schema->{identity};
1526 52 my $has_relations = exists $schema->{relations};
1527 52 ▶ return if !$has_identity && !$has_relations;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_identity or $has_relations 43 9 52
Condition100%3/3
$has_identity or $has_relations
l!l&&r!l&&!r
4543
MC/DC100%2/2
$has_identity or $has_relations
$has_identity $has_relations
Truth table100%3/3
A or B
A= $has_identityB= $has_relations
ABresult
1X1
011
000
1528  
1529 9 ▶ if ( !$self->_has_enabled_extension('JSONStructureRelations') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_enabled_extension("JSONStructureRelations")) 0 9 9
1530 0 ▶ if ($has_identity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_identity) 0 0 0
1531 0 $self->_add_error(
1532 SCHEMA_EXTENSION_KEYWORD_NOT_ENABLED,
1533 "'identity' requires JSONStructureRelations extension.",
1534 "$path/identity"
1535 );
1536 }
1537 0 ▶ if ($has_relations) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_relations) 0 0 0
1538 0 $self->_add_error(
1539 SCHEMA_EXTENSION_KEYWORD_NOT_ENABLED,
1540 "'relations' requires JSONStructureRelations extension.",
1541 "$path/relations"
1542 );
1543 }
1544 }
1545  
1546 9 ▶ my $supports_relations = defined $type && !ref($type) && ( $type eq 'object' || $type eq 'tuple' );
Condition55%5/9
defined $type && !ref($type) && ($type eq 'object' || $type eq 'tuple')
!ll&&!rl&&r
027
defined $type and not ref $type
!ll&&!rl&&r
009
$type eq "object" or $type eq "tuple"
l!l&&r!l&&!r
702
MC/DC25%1/4
defined $type && !ref($type) && ($type eq 'object' || $type eq 'tuple')
defined $type not ref $type $type eq "object" $type eq "tuple"
Truth table40%2/5
A and not(B) && C or D
A= defined $typeB= not ref $typeC= $type eq "object"D= $type eq "tuple"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1547  
1548 9 ▶ if ($has_identity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_identity) 4 5 9
1549 4 my $identity = $schema->{identity};
1550 4 ▶ if ( !$supports_relations ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($supports_relations) 1 3 4
1551 1 $self->_add_error(
1552 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1553 "'identity' can only appear in object or tuple schemas.",
1554 "$path/identity"
1555 );
1556 }
1557  
1558 4 ▶ if ( ref($identity) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $identity ne 'ARRAY') { } 1 3 4
1559 1 $self->_add_error(
1560 SCHEMA_KEYWORD_INVALID_TYPE,
1561 "'identity' must be an array of strings.",
1562 "$path/identity"
1563 );
1564 }
1565 else {
1566 3 ▶ my $properties = ref( $schema->{properties} ) eq 'HASH' ? $schema->{properties} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $schema->{'properties'} eq 'HASH' ? : 2 1 3
1567 3 for my $i ( 0 .. $#$identity ) {
1568 4 my $item = $identity->[$i];
1569 4 my $item_path = "$path/identity[$i]";
1570 4 ▶ if ( !defined $item || ref($item) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $item or ref $item) 0 4 4
Condition33%1/3
not defined $item or ref $item
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $item or ref $item
not defined $item ref $item
Truth table33%1/3
not(A) or B
A= not defined $itemB= ref $item
ABresult
1X1
011
000
1571 0 $self->_add_error(
1572 SCHEMA_KEYWORD_INVALID_TYPE,
1573 "'identity[$i]' must be a string.",
1574 $item_path
1575 );
1576 0 next;
1577 }
1578  
1579 4 ▶ if ( !exists $properties->{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $properties->{$item}) 2 2 4
1580 2 $self->_add_error(
1581 SCHEMA_REQUIRED_PROPERTY_NOT_DEFINED,
1582 "'identity' references property '$item' that is not in 'properties'.",
1583 $item_path
1584 );
1585 }
1586 }
1587 }
1588 }
1589  
1590 9 ▶ return if !$has_relations;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_relations 4 5 9
1591  
1592 5 ▶ if ( !$supports_relations ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($supports_relations) 1 4 5
1593 1 $self->_add_error(
1594 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1595 "'relations' can only appear in object or tuple schemas.",
1596 "$path/relations"
1597 );
1598 }
1599  
1600 5 my $relations = $schema->{relations};
1601 5 ▶ if ( ref($relations) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $relations ne "HASH") 0 5 5
1602 0 $self->_add_error(
1603 SCHEMA_KEYWORD_INVALID_TYPE,
1604 "'relations' must be an object.",
1605 "$path/relations"
1606 );
1607 0 return;
1608 }
1609  
1610 5 for my $relation_name ( keys %$relations ) {
1611 6 my $relation = $relations->{$relation_name};
1612 6 my $relation_path = "$path/relations/$relation_name";
1613  
1614 6 ▶ if ( ref($relation) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $relation ne "HASH") 0 6 6
1615 0 $self->_add_error(
1616 SCHEMA_KEYWORD_INVALID_TYPE,
1617 'Relation declaration must be an object.',
1618 $relation_path
1619 );
1620 0 next;
1621 }
1622  
1623 6 ▶ if ( !exists $relation->{targettype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $relation->{'targettype'}) { } 1 5 6
1624 1 $self->_add_error(
1625 SCHEMA_KEYWORD_INVALID_TYPE,
1626 "Relation declaration must have 'targettype'.",
1627 "$relation_path/targettype"
1628 );
1629 }
1630 else {
1631 5 my $targettype = $relation->{targettype};
1632 5 ▶ if ( ref($targettype) ne 'HASH' || !exists $targettype->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $targettype ne 'HASH' or not exists $targettype->{'$ref'}) { } 0 5 5
Condition33%1/3
ref $targettype ne "HASH" or not exists $targettype->{"\$ref"}
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $targettype ne "HASH" or not exists $targettype->{"\$ref"}
ref $targettype ne "HASH" not exists $targettype->{"\$ref"}
Truth table33%1/3
A or not(B)
A= ref $targettype ne "HASH"B= not exists $targettype->{"\$ref"}
ABresult
1X1
011
000
1633 0 $self->_add_error(
1634 SCHEMA_KEYWORD_INVALID_TYPE,
1635 "'targettype' must be an object with '\$ref'.",
1636 "$relation_path/targettype"
1637 );
1638 }
1639 else {
1640 5 $self->_validate_ref( $targettype->{'$ref'}, "$relation_path/targettype/\$ref" );
1641 }
1642 }
1643  
1644 6 ▶ if ( !exists $relation->{cardinality} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $relation->{'cardinality'}) { } 1 5 6
1645 1 $self->_add_error(
1646 SCHEMA_KEYWORD_INVALID_TYPE,
1647 "Relation declaration must have 'cardinality'.",
1648 "$relation_path/cardinality"
1649 );
1650 }
1651 else {
1652 5 my $cardinality = $relation->{cardinality};
1653 5 ▶ if ( !defined $cardinality || ref($cardinality) || ( $cardinality ne 'single' && $cardinality ne 'multiple' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cardinality or ref $cardinality or $cardinality ne "single" and $cardinality ne "multiple") 1 4 5
Condition66%6/9
not defined $cardinality or ref $cardinality or $cardinality ne "single" and $cardinality ne "multiple"
l!l&&r!l&&!r
014
not defined $cardinality or ref $cardinality
l!l&&r!l&&!r
005
$cardinality ne "single" and $cardinality ne "multiple"
!ll&&!rl&&r
311
MC/DC50%2/4
not defined $cardinality or ref $cardinality or $cardinality ne "single" and $cardinality ne "multiple"
not defined $cardinality ref $cardinality $cardinality ne "single" $cardinality ne "multiple"
Truth table60%3/5
not(A) or B or C and D
A= not defined $cardinalityB= ref $cardinalityC= $cardinality ne "single"D= $cardinality ne "multiple"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
1654 1 $self->_add_error(
1655 SCHEMA_KEYWORD_INVALID_TYPE,
1656 "'cardinality' must be 'single' or 'multiple'.",
1657 "$relation_path/cardinality"
1658 );
1659 }
1660 }
1661  
1662 6 ▶ if ( exists $relation->{scope} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $relation->{"scope"}) 1 5 6
1663 1 my $scope = $relation->{scope};
1664 1 ▶ if ( !ref($scope) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $scope) { } 1 0 1
elsif (ref $scope eq 'ARRAY') { } 0 0 0
1665 # Valid string scope
1666 }
1667 elsif ( ref($scope) eq 'ARRAY' ) {
1668 0 for my $i ( 0 .. $#$scope ) {
1669 0 ▶ if ( !defined $scope->[$i] || ref( $scope->[$i] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $scope->[$i] or ref $scope->[$i]) 0 0 0
Condition0%0/3
not defined $scope->[$i] or ref $scope->[$i]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $scope->[$i] or ref $scope->[$i]
not defined $scope->[$i] ref $scope->[$i]
Truth table0%0/3
not(A) or B
A= not defined $scope->[$i]B= ref $scope->[$i]
ABresult
1X1
011
000
1670 0 $self->_add_error(
1671 SCHEMA_KEYWORD_INVALID_TYPE,
1672 "'scope' array items must be strings.",
1673 "$relation_path/scope[$i]"
1674 );
1675 }
1676 }
1677 }
1678 else {
1679 0 $self->_add_error(
1680 SCHEMA_KEYWORD_INVALID_TYPE,
1681 "'scope' must be a string or an array of strings.",
1682 "$relation_path/scope"
1683 );
1684 }
1685 }
1686  
1687 6 ▶ if ( exists $relation->{qualifiertype} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $relation->{"qualifiertype"}) 1 5 6
1688 1 my $qualifiertype = $relation->{qualifiertype};
1689 1 ▶ if ( ref($qualifiertype) ne 'HASH' || !exists $qualifiertype->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $qualifiertype ne 'HASH' or not exists $qualifiertype->{'$ref'}) { } 0 1 1
Condition33%1/3
ref $qualifiertype ne "HASH" or not exists $qualifiertype->{"\$ref"}
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $qualifiertype ne "HASH" or not exists $qualifiertype->{"\$ref"}
ref $qualifiertype ne "HASH" not exists $qualifiertype->{"\$ref"}
Truth table33%1/3
A or not(B)
A= ref $qualifiertype ne "HASH"B= not exists $qualifiertype->{"\$ref"}
ABresult
1X1
011
000
1690 0 $self->_add_error(
1691 SCHEMA_KEYWORD_INVALID_TYPE,
1692 "'qualifiertype' must be an object with '\$ref'.",
1693 "$relation_path/qualifiertype"
1694 );
1695 }
1696 else {
1697 1 $self->_validate_ref( $qualifiertype->{'$ref'}, "$relation_path/qualifiertype/\$ref" );
1698 }
1699 }
1700 }
1701 }
1702  
1703 52 ▶sub _check_constraint_type_mismatch {
Subroutine _check_constraint_type_mismatch: called CC=7 SCAR=20.6
1704 52 my ( $self, $schema, $type, $path ) = @_;
1705  
1706 # Numeric constraints can only be on numeric types
1707 52 my @numeric_types =
1708 qw(int8 int16 int32 int64 uint8 uint16 uint32 uint64 float16 float32 float64 decimal integer double number float);
1709 52 my $is_numeric = grep { $_ eq $type } @numeric_types;
1710  
1711 52 for my $keyword (
1712 qw(minimum maximum exclusiveMinimum exclusiveMaximum multipleOf))
1713 {
1714 260 ▶ if ( exists $schema->{$keyword} && !$is_numeric ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{$keyword} and not $is_numeric) 0 260 260
Condition66%2/3
exists $schema->{$keyword} and not $is_numeric
!ll&&!rl&&r
25820
MC/DC0%0/2
exists $schema->{$keyword} and not $is_numeric
exists $schema->{$keyword} not $is_numeric
Truth table66%2/3
A and not(B)
A= exists $schema->{$keyword}B= not $is_numeric
ABresult
0X0
100
111
1715 0 $self->_add_error(
1716 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1717 "Constraint '$keyword' is only valid for numeric types, not '$type'",
1718 "$path/$keyword"
1719 );
1720 }
1721 }
1722  
1723 # String constraints can only be on string types
1724 52 my @string_types =
1725 qw(string date time datetime duration uri base64 binary uuid jsonpointer name);
1726 52 my $is_string = grep { $_ eq $type } @string_types;
1727  
1728 52 for my $keyword (
1729 qw(minLength maxLength pattern format contentEncoding contentMediaType))
1730 {
1731 312 ▶ if ( exists $schema->{$keyword} && !$is_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{$keyword} and not $is_string) 0 312 312
Condition66%2/3
exists $schema->{$keyword} and not $is_string
!ll&&!rl&&r
31020
MC/DC0%0/2
exists $schema->{$keyword} and not $is_string
exists $schema->{$keyword} not $is_string
Truth table66%2/3
A and not(B)
A= exists $schema->{$keyword}B= not $is_string
ABresult
0X0
100
111
1732 0 $self->_add_error(
1733 SCHEMA_CONSTRAINT_TYPE_MISMATCH,
1734 "Constraint '$keyword' is only valid for string types, not '$type'",
1735 "$path/$keyword"
1736 );
1737 }
1738 }
1739 }
1740  
1741 52 ▶sub _validate_extended_keywords {
Subroutine _validate_extended_keywords: called CC=37 SCAR=58.1
1742 52 my ( $self, $schema, $type, $path ) = @_;
1743  
1744 52 ▶ $type //= '';
Condition50%1/2
$type //= ""
l!l
520
MC/DC0%0/1
$type //= ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
1745  
1746 52 $self->_validate_units_keywords( $schema, $type, $path );
1747 52 $self->_validate_ucum_unit_keyword( $schema, $type, $path );
1748 52 $self->_validate_relations_keywords( $schema, $type, $path );
1749  
1750 # Check constraint-type mismatches
1751 52 $self->_check_constraint_type_mismatch( $schema, $type, $path );
1752  
1753 # Check numeric constraints
1754 52 for my $keyword ( keys %NUMERIC_VALIDATION_KEYWORDS ) {
1755 260 ▶ if ( exists $schema->{$keyword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{$keyword}) 2 258 260
1756 2 my $value = $schema->{$keyword};
1757 2 ▶ if ( !defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value or not $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u) 0 2 2
Condition33%2/6
not defined $value or ref $value or not $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
l!l&&r!l&&!r
002
not defined $value or ref $value
l!l&&r!l&&!r
002
MC/DC0%0/3
not defined $value or ref $value or not $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
not defined $value ref $value not $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
ABCresult
1XX1
01X1
0011
0000
1758 || ref($value)
1759 || $value !~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/ )
1760 {
1761 0 $self->_add_error(
1762 SCHEMA_NUMBER_CONSTRAINT_INVALID,
1763 "$keyword must be a number",
1764 "$path/$keyword"
1765 );
1766 }
1767 }
1768 }
1769  
1770 # Check min/max relationships
1771 52 ▶ if ( exists $schema->{minimum} && exists $schema->{maximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minimum"} and exists $schema->{"maximum"}) 1 51 52
Condition66%2/3
exists $schema->{"minimum"} and exists $schema->{"maximum"}
!ll&&!rl&&r
5101
MC/DC50%1/2
exists $schema->{"minimum"} and exists $schema->{"maximum"}
exists $schema->{"minimum"} exists $schema->{"maximum"}
Truth table66%2/3
A and B
A= exists $schema->{"minimum"}B= exists $schema->{"maximum"}
ABresult
0X0
100
111
1772 1 ▶ if ( $schema->{minimum} > $schema->{maximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"minimum"} > $schema->{"maximum"}) 1 0 1
1773 1 $self->_add_error( SCHEMA_MIN_GREATER_THAN_MAX,
1774 "'minimum' cannot be greater than 'maximum'", $path );
1775 }
1776 }
1777  
1778 52 ▶ if ( exists $schema->{minLength} && exists $schema->{maxLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minLength"} and exists $schema->{"maxLength"}) 0 52 52
Condition33%1/3
exists $schema->{"minLength"} and exists $schema->{"maxLength"}
!ll&&!rl&&r
5200
MC/DC0%0/2
exists $schema->{"minLength"} and exists $schema->{"maxLength"}
exists $schema->{"minLength"} exists $schema->{"maxLength"}
Truth table33%1/3
A and B
A= exists $schema->{"minLength"}B= exists $schema->{"maxLength"}
ABresult
0X0
100
111
1779 0 ▶ if ( $schema->{minLength} > $schema->{maxLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"minLength"} > $schema->{"maxLength"}) 0 0 0
1780 0 $self->_add_error( SCHEMA_MIN_GREATER_THAN_MAX,
1781 "'minLength' cannot be greater than 'maxLength'", $path );
1782 }
1783 }
1784  
1785 52 ▶ if ( exists $schema->{minItems} && exists $schema->{maxItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minItems"} and exists $schema->{"maxItems"}) 0 52 52
Condition33%1/3
exists $schema->{"minItems"} and exists $schema->{"maxItems"}
!ll&&!rl&&r
5200
MC/DC0%0/2
exists $schema->{"minItems"} and exists $schema->{"maxItems"}
exists $schema->{"minItems"} exists $schema->{"maxItems"}
Truth table33%1/3
A and B
A= exists $schema->{"minItems"}B= exists $schema->{"maxItems"}
ABresult
0X0
100
111
1786 0 ▶ if ( $schema->{minItems} > $schema->{maxItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"minItems"} > $schema->{"maxItems"}) 0 0 0
1787 0 $self->_add_error( SCHEMA_MIN_GREATER_THAN_MAX,
1788 "'minItems' cannot be greater than 'maxItems'", $path );
1789 }
1790 }
1791  
1792 # Check for negative values where not allowed
1793 52 ▶ if ( exists $schema->{minItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minItems"}) 0 52 52
1794 0 my $value = $schema->{minItems};
1795 0 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0) 0 0 0
Condition0%0/9
defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0
!ll&&!rl&&r
000
defined $value and not ref $value and $value =~ /^-?\d+$/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0
defined $value not ref $value $value =~ /^-?\d+$/u $value < 0
Truth table0%0/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= $value =~ /^-?\d+$/uD= $value < 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1796 && !ref($value)
1797 && $value =~ /^-?\d+$/
1798 && $value < 0 )
1799 {
1800 0 $self->_add_error( SCHEMA_MIN_ITEMS_NEGATIVE,
1801 'minItems cannot be negative',
1802 "$path/minItems" );
1803 }
1804 }
1805  
1806 52 ▶ if ( exists $schema->{minLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minLength"}) 0 52 52
1807 0 my $value = $schema->{minLength};
1808 0 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0) 0 0 0
Condition0%0/9
defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0
!ll&&!rl&&r
000
defined $value and not ref $value and $value =~ /^-?\d+$/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $value and not ref $value and $value =~ /^-?\d+$/u and $value < 0
defined $value not ref $value $value =~ /^-?\d+$/u $value < 0
Truth table0%0/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= $value =~ /^-?\d+$/uD= $value < 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1809 && !ref($value)
1810 && $value =~ /^-?\d+$/
1811 && $value < 0 )
1812 {
1813 0 $self->_add_error( SCHEMA_MIN_LENGTH_NEGATIVE,
1814 'minLength cannot be negative',
1815 "$path/minLength" );
1816 }
1817 }
1818  
1819 # Check multipleOf must be positive
1820 52 ▶ if ( exists $schema->{multipleOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"multipleOf"}) 0 52 52
1821 0 my $value = $schema->{multipleOf};
1822 0 ▶ if ( defined $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u) 0 0 0
Condition0%0/6
defined $value and not ref $value and $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value and not ref $value and $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
defined $value not ref $value $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
Truth table0%0/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/u
ABCresult
0XX0
10X0
1100
1111
1823 && !ref($value)
1824 && $value =~ /^-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?$/ )
1825 {
1826 0 ▶ if ( $value <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= 0) 0 0 0
1827 0 $self->_add_error(
1828 SCHEMA_MULTIPLE_OF_NOT_POSITIVE,
1829 'multipleOf must be greater than 0',
1830 "$path/multipleOf"
1831 );
1832 }
1833 }
1834 }
1835  
1836 # Check pattern
1837 52 ▶ if ( exists $schema->{pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"pattern"}) 2 50 52
1838 2 my $pattern = $schema->{pattern};
1839 2 ▶ if ( !defined $pattern || ref($pattern) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pattern or ref $pattern) { } 0 2 2
Condition33%1/3
not defined $pattern or ref $pattern
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $pattern or ref $pattern
not defined $pattern ref $pattern
Truth table33%1/3
not(A) or B
A= not defined $patternB= ref $pattern
ABresult
1X1
011
000
1840 0 $self->_add_error( SCHEMA_PATTERN_NOT_STRING,
1841 'pattern must be a string',
1842 "$path/pattern" );
1843 }
1844 else {
1845 2 my $pattern_ok = eval { qr/$pattern/; 1 };
1846 2 ▶ if ( !$pattern_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern_ok) 1 1 2
1847 1 $self->_add_error( SCHEMA_PATTERN_INVALID,
1848 "pattern is not a valid regular expression: '$pattern'",
1849 "$path/pattern" );
1850 }
1851 }
1852 }
1853  
1854 # Check uniqueItems
1855 52 ▶ if ( exists $schema->{uniqueItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"uniqueItems"}) 0 52 52
1856 0 my $value = $schema->{uniqueItems};
1857 0 ▶ if ( !_is_boolean($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_boolean($value)) 0 0 0
1858 0 $self->_add_error(
1859 SCHEMA_UNIQUE_ITEMS_NOT_BOOLEAN,
1860 'uniqueItems must be a boolean',
1861 "$path/uniqueItems"
1862 );
1863 }
1864 }
1865 }
1866  
1867 # List of known JSON boolean classes from various JSON implementations
1868 17 my @JSON_BOOL_CLASSES = qw(
1869 JSON::PP::Boolean
1870 JSON::XS::Boolean
1871 Cpanel::JSON::XS::Boolean
1872 JSON::Tiny::_Bool
1873 Mojo::JSON::_Bool
1874 Types::Serialiser::Boolean
1875 );
1876  
1877 # Helper to check if a value is a JSON boolean from any JSON parser
1878 6 ▶sub _is_json_bool {
Subroutine _is_json_bool: called CC=6 SCAR=30.1
1879 6 my ($value) = @_;
1880 6 ▶ return 0 unless defined $value && blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and builtin::blessed($value) 6 0 6
Condition33%1/3
defined $value and builtin::blessed($value)
!ll&&!rl&&r
060
MC/DC0%0/2
defined $value and builtin::blessed($value)
defined $value builtin::blessed($value)
Truth table33%1/3
A and B
A= defined $valueB= builtin::blessed($value)
ABresult
0X0
100
111
1881 0 for my $class (@JSON_BOOL_CLASSES) {
1882 0 ▶ return 1 if $value->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa($class) 0 0 0
1883 }
1884 0 ▶ return 1 if JSON::MaybeXS::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 0 0 0
1885 0 return 0;
1886 }
1887  
1888 0 ▶sub _is_boolean {
Subroutine _is_boolean: not called CC=6 SCAR=37.4
1889 0 my ($value) = @_;
1890  
1891 # JSON booleans are blessed references
1892 0 ▶ return 1 if _is_json_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_json_bool($value) 0 0 0
1893 0 ▶ return 0 if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
1894 0 ▶ return 1 if $value =~ /^[01]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^[01]$/u 0 0 0
1895 0 ▶ return 1 if $value eq 'true' || $value eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "true" or $value eq "false" 0 0 0
Condition0%0/3
$value eq "true" or $value eq "false"
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "true" or $value eq "false"
$value eq "true" $value eq "false"
Truth table0%0/3
A or B
A= $value eq "true"B= $value eq "false"
ABresult
1X1
011
000
1896 0 return 0;
1897 }
1898  
1899 0 ▶sub _validate_altnames {
Subroutine _validate_altnames: not called CC=5 SCAR=34.0
1900 0 my ( $self, $altnames, $path ) = @_;
1901  
1902 0 ▶ if ( ref($altnames) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $altnames ne "HASH") 0 0 0
1903 0 $self->_add_error( SCHEMA_ALTNAMES_NOT_OBJECT,
1904 'altnames must be an object', $path );
1905 0 return;
1906 }
1907  
1908 0 for my $key ( keys %$altnames ) {
1909 0 my $value = $altnames->{$key};
1910 0 ▶ if ( !defined $value || ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 0 0 0
Condition0%0/3
not defined $value or ref $value
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table0%0/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
1911 0 $self->_add_error(
1912 SCHEMA_ALTNAMES_VALUE_NOT_STRING,
1913 'altnames values must be strings',
1914 "$path/$key"
1915 );
1916 }
1917 }
1918 }
1919  
1920 0 ▶sub _process_imports {
Subroutine _process_imports: not called CC=11 SCAR=48.8
1921 0 my ( $self, $obj, $path ) = @_;
1922  
1923 # Process $import and $importdefs recursively
1924 0 ▶ return unless ref($obj) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "HASH" 0 0 0
1925  
1926 0 for my $key ( keys %$obj ) {
1927 0 ▶ if ( $key eq '$import' || $key eq '$importdefs' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq '$import' or $key eq '$importdefs') { } 0 0 0
elsif (ref $obj->{$key} eq 'HASH') { } 0 0 0
elsif (ref $obj->{$key} eq 'ARRAY') { } 0 0 0
Condition0%0/3
$key eq "\$import" or $key eq "\$importdefs"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "\$import" or $key eq "\$importdefs"
$key eq "\$import" $key eq "\$importdefs"
Truth table0%0/3
A or B
A= $key eq "\$import"B= $key eq "\$importdefs"
ABresult
1X1
011
000
1928  
1929 # Handle import (implementation would fetch external schemas)
1930 # For now, just validate the URI is a string
1931 0 my $uri = $obj->{$key};
1932 0 ▶ if ( !defined $uri || ref($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $uri or ref $uri) 0 0 0
Condition0%0/3
not defined $uri or ref $uri
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $uri or ref $uri
not defined $uri ref $uri
Truth table0%0/3
not(A) or B
A= not defined $uriB= ref $uri
ABresult
1X1
011
000
1933 0 $self->_add_error( SCHEMA_KEYWORD_INVALID_TYPE,
1934 "$key must be a string", "$path/$key" );
1935 }
1936 }
1937 elsif ( ref( $obj->{$key} ) eq 'HASH' ) {
1938 0 $self->_process_imports( $obj->{$key}, "$path/$key" );
1939 }
1940 elsif ( ref( $obj->{$key} ) eq 'ARRAY' ) {
1941 0 for my $i ( 0 .. $#{ $obj->{$key} } ) {
1942 0 ▶ if ( ref( $obj->{$key}[$i] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj->{$key}[$i] eq "HASH") 0 0 0
1943 0 $self->_process_imports( $obj->{$key}[$i],
1944 "$path/$key\[$i]" );
1945 }
1946 }
1947 }
1948 }
1949 }
1950  
1951 17 1;
1952  
1953 __END__
« lib/JSON/Structure/InstanceValidator.pm ↑ Summary lib/JSON/Structure/JsonSourceLocator.pm »