lib/JSON/Structure/InstanceValidator.pm

filter: Statementstmt 76.3% 484 / 634 Branchbran 65.6% 310 / 472 Conditioncond 43.5% 118 / 271 MC/DCmcdc 27.3% 41 / 150 Subroutinesub 93.8% 46 / 49 Podpod 50.0% 1 / 2 totaltotal 63.3% 1000 / 1578 CC 352 SCAR 46.9
CC
352
Cov
66%
_check_type
992.0
_validate_format
306.0
_values_equal
73.5
CRAP 108.5
↑ Summary lib/JSON/Structure/SchemaValidator.pm »
1 package JSON::Structure::InstanceValidator;
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 B;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Scalar::Util qw(looks_like_number blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 ▶use JSON::Structure::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use JSON::Structure::ErrorCodes qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 17 ▶use JSON::Structure::JsonSourceLocator;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # List of known JSON boolean classes from various JSON implementations
20 17 my @JSON_BOOL_CLASSES = qw(
21 JSON::PP::Boolean
22 JSON::XS::Boolean
23 Cpanel::JSON::XS::Boolean
24 JSON::Tiny::_Bool
25 Mojo::JSON::_Bool
26 Types::Serialiser::Boolean
27 );
28  
29 # Helper to check if a value is a JSON boolean from any JSON parser.
30 # Uses blessed() and isa() to support multiple JSON implementations:
31 # JSON::PP, JSON::XS, Cpanel::JSON::XS, etc.
32 1085 ▶sub _is_json_bool {
Subroutine _is_json_bool: called CC=6 SCAR=21.2
33 1085 my ($value) = @_;
34 1085 ▶ return 0 unless defined $value && blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and builtin::blessed($value) 1056 29 1085
Condition66%2/3
defined $value and builtin::blessed($value)
!ll&&!rl&&r
0105629
MC/DC50%1/2
defined $value and builtin::blessed($value)
defined $value builtin::blessed($value)
Truth table66%2/3
A and B
A= defined $valueB= builtin::blessed($value)
ABresult
0X0
100
111
35 29 for my $class (@JSON_BOOL_CLASSES) {
36 29 ▶ return 1 if $value->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $value->isa($class) 29 0 29
37 }
38  
39 # Also check for is_bool if available (JSON::MaybeXS compatibility)
40 0 ▶ return 1 if JSON::MaybeXS::is_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($value) 0 0 0
41 0 return 0;
42 }
43  
44 # Helper to check if a scalar was a number in the original JSON.
45 # Uses B module to inspect internal flags set by JSON parsers during parsing.
46 # Note: This approach relies on Perl's internal SV flags which are set when
47 # JSON parsers parse numeric literals. The flags IOK (integer OK) and NOK
48 # (numeric OK) indicate the value originated as a JSON number.
49 # Limitation: May behave differently with dualvars or tied scalars.
50 247 ▶sub _is_numeric {
Subroutine _is_numeric: called CC=5 SCAR=17.0
51 247 my ($value) = @_;
52 247 ▶ return 0 unless defined $value && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 0 247 247
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
00247
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
53  
54 # Exclude booleans first
55 247 ▶ return 0 if _is_json_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_json_bool($value) 0 247 247
56 247 my $b_obj = B::svref_2object( \$value );
57 247 my $flags = $b_obj->FLAGS;
58  
59 # Check if the value has numeric flags set (IOK/NOK)
60 247 ▶ return ( $flags & ( B::SVf_IOK | B::SVf_NOK ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 768 ? : 219 28 247
61 }
62  
63 # Helper to check if a value is a pure string (no numeric flags).
64 # A "pure string" is a non-reference scalar that:
65 # 1. Is not a JSON boolean
66 # 2. Has POK (string OK) flag set
67 # 3. Does NOT have IOK/NOK (numeric) flags set
68 # This distinguishes JSON string values like "123" from numeric 123.
69 # Note: Numeric-looking strings (e.g., "42") are treated as strings per
70 # JSON Structure semantics - the JSON encoding determines the type.
71 97 ▶sub _is_pure_string {
Subroutine _is_pure_string: called CC=5 SCAR=17.5
72 97 my ($value) = @_;
73 97 ▶ return 0 unless defined $value && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 0 97 97
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0097
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
74  
75 # If it's a boolean, it's not a pure string
76 97 ▶ return 0 if _is_json_bool($value);
Branch: 1 branch
BranchTrueFalseTotal
if _is_json_bool($value) 0 97 97
77 97 my $b_obj = B::svref_2object( \$value );
78 97 my $flags = $b_obj->FLAGS;
79  
80 # Check if POK (string) is set but not IOK/NOK (numeric)
81 97 ▶ return ( $flags & B::SVf_POK ) && !( $flags & ( B::SVf_IOK | B::SVf_NOK ) );
Condition66%2/3
$flags & 1024 && !($flags & 768)
!ll&&!rl&&r
11086
MC/DC50%1/2
$flags & 1024 && !($flags & 768)
$flags & 1024 !($flags & 768)
Truth table66%2/3
A && not(B)
A= $flags & 1024B= !($flags & 768)
ABresult
0X0
100
111
82 }
83  
84 =head1 NAME
85  
86 JSON::Structure::InstanceValidator - Validate JSON instances against JSON Structure schemas
87  
88 =head1 SYNOPSIS
89  
90 use JSON::Structure::InstanceValidator;
91 use JSON::PP;
92
93 my $schema = decode_json($schema_json);
94 my $validator = JSON::Structure::InstanceValidator->new(schema => $schema);
95
96 my $instance = decode_json($instance_json);
97 my $result = $validator->validate($instance, $instance_json);
98
99 if ($result->is_valid) {
100 say "Instance is valid!";
101 } else {
102 for my $error (@{$result->errors}) {
103 say $error->to_string;
104 }
105 }
106  
107 =head1 DESCRIPTION
108  
109 Validates JSON data instances against JSON Structure schemas.
110  
111 =cut
112  
113 # Integer type ranges
114 17 my %INT_RANGES = (
115 int8 => { min => -128, max => 127 },
116 uint8 => { min => 0, max => 255 },
117 int16 => { min => -32768, max => 32767 },
118 uint16 => { min => 0, max => 65535 },
119 int32 => { min => -2147483648, max => 2147483647 },
120 uint32 => { min => 0, max => 4294967295 },
121 int64 => { min => -9223372036854775808, max => 9223372036854775807 },
122 uint64 => { min => 0, max => 18446744073709551615 },
123 integer => { min => -2147483648, max => 2147483647 }, # Alias for int32
124 );
125  
126 # Regex patterns for format validation
127 17 my $DATE_REGEX = qr/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/;
128 17 my $TIME_REGEX =
129 qr/^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)?$/i;
130 17 my $DATETIME_REGEX =
131 qr/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)?$/i;
132 17 my $DURATION_REGEX =
133 qr/^P(?:(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+(?:\.\d+)?S)?)?$/;
134 17 my $UUID_REGEX =
135 qr/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
136 17 my $URI_REGEX =
137 qr/^[a-zA-Z][a-zA-Z0-9+\-.]*:/; # Any valid scheme (not just :// based)
138 17 my $JSONPOINTER_REGEX = qr/^(?:\/(?:[^~\/]|~[01])*)*$/;
139 17 my $EMAIL_REGEX = qr/^[^\s@]+@[^\s@]+\.[^\s@]+$/;
140 17 my $IPV4_REGEX =
141 qr/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
142 17 my $HOSTNAME_REGEX =
143 qr/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
144  
145 115 ▶sub new {
Subroutine new: called CC=4 SCAR=14.3
Pod undocumented
146 115 my ( $class, %args ) = @_;
147  
148 my $self = bless {
149 schema => $args{schema},
150 schema_text => $args{schema_text},
151 extended => $args{extended} // 0,
152 allow_import => $args{allow_import} // 0,
153 115 ▶ max_validation_depth => $args{max_validation_depth} // 64,
Condition66%4/6
$args{"extended"} // 0
l!l
6748
$args{"allow_import"} // 0
l!l
0115
$args{"max_validation_depth"} // 64
l!l
0115
MC/DC100%1/1
$args{"extended"} // 0
$args{"extended"}
MC/DC0%0/1
$args{"allow_import"} // 0
$args{"allow_import"}
MC/DC0%0/1
$args{"max_validation_depth"} // 64
$args{"max_validation_depth"}
Truth table100%2/2
A
A= $args{"extended"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"allow_import"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"max_validation_depth"}
Aresult
11
00
154 errors => [],
155 warnings => [],
156 source_locator => undef,
157 current_depth => 0,
158 }, $class;
159  
160 115 return $self;
161 }
162  
163 =head2 validate($instance, $source_text)
164  
165 Validates a JSON instance against the schema.
166  
167 Returns a ValidationResult object with errors and warnings.
168  
169 =cut
170  
171 481 ▶sub validate {
Subroutine validate: called CC=6 SCAR=18.3
Pod documented
172 481 my ( $self, $instance, $source_text ) = @_;
173  
174 # Reset state
175 481 $self->{errors} = [];
176 481 $self->{warnings} = [];
177 481 $self->{current_depth} = 0;
178  
179 # Initialize source locator
180 481 ▶ if ( defined $source_text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source_text) { } 1 480 481
181 $self->{source_locator} =
182 1 JSON::Structure::JsonSourceLocator->new($source_text);
183 }
184 else {
185 480 $self->{source_locator} = undef;
186 }
187  
188 481 my $schema = $self->{schema};
189  
190 # Handle null schema
191 481 ▶ if ( !defined $schema ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $schema) 0 481 481
192 0 $self->_add_error( INSTANCE_SCHEMA_FALSE, 'Schema is null', '#' );
193 0 return $self->_make_result();
194 }
195  
196 # Find the root schema to validate against
197 481 my $root_schema = $schema;
198  
199 # Check for $root reference
200 481 ▶ if ( ref($schema) eq 'HASH' && exists $schema->{'$root'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and exists $schema->{"\$root"}) 3 478 481
Condition100%3/3
ref $schema eq "HASH" and exists $schema->{"\$root"}
!ll&&!rl&&r
104683
MC/DC100%2/2
ref $schema eq "HASH" and exists $schema->{"\$root"}
ref $schema eq "HASH" exists $schema->{"\$root"}
Truth table100%3/3
A and B
A= ref $schema eq "HASH"B= exists $schema->{"\$root"}
ABresult
0X0
100
111
201 3 my $resolved = $self->_resolve_ref( $schema->{'$root'}, $schema );
202 3 ▶ if ( !defined $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resolved) 0 3 3
203 0 $self->_add_error( INSTANCE_ROOT_UNRESOLVED,
204 "Unable to resolve \$root reference: $schema->{'$root'}", '#' );
205 0 return $self->_make_result();
206 }
207 3 $root_schema = $resolved;
208 }
209  
210 # Validate the instance
211 481 $self->_validate_value( $instance, $root_schema, '#', '#' );
212  
213 481 return $self->_make_result();
214 }
215  
216 481 ▶sub _make_result {
Subroutine _make_result: called CC=1 SCAR=0.0
217 481 my ($self) = @_;
218  
219 return JSON::Structure::Types::ValidationResult->new(
220 481 is_valid => scalar( @{ $self->{errors} } ) == 0,
221 errors => $self->{errors},
222 warnings => $self->{warnings},
223 481 );
224 }
225  
226 226 ▶sub _add_error {
Subroutine _add_error: called CC=2 SCAR=6.9
227 226 my ( $self, $code, $message, $path, $schema_path ) = @_;
228  
229 my $location =
230 $self->{source_locator}
231 226 ▶ ? $self->{source_locator}->get_location($path)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'source_locator'} ? : 1 225 226
232 : JSON::Structure::Types::JsonLocation->unknown();
233  
234 226 push @{ $self->{errors} },
235 JSON::Structure::Types::ValidationError->new(
236 code => $code,
237 message => $message,
238 path => $path,
239 severity => JSON::Structure::Types::ValidationSeverity::ERROR,
240 location => $location,
241 schema_path => $schema_path,
242 );
243 }
244  
245 688 ▶sub _validate_value {
Subroutine _validate_value: called CC=25 SCAR=35.8
246 688 my ( $self, $value, $schema, $path, $schema_path ) = @_;
247  
248 # Check depth
249 688 $self->{current_depth}++;
250 688 ▶ if ( $self->{current_depth} > $self->{max_validation_depth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_depth"} > $self->{"max_validation_depth"}) 0 688 688
251 0 $self->_add_error(
252 INSTANCE_MAX_DEPTH_EXCEEDED,
253 "Maximum validation depth ($self->{max_validation_depth}) exceeded",
254 $path,
255 $schema_path
256 );
257 0 $self->{current_depth}--;
258 0 return;
259 }
260  
261 # Handle boolean schemas (both raw scalars and JSON boolean objects)
262 688 ▶ if ( !ref($schema) || _is_json_bool($schema) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $schema or _is_json_bool($schema)) 10 678 688
Condition66%2/3
not ref $schema or _is_json_bool($schema)
l!l&&r!l&&!r
010678
MC/DC50%1/2
not ref $schema or _is_json_bool($schema)
not ref $schema _is_json_bool($schema)
Truth table66%2/3
not(A) or B
A= not ref $schemaB= _is_json_bool($schema)
ABresult
1X1
011
000
263 10 ▶ if ( _is_false($schema) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_false($schema)) 5 5 10
264 5 $self->_add_error( INSTANCE_SCHEMA_FALSE,
265 "Schema 'false' rejects all values",
266 $path, $schema_path );
267 }
268  
269 # true schema accepts everything
270 10 $self->{current_depth}--;
271 10 return;
272 }
273  
274 678 ▶ if ( ref($schema) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema ne "HASH") 0 678 678
275 0 $self->{current_depth}--;
276 0 return;
277 }
278  
279 # Handle type with $ref
280 678 ▶ if ( exists $schema->{type}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"type"} and ref $schema->{"type"} eq "HASH" and exists $schema->{"type"}{"\$ref"}) 5 673 678
Condition83%5/6
exists $schema->{"type"} and ref $schema->{"type"} eq "HASH" and exists $schema->{"type"}{"\$ref"}
!ll&&!rl&&r
67305
exists $schema->{"type"} and ref $schema->{"type"} eq "HASH"
!ll&&!rl&&r
286455
MC/DC66%2/3
exists $schema->{"type"} and ref $schema->{"type"} eq "HASH" and exists $schema->{"type"}{"\$ref"}
exists $schema->{"type"} ref $schema->{"type"} eq "HASH" exists $schema->{"type"}{"\$ref"}
Truth table75%3/4
A and B and C
A= exists $schema->{"type"}B= ref $schema->{"type"} eq "HASH"C= exists $schema->{"type"}{"\$ref"}
ABCresult
0XX0
10X0
1100
1111
281 && ref( $schema->{type} ) eq 'HASH'
282 && exists $schema->{type}{'$ref'} )
283 {
284 my $resolved =
285 5 $self->_resolve_ref( $schema->{type}{'$ref'}, $self->{schema} );
286 5 ▶ if ( !defined $resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $resolved) 0 5 5
287 0 $self->_add_error( INSTANCE_REF_UNRESOLVED,
288 "Unable to resolve reference: $schema->{type}{'$ref'}",
289 $path, $schema_path );
290 0 $self->{current_depth}--;
291 0 return;
292 }
293  
294 # Merge the resolved schema with any additional constraints
295 5 my $merged = {%$resolved};
296 5 for my $key ( keys %$schema ) {
297 20 ▶ next if $key eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "type" 5 15 20
298 15 $merged->{$key} = $schema->{$key};
299 }
300 5 $self->_validate_value( $value, $merged, $path, $schema_path );
301 5 $self->{current_depth}--;
302 5 return;
303 }
304  
305 # Validate const
306 673 ▶ if ( exists $schema->{const} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"const"}) 7 666 673
307 7 ▶ if ( !$self->_values_equal( $value, $schema->{const} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_values_equal($value, $schema->{"const"})) 3 4 7
308 3 $self->_add_error( INSTANCE_CONST_MISMATCH,
309 'Value must equal const value',
310 $path, $schema_path );
311 }
312 7 $self->{current_depth}--;
313 7 return;
314 }
315  
316 # Validate enum
317 666 ▶ if ( exists $schema->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"enum"}) 11 655 666
318 11 my $found = 0;
319 11 for my $enum_val ( @{ $schema->{enum} } ) {
320 24 ▶ if ( $self->_values_equal( $value, $enum_val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_values_equal($value, $enum_val)) 7 17 24
321 7 $found = 1;
322 7 last;
323 }
324 }
325 11 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 4 7 11
326 4 $self->_add_error( INSTANCE_ENUM_MISMATCH,
327 'Value must be one of the enum values',
328 $path, $schema_path );
329 }
330 }
331  
332 # Validate type
333 666 my $type = $schema->{type};
334  
335 666 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 643 23 666
336 643 ▶ if ( ref($type) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $type eq 'ARRAY') { } 0 643 643
elsif (not ref $type) { } 643 0 643
337  
338 # Union type - value must match at least one
339 0 my $matched = 0;
340 0 for my $t (@$type) {
341 0 ▶ if ( $self->_check_type( $value, $t ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_type($value, $t)) 0 0 0
342 0 $matched = 1;
343 0 last;
344 }
345 }
346 0 ▶ if ( !$matched ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 0 0 0
347 0 $self->_add_error( INSTANCE_TYPE_MISMATCH,
348 "Value must be one of: " . join( ', ', @$type ),
349 $path, $schema_path );
350 }
351 }
352 elsif ( !ref($type) ) {
353 643 $self->_validate_type( $value, $type, $schema, $path,
354 $schema_path );
355 }
356 }
357  
358 # Validate composition keywords in extended mode
359 666 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 392 274 666
360 392 $self->_validate_composition( $value, $schema, $path, $schema_path );
361 }
362  
363 666 $self->{current_depth}--;
364 }
365  
366 643 ▶sub _validate_type {
Subroutine _validate_type: called CC=28 SCAR=33.5
367 643 my ( $self, $value, $type, $schema, $path, $schema_path ) = @_;
368  
369 # Validate based on type
370 643 ▶ if ( $type eq 'null' ) {
Branch: 21 branches
BranchTrueFalseTotal
if ($type eq 'null') { } 7 636 643
elsif ($type eq 'boolean') { } 12 624 636
elsif ($type eq 'string') { } 104 520 624
elsif ($type eq 'number' or $type eq 'float' or $type eq 'double' or $type eq 'float8' or $type eq 'decimal') { } 91 429 520
elsif ($type eq 'integer' or exists $INT_RANGES{$type}) { } 178 251 429
elsif ($type eq 'int128' or $type eq 'uint128') { } 0 251 251
elsif ($type eq 'object') { } 66 185 251
elsif ($type eq 'array') { } 17 168 185
elsif ($type eq 'set') { } 11 157 168
elsif ($type eq 'map') { } 15 142 157
elsif ($type eq 'tuple') { } 7 135 142
elsif ($type eq 'choice') { } 11 124 135
elsif ($type eq 'any') { } 6 118 124
elsif ($type eq 'date') { } 15 103 118
elsif ($type eq 'time') { } 18 85 103
elsif ($type eq 'datetime') { } 12 73 85
elsif ($type eq 'duration') { } 18 55 73
elsif ($type eq 'uuid') { } 15 40 55
elsif ($type eq 'uri') { } 15 25 40
elsif ($type eq 'binary') { } 11 14 25
elsif ($type eq 'jsonpointer') { } 14 0 14
371 7 $self->_validate_null( $value, $path, $schema_path );
372 }
373 elsif ( $type eq 'boolean' ) {
374 12 $self->_validate_boolean( $value, $path, $schema_path );
375 }
376 elsif ( $type eq 'string' ) {
377 104 $self->_validate_string( $value, $schema, $path, $schema_path );
378 }
379 ▶ elsif ($type eq 'number'
Condition91%11/12
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8" or $type eq "decimal"
l!l&&r!l&&!r
8110429
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8"
l!l&&r!l&&!r
810439
$type eq "number" or $type eq "float" or $type eq "double"
l!l&&r!l&&!r
747439
$type eq "number" or $type eq "float"
l!l&&r!l&&!r
6113446
MC/DC80%4/5
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8" or $type eq "decimal"
$type eq "number" $type eq "float" $type eq "double" $type eq "float8" $type eq "decimal"
Truth table83%5/6
A or B or C or D or E
A= $type eq "number"B= $type eq "float"C= $type eq "double"D= $type eq "float8"E= $type eq "decimal"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
380 || $type eq 'float'
381 || $type eq 'double'
382 || $type eq 'float8'
383 || $type eq 'decimal' )
384 {
385 91 $self->_validate_number( $value, $type, $schema, $path, $schema_path );
386 }
387 ▶ elsif ( $type eq 'integer' || exists $INT_RANGES{$type} ) {
Condition100%3/3
$type eq "integer" or exists $INT_RANGES{$type}
l!l&&r!l&&!r
5173251
MC/DC100%2/2
$type eq "integer" or exists $INT_RANGES{$type}
$type eq "integer" exists $INT_RANGES{$type}
Truth table100%3/3
A or B
A= $type eq "integer"B= exists $INT_RANGES{$type}
ABresult
1X1
011
000
388 178 $self->_validate_integer( $value, $type, $schema, $path, $schema_path );
389 }
390 ▶ elsif ( $type eq 'int128' || $type eq 'uint128' ) {
Condition33%1/3
$type eq "int128" or $type eq "uint128"
l!l&&r!l&&!r
00251
MC/DC0%0/2
$type eq "int128" or $type eq "uint128"
$type eq "int128" $type eq "uint128"
Truth table33%1/3
A or B
A= $type eq "int128"B= $type eq "uint128"
ABresult
1X1
011
000
391 0 $self->_validate_big_integer( $value, $type, $schema, $path,
392 $schema_path );
393 }
394 elsif ( $type eq 'object' ) {
395 66 $self->_validate_object( $value, $schema, $path, $schema_path );
396 }
397 elsif ( $type eq 'array' ) {
398 17 $self->_validate_array( $value, $schema, $path, $schema_path );
399 }
400 elsif ( $type eq 'set' ) {
401 11 $self->_validate_set( $value, $schema, $path, $schema_path );
402 }
403 elsif ( $type eq 'map' ) {
404 15 $self->_validate_map( $value, $schema, $path, $schema_path );
405 }
406 elsif ( $type eq 'tuple' ) {
407 7 $self->_validate_tuple( $value, $schema, $path, $schema_path );
408 }
409 elsif ( $type eq 'choice' ) {
410 11 $self->_validate_choice( $value, $schema, $path, $schema_path );
411 }
412 elsif ( $type eq 'any' ) {
413  
414 # Any type accepts all values
415 }
416 elsif ( $type eq 'date' ) {
417 15 $self->_validate_date( $value, $path, $schema_path );
418 }
419 elsif ( $type eq 'time' ) {
420 18 $self->_validate_time( $value, $path, $schema_path );
421 }
422 elsif ( $type eq 'datetime' ) {
423 12 $self->_validate_datetime( $value, $path, $schema_path );
424 }
425 elsif ( $type eq 'duration' ) {
426 18 $self->_validate_duration( $value, $path, $schema_path );
427 }
428 elsif ( $type eq 'uuid' ) {
429 15 $self->_validate_uuid( $value, $path, $schema_path );
430 }
431 elsif ( $type eq 'uri' ) {
432 15 $self->_validate_uri( $value, $path, $schema_path );
433 }
434 elsif ( $type eq 'binary' ) {
435 11 $self->_validate_binary( $value, $path, $schema_path );
436 }
437 elsif ( $type eq 'jsonpointer' ) {
438 14 $self->_validate_jsonpointer( $value, $path, $schema_path );
439 }
440 else {
441 0 $self->_add_error( INSTANCE_TYPE_UNKNOWN, "Unknown type: $type",
442 $path, $schema_path );
443 }
444 }
445  
446 0 ▶sub _check_type {
Subroutine _check_type: not called CC=31 SCAR=69.0
447 0 my ( $self, $value, $type ) = @_;
448  
449 0 ▶ if ( $type eq 'null' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'null') { } 0 0 0
elsif ($type eq 'boolean') { } 0 0 0
elsif ($type eq 'string') { } 0 0 0
elsif ($type eq 'number' or $type eq 'float' or $type eq 'double' or $type eq 'float8' or $type eq 'decimal') { } 0 0 0
elsif ($type eq 'integer' or exists $INT_RANGES{$type}) { } 0 0 0
elsif ($type eq 'object' or $type eq 'map' or $type eq 'choice') { } 0 0 0
elsif ($type eq 'array' or $type eq 'set' or $type eq 'tuple') { } 0 0 0
elsif ($type eq 'any') { } 0 0 0
450 0 ▶ return !defined $value || ( ref($value) eq '' && $value eq 'null' );
Condition0%0/6
!defined($value) || ref $value eq '' && $value eq 'null'
l!l&&r!l&&!r
000
ref $value eq "" and $value eq "null"
!ll&&!rl&&r
000
MC/DC0%0/3
!defined($value) || ref $value eq '' && $value eq 'null'
!defined($value) ref $value eq "" $value eq "null"
Truth table0%0/4
not(A) || B and C
A= !defined($value)B= ref $value eq ""C= $value eq "null"
ABCresult
1XX1
0111
00X0
0100
451 }
452 elsif ( $type eq 'boolean' ) {
453 0 ▶ return _is_json_bool($value)
Condition0%0/15
_is_json_bool($value) || defined $value && !ref($value) && ($value eq 'true' || $value eq 'false' || $value =~ /^[01]$/u)
l!l&&r!l&&!r
000
defined $value and not ref $value and $value eq "true" || $value eq "false" || $value =~ /^[01]$/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
$value eq "true" or $value eq "false" or $value =~ /^[01]$/u
l!l&&r!l&&!r
000
$value eq "true" or $value eq "false"
l!l&&r!l&&!r
000
MC/DC0%0/6
_is_json_bool($value) || defined $value && !ref($value) && ($value eq 'true' || $value eq 'false' || $value =~ /^[01]$/u)
_is_json_bool($value) defined $value not ref $value $value eq "true" $value eq "false" $value =~ /^[01]$/u
Truth table0%0/7
A || B and not(C) and D or E or F
A= _is_json_bool($value)B= defined $valueC= not ref $valueD= $value eq "true"E= $value eq "false"F= $value =~ /^[01]$/u
ABCDEFresult
1XXXXX1
0111XX1
01101X1
0110011
00XXXX0
010XXX0
0110000
454 || ( defined $value
455 && !ref($value)
456 && ( $value eq 'true' || $value eq 'false' || $value =~ /^[01]$/ )
457 );
458 }
459 elsif ( $type eq 'string' ) {
460 0 ▶ return defined $value && !ref($value);
Condition0%0/3
defined $value && !ref($value)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value && !ref($value)
defined $value !ref($value)
Truth table0%0/3
A && not(B)
A= defined $valueB= !ref($value)
ABresult
0X0
100
111
461 }
462 ▶ elsif ($type eq 'number'
Condition0%0/12
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8" or $type eq "decimal"
l!l&&r!l&&!r
000
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8"
l!l&&r!l&&!r
000
$type eq "number" or $type eq "float" or $type eq "double"
l!l&&r!l&&!r
000
$type eq "number" or $type eq "float"
l!l&&r!l&&!r
000
MC/DC0%0/5
$type eq "number" or $type eq "float" or $type eq "double" or $type eq "float8" or $type eq "decimal"
$type eq "number" $type eq "float" $type eq "double" $type eq "float8" $type eq "decimal"
Truth table0%0/6
A or B or C or D or E
A= $type eq "number"B= $type eq "float"C= $type eq "double"D= $type eq "float8"E= $type eq "decimal"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
463 || $type eq 'float'
464 || $type eq 'double'
465 || $type eq 'float8'
466 || $type eq 'decimal' )
467 {
468 0 ▶ return defined $value && !ref($value) && looks_like_number($value);
Condition0%0/6
defined $value && !ref($value) && &looks_like_number($value)
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value && !ref($value) && &looks_like_number($value)
defined $value not ref $value &looks_like_number($value)
Truth table0%0/4
A and not(B) && C
A= defined $valueB= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
469 }
470 ▶ elsif ( $type eq 'integer' || exists $INT_RANGES{$type} ) {
Condition0%0/3
$type eq "integer" or exists $INT_RANGES{$type}
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "integer" or exists $INT_RANGES{$type}
$type eq "integer" exists $INT_RANGES{$type}
Truth table0%0/3
A or B
A= $type eq "integer"B= exists $INT_RANGES{$type}
ABresult
1X1
011
000
471 return
472 0 ▶ defined $value
Condition0%0/9
defined $value && !ref($value) && &looks_like_number($value) && $value == int $value
!ll&&!rl&&r
000
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $value && !ref($value) && &looks_like_number($value) && $value == int $value
defined $value not ref $value &looks_like_number($value) $value == int $value
Truth table0%0/5
A and not(B) and C && D
A= defined $valueB= not ref $valueC= &looks_like_number($value)D= $value == int $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
473 && !ref($value)
474 && looks_like_number($value)
475 && $value == int($value);
476 }
477 ▶ elsif ( $type eq 'object' || $type eq 'map' || $type eq 'choice' ) {
Condition0%0/6
$type eq "object" or $type eq "map" or $type eq "choice"
l!l&&r!l&&!r
000
$type eq "object" or $type eq "map"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "object" or $type eq "map" or $type eq "choice"
$type eq "object" $type eq "map" $type eq "choice"
Truth table0%0/4
A or B or C
A= $type eq "object"B= $type eq "map"C= $type eq "choice"
ABCresult
1XX1
01X1
0011
0000
478 0 return ref($value) eq 'HASH';
479 }
480 ▶ elsif ( $type eq 'array' || $type eq 'set' || $type eq 'tuple' ) {
Condition0%0/6
$type eq "array" or $type eq "set" or $type eq "tuple"
l!l&&r!l&&!r
000
$type eq "array" or $type eq "set"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "array" or $type eq "set" or $type eq "tuple"
$type eq "array" $type eq "set" $type eq "tuple"
Truth table0%0/4
A or B or C
A= $type eq "array"B= $type eq "set"C= $type eq "tuple"
ABCresult
1XX1
01X1
0011
0000
481 0 return ref($value) eq 'ARRAY';
482 }
483 elsif ( $type eq 'any' ) {
484 0 return 1;
485 }
486 else {
487 0 return 0;
488 }
489 }
490  
491 7 ▶sub _validate_null {
Subroutine _validate_null: called CC=4 SCAR=14.1
492 7 my ( $self, $value, $path, $schema_path ) = @_;
493  
494 7 ▶ unless ( !defined $value || ( ref($value) eq '' && $value eq 'null' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $value or ref $value eq "" and $value eq "null") 5 2 7
Condition66%4/6
not defined $value or ref $value eq "" and $value eq "null"
l!l&&r!l&&!r
205
ref $value eq "" and $value eq "null"
!ll&&!rl&&r
140
MC/DC33%1/3
not defined $value or ref $value eq "" and $value eq "null"
not defined $value ref $value eq "" $value eq "null"
Truth table75%3/4
not(A) or B and C
A= not defined $valueB= ref $value eq ""C= $value eq "null"
ABCresult
1XX1
0111
00X0
0100
495 5 $self->_add_error( INSTANCE_NULL_EXPECTED, 'Value must be null',
496 $path, $schema_path );
497 }
498 }
499  
500 12 ▶sub _validate_boolean {
Subroutine _validate_boolean: called CC=2 SCAR=6.9
501 12 my ( $self, $value, $path, $schema_path ) = @_;
502  
503 # Accept JSON booleans from any JSON parser (JSON::PP, JSON::XS, etc.)
504 12 ▶ unless ( _is_json_bool($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_json_bool($value)) 5 7 12
505 5 $self->_add_error( INSTANCE_BOOLEAN_EXPECTED, 'Value must be a boolean',
506 $path, $schema_path );
507 }
508 }
509  
510 104 ▶sub _validate_string {
Subroutine _validate_string: called CC=13 SCAR=25.8
511 104 my ( $self, $value, $schema, $path, $schema_path ) = @_;
512  
513 # Must be defined, non-reference, and a pure string (not a number)
514 104 ▶ unless ( defined $value && !ref($value) && _is_pure_string($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value and _is_pure_string($value)) 18 86 104
Condition83%5/6
defined $value and not ref $value and _is_pure_string($value)
!ll&&!rl&&r
71186
defined $value and not ref $value
!ll&&!rl&&r
0797
MC/DC66%2/3
defined $value and not ref $value and _is_pure_string($value)
defined $value not ref $value _is_pure_string($value)
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= _is_pure_string($value)
ABCresult
0XX0
10X0
1100
1111
515 18 $self->_add_error( INSTANCE_STRING_EXPECTED, 'Value must be a string',
516 $path, $schema_path );
517 18 return;
518 }
519  
520 86 my $len = length($value);
521  
522 # Extended validation
523 86 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 63 23 86
524 63 ▶ if ( exists $schema->{minLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minLength"}) 20 43 63
525 20 ▶ if ( $len < $schema->{minLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $schema->{"minLength"}) 6 14 20
526 6 $self->_add_error(
527 INSTANCE_STRING_MIN_LENGTH,
528 "String length $len is less than minimum $schema->{minLength}",
529 $path,
530 $schema_path
531 );
532 }
533 }
534  
535 63 ▶ if ( exists $schema->{maxLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maxLength"}) 17 46 63
536 17 ▶ if ( $len > $schema->{maxLength} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $schema->{"maxLength"}) 4 13 17
537 4 $self->_add_error( INSTANCE_STRING_MAX_LENGTH,
538 "String length $len exceeds maximum $schema->{maxLength}",
539 $path, $schema_path );
540 }
541 }
542  
543 63 ▶ if ( exists $schema->{pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"pattern"}) 16 47 63
544 16 my $pattern = $schema->{pattern};
545 16 my $pattern_ok = eval {
546 16 ▶ if ( $value !~ qr/$pattern/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ qr/$pattern/u) 8 8 16
547 8 $self->_add_error(
548 INSTANCE_STRING_PATTERN_MISMATCH,
549 "String does not match pattern: $pattern",
550 $path, $schema_path
551 );
552 }
553 16 1;
554 };
555 16 ▶ if ( !$pattern_ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern_ok) 0 16 16
556 0 $self->_add_error( INSTANCE_PATTERN_INVALID,
557 "Invalid regex pattern: $pattern",
558 $path, $schema_path );
559 }
560 }
561  
562 63 ▶ if ( exists $schema->{format} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"format"}) 0 63 63
563 0 $self->_validate_format( $value, $schema->{format}, $path,
564 $schema_path );
565 }
566 }
567 }
568  
569 0 ▶sub _validate_format {
Subroutine _validate_format: not called CC=17 SCAR=57.2
570 0 my ( $self, $value, $format, $path, $schema_path ) = @_;
571  
572 0 ▶ if ( $format eq 'email' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($format eq 'email') { } 0 0 0
elsif ($format eq 'uri') { } 0 0 0
elsif ($format eq 'date') { } 0 0 0
elsif ($format eq 'time') { } 0 0 0
elsif ($format eq 'date-time') { } 0 0 0
elsif ($format eq 'uuid') { } 0 0 0
elsif ($format eq 'ipv4') { } 0 0 0
elsif ($format eq 'hostname') { } 0 0 0
573 0 ▶ unless ( $value =~ $EMAIL_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$EMAIL_REGEX/u) 0 0 0
574 0 $self->_add_error( INSTANCE_FORMAT_EMAIL_INVALID,
575 'String is not a valid email address',
576 $path, $schema_path );
577 }
578 }
579 elsif ( $format eq 'uri' ) {
580 0 ▶ unless ( $value =~ $URI_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$URI_REGEX/u) 0 0 0
581 0 $self->_add_error( INSTANCE_FORMAT_URI_INVALID,
582 'String is not a valid URI',
583 $path, $schema_path );
584 }
585 }
586 elsif ( $format eq 'date' ) {
587 0 ▶ unless ( $value =~ $DATE_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$DATE_REGEX/u) 0 0 0
588 0 $self->_add_error( INSTANCE_FORMAT_DATE_INVALID,
589 'String is not a valid date',
590 $path, $schema_path );
591 }
592 }
593 elsif ( $format eq 'time' ) {
594 0 ▶ unless ( $value =~ $TIME_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$TIME_REGEX/u) 0 0 0
595 0 $self->_add_error( INSTANCE_FORMAT_TIME_INVALID,
596 'String is not a valid time',
597 $path, $schema_path );
598 }
599 }
600 elsif ( $format eq 'date-time' ) {
601 0 ▶ unless ( $value =~ $DATETIME_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$DATETIME_REGEX/u) 0 0 0
602 0 $self->_add_error(
603 INSTANCE_FORMAT_DATETIME_INVALID,
604 'String is not a valid date-time',
605 $path, $schema_path
606 );
607 }
608 }
609 elsif ( $format eq 'uuid' ) {
610 0 ▶ unless ( $value =~ $UUID_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$UUID_REGEX/u) 0 0 0
611 0 $self->_add_error( INSTANCE_FORMAT_UUID_INVALID,
612 'String is not a valid UUID',
613 $path, $schema_path );
614 }
615 }
616 elsif ( $format eq 'ipv4' ) {
617 0 ▶ unless ( $value =~ $IPV4_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$IPV4_REGEX/u) 0 0 0
618 0 $self->_add_error( INSTANCE_FORMAT_IPV4_INVALID,
619 'String is not a valid IPv4 address',
620 $path, $schema_path );
621 }
622 }
623 elsif ( $format eq 'hostname' ) {
624 0 ▶ unless ( $value =~ $HOSTNAME_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$HOSTNAME_REGEX/u) 0 0 0
625 0 $self->_add_error(
626 INSTANCE_FORMAT_HOSTNAME_INVALID,
627 'String is not a valid hostname',
628 $path, $schema_path
629 );
630 }
631 }
632  
633 # Other formats are not strictly enforced
634 }
635  
636 91 ▶sub _validate_number {
Subroutine _validate_number: called CC=9 SCAR=22.1
637 91 my ( $self, $value, $type, $schema, $path, $schema_path ) = @_;
638  
639 # Decimal type can accept string representations for high precision
640 91 ▶ if ( $type eq 'decimal' && defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "decimal" and defined $value and not ref $value) 10 81 91
Condition66%4/6
$type eq "decimal" and defined $value and not ref $value
!ll&&!rl&&r
81010
$type eq "decimal" and defined $value
!ll&&!rl&&r
81010
MC/DC33%1/3
$type eq "decimal" and defined $value and not ref $value
$type eq "decimal" defined $value not ref $value
Truth table50%2/4
A and B and not(C)
A= $type eq "decimal"B= defined $valueC= not ref $value
ABCresult
0XX0
10X0
1100
1111
641  
642 # Accept numeric values or string representations of numbers
643 10 ▶ if ( _is_numeric($value)
Branch: 1 branch
BranchTrueFalseTotal
if (_is_numeric($value) or $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/u) 9 1 10
Condition100%3/3
_is_numeric($value) or $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
l!l&&r!l&&!r
631
MC/DC100%2/2
_is_numeric($value) or $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
_is_numeric($value) $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
Truth table100%3/3
A or B
A= _is_numeric($value)B= $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/u
ABresult
1X1
011
000
644 || $value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/ )
645 {
646 9 $self->_validate_numeric_constraints( $value, $schema, $path,
647 $schema_path );
648 9 return;
649 }
650 }
651  
652 # Must be a numeric value, not a string that looks like a number
653 82 ▶ unless ( defined $value && !ref($value) && _is_numeric($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value and _is_numeric($value)) 11 71 82
Condition83%5/6
defined $value and not ref $value and _is_numeric($value)
!ll&&!rl&&r
11071
defined $value and not ref $value
!ll&&!rl&&r
0181
MC/DC66%2/3
defined $value and not ref $value and _is_numeric($value)
defined $value not ref $value _is_numeric($value)
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= _is_numeric($value)
ABCresult
0XX0
10X0
1100
1111
654 11 $self->_add_error( INSTANCE_NUMBER_EXPECTED, 'Value must be a number',
655 $path, $schema_path );
656 11 return;
657 }
658  
659 71 $self->_validate_numeric_constraints( $value, $schema, $path,
660 $schema_path );
661 }
662  
663 178 ▶sub _validate_integer {
Subroutine _validate_integer: called CC=15 SCAR=27.2
664 178 my ( $self, $value, $type, $schema, $path, $schema_path ) = @_;
665  
666 # int64 and uint64 can accept string representations for large values
667 178 ▶ if ( ( $type eq 'int64' || $type eq 'uint64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "int64" || $type eq "uint64" and defined $value and not ref $value and $value =~ /^-?\d+$/u) 18 160 178
Condition83%10/12
$type eq "int64" || $type eq "uint64" and defined $value and not ref $value and $value =~ /^-?\d+$/u
!ll&&!rl&&r
156418
$type eq "int64" || $type eq "uint64" and defined $value and not ref $value
!ll&&!rl&&r
156022
$type eq "int64" || $type eq "uint64" and defined $value
!ll&&!rl&&r
156022
$type eq "int64" or $type eq "uint64"
l!l&&r!l&&!r
139156
MC/DC60%3/5
$type eq "int64" || $type eq "uint64" and defined $value and not ref $value and $value =~ /^-?\d+$/u
$type eq "int64" $type eq "uint64" defined $value not ref $value $value =~ /^-?\d+$/u
Truth table55%5/9
A or B and C and not(D) and E
A= $type eq "int64"B= $type eq "uint64"C= defined $valueD= not ref $valueE= $value =~ /^-?\d+$/u
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
668 && defined $value
669 && !ref($value)
670 && $value =~ /^-?\d+$/ )
671 {
672 # String representation of large integer - valid
673 18 my $range = $INT_RANGES{$type};
674  
675 # Use Math::BigInt for proper range checking with large values
676 18 require Math::BigInt;
677 18 my $big_val = Math::BigInt->new($value);
678 18 my $big_min = Math::BigInt->new( $range->{min} );
679 18 my $big_max = Math::BigInt->new( $range->{max} );
680  
681 18 ▶ if ( $big_val < $big_min || $big_val > $big_max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($big_val < $big_min or $big_val > $big_max) 3 15 18
Condition66%2/3
$big_val < $big_min or $big_val > $big_max
l!l&&r!l&&!r
3015
MC/DC50%1/2
$big_val < $big_min or $big_val > $big_max
$big_val < $big_min $big_val > $big_max
Truth table66%2/3
A or B
A= $big_val < $big_minB= $big_val > $big_max
ABresult
1X1
011
000
682 3 $self->_add_error( INSTANCE_INT_RANGE_INVALID,
683 "Value $value is not a valid $type",
684 $path, $schema_path );
685 }
686  
687 18 $self->_validate_numeric_constraints( $value, $schema, $path,
688 $schema_path );
689 18 return;
690 }
691  
692 # Must be a numeric value, not a string
693 160 ▶ unless ( defined $value && !ref($value) && _is_numeric($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value and _is_numeric($value)) 18 142 160
Condition83%5/6
defined $value and not ref $value and _is_numeric($value)
!ll&&!rl&&r
414142
defined $value and not ref $value
!ll&&!rl&&r
04156
MC/DC66%2/3
defined $value and not ref $value and _is_numeric($value)
defined $value not ref $value _is_numeric($value)
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= _is_numeric($value)
ABCresult
0XX0
10X0
1100
1111
694 18 $self->_add_error( INSTANCE_INTEGER_EXPECTED,
695 'Value must be an integer',
696 $path, $schema_path );
697 18 return;
698 }
699  
700 # Check it's actually an integer
701 142 ▶ if ( $value != int($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != int $value) 3 139 142
702 3 $self->_add_error( INSTANCE_INTEGER_EXPECTED,
703 'Value must be an integer',
704 $path, $schema_path );
705 3 return;
706 }
707  
708 # Check range for specific integer types
709 139 ▶ if ( exists $INT_RANGES{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INT_RANGES{$type}) 139 0 139
710 139 my $range = $INT_RANGES{$type};
711 139 ▶ if ( $value < $range->{min} || $value > $range->{max} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $range->{"min"} or $value > $range->{"max"}) 16 123 139
Condition100%3/3
$value < $range->{"min"} or $value > $range->{"max"}
l!l&&r!l&&!r
88123
MC/DC100%2/2
$value < $range->{"min"} or $value > $range->{"max"}
$value < $range->{"min"} $value > $range->{"max"}
Truth table100%3/3
A or B
A= $value < $range->{"min"}B= $value > $range->{"max"}
ABresult
1X1
011
000
712 16 $self->_add_error( INSTANCE_INT_RANGE_INVALID,
713 "Value $value is not a valid $type",
714 $path, $schema_path );
715 }
716 }
717  
718 139 $self->_validate_numeric_constraints( $value, $schema, $path,
719 $schema_path );
720 }
721  
722 0 ▶sub _validate_big_integer {
Subroutine _validate_big_integer: not called CC=7 SCAR=40.3
723 0 my ( $self, $value, $type, $schema, $path, $schema_path ) = @_;
724  
725 # Big integers can be numbers or strings
726 0 my $num_value;
727  
728 0 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
729 0 $self->_add_error( INSTANCE_INTEGER_EXPECTED,
730 "Value must be a valid $type",
731 $path, $schema_path );
732 0 return;
733 }
734  
735 0 ▶ if ( ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
736 0 $self->_add_error( INSTANCE_INTEGER_EXPECTED,
737 "Value must be a valid $type",
738 $path, $schema_path );
739 0 return;
740 }
741  
742 # Accept both number and string representation
743 0 ▶ if ( looks_like_number($value) || $value =~ /^-?\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($value) or $value =~ /^-?\d+$/u) { } 0 0 0
Condition0%0/3
&looks_like_number($value) or $value =~ /^-?\d+$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
&looks_like_number($value) or $value =~ /^-?\d+$/u
&looks_like_number($value) $value =~ /^-?\d+$/u
Truth table0%0/3
A or B
A= &looks_like_number($value)B= $value =~ /^-?\d+$/u
ABresult
1X1
011
000
744 0 $num_value = $value;
745 }
746 else {
747 0 $self->_add_error( INSTANCE_INTEGER_EXPECTED,
748 "Value must be a valid $type",
749 $path, $schema_path );
750 0 return;
751 }
752  
753 # Check for unsigned
754 0 ▶ if ( $type eq 'uint128' && $num_value < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "uint128" and $num_value < 0) 0 0 0
Condition0%0/3
$type eq "uint128" and $num_value < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "uint128" and $num_value < 0
$type eq "uint128" $num_value < 0
Truth table0%0/3
A and B
A= $type eq "uint128"B= $num_value < 0
ABresult
0X0
100
111
755 0 $self->_add_error( INSTANCE_INT_RANGE_INVALID,
756 "Value $value is not a valid $type",
757 $path, $schema_path );
758 }
759 }
760  
761 237 ▶sub _validate_numeric_constraints {
Subroutine _validate_numeric_constraints: called CC=13 SCAR=25.7
762 237 my ( $self, $value, $schema, $path, $schema_path ) = @_;
763  
764 237 ▶ return unless $self->{extended};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"extended"} 67 170 237
765  
766 170 ▶ if ( exists $schema->{minimum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minimum"}) 23 147 170
767 23 ▶ if ( $value < $schema->{minimum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < $schema->{"minimum"}) 6 17 23
768 6 $self->_add_error( INSTANCE_NUMBER_MINIMUM,
769 "Value $value is less than minimum $schema->{minimum}",
770 $path, $schema_path );
771 }
772 }
773  
774 170 ▶ if ( exists $schema->{maximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maximum"}) 18 152 170
775 18 ▶ if ( $value > $schema->{maximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > $schema->{"maximum"}) 4 14 18
776 4 $self->_add_error( INSTANCE_NUMBER_MAXIMUM,
777 "Value $value exceeds maximum $schema->{maximum}",
778 $path, $schema_path );
779 }
780 }
781  
782 170 ▶ if ( exists $schema->{exclusiveMinimum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"exclusiveMinimum"}) 13 157 170
783 13 ▶ if ( $value <= $schema->{exclusiveMinimum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= $schema->{"exclusiveMinimum"}) 4 9 13
784 4 $self->_add_error(
785 INSTANCE_NUMBER_EXCLUSIVE_MINIMUM,
786 "Value $value must be greater than $schema->{exclusiveMinimum}",
787 $path,
788 $schema_path
789 );
790 }
791 }
792  
793 170 ▶ if ( exists $schema->{exclusiveMaximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"exclusiveMaximum"}) 13 157 170
794 13 ▶ if ( $value >= $schema->{exclusiveMaximum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value >= $schema->{"exclusiveMaximum"}) 4 9 13
795 4 $self->_add_error(
796 INSTANCE_NUMBER_EXCLUSIVE_MAXIMUM,
797 "Value $value must be less than $schema->{exclusiveMaximum}",
798 $path, $schema_path
799 );
800 }
801 }
802  
803 170 ▶ if ( exists $schema->{multipleOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"multipleOf"}) 19 151 170
804 19 my $mult = $schema->{multipleOf};
805 19 ▶ if ( $mult != 0 && ( $value / $mult ) != int( $value / $mult ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mult != 0 and $value / $mult != int $value / $mult) 6 13 19
Condition66%2/3
$mult != 0 and $value / $mult != int $value / $mult
!ll&&!rl&&r
0136
MC/DC50%1/2
$mult != 0 and $value / $mult != int $value / $mult
$mult != 0 $value / $mult != int $value / $mult
Truth table66%2/3
A and B
A= $mult != 0B= $value / $mult != int $value / $mult
ABresult
0X0
100
111
806 6 $self->_add_error( INSTANCE_NUMBER_MULTIPLE_OF,
807 "Value $value is not a multiple of $mult",
808 $path, $schema_path );
809 }
810 }
811 }
812  
813 66 ▶sub _validate_object {
Subroutine _validate_object: called CC=21 SCAR=31.9
814 66 my ( $self, $value, $schema, $path, $schema_path ) = @_;
815  
816 66 ▶ unless ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH") 3 63 66
817 3 $self->_add_error( INSTANCE_OBJECT_EXPECTED, 'Value must be an object',
818 $path, $schema_path );
819 3 return;
820 }
821  
822 63 ▶ my $properties = $schema->{properties} // {};
Condition100%2/2
$schema->{"properties"} // {}
l!l
5211
MC/DC100%1/1
$schema->{"properties"} // {}
$schema->{"properties"}
Truth table100%2/2
A
A= $schema->{"properties"}
Aresult
11
00
823 63 ▶ my $required = $schema->{required} // [];
Condition100%2/2
$schema->{"required"} // []
l!l
2241
MC/DC100%1/1
$schema->{"required"} // []
$schema->{"required"}
Truth table100%2/2
A
A= $schema->{"required"}
Aresult
11
00
824 63 my $additional = $schema->{additionalProperties};
825  
826 # Check required properties
827 63 for my $prop (@$required) {
828 24 ▶ unless ( exists $value->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $value->{$prop}) 7 17 24
829 7 $self->_add_error(
830 INSTANCE_REQUIRED_PROPERTY_MISSING,
831 "Missing required property: $prop",
832 $path, "$schema_path/required"
833 );
834 }
835 }
836  
837 # Validate properties
838 63 for my $prop_name ( keys %$value ) {
839 90 my $prop_path = "$path/$prop_name";
840  
841 90 ▶ if ( exists $properties->{$prop_name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $properties->{$prop_name}) { } 63 27 90
elsif (defined $additional) { } 4 23 27
842 63 my $prop_schema = $properties->{$prop_name};
843 63 $self->_validate_value( $value->{$prop_name}, $prop_schema,
844 $prop_path, "$schema_path/properties/$prop_name" );
845 }
846 elsif ( defined $additional ) {
847 4 ▶ if ( _is_false($additional) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (_is_false($additional)) { } 2 2 4
elsif (ref $additional eq 'HASH') { } 2 0 2
848 2 $self->_add_error(
849 INSTANCE_ADDITIONAL_PROPERTY_NOT_ALLOWED,
850 "Additional property not allowed: $prop_name",
851 $prop_path,
852 "$schema_path/additionalProperties"
853 );
854 }
855 elsif ( ref($additional) eq 'HASH' ) {
856 2 $self->_validate_value( $value->{$prop_name}, $additional,
857 $prop_path, "$schema_path/additionalProperties" );
858 }
859 }
860 }
861  
862 # Extended validation
863 63 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 42 21 63
864 42 my $count = scalar( keys %$value );
865  
866 42 ▶ if ( exists $schema->{minProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minProperties"}) 4 38 42
867 4 ▶ if ( $count < $schema->{minProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $schema->{"minProperties"}) 2 2 4
868 2 $self->_add_error(
869 INSTANCE_MIN_PROPERTIES,
870 "Object has $count properties, minimum is $schema->{minProperties}",
871 $path,
872 $schema_path
873 );
874 }
875 }
876  
877 42 ▶ if ( exists $schema->{maxProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maxProperties"}) 4 38 42
878 4 ▶ if ( $count > $schema->{maxProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $schema->{"maxProperties"}) 1 3 4
879 1 $self->_add_error(
880 INSTANCE_MAX_PROPERTIES,
881 "Object has $count properties, maximum is $schema->{maxProperties}",
882 $path,
883 $schema_path
884 );
885 }
886 }
887  
888 42 ▶ if ( exists $schema->{dependentRequired} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"dependentRequired"}) 0 42 42
889 0 for my $prop ( keys %{ $schema->{dependentRequired} } ) {
890 0 ▶ if ( exists $value->{$prop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$prop}) 0 0 0
891 0 my $deps = $schema->{dependentRequired}{$prop};
892 0 for my $dep (@$deps) {
893 0 ▶ unless ( exists $value->{$dep} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $value->{$dep}) 0 0 0
894 0 $self->_add_error(
895 INSTANCE_DEPENDENT_REQUIRED,
896 "Property '$prop' requires property '$dep'",
897 $path,
898 $schema_path
899 );
900 }
901 }
902 }
903 }
904 }
905 }
906 }
907  
908 17 ▶sub _validate_array {
Subroutine _validate_array: called CC=16 SCAR=37.7
909 17 my ( $self, $value, $schema, $path, $schema_path ) = @_;
910  
911 17 ▶ unless ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY") 1 16 17
912 1 $self->_add_error( INSTANCE_ARRAY_EXPECTED, 'Value must be an array',
913 $path, $schema_path );
914 1 return;
915 }
916  
917 # Validate items
918 16 ▶ if ( exists $schema->{items} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"items"}) 16 0 16
919 16 my $items_schema = $schema->{items};
920 16 for my $i ( 0 .. $#$value ) {
921 33 $self->_validate_value(
922 $value->[$i], $items_schema,
923 "$path/$i", "$schema_path/items"
924 );
925 }
926 }
927  
928 # Extended validation
929 16 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 13 3 16
930 13 my $count = scalar(@$value);
931  
932 13 ▶ if ( exists $schema->{minItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minItems"}) 7 6 13
933 7 ▶ if ( $count < $schema->{minItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $schema->{"minItems"}) 3 4 7
934 3 $self->_add_error( INSTANCE_MIN_ITEMS,
935 "Array has $count items, minimum is $schema->{minItems}",
936 $path, $schema_path );
937 }
938 }
939  
940 13 ▶ if ( exists $schema->{maxItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maxItems"}) 7 6 13
941 7 ▶ if ( $count > $schema->{maxItems} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $schema->{"maxItems"}) 2 5 7
942 2 $self->_add_error( INSTANCE_MAX_ITEMS,
943 "Array has $count items, maximum is $schema->{maxItems}",
944 $path, $schema_path );
945 }
946 }
947  
948 13 ▶ if ( exists $schema->{contains} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"contains"}) 0 13 13
949 0 my $contains_count = 0;
950 0 for my $item (@$value) {
951  
952 # Create a temporary validator to check
953 0 my $temp_errors = $self->{errors};
954 0 $self->{errors} = [];
955 $self->_validate_value( $item, $schema->{contains},
956 0 "$path/contains", "$schema_path/contains" );
957 0 ▶ if ( @{ $self->{errors} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"errors"};} == 0) 0 0 0
958 0 $contains_count++;
959 }
960 0 $self->{errors} = $temp_errors;
961 }
962  
963 0 ▶ my $min_contains = $schema->{minContains} // 1;
Condition0%0/2
$schema->{"minContains"} // 1
l!l
00
MC/DC0%0/1
$schema->{"minContains"} // 1
$schema->{"minContains"}
Truth table0%0/2
A
A= $schema->{"minContains"}
Aresult
11
00
964 0 my $max_contains = $schema->{maxContains};
965  
966 0 ▶ if ( $contains_count < $min_contains ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contains_count < $min_contains) 0 0 0
967 0 $self->_add_error(
968 INSTANCE_MIN_CONTAINS,
969 "Array must contain at least $min_contains matching items (found $contains_count)",
970 $path,
971 $schema_path
972 );
973 }
974  
975 0 ▶ if ( defined $max_contains && $contains_count > $max_contains ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_contains and $contains_count > $max_contains) 0 0 0
Condition0%0/3
defined $max_contains and $contains_count > $max_contains
!ll&&!rl&&r
000
MC/DC0%0/2
defined $max_contains and $contains_count > $max_contains
defined $max_contains $contains_count > $max_contains
Truth table0%0/3
A and B
A= defined $max_containsB= $contains_count > $max_contains
ABresult
0X0
100
111
976 0 $self->_add_error(
977 INSTANCE_MAX_CONTAINS,
978 "Array must contain at most $max_contains matching items (found $contains_count)",
979 $path,
980 $schema_path
981 );
982 }
983 }
984 }
985 }
986  
987 11 ▶sub _validate_set {
Subroutine _validate_set: called CC=6 SCAR=18.4
988 11 my ( $self, $value, $schema, $path, $schema_path ) = @_;
989  
990 11 ▶ unless ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY") 0 11 11
991 0 $self->_add_error( INSTANCE_SET_EXPECTED,
992 'Value must be an array (set)',
993 $path, $schema_path );
994 0 return;
995 }
996  
997 # Check for uniqueness
998 11 my %seen;
999 11 for my $i ( 0 .. $#$value ) {
1000 27 my $key = $self->_value_to_key( $value->[$i] );
1001 27 ▶ if ( exists $seen{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) 5 22 27
1002 5 $self->_add_error( INSTANCE_SET_DUPLICATE,
1003 "Set contains duplicate value at index $i",
1004 "$path/$i", $schema_path );
1005 }
1006 27 $seen{$key} = 1;
1007 }
1008  
1009 # Validate items
1010 11 ▶ if ( exists $schema->{items} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"items"}) 11 0 11
1011 11 my $items_schema = $schema->{items};
1012 11 for my $i ( 0 .. $#$value ) {
1013 27 $self->_validate_value(
1014 $value->[$i], $items_schema,
1015 "$path/$i", "$schema_path/items"
1016 );
1017 }
1018 }
1019 }
1020  
1021 15 ▶sub _validate_map {
Subroutine _validate_map: called CC=18 SCAR=32.3
1022 15 my ( $self, $value, $schema, $path, $schema_path ) = @_;
1023  
1024 15 ▶ unless ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH") 0 15 15
1025 0 $self->_add_error( INSTANCE_MAP_EXPECTED,
1026 'Value must be an object (map)',
1027 $path, $schema_path );
1028 0 return;
1029 }
1030  
1031 # Validate values
1032 15 ▶ if ( exists $schema->{values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"values"}) 15 0 15
1033 15 my $values_schema = $schema->{values};
1034 15 for my $key ( keys %$value ) {
1035 $self->_validate_value(
1036 22 $value->{$key}, $values_schema,
1037 "$path/$key", "$schema_path/values"
1038 );
1039 }
1040 }
1041  
1042 # Extended validation
1043 15 ▶ if ( $self->{extended} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"extended"}) 12 3 15
1044 12 my $count = scalar( keys %$value );
1045  
1046 # Check minProperties (for object type)
1047 12 ▶ if ( exists $schema->{minProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minProperties"}) 0 12 12
1048 0 ▶ if ( $count < $schema->{minProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $schema->{"minProperties"}) 0 0 0
1049 0 $self->_add_error(
1050 INSTANCE_MAP_MIN_ENTRIES,
1051 "Map has $count entries, minimum is $schema->{minProperties}",
1052 $path,
1053 $schema_path
1054 );
1055 }
1056 }
1057  
1058 # Check maxProperties (for object type)
1059 12 ▶ if ( exists $schema->{maxProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maxProperties"}) 0 12 12
1060 0 ▶ if ( $count > $schema->{maxProperties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $schema->{"maxProperties"}) 0 0 0
1061 0 $self->_add_error(
1062 INSTANCE_MAP_MAX_ENTRIES,
1063 "Map has $count entries, maximum is $schema->{maxProperties}",
1064 $path,
1065 $schema_path
1066 );
1067 }
1068 }
1069  
1070 # Check minEntries (for map type)
1071 12 ▶ if ( exists $schema->{minEntries} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minEntries"}) 3 9 12
1072 3 ▶ if ( $count < $schema->{minEntries} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $schema->{"minEntries"}) 1 2 3
1073 1 $self->_add_error(
1074 INSTANCE_MAP_MIN_ENTRIES,
1075 "Map has $count entries, minimum is $schema->{minEntries}",
1076 $path,
1077 $schema_path
1078 );
1079 }
1080 }
1081  
1082 # Check maxEntries (for map type)
1083 12 ▶ if ( exists $schema->{maxEntries} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"maxEntries"}) 3 9 12
1084 3 ▶ if ( $count > $schema->{maxEntries} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > $schema->{"maxEntries"}) 1 2 3
1085 1 $self->_add_error(
1086 INSTANCE_MAP_MAX_ENTRIES,
1087 "Map has $count entries, maximum is $schema->{maxEntries}",
1088 $path,
1089 $schema_path
1090 );
1091 }
1092 }
1093  
1094 # Check keyNames pattern
1095 12 ▶ if ( exists $schema->{keyNames} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"keyNames"}) 3 9 12
1096 3 my $key_schema = $schema->{keyNames};
1097 3 ▶ if ( ref($key_schema) eq 'HASH' && exists $key_schema->{pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key_schema eq "HASH" and exists $key_schema->{"pattern"}) 3 0 3
Condition33%1/3
ref $key_schema eq "HASH" and exists $key_schema->{"pattern"}
!ll&&!rl&&r
003
MC/DC0%0/2
ref $key_schema eq "HASH" and exists $key_schema->{"pattern"}
ref $key_schema eq "HASH" exists $key_schema->{"pattern"}
Truth table33%1/3
A and B
A= ref $key_schema eq "HASH"B= exists $key_schema->{"pattern"}
ABresult
0X0
100
111
1098 3 my $pattern = $key_schema->{pattern};
1099 3 for my $key ( keys %$value ) {
1100 4 ▶ if ( $key !~ /$pattern/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /$pattern/u) 2 2 4
1101 2 $self->_add_error(
1102 INSTANCE_MAP_KEY_PATTERN_MISMATCH,
1103 "Map key '$key' does not match pattern '$pattern'",
1104 "$path/$key",
1105 "$schema_path/keyNames/pattern"
1106 );
1107 }
1108 }
1109 }
1110 }
1111 }
1112 }
1113  
1114 7 ▶sub _validate_tuple {
Subroutine _validate_tuple: called CC=12 SCAR=26.6
1115 7 my ( $self, $value, $schema, $path, $schema_path ) = @_;
1116  
1117 7 ▶ unless ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY") 0 7 7
1118 0 $self->_add_error( INSTANCE_TUPLE_EXPECTED,
1119 'Value must be an array (tuple)',
1120 $path, $schema_path );
1121 0 return;
1122 }
1123  
1124 7 ▶ my $properties = $schema->{properties} // {};
Condition50%1/2
$schema->{"properties"} // {}
l!l
70
MC/DC0%0/1
$schema->{"properties"} // {}
$schema->{"properties"}
Truth table50%1/2
A
A= $schema->{"properties"}
Aresult
11
00
1125 7 ▶ my $tuple_order = $schema->{tuple} // [];
Condition50%1/2
$schema->{"tuple"} // []
l!l
70
MC/DC0%0/1
$schema->{"tuple"} // []
$schema->{"tuple"}
Truth table50%1/2
A
A= $schema->{"tuple"}
Aresult
11
00
1126 7 my $expected_count = scalar(@$tuple_order);
1127 7 my $actual_count = scalar(@$value);
1128  
1129 # Check length
1130 7 ▶ if ( $actual_count != $expected_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_count != $expected_count) 3 4 7
1131 3 my $items = $schema->{items};
1132 3 ▶ if ( !defined $items || _is_false($items) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $items or _is_false($items)) 3 0 3
Condition33%1/3
not defined $items or _is_false($items)
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $items or _is_false($items)
not defined $items _is_false($items)
Truth table33%1/3
not(A) or B
A= not defined $itemsB= _is_false($items)
ABresult
1X1
011
000
1133 3 ▶ if ( $actual_count > $expected_count ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($actual_count > $expected_count) { } 1 2 3
elsif ($actual_count < $expected_count) { } 2 0 2
1134 1 $self->_add_error(
1135 INSTANCE_TUPLE_ADDITIONAL_ITEMS,
1136 "Tuple has $actual_count items but only $expected_count are defined",
1137 $path,
1138 $schema_path
1139 );
1140 }
1141 elsif ( $actual_count < $expected_count ) {
1142 2 $self->_add_error(
1143 INSTANCE_TUPLE_LENGTH_MISMATCH,
1144 "Tuple has $actual_count items but schema defines $expected_count",
1145 $path,
1146 $schema_path
1147 );
1148 }
1149 }
1150 }
1151  
1152 # Validate each tuple element
1153 7 for my $i ( 0 .. $#$tuple_order ) {
1154 18 ▶ last if $i >= $actual_count;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $actual_count 2 16 18
1155 16 my $prop_name = $tuple_order->[$i];
1156 16 ▶ if ( exists $properties->{$prop_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $properties->{$prop_name}) 16 0 16
1157 $self->_validate_value(
1158 16 $value->[$i], $properties->{$prop_name},
1159 "$path/$i", "$schema_path/properties/$prop_name"
1160 );
1161 }
1162 }
1163 }
1164  
1165 11 ▶sub _validate_choice {
Subroutine _validate_choice: called CC=13 SCAR=27.6
1166 11 my ( $self, $value, $schema, $path, $schema_path ) = @_;
1167  
1168 11 my $choices = $schema->{choices};
1169 11 ▶ unless ( defined $choices && ref($choices) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $choices and ref $choices eq "HASH") 0 11 11
Condition33%1/3
defined $choices and ref $choices eq "HASH"
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $choices and ref $choices eq "HASH"
defined $choices ref $choices eq "HASH"
Truth table33%1/3
A and B
A= defined $choicesB= ref $choices eq "HASH"
ABresult
0X0
100
111
1170 0 $self->_add_error(
1171 INSTANCE_CHOICE_MISSING_CHOICES,
1172 "Choice schema must have 'choices'",
1173 $path, $schema_path
1174 );
1175 0 return;
1176 }
1177  
1178 11 my $selector = $schema->{selector};
1179  
1180 11 ▶ if ( defined $selector ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) { } 8 3 11
1181  
1182 # Selector-based choice - value MUST be an object
1183 8 ▶ unless ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "HASH") 0 8 8
1184 0 $self->_add_error( INSTANCE_CHOICE_EXPECTED,
1185 'Value must be an object (choice with selector)',
1186 $path, $schema_path );
1187 0 return;
1188 }
1189  
1190 8 ▶ unless ( exists $value->{$selector} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $value->{$selector}) 2 6 8
1191 2 $self->_add_error(
1192 INSTANCE_CHOICE_SELECTOR_MISSING,
1193 "Choice requires selector property: $selector",
1194 $path, $schema_path
1195 );
1196 2 return;
1197 }
1198  
1199 6 my $choice_name = $value->{$selector};
1200 6 ▶ unless ( defined $choice_name && !ref($choice_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $choice_name and not ref $choice_name) 0 6 6
Condition33%1/3
defined $choice_name and not ref $choice_name
!ll&&!rl&&r
006
MC/DC0%0/2
defined $choice_name and not ref $choice_name
defined $choice_name not ref $choice_name
Truth table33%1/3
A and not(B)
A= defined $choice_nameB= not ref $choice_name
ABresult
0X0
100
111
1201 0 $self->_add_error(
1202 INSTANCE_CHOICE_SELECTOR_NOT_STRING,
1203 'Selector value must be a string',
1204 "$path/$selector", $schema_path
1205 );
1206 0 return;
1207 }
1208  
1209 6 ▶ unless ( exists $choices->{$choice_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $choices->{$choice_name}) 2 4 6
1210 2 $self->_add_error( INSTANCE_CHOICE_UNKNOWN,
1211 "Unknown choice: $choice_name",
1212 "$path/$selector", $schema_path );
1213 2 return;
1214 }
1215  
1216 # Validate against the selected choice schema
1217 $self->_validate_value(
1218 4 $value, $choices->{$choice_name},
1219 $path, "$schema_path/choices/$choice_name"
1220 );
1221 }
1222 else {
1223 # No selector - try to match against choices
1224 3 my $match_count = 0;
1225 3 my $matched_choice;
1226  
1227 3 for my $choice_name ( keys %$choices ) {
1228 6 my $temp_errors = $self->{errors};
1229 6 $self->{errors} = [];
1230 $self->_validate_value(
1231 6 $value, $choices->{$choice_name},
1232 $path, "$schema_path/choices/$choice_name"
1233 );
1234 6 ▶ if ( @{ $self->{errors} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"errors"};} == 0) 2 4 6
1235 2 $match_count++;
1236 2 $matched_choice = $choice_name;
1237 }
1238 6 $self->{errors} = $temp_errors;
1239 }
1240  
1241 3 ▶ if ( $match_count == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($match_count == 0) { } 1 2 3
elsif ($match_count > 1) { } 0 2 2
1242 1 $self->_add_error( INSTANCE_CHOICE_NO_MATCH,
1243 'Value does not match any choice option',
1244 $path, $schema_path );
1245 }
1246 elsif ( $match_count > 1 ) {
1247 0 $self->_add_error(
1248 INSTANCE_CHOICE_MULTIPLE_MATCHES,
1249 "Value matches $match_count choices (should match exactly one)",
1250 $path,
1251 $schema_path
1252 );
1253 }
1254 }
1255 }
1256  
1257 15 ▶sub _validate_date {
Subroutine _validate_date: called CC=5 SCAR=17.1
1258 15 my ( $self, $value, $path, $schema_path ) = @_;
1259  
1260 15 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 15 15
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1261 0 $self->_add_error( INSTANCE_DATE_EXPECTED, 'Date must be a string',
1262 $path, $schema_path );
1263 0 return;
1264 }
1265  
1266 15 ▶ unless ( $value =~ $DATE_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$DATE_REGEX/u) 8 7 15
1267 8 $self->_add_error( INSTANCE_DATE_FORMAT_INVALID,
1268 "Invalid date format: $value",
1269 $path, $schema_path );
1270 8 return;
1271 }
1272  
1273 # Additional calendar validation
1274 7 ▶ unless ( _is_valid_calendar_date($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_valid_calendar_date($value)) 2 5 7
1275 2 $self->_add_error( INSTANCE_DATE_FORMAT_INVALID,
1276 "Invalid calendar date: $value",
1277 $path, $schema_path );
1278 }
1279 }
1280  
1281 # Helper to validate calendar dates using Time::Local
1282 7 ▶sub _is_valid_calendar_date {
Subroutine _is_valid_calendar_date: called CC=7 SCAR=21.3
1283 7 my ($date_str) = @_;
1284 7 ▶ return 0 unless $date_str =~ /^(\d{4})-(\d{2})-(\d{2})$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $date_str =~ /^(\d{4})-(\d{2})-(\d{2})$/u 0 7 7
1285  
1286 7 my ( $year, $month, $day ) = ( $1, $2, $3 );
1287  
1288 # Basic range check for month
1289 7 ▶ return 0 if $month < 1 || $month > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 0 7 7
Condition33%1/3
$month < 1 or $month > 12
l!l&&r!l&&!r
007
MC/DC0%0/2
$month < 1 or $month > 12
$month < 1 $month > 12
Truth table33%1/3
A or B
A= $month < 1B= $month > 12
ABresult
1X1
011
000
1290 7 ▶ return 0 if $day < 1 || $day > 31;
Branch: 1 branch
BranchTrueFalseTotal
if $day < 1 or $day > 31 0 7 7
Condition33%1/3
$day < 1 or $day > 31
l!l&&r!l&&!r
007
MC/DC0%0/2
$day < 1 or $day > 31
$day < 1 $day > 31
Truth table33%1/3
A or B
A= $day < 1B= $day > 31
ABresult
1X1
011
000
1291  
1292 # Use Time::Local to validate the date - it throws an error for invalid dates
1293 7 my $valid = eval {
1294 # timelocal expects month 0-11, year as actual year
1295 7 Time::Local::timelocal( 0, 0, 0, $day, $month - 1, $year );
1296 5 1;
1297 };
1298 7 ▶ return $valid ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$valid ? : 5 2 7
1299 }
1300  
1301 18 ▶sub _validate_time {
Subroutine _validate_time: called CC=4 SCAR=15.9
1302 18 my ( $self, $value, $path, $schema_path ) = @_;
1303  
1304 18 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 18 18
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1305 0 $self->_add_error( INSTANCE_TIME_EXPECTED, 'Time must be a string',
1306 $path, $schema_path );
1307 0 return;
1308 }
1309  
1310 18 ▶ unless ( $value =~ $TIME_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$TIME_REGEX/u) 8 10 18
1311 8 $self->_add_error( INSTANCE_TIME_FORMAT_INVALID,
1312 "Invalid time format: $value",
1313 $path, $schema_path );
1314 }
1315 }
1316  
1317 12 ▶sub _validate_datetime {
Subroutine _validate_datetime: called CC=4 SCAR=15.9
1318 12 my ( $self, $value, $path, $schema_path ) = @_;
1319  
1320 12 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 12 12
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1321 0 $self->_add_error( INSTANCE_DATETIME_EXPECTED,
1322 'DateTime must be a string',
1323 $path, $schema_path );
1324 0 return;
1325 }
1326  
1327 12 ▶ unless ( $value =~ $DATETIME_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$DATETIME_REGEX/u) 5 7 12
1328 5 $self->_add_error(
1329 INSTANCE_DATETIME_FORMAT_INVALID,
1330 "Invalid datetime format: $value",
1331 $path, $schema_path
1332 );
1333 }
1334 }
1335  
1336 18 ▶sub _validate_duration {
Subroutine _validate_duration: called CC=6 SCAR=19.6
1337 18 my ( $self, $value, $path, $schema_path ) = @_;
1338  
1339 18 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 18 18
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1340 0 $self->_add_error( INSTANCE_DURATION_EXPECTED,
1341 'Duration must be a string',
1342 $path, $schema_path );
1343 0 return;
1344 }
1345  
1346 18 ▶ unless ( $value =~ $DURATION_REGEX && $value ne 'P' && $value ne 'PT' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$DURATION_REGEX/u and $value ne "P" and $value ne "PT") 6 12 18
Condition83%5/6
$value =~ /$DURATION_REGEX/u and $value ne "P" and $value ne "PT"
!ll&&!rl&&r
6012
$value =~ /$DURATION_REGEX/u and $value ne "P"
!ll&&!rl&&r
5112
MC/DC66%2/3
$value =~ /$DURATION_REGEX/u and $value ne "P" and $value ne "PT"
$value =~ /$DURATION_REGEX/u $value ne "P" $value ne "PT"
Truth table75%3/4
A and B and C
A= $value =~ /$DURATION_REGEX/uB= $value ne "P"C= $value ne "PT"
ABCresult
0XX0
10X0
1100
1111
1347 6 $self->_add_error(
1348 INSTANCE_DURATION_FORMAT_INVALID,
1349 "Invalid duration format: $value",
1350 $path, $schema_path
1351 );
1352 }
1353 }
1354  
1355 15 ▶sub _validate_uuid {
Subroutine _validate_uuid: called CC=4 SCAR=15.9
1356 15 my ( $self, $value, $path, $schema_path ) = @_;
1357  
1358 15 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 15 15
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1359 0 $self->_add_error( INSTANCE_UUID_EXPECTED, 'UUID must be a string',
1360 $path, $schema_path );
1361 0 return;
1362 }
1363  
1364 15 ▶ unless ( $value =~ $UUID_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$UUID_REGEX/u) 8 7 15
1365 8 $self->_add_error( INSTANCE_UUID_FORMAT_INVALID,
1366 "Invalid UUID format: $value",
1367 $path, $schema_path );
1368 }
1369 }
1370  
1371 15 ▶sub _validate_uri {
Subroutine _validate_uri: called CC=4 SCAR=15.9
1372 15 my ( $self, $value, $path, $schema_path ) = @_;
1373  
1374 15 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 15 15
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1375 0 $self->_add_error( INSTANCE_URI_EXPECTED, 'URI must be a string',
1376 $path, $schema_path );
1377 0 return;
1378 }
1379  
1380 15 ▶ unless ( $value =~ $URI_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$URI_REGEX/u) 6 9 15
1381 6 $self->_add_error( INSTANCE_URI_FORMAT_INVALID,
1382 "Invalid URI format: $value",
1383 $path, $schema_path );
1384 }
1385 }
1386  
1387 11 ▶sub _validate_binary {
Subroutine _validate_binary: called CC=11 SCAR=24.6
1388 11 my ( $self, $value, $path, $schema_path ) = @_;
1389  
1390 11 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 11 11
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1391 0 $self->_add_error( INSTANCE_BINARY_EXPECTED,
1392 'Binary must be a base64 string',
1393 $path, $schema_path );
1394 0 return;
1395 }
1396  
1397 # Validate base64 encoding - must be valid base64 characters only
1398 # and proper padding
1399 11 my $valid = 1;
1400  
1401 # Empty string is valid base64
1402 11 ▶ if ( $value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "") 2 9 11
1403 2 return;
1404 }
1405  
1406 # Base64 must only contain [A-Za-z0-9+/=]
1407 9 ▶ unless ( $value =~ /^[A-Za-z0-9+\/]*={0,2}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ m[^[A-Za-z0-9+/]*={0,2}$]u) 1 8 9
1408 1 $valid = 0;
1409 }
1410  
1411 # Length must be multiple of 4 after padding
1412 9 ▶ if ( $valid && length($value) % 4 != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid and length($value) % 4 != 0) 2 7 9
Condition100%3/3
$valid and length($value) % 4 != 0
!ll&&!rl&&r
162
MC/DC100%2/2
$valid and length($value) % 4 != 0
$valid length($value) % 4 != 0
Truth table100%3/3
A and B
A= $validB= length($value) % 4 != 0
ABresult
0X0
100
111
1413 2 $valid = 0;
1414 }
1415  
1416 # Check for proper padding
1417 9 ▶ if ( $valid && $value =~ /=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid and $value =~ /=/u) 5 4 9
Condition100%3/3
$valid and $value =~ /=/u
!ll&&!rl&&r
315
MC/DC100%2/2
$valid and $value =~ /=/u
$valid $value =~ /=/u
Truth table100%3/3
A and B
A= $validB= $value =~ /=/u
ABresult
0X0
100
111
1418  
1419 # = can only appear at end
1420 5 ▶ unless ( $value =~ /^[A-Za-z0-9+\/]*={1,2}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ m[^[A-Za-z0-9+/]*={1,2}$]u) 0 5 5
1421 0 $valid = 0;
1422 }
1423 }
1424  
1425 9 ▶ unless ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid) 3 6 9
1426 3 $self->_add_error(
1427 INSTANCE_BINARY_ENCODING_INVALID,
1428 'Invalid base64 encoding',
1429 $path, $schema_path
1430 );
1431 }
1432 }
1433  
1434 14 ▶sub _validate_jsonpointer {
Subroutine _validate_jsonpointer: called CC=5 SCAR=17.5
1435 14 my ( $self, $value, $path, $schema_path ) = @_;
1436  
1437 14 ▶ unless ( defined $value && !ref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and not ref $value) 0 14 14
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
1438 0 $self->_add_error( INSTANCE_JSONPOINTER_EXPECTED,
1439 'JSON Pointer must be a string',
1440 $path, $schema_path );
1441 0 return;
1442 }
1443  
1444 # Empty string is valid JSON Pointer (root)
1445 14 ▶ return if $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" 2 12 14
1446  
1447 12 ▶ unless ( $value =~ $JSONPOINTER_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$JSONPOINTER_REGEX/u) 4 8 12
1448 4 $self->_add_error(
1449 INSTANCE_JSONPOINTER_FORMAT_INVALID,
1450 "Invalid JSON Pointer format: $value",
1451 $path, $schema_path
1452 );
1453 }
1454 }
1455  
1456 392 ▶sub _validate_composition {
Subroutine _validate_composition: called CC=18 SCAR=29.1
1457 392 my ( $self, $value, $schema, $path, $schema_path ) = @_;
1458  
1459 # allOf
1460 392 ▶ if ( exists $schema->{allOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"allOf"}) 2 390 392
1461 2 for my $i ( 0 .. $#{ $schema->{allOf} } ) {
1462 $self->_validate_value(
1463 4 $value, $schema->{allOf}[$i],
1464 $path, "$schema_path/allOf/$i"
1465 );
1466 }
1467 }
1468  
1469 # anyOf
1470 392 ▶ if ( exists $schema->{anyOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"anyOf"}) 4 388 392
1471 4 my $matched = 0;
1472 4 my $temp_errors = $self->{errors};
1473  
1474 4 for my $sub_schema ( @{ $schema->{anyOf} } ) {
1475 7 $self->{errors} = [];
1476 7 $self->_validate_value( $value, $sub_schema, $path,
1477 "$schema_path/anyOf" );
1478 7 ▶ if ( @{ $self->{errors} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"errors"};} == 0) 2 5 7
1479 2 $matched = 1;
1480 2 last;
1481 }
1482 }
1483  
1484 4 $self->{errors} = $temp_errors;
1485  
1486 4 ▶ unless ($matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 2 2 4
1487 2 $self->_add_error( INSTANCE_ANY_OF_NONE_MATCHED,
1488 'Value must match at least one schema in anyOf',
1489 $path, $schema_path );
1490 }
1491 }
1492  
1493 # oneOf
1494 392 ▶ if ( exists $schema->{oneOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"oneOf"}) 5 387 392
1495 5 my $match_count = 0;
1496 5 my $temp_errors = $self->{errors};
1497  
1498 5 for my $sub_schema ( @{ $schema->{oneOf} } ) {
1499 10 $self->{errors} = [];
1500 10 $self->_validate_value( $value, $sub_schema, $path,
1501 "$schema_path/oneOf" );
1502 10 ▶ if ( @{ $self->{errors} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"errors"};} == 0) 5 5 10
1503 5 $match_count++;
1504 }
1505 }
1506  
1507 5 $self->{errors} = $temp_errors;
1508  
1509 5 ▶ if ( $match_count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match_count != 1) 2 3 5
1510 2 $self->_add_error(
1511 INSTANCE_ONE_OF_INVALID_COUNT,
1512 "Value must match exactly one schema in oneOf (matched $match_count)",
1513 $path,
1514 $schema_path
1515 );
1516 }
1517 }
1518  
1519 # not
1520 392 ▶ if ( exists $schema->{not} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"not"}) 4 388 392
1521 4 my $temp_errors = $self->{errors};
1522 4 $self->{errors} = [];
1523 4 $self->_validate_value( $value, $schema->{not}, $path,
1524 "$schema_path/not" );
1525 4 my $matched = @{ $self->{errors} } == 0;
1526 4 $self->{errors} = $temp_errors;
1527  
1528 4 ▶ if ($matched) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched) 1 3 4
1529 1 $self->_add_error( INSTANCE_NOT_MATCHED,
1530 "Value must not match the schema in 'not'",
1531 $path, $schema_path );
1532 }
1533 }
1534  
1535 # if/then/else
1536 392 ▶ if ( exists $schema->{if} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"if"}) 2 390 392
1537 2 my $temp_errors = $self->{errors};
1538 2 $self->{errors} = [];
1539 2 $self->_validate_value( $value, $schema->{if}, $path,
1540 "$schema_path/if" );
1541 2 my $if_matched = @{ $self->{errors} } == 0;
1542 2 $self->{errors} = $temp_errors;
1543  
1544 2 ▶ if ( $if_matched && exists $schema->{then} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($if_matched and exists $schema->{'then'}) { } 2 0 2
elsif (not $if_matched and exists $schema->{'else'}) { } 0 0 0
Condition33%1/3
$if_matched and exists $schema->{"then"}
!ll&&!rl&&r
002
MC/DC0%0/2
$if_matched and exists $schema->{"then"}
$if_matched exists $schema->{"then"}
Truth table33%1/3
A and B
A= $if_matchedB= exists $schema->{"then"}
ABresult
0X0
100
111
1545 2 $self->_validate_value( $value, $schema->{then}, $path,
1546 "$schema_path/then" );
1547 }
1548 ▶ elsif ( !$if_matched && exists $schema->{else} ) {
Condition0%0/3
not $if_matched and exists $schema->{"else"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $if_matched and exists $schema->{"else"}
not $if_matched exists $schema->{"else"}
Truth table0%0/3
not(A) and B
A= not $if_matchedB= exists $schema->{"else"}
ABresult
0X0
100
111
1549 0 $self->_validate_value( $value, $schema->{else}, $path,
1550 "$schema_path/else" );
1551 }
1552 }
1553 }
1554  
1555 8 ▶sub _resolve_ref {
Subroutine _resolve_ref: called CC=11 SCAR=32.6
1556 8 my ( $self, $ref, $root ) = @_;
1557  
1558 # Handle # prefix
1559 8 $ref =~ s/^#//;
1560  
1561 8 ▶ return $root if $ref eq '' || $ref eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "" or $ref eq "/" 0 8 8
Condition33%1/3
$ref eq "" or $ref eq "/"
l!l&&r!l&&!r
008
MC/DC0%0/2
$ref eq "" or $ref eq "/"
$ref eq "" $ref eq "/"
Truth table33%1/3
A or B
A= $ref eq ""B= $ref eq "/"
ABresult
1X1
011
000
1562  
1563 8 my @segments = split m{/}, $ref;
1564 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
1565  
1566 8 my $current = $root;
1567  
1568 8 for my $segment (@segments) {
1569  
1570 # Unescape JSON Pointer tokens
1571 16 $segment =~ s/~1/\//g;
1572 16 $segment =~ s/~0/~/g;
1573  
1574 16 ▶ if ( ref($current) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $current eq 'HASH') { } 16 0 16
elsif (ref $current eq 'ARRAY') { } 0 0 0
1575 16 ▶ return undef unless exists $current->{$segment};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$segment} 0 16 16
1576 16 $current = $current->{$segment};
1577 }
1578 elsif ( ref($current) eq 'ARRAY' ) {
1579 0 ▶ return undef unless $segment =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $segment =~ /^\d+$/u 0 0 0
1580 0 my $idx = int($segment);
1581 0 ▶ return undef if $idx >= @$current;
Branch: 1 branch
BranchTrueFalseTotal
if $idx >= @$current 0 0 0
1582 0 $current = $current->[$idx];
1583 }
1584 else {
1585 0 return undef;
1586 }
1587 }
1588  
1589 8 return $current;
1590 }
1591  
1592 31 ▶sub _values_equal {
Subroutine _values_equal: called CC=16 SCAR=43.0
1593 31 my ( $self, $a, $b ) = @_;
1594  
1595 # Handle undefined
1596 31 ▶ if ( !defined $a && !defined $b ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a or defined $b) 0 31 31
Condition33%1/3
defined $a or defined $b
l!l&&r!l&&!r
3100
MC/DC0%0/2
defined $a or defined $b
defined $a defined $b
Truth table33%1/3
A or B
A= defined $aB= defined $b
ABresult
1X1
011
000
1597 0 return 1;
1598 }
1599 31 ▶ if ( !defined $a || !defined $b ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a and defined $b) 0 31 31
Condition33%1/3
defined $a and defined $b
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $a and defined $b
defined $a defined $b
Truth table33%1/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
1600 0 return 0;
1601 }
1602  
1603 # Handle different types
1604 31 my $ref_a = ref($a);
1605 31 my $ref_b = ref($b);
1606  
1607 31 ▶ if ( $ref_a ne $ref_b ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_a ne $ref_b) 3 28 31
1608 3 return 0;
1609 }
1610  
1611 28 ▶ if ( $ref_a eq '' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_a eq '') { } 27 1 28
elsif ($ref_a eq 'ARRAY') { } 0 1 1
elsif ($ref_a eq 'HASH') { } 0 1 1
1612  
1613 # Scalars
1614 27 return $a eq $b;
1615 }
1616 elsif ( $ref_a eq 'ARRAY' ) {
1617 0 ▶ return 0 if @$a != @$b;
Branch: 1 branch
BranchTrueFalseTotal
if @$a != @$b 0 0 0
1618 0 for my $i ( 0 .. $#$a ) {
1619 0 ▶ return 0 unless $self->_values_equal( $a->[$i], $b->[$i] );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_values_equal($a->[$i], $b->[$i]) 0 0 0
1620 }
1621 0 return 1;
1622 }
1623 elsif ( $ref_a eq 'HASH' ) {
1624 0 my @keys_a = sort keys %$a;
1625 0 my @keys_b = sort keys %$b;
1626 0 ▶ return 0 if @keys_a != @keys_b;
Branch: 1 branch
BranchTrueFalseTotal
if @keys_a != @keys_b 0 0 0
1627 0 for my $i ( 0 .. $#keys_a ) {
1628 0 ▶ return 0 if $keys_a[$i] ne $keys_b[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $keys_a[$i] ne $keys_b[$i] 0 0 0
1629 return 0
1630 unless $self->_values_equal( $a->{ $keys_a[$i] },
1631 0 ▶ $b->{ $keys_b[$i] } );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_values_equal($$a{$keys_a[$i]}, $$b{$keys_b[$i]}) 0 0 0
1632 }
1633 0 return 1;
1634 }
1635  
1636 # Fallback
1637 1 return $a eq $b;
1638 }
1639  
1640 27 ▶sub _value_to_key {
Subroutine _value_to_key: called CC=7 SCAR=31.6
1641 27 my ( $self, $value ) = @_;
1642  
1643 27 ▶ if ( !defined $value ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 27 27
elsif (not ref $value) { } 27 0 27
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
1644 0 return 'null';
1645 }
1646 elsif ( !ref($value) ) {
1647 27 ▶ if ( _is_json_bool($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_json_bool($value)) 0 27 27
1648 0 ▶ return $value ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
1649 }
1650 27 return "s:$value";
1651 }
1652 elsif ( ref($value) eq 'ARRAY' ) {
1653 return
1654 0 'a:[' . join( ',', map { $self->_value_to_key($_) } @$value ) . ']';
1655 }
1656 elsif ( ref($value) eq 'HASH' ) {
1657 return 'o:{'
1658 . join( ',',
1659 0 map { "$_:" . $self->_value_to_key( $value->{$_} ) }
1660 sort keys %$value ) . '}';
1661 }
1662 else {
1663 0 return "?:$value";
1664 }
1665 }
1666  
1667 14 ▶sub _is_false {
Subroutine _is_false: called CC=8 SCAR=25.8
1668 14 my ($value) = @_;
1669 14 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 14 14
1670 14 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "" and $value eq 0 || $value eq "false" || $value eq "" 0 14 14
Condition11%1/9
ref $value eq "" and $value eq 0 || $value eq "false" || $value eq ""
!ll&&!rl&&r
1400
$value eq 0 or $value eq "false" or $value eq ""
l!l&&r!l&&!r
000
$value eq 0 or $value eq "false"
l!l&&r!l&&!r
000
MC/DC0%0/4
ref $value eq "" and $value eq 0 || $value eq "false" || $value eq ""
ref $value eq "" $value eq 0 $value eq "false" $value eq ""
Truth table20%1/5
A and B or C or D
A= ref $value eq ""B= $value eq 0C= $value eq "false"D= $value eq ""
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1671 if ref($value) eq ''
1672 && ( $value eq '0' || $value eq 'false' || $value eq '' );
1673 14 ▶ return 1 if _is_json_bool($value) && !$value;
Branch: 1 branch
BranchTrueFalseTotal
if _is_json_bool($value) and not $value 7 7 14
Condition100%3/3
_is_json_bool($value) and not $value
!ll&&!rl&&r
257
MC/DC100%2/2
_is_json_bool($value) and not $value
_is_json_bool($value) not $value
Truth table100%3/3
A and not(B)
A= _is_json_bool($value)B= not $value
ABresult
0X0
100
111
1674 7 return 0;
1675 }
1676  
1677 17 1;
1678  
1679 __END__
↑ Summary lib/JSON/Structure/SchemaValidator.pm »