lib/JSON/Schema/Validate.pm

filter: Statementstmt 73.8% 1391 / 1883 Branchbran 64.1% 828 / 1290 Conditioncond 47.8% 342 / 715 MC/DCmcdc 27.5% 110 / 400 Subroutinesub 83.3% 125 / 150 Podpod 100% 30 / 30 totaltotal 63.2% 2826 / 4468 CC 975 SCAR 74.1
CC
975
Cov
66%
_compile_js_node
15006.0
_js_reference_target
132.0
validate
95.5
CRAP 1655.3
↑ Summary
1 ##----------------------------------------------------------------------------
2 ## JSON Schema Validator - ~/lib/JSON/Schema/Validate.pm
3 ## Version v0.9.2
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2025/11/07
7 ## Modified 2026/11/28
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.##
12 ##----------------------------------------------------------------------------##
13 package JSON::Schema::Validate;
14 BEGIN
15 52 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 52 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 52 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 52 ▶ use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 52 ▶ use vars qw( $VERSION $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 52 ▶ use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 52 ▶ use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 52 ▶ use Scalar::Util qw( blessed looks_like_number reftype refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 52 ▶ use List::Util qw( first any all );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 52 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 52 our $VERSION = 'v0.9.2';
26 };
27  
28 52 ▶use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 sub new
33 547 ▶{
Subroutine new: called CC=26 SCAR=33.4
Pod documented
34 547 my $class = shift( @_ );
35 547 my $schema = shift( @_ );
36 547 ▶ if( defined( $schema ) && ref( $schema ) ne 'HASH' && !JSON::is_bool( $schema ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema and ref $schema ne "HASH" and not &JSON::is_bool($schema)) 0 547 547
Condition66%4/6
defined $schema and ref $schema ne "HASH" and not &JSON::is_bool($schema)
!ll&&!rl&&r
54340
defined $schema and ref $schema ne "HASH"
!ll&&!rl&&r
05434
MC/DC0%0/3
defined $schema and ref $schema ne "HASH" and not &JSON::is_bool($schema)
defined $schema ref $schema ne "HASH" not &JSON::is_bool($schema)
Truth table50%2/4
A and B and not(C)
A= defined $schemaB= ref $schema ne "HASH"C= not &JSON::is_bool($schema)
ABCresult
0XX0
10X0
1100
1111
37 {
38 0 die( "You provided a value (", overload::StrVal( $schema ), "), but it is not a hash reference or a JSON boolean. You must create a new $class object like this: $class->new( \$schema, \%opts );" );
39 }
40  
41 547 my $self =
42 {
43 comment_handler => undef,
44 # boolean
45 compile_on => 0,
46 # { schema, resource_index, base, fn_index }
47 compiled => undef,
48 # boolean; when 0, failures don’t invalidate; when 1, they do
49 content_assert => 0,
50 content_decoders => {},
51 errors => [],
52 # boolean; when true, then non-standard extensions are enabled.
53 extensions => 0,
54 formats => {},
55 # boolean
56 ignore_req_vocab => 0,
57 last_error => '',
58 last_trace => [],
59 max_errors => 200,
60 media_validators => {},
61 # boolean
62 normalize_instance => 1,
63 # boolean: when true, prune unknown properties before validate()
64 prune_unknown => 0,
65 # ($abs_uri) -> $schema_hashref
66 resolver => undef,
67 schema => _clone( $schema ),
68 # 0 = unlimited
69 trace_limit => 0,
70 # boolean
71 trace_on => 0,
72 # 0 = record all
73 trace_sample => 0,
74 # boolean; when true, 'uniqueKeys' extension is enabled.
75 unique_keys => 0,
76 # internal boolean; not an option
77 vocab_checked => 0,
78 vocab_support => {},
79 };
80  
81 547 bless( $self, $class );
82 547 my $opts = $self->_get_args_as_hash( @_ );
83 547 my @bool_options = qw(
84 content_assert
85 extensions
86 ignore_req_vocab
87 normalize_instance
88 prune_unknown
89 unique_keys
90 );
91 547 foreach my $opt ( @bool_options )
92 {
93 3282 ▶ next unless( exists( $opts->{ $opt } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$opt} 3070 212 3282
94 212 ▶ $self->{ $opt } = $opts->{ $opt } ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
$opts->{$opt} ? : 116 96 212
95 }
96 # Make sure the boolean value for 'extensions' is propagated to 'unique_keys' unless the option 'unique_keys' has been explicitly specified, and then we do not want to overwrite it.
97 547 ▶ $self->{unique_keys} = $self->{extensions} unless( exists( $opts->{unique_keys} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"unique_keys"} 547 0 547
98 547 ▶ if( exists( $opts->{ignore_unknown_required_vocab} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"ignore_unknown_required_vocab"}) 1 546 547
99 {
100 1 ▶ $self->{ignore_req_vocab} = $opts->{ignore_unknown_required_vocab} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'ignore_unknown_required_vocab'} ? : 1 0 1
101 }
102 547 ▶ if( exists( $opts->{compile} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"compile"}) 437 110 547
103 {
104 437 ▶ $self->{compile_on} = $opts->{compile} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'compile'} ? : 223 214 437
105 }
106 547 ▶ if( exists( $opts->{trace} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"trace"}) 6 541 547
107 {
108 6 ▶ $self->{trace_on} = $opts->{trace} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'trace'} ? : 6 0 6
109 }
110  
111 547 my @other_options = qw( max_errors trace_limit );
112 547 foreach my $opt ( @other_options )
113 {
114 1094 ▶ next unless( exists( $opts->{ $opt } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$opt} 1083 11 1094
115 11 $self->{ $opt } = $opts->{ $opt };
116 }
117  
118 547 ▶ if( exists( $opts->{trace_sample} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"trace_sample"}) 1 546 547
119 {
120 # Check for percentage integer (0 to 100)
121 1 ▶ if( $opts->{trace_sample} =~ /^([0-9]{1,2}|100)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'trace_sample'} =~ /^([0-9]{1,2}|100)$/u) { } 1 0 1
122 {
123 1 $self->{trace_sample} = $opts->{trace_sample};
124 }
125 else
126 {
127 0 ▶ warn( "Warning only: invalid value for option 'trace_sample'." ) if( warnings::enabled() );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
128 }
129 }
130  
131 # User-supplied format callbacks (override precedence left to caller order)
132 547 ▶ if( $opts->{format} && ref( $opts->{format} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"format"} and ref $opts->{"format"} eq "HASH") 3 544 547
Condition66%2/3
$opts->{"format"} and ref $opts->{"format"} eq "HASH"
!ll&&!rl&&r
54403
MC/DC50%1/2
$opts->{"format"} and ref $opts->{"format"} eq "HASH"
$opts->{"format"} ref $opts->{"format"} eq "HASH"
Truth table66%2/3
A and B
A= $opts->{"format"}B= ref $opts->{"format"} eq "HASH"
ABresult
0X0
100
111
133 {
134 3 $self->{formats}->{ $_ } = $opts->{format}->{ $_ } for( keys( %{$opts->{format}} ) );
135 }
136 547 ▶ $self->{vocab_support} = $opts->{vocab_support} ? { %{$opts->{vocab_support}} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'vocab_support'} ? : 1 546 547
137  
138 547 ▶ $self->_check_vocabulary_required unless( $self->{ignore_req_vocab} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ignore_req_vocab"} 546 1 547
139 546 ▶ $self->_register_builtin_media_validators() if( $self->{content_assert} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_assert"} 13 533 546
140 546 ▶ $self->{compiled} = _compile_root( $self->{schema} ) if( $self->{compile_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"compile_on"} 223 323 546
141 546 return( $self );
142 }
143  
144 # $js->compile -> enables it
145 # $js->compile(1) -> enables it
146 # $js->compile(0) -> disables it
147 sub compile
148 1 ▶{
Subroutine compile: called CC=4 SCAR=15.0
Pod documented
149 1 my( $self, $bool ) = @_;
150 1 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 0 1 1
151 1 $self->{compile_on} = $on;
152  
153 1 ▶ if( $self->{compile_on} && !$self->{compiled} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"compile_on"} and not $self->{"compiled"}) 1 0 1
Condition33%1/3
$self->{"compile_on"} and not $self->{"compiled"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"compile_on"} and not $self->{"compiled"}
$self->{"compile_on"} not $self->{"compiled"}
Truth table33%1/3
A and not(B)
A= $self->{"compile_on"}B= not $self->{"compiled"}
ABresult
0X0
100
111
154 {
155 1 $self->{compiled} = _compile_root( $self->{schema} );
156 }
157 1 return( $self );
158 }
159  
160 sub compile_js
161 0 ▶{
Subroutine compile_js: not called CC=7 SCAR=40.3
Pod documented
162 0 my $self = shift( @_ );
163 0 my $opts = $self->_get_args_as_hash( @_ );
164 0 $opts = {%$opts};
165  
166 0 my $schema = $self->{schema};
167 0 ▶ die( "No schema loaded; cannot compile to JavaScript" ) unless( defined( $schema ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $schema 0 0 0
168  
169 # Public JS API name, e.g. "validateIncorporation"
170 my $name = exists( $opts->{name} ) && defined( $opts->{name} ) && length( $opts->{name} )
171 ? $opts->{name}
172 0 ▶ : 'validate';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'name'} && defined $opts->{'name'} && length $opts->{'name'} ? : 0 0 0
Condition0%0/6
exists $opts->{"name"} and defined $opts->{"name"} and length $opts->{"name"}
!ll&&!rl&&r
000
exists $opts->{"name"} and defined $opts->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $opts->{"name"} and defined $opts->{"name"} and length $opts->{"name"}
exists $opts->{"name"} defined $opts->{"name"} length $opts->{"name"}
Truth table0%0/4
A and B and C
A= exists $opts->{"name"}B= defined $opts->{"name"}C= length $opts->{"name"}
ABCresult
0XX0
10X0
1100
1111
173  
174 # Max errors to collect on the client side
175 0 ▶ my $max_errors = exists( $opts->{max_errors} ) ? int( $opts->{max_errors} ) : 200;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'max_errors'} ? : 0 0 0
176 0 ▶ $max_errors = 0 if( $max_errors < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $max_errors < 0 0 0 0
177  
178 0 my %seen; # schema_pointer -> function name
179 0 my $counter = 0; # for generating unique function names
180 0 my @funcs; # accumulated JS validator functions
181  
182 0 my $root_ptr = '#';
183 0 my $index = { resources => {}, nodes => {} };
184 0 $opts->{_resource_base} = _index_schema_202012( $schema, '', '#', $index, 1 );
185 0 $opts->{_resource_index} = $index;
186  
187 # Pass $opts down so we can see ecma => ... inside the compiler.
188 0 my $root_fn = $self->_compile_js_node( $schema, $root_ptr, \%seen, \@funcs, \$counter, $schema, $opts );
189  
190 0 my $js = '';
191  
192 0 $js .= <<'JS_RUNTIME';
193 (function(global)
194 {
195 "use strict";
196  
197 function _jsv_err(ctx, path, keyword, message, schemaPtr)
198 {
199 if(ctx.maxErrors && ctx.errors.length >= ctx.maxErrors)
200 {
201 return;
202 }
203 ctx.errors.push({
204 path: path,
205 keyword: keyword,
206 message: message,
207 schema_pointer: schemaPtr
208 });
209 }
210  
211 function _jsv_typeOf(x)
212 {
213 if(x === null)
214 {
215 return "null";
216 }
217 if(Array.isArray ? Array.isArray(x) : Object.prototype.toString.call(x) === "[object Array]")
218 {
219 return "array";
220 }
221 var t = typeof x;
222 if(t === "number" && isFinite(x) && Math.floor(x) === x)
223 {
224 // distinguish "integer" for convenience
225 return "integer";
226 }
227 return t;
228 }
229  
230 function _jsv_hasOwn(obj, prop)
231 {
232 return Object.prototype.hasOwnProperty.call(obj, prop);
233 }
234  
235 function _jsv_equal(a, b)
236 {
237 if(a === b) return true;
238 if(a === null || b === null || typeof a !== 'object' || typeof b !== 'object') return false;
239 if(Array.isArray(a) !== Array.isArray(b)) return false;
240 var keys = Object.keys(a);
241 if(keys.length !== Object.keys(b).length) return false;
242 for(var i = 0; i < keys.length; i++)
243 {
244 var key = keys[i];
245 if(!_jsv_hasOwn(b, key) || !_jsv_equal(a[key], b[key])) return false;
246 }
247 return true;
248 }
249  
250 function _jsv_strlen(s)
251 {
252 return s.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, 'x').length;
253 }
254  
255 JS_RUNTIME
256  
257 # Public entry point
258 0 $js .= <<"JS_RUNTIME";
259 function $name(instance)
260 {
261 var ctx = { errors: [], maxErrors: $max_errors };
262 $root_fn(instance, "#", ctx);
263 return ctx.errors;
264 }
265  
266 JS_RUNTIME
267  
268 # Attach to global (browser: window) in a conservative way
269 0 $js .= <<"JS_RUNTIME";
270 if(typeof global === 'object' && global)
271 {
272 global.$name = $name;
273 }
274  
275 JS_RUNTIME
276  
277 # Emit all compiled validator functions, indented one level
278 0 $js .= join( "\n\n", map{ ' ' . join( "\n ", split( /\n/, $_ ) ) } @funcs );
279  
280 0 $js .= <<'JS_RUNTIME';
281  
282 })(this);
283 JS_RUNTIME
284  
285 0 return( $js );
286 }
287  
288 # $js->content_checks -> enables it
289 # $js->content_checks(1) -> enables it
290 # $js->content_checks(0) -> disables it
291 sub content_checks
292 5 ▶{
Subroutine content_checks: called CC=4 SCAR=14.6
Pod documented
293 5 my( $self, $bool ) = @_;
294 5 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 5 0 5
295 5 ▶ $self->{content_assert} = $on ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 5 0 5
296 5 ▶ $self->_register_builtin_media_validators() if( $self->{content_assert} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content_assert"} 5 0 5
297 5 return( $self );
298 }
299  
300 # TODO: Backward compatibility, but need to remove it
301 {
302 52 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
303 52 *enable_content_checks = \&content_checks;
304 }
305  
306 23 ▶sub error { $_[0]->{last_error} }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
307  
308 # We return a copy of the array reference containing the error objects
309 37 ▶sub errors { return( [@{$_[0]->{errors}}] ); }
Subroutine errors: called CC=1 SCAR=0.0
Pod documented
310  
311 sub extensions
312 1 ▶{
Subroutine extensions: called CC=2 SCAR=7.0
Pod documented
313 1 my( $self, $bool ) = @_;
314 1 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 1 0 1
315 1 $self->{extensions} = $on;
316 1 $self->unique_keys( $on );
317 1 return( $self );
318 }
319  
320 sub get_trace
321 3 ▶{
Subroutine get_trace: called CC=2 SCAR=7.1
Pod documented
322 3 my( $self ) = @_;
323 3 ▶ return( [@{ $self->{last_trace} || [] }] );
Condition50%1/2
$self->{'last_trace'} || []
l!l
30
MC/DC0%0/1
$self->{'last_trace'} || []
$self->{'last_trace'}
Truth table50%1/2
A
A= $self->{'last_trace'}
Aresult
11
00
324 }
325  
326 # Accessor-only method. See trace_limit for its mutator alter ego.
327 0 ▶sub get_trace_limit { 0 + ( $_[0]->{trace_limit} // 0 ) }
Subroutine get_trace_limit: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"trace_limit"} // 0
l!l
00
MC/DC0%0/1
$_[0]{"trace_limit"} // 0
$_[0]{"trace_limit"}
Truth table0%0/2
A
A= $_[0]{"trace_limit"}
Aresult
11
00
328  
329 # $js->ignore_unknown_required_vocab -> enables it
330 # $js->ignore_unknown_required_vocab(1) -> enables it
331 # $js->ignore_unknown_required_vocab(0) -> disables it
332 sub ignore_unknown_required_vocab
333 0 ▶{
Subroutine ignore_unknown_required_vocab: not called CC=2 SCAR=17.9
Pod documented
334 0 my( $self, $bool ) = @_;
335 0 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 0 0 0
336 0 $self->{ignore_req_vocab} = $on;
337 0 return( $self );
338 }
339  
340 0 ▶sub is_compile_enabled { $_[0]->{compile_on} ? 1 : 0 }
Subroutine is_compile_enabled: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'compile_on'} ? : 0 0 0
341  
342 0 ▶sub is_content_checks_enabled { $_[0]->{content_assert} ? 1 : 0 }
Subroutine is_content_checks_enabled: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'content_assert'} ? : 0 0 0
343  
344 # Accessor only method. See trace or the mutator vession.
345 0 ▶sub is_trace_on { $_[0]->{trace_on} ? 1 : 0 }
Subroutine is_trace_on: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'trace_on'} ? : 0 0 0
346  
347 0 ▶sub is_unique_keys_enabled { $_[0]->{unique_keys} ? 1 : 0 }
Subroutine is_unique_keys_enabled: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'unique_keys'} ? : 0 0 0
348  
349 0 ▶sub is_unknown_required_vocab_ignored { $_[0]->{ignore_req_vocab} ? 1 : 0 }
Subroutine is_unknown_required_vocab_ignored: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'ignore_req_vocab'} ? : 0 0 0
350  
351 sub is_valid
352 2 ▶{
Subroutine is_valid: called CC=3 SCAR=11.0
Pod documented
353 2 my $self = shift( @_ );
354 2 my $data = shift( @_ );
355  
356 2 my $opts = $self->_get_args_as_hash( @_ );
357 # Optional: allow overriding max_errors for this call only
358 # (e.g. $v->is_valid( $data, max_errors => 1 );)
359 2 ▶ $opts->{max_errors} //= 1;
Condition50%1/2
$opts->{"max_errors"} //= 1
l!l
02
MC/DC0%0/1
$opts->{"max_errors"} //= 1
$opts->{"max_errors"}
Truth table50%1/2
A
A= $opts->{"max_errors"}
Aresult
11
00
360  
361 # validate already populates $self->{errors}; is_valid just returns boolean
362 2 ▶ return( $self->validate( $data, $opts ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$self->validate($data, $opts) ? : 1 1 2
363 }
364  
365 # Example:
366 # my $pruned = $js->prune_instance( $incoming_data );
367 sub prune_instance
368 5 ▶{
Subroutine prune_instance: called CC=2 SCAR=7.0
Pod documented
369 5 my( $self, $data ) = @_;
370  
371 # Work on a cloned copy if normalize_instance is on,
372 # to remain consistent with validate().
373 5 ▶ if( $self->{normalize_instance} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"normalize_instance"}) 5 0 5
374 {
375 5 $data = _clone( $data );
376 }
377  
378 5 return( $self->_prune_with_schema( $self->{schema}, $data ) );
379 }
380  
381 sub prune_unknown
382 0 ▶{
Subroutine prune_unknown: not called CC=3 SCAR=24.8
Pod documented
383 0 my( $self, $bool ) = @_;
384 0 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 0 0 0
385 0 ▶ $self->{prune_unknown} = $on ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
386 0 return( $self );
387 }
388  
389 sub register_builtin_formats
390 47 ▶{
Subroutine register_builtin_formats: called CC=7 SCAR=23.4
Pod documented
391 47 my( $self ) = @_;
392  
393 47 local $@;
394 0 ▶ my $has_iso = eval{ require DateTime::Format::ISO8601; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require DateTime::Format::ISO8601; 1 } } ? : 0 47 47
395 47 ▶ my $has_tp = eval{ require Time::Piece; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Time::Piece; 1 } } ? : 47 0 47
396 0 ▶ my $has_dt = eval{ require DateTime; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require DateTime; 1 } } ? : 0 47 47
397 0 ▶ my $has_idn = eval{ require Net::IDN::Encode; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::IDN::Encode; 1 } } ? : 0 47 47
398 # perl -MRegexp::Common=Email::Address -lE 'say $Regexp::Common::RE{Email}{Address}'
399 47 state $email_re = qr/\A(?:(?^u:(?:(?^u:(?>(?^u:(?^u:(?>(?^u:(?>(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|\.|\s*"(?^u:(?^u:[^\\"])|(?^u:\\(?^u:[^\x0A\x0D])))+"\s*))+))|(?>(?^u:(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*"(?^u:(?^u:[^\\"])|(?^u:\\(?^u:[^\x0A\x0D])))*"(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*)))+))?)(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*<(?^u:(?^u:(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*(?^u:(?>[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?:\.[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+)*))(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*"(?^u:(?^u:[^\\"])|(?^u:\\(?^u:[^\x0A\x0D])))*"(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*)))\@(?^u:(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*(?^u:(?>[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?:\.[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+)*))(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*\[(?:\s*(?^u:(?^u:[^\[\]\\])|(?^u:\\(?^u:[^\x0A\x0D]))))*\s*\](?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))))>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*)))|(?^u:(?^u:(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*(?^u:(?>[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?:\.[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+)*))(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*"(?^u:(?^u:[^\\"])|(?^u:\\(?^u:[^\x0A\x0D])))*"(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*)))\@(?^u:(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*(?^u:(?>[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+(?:\.[^\x00-\x1F\x7F()<>\[\]:;@\\,."\s]+)*))(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*))|(?^u:(?>(?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*\[(?:\s*(?^u:(?^u:[^\[\]\\])|(?^u:\\(?^u:[^\x0A\x0D]))))*\s*\](?^u:(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))|(?>\s+))*)))))(?>(?^u:(?>\s*\((?:\s*(?^u:(?^u:(?>[^()\\]+))|(?^u:\\(?^u:[^\x0A\x0D]))|))*\s*\)\s*))*)))\z/;
400  
401 47 my %F;
402  
403 # RFC3339 date-time / date / time
404 $F{'date-time'} = sub
405 2 ▶ {
Subroutine __ANON__: called CC=11 SCAR=34.5
406 2 my( $s ) = @_;
407 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
408  
409 # Preferred path when DateTime::Format::ISO8601 is available
410 2 ▶ if( $has_iso )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_iso) 0 2 2
411 {
412 0 ▶ return( eval{ DateTime::Format::ISO8601->parse_datetime( $s ) ? 1 : 0 } ? 1 : 0 );
Branch: 2 branches
BranchTrueFalseTotal
eval { do { 'DateTime::Format::ISO8601'->parse_datetime($s) ? 1 : 0 } } ? : 0 0 0
'DateTime::Format::ISO8601'->parse_datetime($s) ? : 0 0 0
413 }
414  
415 # YYYY-MM-DDThh:mm:ss[.fraction](Z|±hh:mm)
416 2 ▶ return(0) unless( $s =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\A (\d{4})-(\d{2})-(\d{2}) # date T (\d{2}):(\d{2}):(\d{2}) # time (?:\.\d+)? # optional fraction (?:Z|[+\-]\d{2}:\d{2}) # offset \z/ux 0 2 2
417 (\d{4})-(\d{2})-(\d{2}) # date
418 T
419 (\d{2}):(\d{2}):(\d{2}) # time
420 (?:\.\d+)? # optional fraction
421 (?:Z|[+\-]\d{2}:\d{2}) # offset
422 \z/x );
423  
424 2 my( $y, $m, $d, $H, $M, $S ) = ( $1, $2, $3, $4, $5, $6 );
425  
426 # Time::Piece is a core module (available since perl 5.10): use it first
427 2 ▶ if( $has_tp )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_tp) 2 0 2
428 {
429 # strptime validates calendar correctness (e.g. rejects Feb 30)
430 # We feed it the bare datetime without offset/fraction (already captured above)
431 2 my $bare = sprintf( '%04d-%02d-%02dT%02d:%02d:%02d', $y, $m, $d, $H, $M, $S );
432 2 ▶ return( eval{ Time::Piece->strptime( $bare, '%Y-%m-%dT%H:%M:%S' ); 1 } ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'Time::Piece'->strptime($bare, '%Y-%m-%dT%H:%M:%S'); 1 } } ? : 1 1 2
433 }
434  
435 # Fall back to DateTime if available
436 0 ▶ if( $has_dt )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_dt) 0 0 0
437 {
438 return( eval
439 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'DateTime'->new('year', $y, 'month', $m, 'day', $d, 'hour', $H, 'minute', $M, 'second', $S); 1 } } ? : 0 0 0
440 0 DateTime->new(
441 year => $y,
442 month => $m,
443 day => $d,
444 hour => $H,
445 minute => $M,
446 second => $S
447 );
448 0 1
449 } ? 1 : 0 );
450 }
451  
452 # Last resort: the regex above was already sufficient for structural checks
453 0 return(1);
454 47 };
455  
456 $F{'date'} = sub
457 2 ▶ {
Subroutine __ANON__: called CC=8 SCAR=27.7
458 2 my( $s ) = @_;
459 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
460 2 ▶ return(0) unless( $s =~ /\A(\d{4})-(\d{2})-(\d{2})\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\A(\d{4})-(\d{2})-(\d{2})\z/u 0 2 2
461 2 my( $y, $m, $d ) = ( $1, $2, $3 );
462  
463 # Time::Piece is core - prefer it
464 2 ▶ if( $has_tp )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_tp) 2 0 2
465 {
466 2 ▶ return( eval{ Time::Piece->strptime( $s, '%Y-%m-%d' ); 1 } ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'Time::Piece'->strptime($s, '%Y-%m-%d'); 1 } } ? : 1 1 2
467 }
468  
469 # Fall back to DateTime if available
470 0 ▶ if( $has_dt )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_dt) 0 0 0
471 {
472 0 ▶ return( eval{ DateTime->new( year => $y, month => $m, day => $d ); 1 } ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { 'DateTime'->new('year', $y, 'month', $m, 'day', $d); 1 } } ? : 0 0 0
473 }
474  
475 # Last resort: structural regex already passed above
476 0 return(1);
477 47 };
478  
479 $F{'time'} = sub
480 2 ▶ {
Subroutine __ANON__: called CC=7 SCAR=26.5
481 2 my( $s ) = @_;
482 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
483 2 ▶ if( $has_iso )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_iso) 0 2 2
484 {
485 0 ▶ return eval{ DateTime::Format::ISO8601->parse_datetime( "1970-01-01T$s" ) ? 1 : 0 } ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
eval { do { 'DateTime::Format::ISO8601'->parse_datetime("1970-01-01T$s") ? 1 : 0 } } ? : 0 0 0
'DateTime::Format::ISO8601'->parse_datetime("1970-01-01T$s") ? : 0 0 0
486 }
487 2 ▶ return $s =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A (?:[01]\d|2[0-3]) # HH : (?:[0-5]\d) # MM : (?:[0-5]\d) # SS (?:\.\d+)? # .fraction (?:Z|[+\-](?:[01]\d|2[0-3]):[0-5]\d)? # offset \z/ux ? : 1 1 2
488 (?:[01]\d|2[0-3]) # HH
489 :
490 (?:[0-5]\d) # MM
491 :
492 (?:[0-5]\d) # SS
493 (?:\.\d+)? # .fraction
494 (?:Z|[+\-](?:[01]\d|2[0-3]):[0-5]\d)? # offset
495 \z/x ? 1 : 0;
496 47 };
497  
498 # Duration (RFC 3339 §5.6 / ISO 8601)
499 # Valid: P1Y, P3M, P1Y2M3DT4H5M6S, PT10S, P1W
500 # Invalid: P, PT, P1Y2M3DT (T with nothing after it, or no designator at all)
501 $F{'duration'} = sub
502 2 ▶ {
Subroutine __ANON__: called CC=11 SCAR=32.1
503 2 my( $s ) = @_;
504 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
505  
506 # Week form: PnW (stands alone; no mixing with other designators per ISO 8601)
507 2 ▶ return(1) if( $s =~ /\AP\d+W\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\AP\d+W\z/u 0 2 2
508  
509 # Full form: at least one date *or* time component must be present
510 # The T separator must be followed by at least one time designator
511 2 ▶ return( $s =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A P (?:(\d+)Y)? (?:(\d+)M)? (?:(\d+)D)? (?:T (?=\d) # T must be followed by at least one digit (?:(\d+)H)? (?:(\d+)M)? (?:(\d+(?:\.\d+)?)S)? )? \z/ux && (defined $1 || defined $2 || defined $3 || defined $4 || defined $5 || defined $6) ? : 1 1 2
Condition38%7/18
$s =~ /\A P (?:(\d+)Y)? (?:(\d+)M)? (?:(\d+)D)? (?:T (?=\d) # T must be followed by at least one digit (?:(\d+)H)? (?:(\d+)M)? (?:(\d+(?:\.\d+)?)S)? )? \z/ux and defined $1 || defined $2 || defined $3 || defined $4 || defined $5 || defined $6
!ll&&!rl&&r
101
defined $1 or defined $2 or defined $3 or defined $4 or defined $5 or defined $6
l!l&&r!l&&!r
100
defined $1 or defined $2 or defined $3 or defined $4 or defined $5
l!l&&r!l&&!r
100
defined $1 or defined $2 or defined $3 or defined $4
l!l&&r!l&&!r
100
defined $1 or defined $2 or defined $3
l!l&&r!l&&!r
100
defined $1 or defined $2
l!l&&r!l&&!r
100
MC/DC14%1/7
$s =~ /\A P (?:(\d+)Y)? (?:(\d+)M)? (?:(\d+)D)? (?:T (?=\d) # T must be followed by at least one digit (?:(\d+)H)? (?:(\d+)M)? (?:(\d+(?:\.\d+)?)S)? )? \z/ux and defined $1 || defined $2 || defined $3 || defined $4 || defined $5 || defined $6
$s =~ /\A P (?:(\d+)Y)? (?:(\d+)M)? (?:(\d+)D)? (?:T (?=\d) # T must be followed by at least one digit (?:(\d+)H)? (?:(\d+)M)? (?:(\d+(?:\.\d+)?)S)? )? \z/ux defined $1 defined $2 defined $3 defined $4 defined $5 defined $6
Truth table25%2/8
A and B or C or D or E or F or G
A= $s =~ /\A P (?:(\d+)Y)? (?:(\d+)M)? (?:(\d+)D)? (?:T (?=\d) # T must be followed by at least one digit (?:(\d+)H)? (?:(\d+)M)? (?:(\d+(?:\.\d+)?)S)? )? \z/uxB= defined $1C= defined $2D= defined $3E= defined $4F= defined $5G= defined $6
ABCDEFGresult
0XXXXXX0
10000000
11XXXXX1
101XXXX1
1001XXX1
10001XX1
100001X1
10000011
512 P
513 (?:(\d+)Y)?
514 (?:(\d+)M)?
515 (?:(\d+)D)?
516 (?:T
517 (?=\d) # T must be followed by at least one digit
518 (?:(\d+)H)?
519 (?:(\d+)M)?
520 (?:(\d+(?:\.\d+)?)S)?
521 )?
522 \z/x && ( defined($1) || defined($2) || defined($3) || defined($4) || defined($5) || defined($6) )
523 ? 1 : 0 );
524 47 };
525  
526 # Email / IDN email
527 # Plain email (ASCII) - unchanged
528 $F{'email'} = sub
529 13 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
530 13 my( $s ) = @_;
531 13 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 13 13
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
532 13 ▶ return( $s =~ $email_re ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /$email_re/u ? : 12 1 13
533 47 };
534  
535 # IDN email: punycode the domain, validate with same regex
536 $F{'idn-email'} = sub
537 1 ▶ {
Subroutine __ANON__: called CC=10 SCAR=34.5
538 1 my( $s ) = @_;
539 1 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 1 1
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
001
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
540 1 ▶ return(0) unless( $s =~ /\A(.+)\@(.+)\z/s ); # keep local-part as-is (EAI allows UTF-8)
Branch: 1 branch
BranchTrueFalseTotal
unless $s =~ /\A(.+)\@(.+)\z/su 0 1 1
541 1 my( $local, $domain ) = ( $1, $2 );
542  
543 1 ▶ if( $has_idn )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_idn) 0 1 1
544 {
545 0 local $@;
546 0 my $ascii = eval{ Net::IDN::Encode::domain_to_ascii( $domain ) };
547 0 ▶ return(0) unless( defined( $ascii ) && length( $ascii ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ascii and length $ascii 0 0 0
Condition0%0/3
defined $ascii and length $ascii
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ascii and length $ascii
defined $ascii length $ascii
Truth table0%0/3
A and B
A= defined $asciiB= length $ascii
ABresult
0X0
100
111
548  
549 0 my $candidate = "$local\@$ascii";
550 0 ▶ return( $candidate =~ $email_re ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$candidate =~ /$email_re/u ? : 0 0 0
551 }
552  
553 # Fallback: if the domain already *looks* ASCII, validate directly
554 1 ▶ if( $domain =~ /\A[[:ascii:]]+\z/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /\A[[:ascii:]]+\z/u) 1 0 1
555 {
556 1 my $candidate = "$local\@$domain";
557 1 ▶ return( $candidate =~ $email_re ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$candidate =~ /$email_re/u ? : 1 0 1
558 }
559  
560 # Without IDN module, fall back to permissive Unicode domain check + ASCII regex
561 0 return(0);
562 47 };
563  
564 # Hostnames
565 $F{'hostname'} = sub
566 2 ▶ {
Subroutine __ANON__: called CC=8 SCAR=23.4
567 2 my( $s ) = @_;
568 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
569 2 ▶ return(0) if( length( $s ) > 253 );
Branch: 1 branch
BranchTrueFalseTotal
if length $s > 253 0 2 2
570 2 for my $label ( split( /\./, $s ) )
571 {
572 3 ▶ return(0) unless( length( $label ) >= 1 && length( $label ) <= 63 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $label >= 1 and length $label <= 63 0 3 3
Condition33%1/3
length $label >= 1 and length $label <= 63
!ll&&!rl&&r
003
MC/DC0%0/2
length $label >= 1 and length $label <= 63
length $label >= 1 length $label <= 63
Truth table33%1/3
A and B
A= length $label >= 1B= length $label <= 63
ABresult
0X0
100
111
573 3 ▶ return(0) unless( $label =~ /\A[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /\A[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\z/u 1 2 3
574 }
575 1 return(1);
576 47 };
577  
578 $F{'idn-hostname'} = sub
579 1 ▶ {
Subroutine __ANON__: called CC=12 SCAR=37.0
580 1 my( $s ) = @_;
581 1 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 1 1
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
001
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
582 1 ▶ if( $has_idn )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_idn) 0 1 1
583 {
584 0 local $@;
585 0 my $ascii = eval{ Net::IDN::Encode::domain_to_ascii( $s ) };
586 0 ▶ return(0) unless( defined( $ascii ) && length( $ascii ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ascii and length $ascii 0 0 0
Condition0%0/3
defined $ascii and length $ascii
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ascii and length $ascii
defined $ascii length $ascii
Truth table0%0/3
A and B
A= defined $asciiB= length $ascii
ABresult
0X0
100
111
587 0 ▶ return( $F{'hostname'}->( $ascii ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$F{'hostname'}->($ascii) ? : 0 0 0
588 }
589  
590 # Fallback: permissive Unicode label check (as you had), then ASCII hostname rule
591 1 ▶ return(0) if( length( $s ) > 253 );
Branch: 1 branch
BranchTrueFalseTotal
if length $s > 253 0 1 1
592 1 for my $label ( split( /\./, $s ) )
593 {
594 2 ▶ return(0) unless( length( $label ) >= 1 && length( $label ) <= 63 );
Branch: 1 branch
BranchTrueFalseTotal
unless length $label >= 1 and length $label <= 63 0 2 2
Condition33%1/3
length $label >= 1 and length $label <= 63
!ll&&!rl&&r
002
MC/DC0%0/2
length $label >= 1 and length $label <= 63
length $label >= 1 length $label <= 63
Truth table33%1/3
A and B
A= length $label >= 1B= length $label <= 63
ABresult
0X0
100
111
595 2 ▶ return(0) unless( $label =~ /\A[[:alnum:]\pL\pN](?:[[:alnum:]\pL\pN\-]*[[:alnum:]\pL\pN])?\z/u );
Branch: 1 branch
BranchTrueFalseTotal
unless $label =~ /\A[[:alnum:]\pL\pN](?:[[:alnum:]\pL\pN\-]*[[:alnum:]\pL\pN])?\z/u 0 2 2
596 }
597 1 return(1);
598 47 };
599  
600 # IP addresses
601 $F{'ipv4'} = sub
602 2 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
603 2 my( $s ) = @_;
604 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
605 2 ▶ return $s =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\. (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\. (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\. (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) \z/ux ? : 1 1 2
606 (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.
607 (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.
608 (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.
609 (25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
610 \z/x ? 1 : 0;
611 47 };
612  
613 $F{'ipv6'} = sub
614 2 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
615 2 my( $s ) = @_;
616 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
617 2 ▶ return $s =~ /\A
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A (?: (?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4} | (?:[0-9A-Fa-f]{1,4}:){1,7}: | (?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4} | (?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2} | (?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3} | (?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4} | (?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5} | [0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6}) | :(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:) | (?:[0-9A-Fa-f]{1,4}:){6} (?:\d{1,3}\.){3}\d{1,3} | ::(?:[0-9A-Fa-f]{1,4}:){0,5} (?:\d{1,3}\.){3}\d{1,3} | (?:[0-9A-Fa-f]{1,4}:){1,5}: (?:\d{1,3}\.){3}\d{1,3} ) \z/ux ? : 1 1 2
618 (?: (?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}
619 | (?:[0-9A-Fa-f]{1,4}:){1,7}:
620 | (?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}
621 | (?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}
622 | (?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}
623 | (?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}
624 | (?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}
625 | [0-9A-Fa-f]{1,4}:(?:(?::[0-9A-Fa-f]{1,4}){1,6})
626 | :(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:)
627 | (?:[0-9A-Fa-f]{1,4}:){6}
628 (?:\d{1,3}\.){3}\d{1,3}
629 | ::(?:[0-9A-Fa-f]{1,4}:){0,5}
630 (?:\d{1,3}\.){3}\d{1,3}
631 | (?:[0-9A-Fa-f]{1,4}:){1,5}:
632 (?:\d{1,3}\.){3}\d{1,3}
633 )
634 \z/x ? 1 : 0;
635 47 };
636  
637 # URI/IRI
638 $F{'uri'} = sub
639 2 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
640 2 my( $s ) = @_;
641 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
642 2 ▶ return( $s =~ /\A[A-Za-z][A-Za-z0-9+\-.]*:[^\s]+\z/ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A[A-Za-z][A-Za-z0-9+\-.]*:[^\s]+\z/u ? : 1 1 2
643 47 };
644  
645 $F{'uri-reference'} = sub
646 1 ▶ {
Subroutine __ANON__: called CC=4 SCAR=16.1
647 1 my( $s ) = @_;
648 1 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 1 1
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
001
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
649 1 ▶ return( $s =~ /\A(?:[A-Za-z][A-Za-z0-9+\-.]*:)?[^\s]+\z/ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A(?:[A-Za-z][A-Za-z0-9+\-.]*:)?[^\s]+\z/u ? : 1 0 1
650 47 };
651  
652 $F{'iri'} = sub
653 1 ▶ {
Subroutine __ANON__: called CC=4 SCAR=16.1
654 1 my( $s ) = @_;
655 1 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 1 1
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
001
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
656 # Must have a scheme (letters followed by colon) per RFC 3987 §2.2
657 # Scheme: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
658 # After scheme: at least one non-whitespace character
659 1 ▶ return( $s =~ /\A[A-Za-z][A-Za-z0-9+\-.]*:[^\s]+\z/u ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A[A-Za-z][A-Za-z0-9+\-.]*:[^\s]+\z/u ? : 1 0 1
660 47 };
661  
662 # UUID
663 $F{'uuid'} = sub
664 3 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.9
665 3 my( $s ) = @_;
666 3 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 3 3
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
003
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
667 3 ▶ return( $s =~ /\A[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12}\z/ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\A[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12}\z/u ? : 1 2 3
668 47 };
669  
670 # JSON Pointer / Relative JSON Pointer
671 $F{'json-pointer'} = sub
672 3 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.7
673 3 my( $s ) = @_;
674 3 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 3 3
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
003
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
675 3 ▶ return(1) if( $s eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 1 2 3
676 2 ▶ return( $s =~ m{\A/(?:[^~/]|~[01])*(?:/(?:[^~/]|~[01])*)*\z} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ m[\A/(?:[^~/]|~[01])*(?:/(?:[^~/]|~[01])*)*\z]u ? : 1 1 2
677 47 };
678  
679 $F{'relative-json-pointer'} = sub
680 3 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.7
681 3 my( $s ) = @_;
682 3 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 3 3
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
003
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
683 3 ▶ return(1) if( $s =~ /\A0\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\A0\z/u 1 2 3
684 2 ▶ return( $s =~ m,\A[1-9]\d*(?:#|(?:/(?:[^~/]|~[01])*)*)\z, ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s =~ m[\A[1-9]\d*(?:#|(?:/(?:[^~/]|~[01])*)*)\z]u ? : 1 1 2
685 47 };
686  
687 # Regex
688 $F{'regex'} = sub
689 2 ▶ {
Subroutine __ANON__: called CC=4 SCAR=14.3
690 2 my( $s ) = @_;
691 2 ▶ return(0) unless( defined( $s ) && !ref( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and not ref $s 0 2 2
Condition33%1/3
defined $s and not ref $s
!ll&&!rl&&r
002
MC/DC0%0/2
defined $s and not ref $s
defined $s not ref $s
Truth table33%1/3
A and not(B)
A= defined $sB= not ref $s
ABresult
0X0
100
111
692 2 local $@;
693 2 ▶ return( eval{ "" =~ /$s/; 1 } ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
eval { do { '' =~ /$s/u; 1 } } ? : 1 1 2
694 47 };
695  
696 47 ▶ while( my( $k, $v ) = each( %F ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %F) 799 47 846
697 {
698 799 ▶ $self->{formats}->{ $k } = $v unless( exists( $self->{formats}->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"formats"}{$k} 799 0 799
699 }
700  
701 47 return( $self );
702 }
703  
704 sub register_content_decoder
705 1 ▶{
Subroutine register_content_decoder: called CC=2 SCAR=7.4
Pod documented
706 1 my( $self, $name, $cb ) = @_;
707 1 ▶ if( ref( $cb ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 1 0 1
708 {
709 1 $self->{content_decoders}->{ lc( "$name" ) } = $cb;
710 }
711 else
712 {
713 0 die( "content decoder must be a code reference" );
714 }
715 1 return( $self );
716 }
717  
718 sub register_format
719 0 ▶{
Subroutine register_format: not called CC=4 SCAR=30.0
Pod documented
720 0 my( $self, $name, $code ) = @_;
721 0 ▶ die( "format name required" ) unless( defined( $name ) && length( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
722 0 ▶ die( "format validator must be a coderef" ) unless( ref( $code ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
723 0 $self->{formats}->{ $name } = $code;
724 0 return( $self );
725 }
726  
727 sub register_media_validator
728 19 ▶{
Subroutine register_media_validator: called CC=2 SCAR=7.4
Pod documented
729 19 my( $self, $type, $cb ) = @_;
730 19 ▶ if( ref( $cb ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 19 0 19
731 {
732 19 $self->{media_validators}->{ lc( "$type" ) } = $cb;
733 }
734 else
735 {
736 0 die( "media validator must be a code reference" );
737 }
738 19 return( $self );
739 }
740  
741 sub set_comment_handler
742 4 ▶{
Subroutine set_comment_handler: called CC=5 SCAR=16.2
Pod documented
743 4 my( $self, $code ) = @_;
744 4 ▶ if( @_ > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 4 0 4
745 {
746 4 ▶ if( defined( $code ) && ref( $code ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and ref $code ne "CODE") 1 3 4
Condition100%3/3
defined $code and ref $code ne "CODE"
!ll&&!rl&&r
121
MC/DC100%2/2
defined $code and ref $code ne "CODE"
defined $code ref $code ne "CODE"
Truth table100%3/3
A and B
A= defined $codeB= ref $code ne "CODE"
ABresult
0X0
100
111
747 {
748 1 ▶ warn( "Warning only: the handler provided is not a code reference." ) if( warnings::enabled() );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 1 0 1
749 1 return( $self );
750 }
751 3 $self->{comment_handler} = $code;
752 }
753 3 return( $self );
754 }
755  
756 sub set_resolver
757 20 ▶{
Subroutine set_resolver: called CC=5 SCAR=20.9
Pod documented
758 20 my( $self, $code ) = @_;
759 20 ▶ if( @_ > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 20 0 20
760 {
761 20 ▶ if( defined( $code ) && ref( $code ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and ref $code ne "CODE") 0 20 20
Condition33%1/3
defined $code and ref $code ne "CODE"
!ll&&!rl&&r
0200
MC/DC0%0/2
defined $code and ref $code ne "CODE"
defined $code ref $code ne "CODE"
Truth table33%1/3
A and B
A= defined $codeB= ref $code ne "CODE"
ABresult
0X0
100
111
762 {
763 0 ▶ warn( "Warning only: the handler provided is not a code reference." ) if( warnings::enabled() );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 0 0
764 0 return( $self );
765 }
766 20 $self->{resolver} = $code;
767 }
768 20 return( $self );
769 }
770  
771 sub set_vocabulary_support
772 0 ▶{
Subroutine set_vocabulary_support: not called CC=2 SCAR=17.9
Pod documented
773 0 my( $self, $h ) = @_;
774 0 ▶ $self->{vocab_support} = { %{ $h || {} } };
Condition0%0/2
$h || {}
l!l
00
MC/DC0%0/1
$h || {}
$h
Truth table0%0/2
A
A= $h
Aresult
11
00
775 0 $self->{vocab_checked} = 0;
776 0 return( $self );
777 }
778  
779 # This is a mutator only method
780 # $js->trace -> enables it
781 # $js->trace(1) -> enables it
782 # $js->trace(0) -> disables it
783 # Always returns the object
784 # See is_trace_on for the accessor method
785 sub trace
786 1 ▶{
Subroutine trace: called CC=2 SCAR=7.0
Pod documented
787 1 my( $self, $bool ) = @_;
788 1 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 1 0 1
789 1 $self->{trace_on} = $on;
790 1 return( $self );
791 }
792  
793 sub trace_limit
794 0 ▶{
Subroutine trace_limit: not called CC=2 SCAR=17.9
Pod documented
795 0 my( $self, $n ) = @_;
796 0 ▶ $self->{trace_limit} = 0 + ( $n || 0 );
Condition0%0/2
$n || 0
l!l
00
MC/DC0%0/1
$n || 0
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
797 0 return( $self );
798 }
799  
800 sub trace_sample
801 0 ▶{
Subroutine trace_sample: not called CC=2 SCAR=17.9
Pod documented
802 0 my( $self, $pct ) = @_;
803 0 ▶ $self->{trace_sample} = 0 + ( $pct || 0 );
Condition0%0/2
$pct || 0
l!l
00
MC/DC0%0/1
$pct || 0
$pct
Truth table0%0/2
A
A= $pct
Aresult
11
00
804 0 return( $self );
805 }
806  
807 sub unique_keys
808 9 ▶{
Subroutine unique_keys: called CC=2 SCAR=7.0
Pod documented
809 9 my( $self, $bool ) = @_;
810 9 ▶ my $on = defined( $bool ) ? $bool : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $bool ? : 9 0 9
811 9 $self->{unique_keys} = $on;
812 9 return( $self );
813 }
814  
815 sub validate
816 678 ▶{
Subroutine validate: called CC=30 SCAR=45.6
Pod documented
817 678 my $self = shift( @_ );
818 678 my $data = shift( @_ );
819 678 my $opts = $self->_get_args_as_hash( @_ );
820  
821 678 $self->{errors} = [];
822 678 $self->{last_error} = '';
823  
824 # Ensure we have the compiled root (indexing/anchors) even in lazy mode
825 678 ▶ $self->{compiled} = _compile_root( $self->{schema} ) unless( $self->{compiled} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"compiled"} 316 362 678
826  
827 # One-time $vocabulary check (Draft 2020-12)
828 678 ▶ if( !$self->{vocab_checked} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"vocab_checked"}) 0 678 678
829 {
830 0 my $root = $self->{schema};
831 0 ▶ if( ref( $root ) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH") 0 0 0
Condition0%0/3
ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH"
ref $root eq "HASH" ref $root->{"\$vocabulary"} eq "HASH"
Truth table0%0/3
A and B
A= ref $root eq "HASH"B= ref $root->{"\$vocabulary"} eq "HASH"
ABresult
0X0
100
111
832 ref( $root->{'$vocabulary'} ) eq 'HASH' )
833 {
834 0 my $decl = $root->{'$vocabulary'}; # { uri => JSON::true|false, ... }
835 0 ▶ my $support = $self->{vocab_support} || {};
Condition0%0/2
$self->{'vocab_support'} || {}
l!l
00
MC/DC0%0/1
$self->{'vocab_support'} || {}
$self->{'vocab_support'}
Truth table0%0/2
A
A= $self->{'vocab_support'}
Aresult
11
00
836 0 for my $uri ( keys( %$decl ) )
837 {
838 0 ▶ next unless( $decl->{ $uri } ); # only enforce for required=true
Branch: 1 branch
BranchTrueFalseTotal
unless $decl->{$uri} 0 0 0
839 0 ▶ next if( $support->{ $uri } ); # caller says it's supported
Branch: 1 branch
BranchTrueFalseTotal
if $support->{$uri} 0 0 0
840 0 ▶ unless( $self->{ignore_req_vocab} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ignore_req_vocab"}) 0 0 0
841 {
842 0 die( "Required vocabulary not supported: $uri" );
843 }
844 }
845 }
846 0 $self->{vocab_checked} = 1;
847 }
848  
849 # Because Perl scalar are not JSON scalar, we force the Perl structure into strict JSON types, eliminating all Perl duality and guaranteeing predictable validation semantics.
850 678 ▶ if( $self->{normalize_instance} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"normalize_instance"}) 582 96 678
851 {
852 582 $data = _clone( $data );
853 }
854  
855 # Optional pre-validation pruning of unknown properties / nested objects.
856 # This only happens if explicitly enabled via prune_unknown => 1.
857 678 ▶ if( $self->{prune_unknown} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prune_unknown"}) 1 677 678
858 {
859 1 $data = $self->_prune_with_schema( $self->{schema}, $data );
860 }
861  
862 my $ctx =
863 {
864 root => $self->{compiled},
865 resource_index => _copy_resource_index( $self->{compiled}->{resource_index} ),
866 external_fn_index => {},
867 instance_root => $data,
868 resolver => $self->{resolver},
869 formats => $self->{formats},
870 errors => $self->{errors},
871 max_errors => ( ( defined( $opts->{max_errors} ) && $opts->{max_errors} =~ /^\d+$/ ) ? $opts->{max_errors} : $self->{max_errors} ),
872 error_count => 0,
873  
874 # paths / recursion
875 ptr_stack => ['#'],
876 id_stack => [ $self->{compiled}->{base} ],
877 dyn_stack => [ {} ], # dynamicAnchor scope frames
878 visited => {}, # "resource|schema_ptr|inst_addr" => 1
879  
880 # annotation (for unevaluated*)
881 ann_mode => 1,
882 compile_on => ( defined( $opts->{compile_on} ) ? ( $opts->{compile_on} ? 1 : 0 ) : ( $self->{compile_on} ? 1 : 0 ) ),
883  
884 # trace
885 trace_on => ( defined( $opts->{trace_on} ) ? ( $opts->{trace_on} ? 1 : 0 ) : ( $self->{trace_on} ? 1 : 0 ) ),
886 trace_sample => $self->{trace_sample} || 0,
887 trace_limit => ( defined( $opts->{trace_limit} ) && $opts->{trace_limit} =~ /^\d+$/ ) ? $opts->{trace_limit} : ( $self->{trace_limit} || 0 ),
888 trace => [],
889  
890 # content assertion & helpers
891 content_assert => ( defined( $opts->{content_assert} ) ? ( $opts->{content_assert} ? 1 : 0 ) : ( $self->{content_assert} ? 1 : 0 ) ),
892 media_validators => $self->{media_validators},
893 content_decoders => $self->{content_decoders},
894  
895 comment_handler => $self->{comment_handler},
896  
897 # extensions
898 unique_keys => $self->{unique_keys},
899 extensions => $self->{extensions},
900 678 ▶ };
Branch: 11 branches
BranchTrueFalseTotal
defined $opts->{'max_errors'} && $opts->{'max_errors'} =~ /^\d+$/u ? : 5 673 678
defined $opts->{'compile_on'} ? : 0 678 678
$opts->{'compile_on'} ? : 0 0 0
$self->{'compile_on'} ? : 243 435 678
defined $opts->{'trace_on'} ? : 0 678 678
$opts->{'trace_on'} ? : 0 0 0
$self->{'trace_on'} ? : 6 672 678
defined $opts->{'trace_limit'} && $opts->{'trace_limit'} =~ /^\d+$/u ? : 0 678 678
defined $opts->{'content_assert'} ? : 0 678 678
$opts->{'content_assert'} ? : 0 0 0
$self->{'content_assert'} ? : 28 650 678
Condition70%7/10
defined $opts->{"max_errors"} and $opts->{"max_errors"} =~ /^\d+$/u
!ll&&!rl&&r
67305
$self->{'trace_sample'} || 0
l!l
2676
defined $opts->{"trace_limit"} and $opts->{"trace_limit"} =~ /^\d+$/u
!ll&&!rl&&r
67800
$self->{"trace_limit"} or 0
l!l
7671
MC/DC50%1/2
defined $opts->{"max_errors"} and $opts->{"max_errors"} =~ /^\d+$/u
defined $opts->{"max_errors"} $opts->{"max_errors"} =~ /^\d+$/u
MC/DC100%1/1
$self->{'trace_sample'} || 0
$self->{'trace_sample'}
MC/DC0%0/2
defined $opts->{"trace_limit"} and $opts->{"trace_limit"} =~ /^\d+$/u
defined $opts->{"trace_limit"} $opts->{"trace_limit"} =~ /^\d+$/u
MC/DC100%1/1
$self->{"trace_limit"} or 0
$self->{"trace_limit"}
Truth table66%2/3
A and B
A= defined $opts->{"max_errors"}B= $opts->{"max_errors"} =~ /^\d+$/u
ABresult
0X0
100
111
Truth table100%2/2
A
A= $self->{'trace_sample'}
Aresult
11
00
Truth table33%1/3
A and B
A= defined $opts->{"trace_limit"}B= $opts->{"trace_limit"} =~ /^\d+$/u
ABresult
0X0
100
111
Truth table100%2/2
A
A= $self->{"trace_limit"}
Aresult
11
00
901  
902 # Guarantee at least one trace entry when trace is enabled
903 678 ▶ if( $ctx->{trace_on} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"trace_on"}) 6 672 678
904 {
905 6 push( @{$ctx->{trace}},
906 {
907 schema_ptr => '#',
908 keyword => 'validate',
909 inst_path => '#',
910 outcome => 'start',
911 note => 'start',
912 });
913 }
914  
915 678 my $res = _v( $ctx, '#', $self->{compiled}->{schema}, $data );
916  
917 678 $self->{last_trace} = $ctx->{trace};
918  
919 678 ▶ if( !$res->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->{"ok"}) 360 318 678
920 {
921 # $self->{last_error} = _first_error_text( $self->{errors} );
922 360 ▶ $self->{last_error} = scalar( @{$self->{errors}} ) ? $self->{errors}->[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$self->{'errors'};} ? : 360 0 360
923 360 return(0);
924 }
925 318 return(1);
926 }
927  
928 sub _apply_dynamic_ref
929 29 ▶{
Subroutine _apply_dynamic_ref: called CC=1 SCAR=0.0
930 29 my( $ctx, $schema_ptr, $ref, $inst ) = @_;
931 29 return( _apply_ref( $ctx, $schema_ptr, $ref, $inst, '$dynamicRef' ) );
932 }
933  
934 # $ref and $dynamicRef
935 sub _apply_ref
936 207 ▶{
Subroutine _apply_ref: called CC=34 SCAR=35.8
937 207 my( $ctx, $schema_ptr, $ref, $inst, $keyword ) = @_;
938 207 ▶ $keyword ||= '$ref';
Condition100%2/2
$keyword ||= "\$ref"
l!l
29178
MC/DC100%1/1
$keyword ||= "\$ref"
$keyword
Truth table100%2/2
A
A= $keyword
Aresult
11
00
939 207 my $abs = _resolve_uri( $ctx->{id_stack}->[-1], $ref );
940 207 my( $uri, $fragment ) = split( /#/, $abs, 2 );
941 207 ▶ $uri = '' unless( defined( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 207 207
942 207 my $index = $ctx->{resource_index};
943  
944 # Resolve external resources once per validation, without retaining resolver answers across calls.
945 207 ▶ if( !exists( $index->{resources}->{ $uri } ) && $ctx->{resolver} )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $index->{"resources"}{$uri} and $ctx->{"resolver"}) 41 166 207
Condition66%2/3
not exists $index->{"resources"}{$uri} and $ctx->{"resolver"}
!ll&&!rl&&r
166041
MC/DC50%1/2
not exists $index->{"resources"}{$uri} and $ctx->{"resolver"}
not exists $index->{"resources"}{$uri} $ctx->{"resolver"}
Truth table66%2/3
not(A) and B
A= not exists $index->{"resources"}{$uri}B= $ctx->{"resolver"}
ABresult
0X0
100
111
946 {
947 41 local $@;
948 41 my $ext = eval{ $ctx->{resolver}->( $abs ) };
949 41 ▶ return( _err_res( $ctx, $schema_ptr, "resolver failed for $keyword: $abs", $keyword ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ext eq "HASH" or &JSON::is_bool($ext) 0 41 41
Condition66%2/3
ref $ext eq "HASH" or &JSON::is_bool($ext)
l!l&&r!l&&!r
3920
MC/DC0%0/2
ref $ext eq "HASH" or &JSON::is_bool($ext)
ref $ext eq "HASH" &JSON::is_bool($ext)
Truth table66%2/3
A or B
A= ref $ext eq "HASH"B= &JSON::is_bool($ext)
ABresult
1X1
011
000
950 unless( ref( $ext ) eq 'HASH' || JSON::is_bool( $ext ) );
951 41 $ext = eval{ _clone( $ext ) };
952 41 ▶ return( _err_res( $ctx, $schema_ptr, "invalid schema resource for $keyword: $abs", $keyword ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 39 41
953 # A retrieval URI may identify an already indexed canonical resource. Reuse it only if the schema is identical.
954 39 my $canonical = $uri;
955 39 ▶ if( ref( $ext ) eq 'HASH' && defined( $ext->{'$id'} ) && !ref( $ext->{'$id'} ) && length( $ext->{'$id'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ext eq "HASH" and defined $ext->{"\$id"} and not ref $ext->{"\$id"} and length $ext->{"\$id"}) 21 18 39
Condition77%7/9
ref $ext eq "HASH" and defined $ext->{"\$id"} and not ref $ext->{"\$id"} and length $ext->{"\$id"}
!ll&&!rl&&r
18021
ref $ext eq "HASH" and defined $ext->{"\$id"} and not ref $ext->{"\$id"}
!ll&&!rl&&r
18021
ref $ext eq "HASH" and defined $ext->{"\$id"}
!ll&&!rl&&r
21621
MC/DC50%2/4
ref $ext eq "HASH" and defined $ext->{"\$id"} and not ref $ext->{"\$id"} and length $ext->{"\$id"}
ref $ext eq "HASH" defined $ext->{"\$id"} not ref $ext->{"\$id"} length $ext->{"\$id"}
Truth table60%3/5
A and B and not(C) and D
A= ref $ext eq "HASH"B= defined $ext->{"\$id"}C= not ref $ext->{"\$id"}D= length $ext->{"\$id"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
956 {
957 21 $canonical = _resolve_uri( $uri, $ext->{'$id'} );
958 21 $canonical =~ s/#$//;
959 }
960 39 ▶ if( my $existing = $index->{resources}->{ $canonical } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing = $index->{'resources'}{$canonical}) { } 4 35 39
961 {
962 return( _err_res( $ctx, $schema_ptr, "conflicting schema resource for $keyword: $abs", $keyword ) )
963 4 ▶ unless( _canon( $existing->{schema} ) eq _canon( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless _canon($existing->{"schema"}) eq _canon($ext) 2 2 4
964 2 $index->{resources}->{ $uri } = $existing;
965 }
966 else
967 {
968 35 my $staged = _copy_resource_index( $index );
969 35 my $base = eval{ _index_schema_202012( $ext, $uri, '#', $staged, 1 ) };
970 35 ▶ return( _err_res( $ctx, $schema_ptr, "invalid schema resource for $keyword: $abs", $keyword ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 35 35
971 # The retrieval URI cannot also identify a different embedded resource in the returned document.
972 return( _err_res( $ctx, $schema_ptr, "conflicting schema resource for $keyword: $abs", $keyword ) )
973 35 ▶ if( $uri ne $base && exists( $staged->{resources}->{ $uri } ) );
Branch: 1 branch
BranchTrueFalseTotal
if $uri ne $base and exists $staged->{"resources"}{$uri} 2 33 35
Condition100%3/3
$uri ne $base and exists $staged->{"resources"}{$uri}
!ll&&!rl&&r
23102
MC/DC100%2/2
$uri ne $base and exists $staged->{"resources"}{$uri}
$uri ne $base exists $staged->{"resources"}{$uri}
Truth table100%3/3
A and B
A= $uri ne $baseB= exists $staged->{"resources"}{$uri}
ABresult
0X0
100
111
974 33 $staged->{resources}->{ $uri } = $staged->{resources}->{ $base };
975 33 %$index = %$staged;
976 }
977 }
978 201 my $resource = $index->{resources}->{ $uri };
979 201 ▶ return( _err_res( $ctx, $schema_ptr, "unresolved $keyword: $ref (abs: $abs)", $keyword ) ) unless( $resource );
Branch: 1 branch
BranchTrueFalseTotal
unless $resource 0 201 201
980 201 my( $node, $ptr );
981 201 ▶ $fragment = '' unless( defined( $fragment ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment 38 163 201
982 # URI fragments are UTF-8 percent-encoded JSON Pointers or plain-name anchors.
983 201 my $decoded = eval{ _decode_fragment( $fragment ) };
984 201 ▶ return( _err_res( $ctx, $schema_ptr, "invalid fragment in $keyword: $abs", $keyword ) ) unless( defined( $decoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoded 0 201 201
985 201 $fragment = $decoded;
986 201 ▶ if( $fragment eq '' )
Branch: 2 branches
BranchTrueFalseTotal
if ($fragment eq '') { } 64 137 201
elsif ($fragment =~ m[^/]u) { } 101 36 137
987 {
988 64 ( $node, $ptr ) = @$resource{qw( schema pointer )};
989 }
990 elsif( $fragment =~ m{^/} )
991 {
992 101 $node = _jsv_resolve_internal_ref( $resource->{schema}, '#' . $fragment );
993 101 $ptr = $resource->{pointer} . $fragment;
994 }
995 else
996 {
997 36 my $entry = $resource->{anchors}->{ $fragment };
998 36 ▶ if( $entry )
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 36 0 36
999 {
1000 # Only a dynamic anchor at the statically resolved target enables dynamic scope lookup.
1001 36 ▶ if( $keyword eq '$dynamicRef' && $entry->{dynamic} )
Branch: 1 branch
BranchTrueFalseTotal
if ($keyword eq "\$dynamicRef" and $entry->{"dynamic"}) 17 19 36
Condition66%2/3
$keyword eq "\$dynamicRef" and $entry->{"dynamic"}
!ll&&!rl&&r
19017
MC/DC50%1/2
$keyword eq "\$dynamicRef" and $entry->{"dynamic"}
$keyword eq "\$dynamicRef" $entry->{"dynamic"}
Truth table66%2/3
A and B
A= $keyword eq "\$dynamicRef"B= $entry->{"dynamic"}
ABresult
0X0
100
111
1002 {
1003 17 for my $scope ( @{$ctx->{id_stack}} )
1004 {
1005 17 my $candidate = $index->{resources}->{ $scope }->{anchors}->{ $fragment };
1006 17 ▶ next unless( $candidate && $candidate->{dynamic} );
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate and $candidate->{"dynamic"} 0 17 17
Condition33%1/3
$candidate and $candidate->{"dynamic"}
!ll&&!rl&&r
0017
MC/DC0%0/2
$candidate and $candidate->{"dynamic"}
$candidate $candidate->{"dynamic"}
Truth table33%1/3
A and B
A= $candidateB= $candidate->{"dynamic"}
ABresult
0X0
100
111
1007 17 $entry = $candidate;
1008 17 last;
1009 }
1010 }
1011 36 ( $node, $ptr ) = @$entry{qw( schema pointer )};
1012 }
1013 }
1014 201 ▶ unless( ref( $node ) eq 'HASH' || JSON::is_bool( $node ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node eq "HASH" or &JSON::is_bool($node)) 5 196 201
Condition100%3/3
ref $node eq "HASH" or &JSON::is_bool($node)
l!l&&r!l&&!r
182145
MC/DC100%2/2
ref $node eq "HASH" or &JSON::is_bool($node)
ref $node eq "HASH" &JSON::is_bool($node)
Truth table100%3/3
A or B
A= ref $node eq "HASH"B= &JSON::is_bool($node)
ABresult
1X1
011
000
1015 {
1016 5 ▶ my $msg = $fragment =~ m{^/} ? "unresolved JSON Pointer fragment in $keyword: $abs" : "unresolved $keyword: $ref (abs: $abs)";
Branch: 1 branch
BranchTrueFalseTotal
$fragment =~ m[^/]u ? : 5 0 5
1017 5 return( _err_res( $ctx, $schema_ptr, $msg, $keyword ) );
1018 }
1019 # Explicit pointers may address legacy schema containers outside standard vocabulary locations.
1020 196 my $base = $resource->{base};
1021 196 ▶ if( ref( $node ) eq 'HASH' && !exists( $index->{nodes}->{ refaddr( $node ) } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "HASH" and not exists $index->{"nodes"}{builtin::refaddr($node)}) 5 191 196
Condition100%3/3
ref $node eq "HASH" and not exists $index->{"nodes"}{builtin::refaddr($node)}
!ll&&!rl&&r
141775
MC/DC100%2/2
ref $node eq "HASH" and not exists $index->{"nodes"}{builtin::refaddr($node)}
ref $node eq "HASH" not exists $index->{"nodes"}{builtin::refaddr($node)}
Truth table100%3/3
A and not(B)
A= ref $node eq "HASH"B= not exists $index->{"nodes"}{builtin::refaddr($node)}
ABresult
0X0
100
111
1022 {
1023 5 my $staged = _copy_resource_index( $index );
1024 5 eval{ _index_schema_202012( $node, $base, $ptr, $staged, 0 ) };
1025 5 ▶ return( _err_res( $ctx, $schema_ptr, "invalid schema resource for $keyword: $abs", $keyword ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
1026 5 %$index = %$staged;
1027 }
1028 # Hash schemas carry their own indexed scope; boolean schemas cannot contain references.
1029 196 return( _v( $ctx, $ptr, $node, $inst ) );
1030 }
1031  
1032 sub _builtin_json_media
1033 6 ▶{
Subroutine _builtin_json_media: called CC=2 SCAR=7.0
1034 6 my( $text, $params ) = @_;
1035 6 local $@;
1036 6 eval{ _decode_json_content( $text ) };
1037 6 ▶ return( 0, 'invalid JSON', undef ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
1038 # Leave the original payload available for contentSchema, including JSON scalars and null.
1039 6 return( 1, undef );
1040 }
1041  
1042 sub _canon
1043 3703 ▶{
Subroutine _canon: called CC=1 SCAR=0.0
1044 3703 my( $v ) = @_;
1045 3703 state $json = JSON->new->allow_nonref(1)->canonical(1)->convert_blessed(1);
1046 3703 return( $json->encode( $v ) );
1047 }
1048  
1049 sub _check_vocabulary_required
1050 546 ▶{
Subroutine _check_vocabulary_required: called CC=8 SCAR=20.9
1051 546 my( $self ) = @_;
1052 546 ▶ return(1) if( $self->{vocab_checked} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"vocab_checked"} 0 546 546
1053  
1054 546 my $root = $self->{schema};
1055 546 ▶ if( ref( $root ) eq 'HASH' && ref( $root->{'$vocabulary'} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH") 2 544 546
Condition100%3/3
ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH"
!ll&&!rl&&r
45402
MC/DC100%2/2
ref $root eq "HASH" and ref $root->{"\$vocabulary"} eq "HASH"
ref $root eq "HASH" ref $root->{"\$vocabulary"} eq "HASH"
Truth table100%3/3
A and B
A= ref $root eq "HASH"B= ref $root->{"\$vocabulary"} eq "HASH"
ABresult
0X0
100
111
1056 {
1057 2 my $decl = $root->{'$vocabulary'};
1058 2 ▶ my $support = $self->{vocab_support} || {};
Condition50%1/2
$self->{'vocab_support'} || {}
l!l
20
MC/DC0%0/1
$self->{'vocab_support'} || {}
$self->{'vocab_support'}
Truth table50%1/2
A
A= $self->{'vocab_support'}
Aresult
11
00
1059 2 for my $uri ( keys( %$decl ) )
1060 {
1061 # required only
1062 2 ▶ next unless( $decl->{ $uri } );
Branch: 1 branch
BranchTrueFalseTotal
unless $decl->{$uri} 1 1 2
1063 1 ▶ next if( $support->{ $uri } );
Branch: 1 branch
BranchTrueFalseTotal
if $support->{$uri} 0 1 1
1064 # TODO: Maybe we should return an exception rather than dying; it would be more user-friendly
1065 1 die( "Required vocabulary not supported: $uri" );
1066 }
1067 }
1068 545 return( $self->{vocab_checked} = 1 );
1069 }
1070  
1071 sub _clone
1072 1175 ▶{
Subroutine _clone: called CC=1 SCAR=0.0
1073 1175 my( $v ) = @_;
1074 1175 state $json = JSON->new->allow_nonref(1)->canonical(1);
1075 1175 return( $json->decode( $json->encode( $v ) ) );
1076 }
1077  
1078 sub _compile_js_node
1079 0 ▶{
Subroutine _compile_js_node: not called CC=122 SCAR=96.2
1080 0 my $self = shift( @_ );
1081 0 my( $S, $sp, $seen, $funcs, $counter_ref, $root, $opts ) = @_;
1082  
1083 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
1084 0 ▶ my $ecma = exists( $opts->{ecma} ) ? $opts->{ecma} : 'auto';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'ecma'} ? : 0 0 0
1085 0 ▶ my $force_unicode =
Condition0%0/6
defined $ecma && $ecma =~ /^\d+$/u && $ecma >= 2018
!ll&&!rl&&r
000
defined $ecma and $ecma =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ecma && $ecma =~ /^\d+$/u && $ecma >= 2018
defined $ecma $ecma =~ /^\d+$/u $ecma >= 2018
Truth table0%0/4
A and B && C
A= defined $ecmaB= $ecma =~ /^\d+$/uC= $ecma >= 2018
ABCresult
0XX0
10X0
1100
1111
1086 defined( $ecma ) &&
1087 $ecma =~ /^\d+$/ &&
1088 $ecma >= 2018;
1089  
1090 # Re-use same JS function for the same schema pointer
1091 0 ▶ if( exists( $seen->{ $sp } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen->{$sp}) 0 0 0
1092 {
1093 0 return( $seen->{ $sp } );
1094 }
1095  
1096 # Support pointer-alias schemas such as:
1097 # definitions => { address => "#/definitions/jp_address" }
1098 0 ▶ if( defined( $S ) && !ref( $S ) && $S =~ /^#\// )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $S and not ref $S and $S =~ m[^#/]u) 0 0 0
Condition0%0/6
defined $S and not ref $S and $S =~ m[^#/]u
!ll&&!rl&&r
000
defined $S and not ref $S
!ll&&!rl&&r
000
MC/DC0%0/3
defined $S and not ref $S and $S =~ m[^#/]u
defined $S not ref $S $S =~ m[^#/]u
Truth table0%0/4
A and not(B) and C
A= defined $SB= not ref $SC= $S =~ m[^#/]u
ABCresult
0XX0
10X0
1100
1111
1099 {
1100 0 my $target_ptr = $S;
1101  
1102 # If we already compiled that pointer, just alias it
1103 0 ▶ if( exists( $seen->{ $target_ptr } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen->{$target_ptr}) 0 0 0
1104 {
1105 0 $seen->{ $sp } = $seen->{ $target_ptr };
1106 0 return( $seen->{ $target_ptr } );
1107 }
1108  
1109 0 my $target_schema = _jsv_resolve_internal_ref( $root, $target_ptr );
1110  
1111 # Follow chains of pointer-aliases
1112 0 ▶ if( defined( $target_schema ) && !ref( $target_schema ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_schema and not ref $target_schema) 0 0 0
Condition0%0/3
defined $target_schema and not ref $target_schema
!ll&&!rl&&r
000
MC/DC0%0/2
defined $target_schema and not ref $target_schema
defined $target_schema not ref $target_schema
Truth table0%0/3
A and not(B)
A= defined $target_schemaB= not ref $target_schema
ABresult
0X0
100
111
1113 {
1114 0 my %followed;
1115 0 ▶ while(
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_schema and not ref $target_schema and $target_schema =~ m[^#/]u) 0 0 0
Condition0%0/6
defined $target_schema and not ref $target_schema and $target_schema =~ m[^#/]u
!ll&&!rl&&r
000
defined $target_schema and not ref $target_schema
!ll&&!rl&&r
000
MC/DC0%0/3
defined $target_schema and not ref $target_schema and $target_schema =~ m[^#/]u
defined $target_schema not ref $target_schema $target_schema =~ m[^#/]u
Truth table0%0/4
A and not(B) and C
A= defined $target_schemaB= not ref $target_schemaC= $target_schema =~ m[^#/]u
ABCresult
0XX0
10X0
1100
1111
1116 defined( $target_schema ) &&
1117 !ref( $target_schema ) &&
1118 $target_schema =~ /^#\//
1119 )
1120 {
1121 0 ▶ if( $followed{ $target_schema }++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($followed{$target_schema}++) 0 0 0
1122 {
1123 0 $target_schema = undef;
1124 0 last;
1125 }
1126 0 $target_ptr = $target_schema;
1127 0 $target_schema = _jsv_resolve_internal_ref( $root, $target_ptr );
1128 }
1129 }
1130  
1131 0 ▶ unless( defined( $target_schema ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target_schema) 0 0 0
1132 {
1133 # Could not resolve: emit a no-op validator (server still enforces)
1134 0 my $id = $$counter_ref++;
1135 0 my $fn = "jsv_node_${id}";
1136 0 $seen->{ $sp } = $fn;
1137 0 push( @$funcs, <<JS_RUNTIME );
1138 // Schema node (unresolved pointer alias)
1139 function $fn(inst, path, ctx)
1140 {
1141 }
1142 JS_RUNTIME
1143 0 return( $fn );
1144 }
1145  
1146 0 ▶ unless( ref( $target_schema ) eq 'HASH' || JSON::is_bool( $target_schema ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $target_schema eq "HASH" or &JSON::is_bool($target_schema)) 0 0 0
Condition0%0/3
ref $target_schema eq "HASH" or &JSON::is_bool($target_schema)
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $target_schema eq "HASH" or &JSON::is_bool($target_schema)
ref $target_schema eq "HASH" &JSON::is_bool($target_schema)
Truth table0%0/3
A or B
A= ref $target_schema eq "HASH"B= &JSON::is_bool($target_schema)
ABresult
1X1
011
000
1147 {
1148 0 ▶ die(
Condition0%0/2
ref $target_schema || 'scalar'
l!l
00
MC/DC0%0/1
ref $target_schema || 'scalar'
ref $target_schema
Truth table0%0/2
A
A= ref $target_schema
Aresult
11
00
1149 "Internal error: pointer-alias '$S' at '$sp' did not resolve to an object schema " .
1150 "(got " . ( ref( $target_schema ) || 'scalar' ) . ")."
1151 );
1152 }
1153  
1154 0 my $base_fn = $self->_compile_js_node( $target_schema, $target_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1155  
1156 # Alias this pointer to the target validator
1157 0 $seen->{ $sp } = $base_fn;
1158 0 return( $base_fn );
1159 }
1160  
1161 0 my $id = $$counter_ref++;
1162 0 my $fn = "jsv_node_$id";
1163 0 $seen->{ $sp } = $fn;
1164  
1165 0 my @body;
1166 0 my $qsp = _js_quote( $sp );
1167 0 my $comment = _js_comment( $sp );
1168 0 push( @body, <<JS_RUNTIME );
1169 // $comment
1170 function $fn(inst, path, ctx)
1171 {
1172 JS_RUNTIME
1173 0 ▶ if( JSON::is_bool( $S ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool($S)) 0 0 0
1174 {
1175 0 ▶ push( @body, " _jsv_err(ctx, path, 'false', 'instance does not satisfy false schema', $qsp);\n" ) unless( $S );
Branch: 1 branch
BranchTrueFalseTotal
unless $S 0 0 0
1176 0 push( @$funcs, join( '', @body ) . "}\n" );
1177 0 return( $fn );
1178 }
1179 0 ▶ unless( ref( $S ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $S eq "HASH") 0 0 0
1180 {
1181 0 push( @$funcs, join( '', @body ) . "}\n" );
1182 0 return( $fn );
1183 }
1184  
1185 # Guard reference recursion at the same instance path. Register the function before compiling its targets.
1186 0 my $has_ref = exists( $S->{'$ref'} );
1187 0 ▶ if( $has_ref )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ref) 0 0 0
1188 {
1189 0 my $guard = _js_quote( $fn );
1190 0 push( @body, <<JS_RUNTIME );
1191 var active = ctx.active || (ctx.active = Object.create(null));
1192 var guard = $guard + ':' + path;
1193 if(active[guard]) return;
1194 active[guard] = true;
1195 try
1196 {
1197 JS_RUNTIME
1198 0 my( $target_schema, $target_ptr ) = _js_reference_target( $S, $opts );
1199 0 ▶ if( defined( $target_schema ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_schema) 0 0 0
1200 {
1201 0 my $base_fn = $self->_compile_js_node( $target_schema, $target_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1202 0 push( @body, <<JS_RUNTIME );
1203 $base_fn(inst, path, ctx);
1204 if(ctx.maxErrors && ctx.errors.length >= ctx.maxErrors) return;
1205 JS_RUNTIME
1206 }
1207 # External or unsupported references remain server-side checks, but local keywords still apply.
1208 }
1209  
1210 # NOTE: combinator (allOf / anyOf / oneOf / not / if-then-else)
1211 0 ▶ if( ref( $S ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S eq "HASH") 0 0 0
1212 {
1213 # allOf – AND of subschemas
1214 0 ▶ if( exists( $S->{allOf} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"allOf"} and ref $S->{"allOf"} eq "ARRAY" and @{$S->{"allOf"};}) 0 0 0
Condition0%0/6
exists $S->{"allOf"} and ref $S->{"allOf"} eq "ARRAY" and @{$S->{"allOf"};}
!ll&&!rl&&r
000
exists $S->{"allOf"} and ref $S->{"allOf"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"allOf"} and ref $S->{"allOf"} eq "ARRAY" and @{$S->{"allOf"};}
exists $S->{"allOf"} ref $S->{"allOf"} eq "ARRAY" @{$S->{"allOf"};}
Truth table0%0/4
A and B and C
A= exists $S->{"allOf"}B= ref $S->{"allOf"} eq "ARRAY"C= @{$S->{"allOf"};}
ABCresult
0XX0
10X0
1100
1111
1215 ref( $S->{allOf} ) eq 'ARRAY' &&
1216 0 @{$S->{allOf}} )
1217 {
1218 0 for my $i ( 0 .. $#{$S->{allOf}} )
1219 {
1220 0 my $sub_sp = _join_ptr( $sp, 'allOf', $i );
1221 0 my $sub_fn = $self->_compile_js_node( $S->{allOf}->[ $i ], $sub_sp, $seen, $funcs, $counter_ref, $root, $opts );
1222  
1223 0 push( @body, <<JS_RUNTIME );
1224 // Schema node
1225 $sub_fn(inst, path, ctx);
1226 if(ctx.maxErrors && ctx.errors.length >= ctx.maxErrors) return;
1227 JS_RUNTIME
1228 }
1229  
1230 # IMPORTANT: we do *not* return here.
1231 # Other local keywords (type, properties, contains, ...) must still run.
1232 }
1233  
1234 # anyOf – at least one must validate (we only emit a single anyOf error)
1235 0 ▶ if( exists( $S->{anyOf} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"anyOf"} and ref $S->{"anyOf"} eq "ARRAY" and @{$S->{"anyOf"};}) 0 0 0
Condition0%0/6
exists $S->{"anyOf"} and ref $S->{"anyOf"} eq "ARRAY" and @{$S->{"anyOf"};}
!ll&&!rl&&r
000
exists $S->{"anyOf"} and ref $S->{"anyOf"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"anyOf"} and ref $S->{"anyOf"} eq "ARRAY" and @{$S->{"anyOf"};}
exists $S->{"anyOf"} ref $S->{"anyOf"} eq "ARRAY" @{$S->{"anyOf"};}
Truth table0%0/4
A and B and C
A= exists $S->{"anyOf"}B= ref $S->{"anyOf"} eq "ARRAY"C= @{$S->{"anyOf"};}
ABCresult
0XX0
10X0
1100
1111
1236 ref( $S->{anyOf} ) eq 'ARRAY' &&
1237 0 @{$S->{anyOf}} )
1238 {
1239 0 push( @body, <<JS_RUNTIME );
1240 (function()
1241 {
1242 var baseErrors = ctx.errors;
1243 var matched = false;
1244  
1245 JS_RUNTIME
1246  
1247 0 for my $i ( 0 .. $#{$S->{anyOf}} )
1248 {
1249 0 my $sub_sp = _join_ptr( $sp, 'anyOf', $i );
1250 0 my $sub_fn = $self->_compile_js_node( $S->{anyOf}->[ $i ], $sub_sp, $seen, $funcs, $counter_ref, $root, $opts );
1251  
1252 0 push( @body, <<JS_RUNTIME );
1253 if(matched) return;
1254 ctx.errors = [];
1255 // Schema node
1256 $sub_fn(inst, path, ctx);
1257 if(ctx.errors.length === 0)
1258 {
1259 matched = true;
1260 ctx.errors = baseErrors;
1261 return;
1262 }
1263 JS_RUNTIME
1264 }
1265  
1266 0 my $sp_qp = _js_quote( $sp );
1267 0 push( @body, <<JS_RUNTIME );
1268 ctx.errors = baseErrors;
1269 if(!matched)
1270 {
1271 _jsv_err(ctx, path, 'anyOf', 'no subschema matched', $sp_qp);
1272 }
1273 })();
1274 // return;
1275 JS_RUNTIME
1276 }
1277  
1278 # oneOf – exactly one must validate
1279 0 ▶ if( exists( $S->{oneOf} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"oneOf"} and ref $S->{"oneOf"} eq "ARRAY" and @{$S->{"oneOf"};}) 0 0 0
Condition0%0/6
exists $S->{"oneOf"} and ref $S->{"oneOf"} eq "ARRAY" and @{$S->{"oneOf"};}
!ll&&!rl&&r
000
exists $S->{"oneOf"} and ref $S->{"oneOf"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"oneOf"} and ref $S->{"oneOf"} eq "ARRAY" and @{$S->{"oneOf"};}
exists $S->{"oneOf"} ref $S->{"oneOf"} eq "ARRAY" @{$S->{"oneOf"};}
Truth table0%0/4
A and B and C
A= exists $S->{"oneOf"}B= ref $S->{"oneOf"} eq "ARRAY"C= @{$S->{"oneOf"};}
ABCresult
0XX0
10X0
1100
1111
1280 ref( $S->{oneOf} ) eq 'ARRAY' &&
1281 0 @{$S->{oneOf}} )
1282 {
1283 0 push( @body, <<JS_RUNTIME );
1284 (function()
1285 {
1286 var baseErrors = ctx.errors;
1287 var hits = 0;
1288  
1289 JS_RUNTIME
1290  
1291 0 for my $i ( 0 .. $#{$S->{oneOf}} )
1292 {
1293 0 my $sub_sp = _join_ptr( $sp, 'oneOf', $i );
1294 0 my $sub_fn = $self->_compile_js_node( $S->{oneOf}->[ $i ], $sub_sp, $seen, $funcs, $counter_ref, $root, $opts );
1295  
1296 0 push( @body, <<JS_RUNTIME );
1297 ctx.errors = [];
1298 // Schema node
1299 $sub_fn(inst, path, ctx);
1300 if(ctx.errors.length === 0)
1301 {
1302 hits++;
1303 }
1304 JS_RUNTIME
1305 }
1306  
1307 0 my $sp_qp = _js_quote( $sp );
1308 0 push( @body, <<JS_RUNTIME );
1309 ctx.errors = baseErrors;
1310 if(hits !== 1)
1311 {
1312 _jsv_err(ctx, path, 'oneOf', 'exactly one subschema must match, but ' + hits + ' did', $sp_qp);
1313 }
1314 })();
1315 // return;
1316 JS_RUNTIME
1317 }
1318  
1319 # not – but we SKIP "negative required" patterns on the client
1320 0 ▶ if( exists( $S->{not} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"not"}) 0 0 0
1321 {
1322 0 my $skip_not = 0;
1323  
1324 0 ▶ if( ref( $S->{not} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S->{"not"} eq "HASH") 0 0 0
1325 {
1326 0 my $N = $S->{not};
1327  
1328 # Direct: { "not": { "required": [...] } }
1329 0 ▶ if( exists( $N->{required} ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (exists $N->{'required'} and ref $N->{'required'} eq 'ARRAY') { } 0 0 0
elsif (exists $N->{'anyOf'} and ref $N->{'anyOf'} eq 'ARRAY') { } 0 0 0
Condition0%0/3
exists $N->{"required"} and ref $N->{"required"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $N->{"required"} and ref $N->{"required"} eq "ARRAY"
exists $N->{"required"} ref $N->{"required"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $N->{"required"}B= ref $N->{"required"} eq "ARRAY"
ABresult
0X0
100
111
1330 ref( $N->{required} ) eq 'ARRAY' )
1331 {
1332 0 $skip_not = 1;
1333 }
1334 # Or: { "not": { "anyOf": [ {required:...}, ... ] } }
1335 ▶ elsif( exists( $N->{anyOf} ) &&
Condition0%0/3
exists $N->{"anyOf"} and ref $N->{"anyOf"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $N->{"anyOf"} and ref $N->{"anyOf"} eq "ARRAY"
exists $N->{"anyOf"} ref $N->{"anyOf"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $N->{"anyOf"}B= ref $N->{"anyOf"} eq "ARRAY"
ABresult
0X0
100
111
1336 ref( $N->{anyOf} ) eq 'ARRAY' )
1337 {
1338 0 my $all_req = 1;
1339 0 for my $elt ( @{$N->{anyOf}} )
1340 {
1341 0 ▶ if( !( ref( $elt ) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elt eq "HASH" and exists $elt->{"required"} and ref $elt->{"required"} eq "ARRAY") 0 0 0
Condition0%0/6
ref $elt eq "HASH" and exists $elt->{"required"} and ref $elt->{"required"} eq "ARRAY"
!ll&&!rl&&r
000
ref $elt eq "HASH" and exists $elt->{"required"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $elt eq "HASH" and exists $elt->{"required"} and ref $elt->{"required"} eq "ARRAY"
ref $elt eq "HASH" exists $elt->{"required"} ref $elt->{"required"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= ref $elt eq "HASH"B= exists $elt->{"required"}C= ref $elt->{"required"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
1342 exists( $elt->{required} ) &&
1343 ref( $elt->{required} ) eq 'ARRAY' ) )
1344 {
1345 0 $all_req = 0;
1346 0 last;
1347 }
1348 }
1349 0 ▶ $skip_not = 1 if( $all_req );
Branch: 1 branch
BranchTrueFalseTotal
if $all_req 0 0 0
1350 }
1351 }
1352  
1353 0 ▶ if( !$skip_not )
Branch: 1 branch
BranchTrueFalseTotal
if (not $skip_not) { } 0 0 0
1354 {
1355 0 my $sub_sp = _join_ptr( $sp, 'not' );
1356 0 my $sub_fn = $self->_compile_js_node( $S->{not}, $sub_sp, $seen, $funcs, $counter_ref, $root, $opts );
1357 0 my $sp_qp = _js_quote( $sp );
1358 0 push( @body, <<JS_RUNTIME );
1359 (function()
1360 {
1361 var baseErrors = ctx.errors;
1362 ctx.errors = [];
1363 // Schema node
1364 $sub_fn(inst, path, ctx);
1365 var failed = (ctx.errors.length > 0);
1366 ctx.errors = baseErrors;
1367 if(!failed)
1368 {
1369 _jsv_err(ctx, path, 'not', 'instance matched forbidden schema', $sp_qp);
1370 }
1371 })();
1372 // return;
1373 JS_RUNTIME
1374 }
1375 else
1376 {
1377 # Skip "not + required" style rules on the client;
1378 # they are enforced server-side only.
1379 0 push( @body, <<JS_RUNTIME );
1380 // NOTE: 'not' is a negative-required pattern; skipped client-side.
1381 JS_RUNTIME
1382 }
1383 }
1384  
1385 # if / then / else (branch errors are enforced, "if" errors are not)
1386 0 ▶ if( exists( $S->{if} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"if"}) 0 0 0
1387 {
1388 0 my $if_sp = _join_ptr( $sp, 'if' );
1389 0 my $if_fn = $self->_compile_js_node( $S->{if}, $if_sp, $seen, $funcs, $counter_ref, $root, $opts );
1390  
1391 0 push( @body, <<JS_RUNTIME );
1392 (function()
1393 {
1394 var baseErrors = ctx.errors;
1395 var tmp = [];
1396 ctx.errors = tmp;
1397 // Schema node
1398 $if_fn(inst, path, ctx);
1399 var failed = (tmp.length > 0);
1400 ctx.errors = baseErrors;
1401 JS_RUNTIME
1402  
1403 0 ▶ if( exists( $S->{then} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"then"}) 0 0 0
1404 {
1405 0 my $then_sp = _join_ptr( $sp, 'then' );
1406 0 my $then_fn = $self->_compile_js_node( $S->{then}, $then_sp, $seen, $funcs, $counter_ref, $root, $opts );
1407 0 push( @body, <<JS_RUNTIME );
1408 if(!failed)
1409 {
1410 $then_fn(inst, path, ctx);
1411 }
1412 JS_RUNTIME
1413 }
1414 0 ▶ if( exists( $S->{else} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"else"}) 0 0 0
1415 {
1416 0 my $else_sp = _join_ptr( $sp, 'else' );
1417 0 my $else_fn = $self->_compile_js_node( $S->{else}, $else_sp, $seen, $funcs, $counter_ref, $root, $opts );
1418 0 push( @body, <<JS_RUNTIME );
1419 if(failed)
1420 {
1421 $else_fn(inst, path, ctx);
1422 }
1423 JS_RUNTIME
1424 }
1425  
1426 0 push( @body, <<JS_RUNTIME );
1427 })();
1428 JS_RUNTIME
1429 # Do NOT return here; this node can also have local constraints
1430 }
1431  
1432 # uniqueKeys extension when enabled
1433 0 ▶ if( $self->{unique_keys} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"unique_keys"} and exists $S->{"uniqueKeys"} and ref $S->{"uniqueKeys"} eq "ARRAY") 0 0 0
Condition0%0/6
$self->{"unique_keys"} and exists $S->{"uniqueKeys"} and ref $S->{"uniqueKeys"} eq "ARRAY"
!ll&&!rl&&r
000
$self->{"unique_keys"} and exists $S->{"uniqueKeys"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"unique_keys"} and exists $S->{"uniqueKeys"} and ref $S->{"uniqueKeys"} eq "ARRAY"
$self->{"unique_keys"} exists $S->{"uniqueKeys"} ref $S->{"uniqueKeys"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= $self->{"unique_keys"}B= exists $S->{"uniqueKeys"}C= ref $S->{"uniqueKeys"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
1434 exists( $S->{uniqueKeys} ) &&
1435 ref( $S->{uniqueKeys} ) eq 'ARRAY' )
1436 {
1437 0 for my $keyset_ref ( @{$S->{uniqueKeys}} )
1438 {
1439 0 ▶ next unless( ref( $keyset_ref ) eq 'ARRAY' && @$keyset_ref );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keyset_ref eq "ARRAY" and @$keyset_ref 0 0 0
Condition0%0/3
ref $keyset_ref eq "ARRAY" and @$keyset_ref
!ll&&!rl&&r
000
MC/DC0%0/2
ref $keyset_ref eq "ARRAY" and @$keyset_ref
ref $keyset_ref eq "ARRAY" @$keyset_ref
Truth table0%0/3
A and B
A= ref $keyset_ref eq "ARRAY"B= @$keyset_ref
ABresult
0X0
100
111
1440  
1441 0 my @keys = map{ _js_quote( $_ ) } @$keyset_ref;
1442 0 my $qsp = _js_quote( $sp );
1443  
1444 0 push( @body, <<JS_RUNTIME );
1445 if(Array.isArray(inst))
1446 {
1447 var seen = {};
1448 for(var i = 0; i < inst.length; i++)
1449 {
1450 var item = inst[i];
1451 var key = '';
1452 try
1453 {
1454 key = [ @keys ].map(function(k){ return item[k]; }).join('\x1E'); // RS separator
1455 }
1456 catch(e) {}
1457 if(seen.hasOwnProperty(key))
1458 {
1459 _jsv_err(ctx, path, 'uniqueKeys', 'duplicate items with keys [@$keyset_ref]', $qsp);
1460 break;
1461 }
1462 seen[key] = true;
1463 }
1464 }
1465 JS_RUNTIME
1466 }
1467 }
1468 }
1469  
1470 0 ▶ my $has_type_keyword = ( ref( $S ) eq 'HASH' && exists( $S->{type} ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $S eq 'HASH' && exists $S->{'type'} ? : 0 0 0
Condition0%0/3
ref $S eq "HASH" and exists $S->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $S eq "HASH" and exists $S->{"type"}
ref $S eq "HASH" exists $S->{"type"}
Truth table0%0/3
A and B
A= ref $S eq "HASH"B= exists $S->{"type"}
ABresult
0X0
100
111
1471  
1472 # NOTE: type
1473 # 1) type
1474 0 ▶ if( exists( $S->{type} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"type"}) 0 0 0
1475 {
1476 0 ▶ my @types = ref( $S->{type} ) eq 'ARRAY' ? @{$S->{type}} : ( $S->{type} );
Branch: 1 branch
BranchTrueFalseTotal
ref $S->{'type'} eq 'ARRAY' ? : 0 0 0
1477  
1478 # Special-case "number" vs "integer": _jsv_typeOf returns "integer" for ints
1479 0 my @checks;
1480 0 for my $t ( @types )
1481 {
1482 0 ▶ if( $t eq 'number' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'number') { } 0 0 0
1483 {
1484 # accept "number" or "integer" from _jsv_typeOf
1485 0 push( @checks, q{tt === 'number' || tt === 'integer'} );
1486 }
1487 else
1488 {
1489 0 my $qt = _js_quote( $t );
1490 0 push( @checks, "tt === $qt" );
1491 }
1492 }
1493  
1494 0 my $cond = join( ' || ', @checks );
1495 0 ▶ my $msg = 'expected type ' .
Branch: 1 branch
BranchTrueFalseTotal
@types == 1 ? : 0 0 0
1496 ( @types == 1 ? $types[0] : '[' . join( ',', @types ) . ']' ) .
1497 ' but found ';
1498 0 my $qmsg = _js_quote( $msg );
1499 0 my $qsp = _js_quote( $sp );
1500  
1501 0 push( @body, <<JS_RUNTIME );
1502 var tt = _jsv_typeOf(inst);
1503 if(!( $cond ))
1504 {
1505 _jsv_err(ctx, path, 'type', $qmsg + tt, $qsp);
1506 return;
1507 }
1508 JS_RUNTIME
1509 }
1510  
1511 # NOTE: enum
1512 # 2) enum
1513 0 ▶ if( exists( $S->{enum} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"enum"} and ref $S->{"enum"} eq "ARRAY" and @{$S->{"enum"};}) 0 0 0
Condition0%0/6
exists $S->{"enum"} and ref $S->{"enum"} eq "ARRAY" and @{$S->{"enum"};}
!ll&&!rl&&r
000
exists $S->{"enum"} and ref $S->{"enum"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"enum"} and ref $S->{"enum"} eq "ARRAY" and @{$S->{"enum"};}
exists $S->{"enum"} ref $S->{"enum"} eq "ARRAY" @{$S->{"enum"};}
Truth table0%0/4
A and B and C
A= exists $S->{"enum"}B= ref $S->{"enum"} eq "ARRAY"C= @{$S->{"enum"};}
ABCresult
0XX0
10X0
1100
1111
1514 ref( $S->{enum} ) eq 'ARRAY' &&
1515 0 @{$S->{enum}} )
1516 {
1517 0 my @vals = @{$S->{enum}};
1518 0 my @vs_js = map{ "JSON.parse(" . _js_quote( _js_literal( $_ ) ) . ")" } @vals;
1519 0 my $qsp = _js_quote( $sp );
1520  
1521 0 local $" = ', ';
1522 0 push( @body, <<JS_RUNTIME );
1523 (function()
1524 {
1525 var ok = false;
1526 var v = inst;
1527 var list = [ @vs_js ];
1528 for(var i = 0; i < list.length; i++)
1529 {
1530 if(_jsv_equal(v, list[i]))
1531 {
1532 ok = true;
1533 break;
1534 }
1535 }
1536 if(!ok)
1537 {
1538 _jsv_err(ctx, path, 'enum', 'value is not in enum list', $qsp);
1539 }
1540 })();
1541 JS_RUNTIME
1542 }
1543  
1544 # NOTE: const
1545 # 3) const compares JSON values without coercion.
1546 0 ▶ if( exists( $S->{const} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"const"}) 0 0 0
1547 {
1548 0 my $cv = _js_quote( _js_literal( $S->{const} ) );
1549 0 my $qsp = _js_quote( $sp );
1550 0 push( @body, <<JS_RUNTIME );
1551 if(!_jsv_equal(inst, JSON.parse($cv)))
1552 {
1553 _jsv_err(ctx, path, 'const', 'value does not match const', $qsp);
1554 }
1555 JS_RUNTIME
1556 }
1557  
1558 # NOTE: required
1559 # 4) required (objects)
1560 0 ▶ if( exists( $S->{required} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"required"} and ref $S->{"required"} eq "ARRAY" and @{$S->{"required"};}) 0 0 0
Condition0%0/6
exists $S->{"required"} and ref $S->{"required"} eq "ARRAY" and @{$S->{"required"};}
!ll&&!rl&&r
000
exists $S->{"required"} and ref $S->{"required"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"required"} and ref $S->{"required"} eq "ARRAY" and @{$S->{"required"};}
exists $S->{"required"} ref $S->{"required"} eq "ARRAY" @{$S->{"required"};}
Truth table0%0/4
A and B and C
A= exists $S->{"required"}B= ref $S->{"required"} eq "ARRAY"C= @{$S->{"required"};}
ABCresult
0XX0
10X0
1100
1111
1561 ref( $S->{required} ) eq 'ARRAY' &&
1562 0 @{$S->{required}} )
1563 {
1564 0 push( @body, <<JS_RUNTIME );
1565 if(_jsv_typeOf(inst) === 'object')
1566 {
1567 JS_RUNTIME
1568  
1569 0 for my $p ( @{$S->{required}} )
1570 {
1571 0 my $qp = _js_quote( $p );
1572 0 my $sp2 = _join_ptr( $sp, 'properties', $p );
1573 0 my $qsp2 = _js_quote( $sp2 );
1574  
1575 0 my $msg = "required property '$p' is missing";
1576 0 my $qmsg = _js_quote( $msg );
1577  
1578 0 push( @body, <<JS_RUNTIME );
1579 if(!_jsv_hasOwn(inst, $qp))
1580 {
1581 _jsv_err(ctx, path, 'required', $qmsg, $qsp2);
1582 }
1583 JS_RUNTIME
1584 }
1585  
1586 0 push( @body, <<JS_RUNTIME );
1587 }
1588 JS_RUNTIME
1589 }
1590  
1591 # NOTE: string && pattern
1592 # 5) string length & pattern
1593 my $has_string_constraints =
1594 exists( $S->{minLength} ) ||
1595 exists( $S->{maxLength} ) ||
1596 0 ▶ exists( $S->{pattern} );
Condition0%0/6
exists $S->{'minLength'} || exists $S->{'maxLength'} || exists $S->{'pattern'}
l!l&&r!l&&!r
000
exists $S->{"minLength"} or exists $S->{"maxLength"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $S->{'minLength'} || exists $S->{'maxLength'} || exists $S->{'pattern'}
exists $S->{"minLength"} exists $S->{"maxLength"} exists $S->{'pattern'}
Truth table0%0/4
A or B || C
A= exists $S->{"minLength"}B= exists $S->{"maxLength"}C= exists $S->{'pattern'}
ABCresult
1XX1
01X1
0011
0000
1597  
1598 0 ▶ if( $has_string_constraints )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_string_constraints) 0 0 0
1599 {
1600 0 my $qsp = _js_quote( $sp );
1601  
1602 0 push( @body, <<JS_RUNTIME );
1603 if(_jsv_typeOf(inst) === 'string')
1604 {
1605 JS_RUNTIME
1606  
1607 0 ▶ if( exists( $S->{minLength} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"minLength"}) 0 0 0
1608 {
1609 0 my $min = int( $S->{minLength} );
1610 0 push( @body, <<JS_RUNTIME );
1611 if(_jsv_strlen(inst) < $min)
1612 {
1613 _jsv_err(ctx, path, 'minLength', 'string shorter than minLength $min', $qsp);
1614 }
1615 JS_RUNTIME
1616 }
1617  
1618 0 ▶ if( exists( $S->{maxLength} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"maxLength"}) 0 0 0
1619 {
1620 0 my $max = int( $S->{maxLength} );
1621 0 push( @body, <<JS_RUNTIME );
1622 if(_jsv_strlen(inst) > $max)
1623 {
1624 _jsv_err(ctx, path, 'maxLength', 'string longer than maxLength $max', $qsp);
1625 }
1626 JS_RUNTIME
1627 }
1628  
1629 0 ▶ if( exists( $S->{pattern} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"pattern"} and defined $S->{"pattern"} and length $S->{"pattern"}) 0 0 0
Condition0%0/6
exists $S->{"pattern"} and defined $S->{"pattern"} and length $S->{"pattern"}
!ll&&!rl&&r
000
exists $S->{"pattern"} and defined $S->{"pattern"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $S->{"pattern"} and defined $S->{"pattern"} and length $S->{"pattern"}
exists $S->{"pattern"} defined $S->{"pattern"} length $S->{"pattern"}
Truth table0%0/4
A and B and C
A= exists $S->{"pattern"}B= defined $S->{"pattern"}C= length $S->{"pattern"}
ABCresult
0XX0
10X0
1100
1111
1630 defined( $S->{pattern} ) &&
1631 length( $S->{pattern} ) )
1632 {
1633 0 my $pat = $S->{pattern};
1634 # my $qpat = _js_quote( $pat );
1635 # from \x{FF70} to \uFF70
1636 # from \p{Katakana} to \p{sc=Katakana}
1637 0 my $qpat = _re_to_js( $pat );
1638  
1639 0 ▶ if( $force_unicode )
Branch: 1 branch
BranchTrueFalseTotal
if ($force_unicode) { } 0 0 0
1640 {
1641 # ecma >= 2018: always try with "u" flag (Unicode mode)
1642 0 push( @body, <<JS_RUNTIME );
1643 try
1644 {
1645 var re = new RegExp("$qpat", "u");
1646 if(!re.test(inst))
1647 {
1648 _jsv_err(ctx, path, 'pattern', 'string does not match pattern', $qsp);
1649 }
1650 }
1651 catch(e)
1652 {
1653 // Browser does not support Unicode property escapes or this pattern.
1654 }
1655 JS_RUNTIME
1656 }
1657 else
1658 {
1659 # auto / ES5 mode: detect "advanced" patterns
1660 0 ▶ if( $pat =~ /\\p\{|\\P\{|\\X|\\R|\(\?[A-Za-z]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($pat =~ /\\p\{|\\P\{|\\X|\\R|\(\?[A-Za-z]/u) { } 0 0 0
1661 {
1662 # Attempt Unicode mode with "u" flag, but gracefully fall back
1663 0 push( @body, <<JS_RUNTIME );
1664 (function()
1665 {
1666 var re = null;
1667 try
1668 {
1669 re = new RegExp("$qpat", "u");
1670 }
1671 catch(e)
1672 {
1673 // Older browser; skip Unicode-property-based pattern on client.
1674 }
1675 if(re && !re.test(inst))
1676 {
1677 _jsv_err(ctx, path, 'pattern', 'string does not match pattern', $qsp);
1678 }
1679 })();
1680 JS_RUNTIME
1681 }
1682 else
1683 {
1684 # Simple ECMA 5-compatible pattern
1685 0 push( @body, <<JS_RUNTIME );
1686 try
1687 {
1688 var re = new RegExp("$qpat");
1689 if(!re.test(inst))
1690 {
1691 _jsv_err(ctx, path, 'pattern', 'string does not match pattern', $qsp);
1692 }
1693 }
1694 catch(e)
1695 {
1696 // If pattern is not JS-compatible, we silently skip on the client.
1697 }
1698 JS_RUNTIME
1699 }
1700 }
1701 }
1702  
1703 0 push( @body, <<JS_RUNTIME );
1704 }
1705 JS_RUNTIME
1706 }
1707  
1708 # NOTE: minimum, maximum, etc
1709 # 6) numeric bounds (minimum/maximum/exclusive*)
1710 my $has_num_constraints =
1711 exists( $S->{minimum} ) ||
1712 exists( $S->{maximum} ) ||
1713 exists( $S->{exclusiveMinimum} ) ||
1714 0 ▶ exists( $S->{exclusiveMaximum} );
Condition0%0/9
exists $S->{'minimum'} || exists $S->{'maximum'} || exists $S->{'exclusiveMinimum'} || exists $S->{'exclusiveMaximum'}
l!l&&r!l&&!r
000
exists $S->{"minimum"} or exists $S->{"maximum"} or exists $S->{"exclusiveMinimum"}
l!l&&r!l&&!r
000
exists $S->{"minimum"} or exists $S->{"maximum"}
l!l&&r!l&&!r
000
MC/DC0%0/4
exists $S->{'minimum'} || exists $S->{'maximum'} || exists $S->{'exclusiveMinimum'} || exists $S->{'exclusiveMaximum'}
exists $S->{"minimum"} exists $S->{"maximum"} exists $S->{"exclusiveMinimum"} exists $S->{'exclusiveMaximum'}
Truth table0%0/5
A or B or C || D
A= exists $S->{"minimum"}B= exists $S->{"maximum"}C= exists $S->{"exclusiveMinimum"}D= exists $S->{'exclusiveMaximum'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1715  
1716 0 ▶ if( $has_num_constraints )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_num_constraints) 0 0 0
1717 {
1718 # For consistency with string/pattern/etc, we report the
1719 # schema pointer of the *owning schema* ($sp), not the
1720 # child keyword location (.../minimum).
1721 0 my $qsp_num = _js_quote( $sp );
1722 0 my $qmsg_expected_num = _js_quote( 'expected number but found ' );
1723  
1724 0 push( @body, <<'JS_RUNTIME' );
1725 var t = _jsv_typeOf(inst);
1726  
1727 // Coerce numeric-looking strings to numbers, for friendlier UX
1728 if(t === 'string' && /^[+-]?(?:\d+|\d*\.\d+)$/.test(inst))
1729 {
1730 inst = +inst;
1731 t = _jsv_typeOf(inst);
1732 }
1733  
1734 if(t === 'number' || t === 'integer')
1735 {
1736 JS_RUNTIME
1737  
1738 0 ▶ if( exists( $S->{minimum} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"minimum"}) 0 0 0
1739 {
1740 0 my $min = 0 + $S->{minimum};
1741 0 push( @body, <<JS_RUNTIME );
1742 if(inst < $min)
1743 {
1744 _jsv_err(ctx, path, 'minimum', 'number is less than minimum $min', $qsp_num);
1745 }
1746 JS_RUNTIME
1747 }
1748  
1749 0 ▶ if( exists( $S->{maximum} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"maximum"}) 0 0 0
1750 {
1751 0 my $max = 0 + $S->{maximum};
1752 0 push( @body, <<JS_RUNTIME );
1753 if(inst > $max)
1754 {
1755 _jsv_err(ctx, path, 'maximum', 'number is greater than maximum $max', $qsp_num);
1756 }
1757 JS_RUNTIME
1758 }
1759  
1760 0 ▶ if( exists( $S->{exclusiveMinimum} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"exclusiveMinimum"}) 0 0 0
1761 {
1762 0 my $emin = 0 + $S->{exclusiveMinimum};
1763 0 push( @body, <<JS_RUNTIME );
1764 if(inst <= $emin)
1765 {
1766 _jsv_err(ctx, path, 'exclusiveMinimum',
1767 'number is <= exclusiveMinimum $emin', $qsp_num);
1768 }
1769 JS_RUNTIME
1770 }
1771  
1772 0 ▶ if( exists( $S->{exclusiveMaximum} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"exclusiveMaximum"}) 0 0 0
1773 {
1774 0 my $emax = 0 + $S->{exclusiveMaximum};
1775 0 push( @body, <<JS_RUNTIME );
1776 if(inst >= $emax)
1777 {
1778 _jsv_err(ctx, path, 'exclusiveMaximum',
1779 'number is >= exclusiveMaximum $emax', $qsp_num);
1780 }
1781 JS_RUNTIME
1782 }
1783  
1784 # Close the "if(t === 'number' || t === 'integer')" and, if needed,
1785 # add a fallback type error when no explicit "type" keyword exists.
1786 0 ▶ if( $has_type_keyword )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_type_keyword) { } 0 0 0
1787 {
1788 0 push( @body, <<'JS_RUNTIME' );
1789 }
1790 JS_RUNTIME
1791 }
1792 else
1793 {
1794 0 push( @body, <<JS_RUNTIME );
1795 }
1796 else
1797 {
1798 _jsv_err(ctx, path, 'type', $qmsg_expected_num + t, $qsp_num);
1799 }
1800 JS_RUNTIME
1801 }
1802 }
1803  
1804 # NOTE: prefixItems / items / minItems / maxItems
1805 # 7) array items & minItems/maxItems
1806 0 ▶ my $prefix = ref( $S->{prefixItems} ) eq 'ARRAY' ? $S->{prefixItems} : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $S->{'prefixItems'} eq 'ARRAY' ? : 0 0 0
1807 my $has_array_len =
1808 exists( $S->{minItems} ) ||
1809 0 ▶ exists( $S->{maxItems} );
Condition0%0/3
exists $S->{'minItems'} || exists $S->{'maxItems'}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $S->{'minItems'} || exists $S->{'maxItems'}
exists $S->{'minItems'} exists $S->{'maxItems'}
Truth table0%0/3
A || B
A= exists $S->{'minItems'}B= exists $S->{'maxItems'}
ABresult
1X1
011
000
1810  
1811 0 ▶ if( @$prefix || exists( $S->{items} ) || $has_array_len )
Branch: 1 branch
BranchTrueFalseTotal
if (@$prefix or exists $S->{"items"} or $has_array_len) 0 0 0
Condition0%0/6
@$prefix or exists $S->{"items"} or $has_array_len
l!l&&r!l&&!r
000
@$prefix or exists $S->{"items"}
l!l&&r!l&&!r
000
MC/DC0%0/3
@$prefix or exists $S->{"items"} or $has_array_len
@$prefix exists $S->{"items"} $has_array_len
Truth table0%0/4
A or B or C
A= @$prefixB= exists $S->{"items"}C= $has_array_len
ABCresult
1XX1
01X1
0011
0000
1812 {
1813 0 my $qsp = _js_quote( $sp );
1814  
1815 # Precompile single-schema "items"
1816 0 my $items_fn;
1817 0 ▶ if( exists( $S->{items} ) && ( ref( $S->{items} ) eq 'HASH' || JSON::is_bool( $S->{items} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"items"} and ref $S->{"items"} eq "HASH" || &JSON::is_bool($S->{"items"})) 0 0 0
Condition0%0/6
exists $S->{"items"} and ref $S->{"items"} eq "HASH" || &JSON::is_bool($S->{"items"})
!ll&&!rl&&r
000
ref $S->{"items"} eq "HASH" or &JSON::is_bool($S->{"items"})
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $S->{"items"} and ref $S->{"items"} eq "HASH" || &JSON::is_bool($S->{"items"})
exists $S->{"items"} ref $S->{"items"} eq "HASH" &JSON::is_bool($S->{"items"})
Truth table0%0/4
A and B or C
A= exists $S->{"items"}B= ref $S->{"items"} eq "HASH"C= &JSON::is_bool($S->{"items"})
ABCresult
0XX0
1000
11X1
1011
1818 {
1819 0 my $items_ptr = _join_ptr( $sp, 'items' );
1820 0 $items_fn = $self->_compile_js_node( $S->{items}, $items_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1821 }
1822  
1823 0 ▶ my $min_items = exists( $S->{minItems} ) ? int( $S->{minItems} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $S->{'minItems'} ? : 0 0 0
1824 0 ▶ my $max_items = exists( $S->{maxItems} ) ? int( $S->{maxItems} ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $S->{'maxItems'} ? : 0 0 0
1825  
1826 0 my( $qmsg_min, $qmsg_max );
1827 0 ▶ if( defined( $min_items ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_items) 0 0 0
1828 {
1829 0 my $msg_min = "array has fewer than $min_items items";
1830 0 $qmsg_min = _js_quote( $msg_min );
1831 }
1832 0 ▶ if( defined( $max_items ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_items) 0 0 0
1833 {
1834 0 my $msg_max = "array has more than $max_items items";
1835 0 $qmsg_max = _js_quote( $msg_max );
1836 }
1837  
1838 0 push( @body, <<JS_RUNTIME );
1839 if(_jsv_typeOf(inst) === 'array')
1840 {
1841 JS_RUNTIME
1842  
1843 0 ▶ if( defined( $min_items ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_items) 0 0 0
1844 {
1845 0 push( @body, <<JS_RUNTIME );
1846 if(inst.length < $min_items)
1847 {
1848 _jsv_err(ctx, path, 'minItems', $qmsg_min, $qsp);
1849 }
1850 JS_RUNTIME
1851 }
1852  
1853 0 ▶ if( defined( $max_items ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_items) 0 0 0
1854 {
1855 0 push( @body, <<JS_RUNTIME );
1856 if(inst.length > $max_items)
1857 {
1858 _jsv_err(ctx, path, 'maxItems', $qmsg_max, $qsp);
1859 }
1860 JS_RUNTIME
1861 }
1862  
1863 # Validate each existing tuple position before applying items to the remainder.
1864 0 for my $i ( 0 .. $#$prefix )
1865 {
1866 0 my $prefix_ptr = _join_ptr( $sp, 'prefixItems', $i );
1867 0 my $prefix_fn = $self->_compile_js_node( $prefix->[$i], $prefix_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1868 0 push( @body, <<JS_RUNTIME );
1869 if(inst.length > $i)
1870 {
1871 $prefix_fn(inst[$i], path + '/' + $i, ctx);
1872 if(ctx.maxErrors && ctx.errors.length >= ctx.maxErrors) return;
1873 }
1874 JS_RUNTIME
1875 }
1876  
1877 0 ▶ if( $items_fn )
Branch: 1 branch
BranchTrueFalseTotal
if ($items_fn) 0 0 0
1878 {
1879 0 my $start = scalar( @$prefix );
1880 0 push( @body, <<JS_RUNTIME );
1881 for(var i = $start; i < inst.length; i++)
1882 {
1883 $items_fn(inst[i], path + '/' + i, ctx);
1884 if(ctx.maxErrors && ctx.errors && ctx.errors.length >= ctx.maxErrors)
1885 {
1886 return;
1887 }
1888 }
1889 JS_RUNTIME
1890 }
1891  
1892 0 push( @body, <<JS_RUNTIME );
1893 }
1894 JS_RUNTIME
1895 }
1896  
1897 # NOTE: properties
1898 # 8) properties (objects) – recurse into children
1899 0 ▶ if( exists( $S->{properties} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"properties"} and ref $S->{"properties"} eq "HASH") 0 0 0
Condition0%0/3
exists $S->{"properties"} and ref $S->{"properties"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $S->{"properties"} and ref $S->{"properties"} eq "HASH"
exists $S->{"properties"} ref $S->{"properties"} eq "HASH"
Truth table0%0/3
A and B
A= exists $S->{"properties"}B= ref $S->{"properties"} eq "HASH"
ABresult
0X0
100
111
1900 ref( $S->{properties} ) eq 'HASH' )
1901 {
1902 0 push( @body, <<JS_RUNTIME );
1903 if(_jsv_typeOf(inst) === 'object')
1904 {
1905 JS_RUNTIME
1906  
1907 0 for my $p ( sort( keys( %{$S->{properties}} ) ) )
1908 {
1909 0 my $child = $S->{properties}->{ $p };
1910 0 my $child_ptr = _join_ptr( $sp, 'properties', $p );
1911 0 my $child_fn = $self->_compile_js_node( $child, $child_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1912  
1913 0 my $qp = _js_quote( $p );
1914 0 my $path_suffix = substr( _join_ptr( '#', $p ), 1 );
1915 0 my $path_suffix_q = _js_quote( $path_suffix );
1916  
1917 0 push( @body, <<JS_RUNTIME );
1918 if(_jsv_hasOwn(inst, $qp))
1919 {
1920 // Schema node
1921 $child_fn(inst[$qp], path + $path_suffix_q, ctx);
1922 }
1923 JS_RUNTIME
1924 }
1925  
1926 0 push( @body, <<JS_RUNTIME );
1927 }
1928 JS_RUNTIME
1929 }
1930  
1931 # NOTE: definitions – recurse into named schemas
1932 0 ▶ if( exists( $S->{definitions} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"definitions"} and ref $S->{"definitions"} eq "HASH") 0 0 0
Condition0%0/3
exists $S->{"definitions"} and ref $S->{"definitions"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $S->{"definitions"} and ref $S->{"definitions"} eq "HASH"
exists $S->{"definitions"} ref $S->{"definitions"} eq "HASH"
Truth table0%0/3
A and B
A= exists $S->{"definitions"}B= ref $S->{"definitions"} eq "HASH"
ABresult
0X0
100
111
1933 ref( $S->{definitions} ) eq 'HASH' )
1934 {
1935 0 for my $name ( sort keys %{ $S->{definitions} } )
1936 {
1937 0 my $child = $S->{definitions}->{ $name };
1938 0 my $child_ptr = _join_ptr( $sp, 'definitions', $name );
1939 0 my $child_fn = $self->_compile_js_node( $child, $child_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1940  
1941 # No runtime call needed here - definitions don't validate by themselves.
1942 # We just need them compiled so pointer-based lookup can see them.
1943 }
1944 }
1945  
1946 # NOTE: contains
1947 # 9) contains / minContains / maxContains (arrays)
1948 0 ▶ if( exists( $S->{contains} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"contains"}) 0 0 0
1949 {
1950 0 my $contains_schema = $S->{contains};
1951 0 my $contains_ptr = _join_ptr( $sp, 'contains' );
1952 0 my $contains_fn = $self->_compile_js_node( $contains_schema, $contains_ptr, $seen, $funcs, $counter_ref, $root, $opts );
1953 0 my $qsp_contains = _js_quote( $contains_ptr );
1954  
1955 0 my $have_min = exists( $S->{minContains} );
1956 0 my $have_max = exists( $S->{maxContains} );
1957 0 ▶ my $min = $have_min ? int( $S->{minContains} ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$have_min ? : 0 0 0
1958 0 ▶ my $max = $have_max ? int( $S->{maxContains} ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$have_max ? : 0 0 0
1959  
1960 0 my $msg_min = "array has fewer than $min items matching contains subschema";
1961 0 my $qmsg_min = _js_quote( $msg_min );
1962 0 my $msg_max = "array has more than $max items matching contains subschema";
1963 0 my $qmsg_max = _js_quote( $msg_max );
1964 0 my $msg_cont = "array does not contain any item matching contains subschema";
1965 0 my $qmsg_cont= _js_quote( $msg_cont );
1966  
1967 0 push( @body, <<JS_RUNTIME );
1968 if(_jsv_typeOf(inst) === 'array')
1969 {
1970 var matchCount = 0;
1971 for(var i = 0; i < inst.length; i++)
1972 {
1973 var tmpCtx = { errors: [], maxErrors: ctx.maxErrors };
1974 // Schema node
1975 $contains_fn(inst[i], path + '/' + i, tmpCtx);
1976 if(tmpCtx.errors.length === 0)
1977 {
1978 matchCount++;
1979 }
1980 }
1981 JS_RUNTIME
1982  
1983 0 ▶ if( $have_min )
Branch: 1 branch
BranchTrueFalseTotal
if ($have_min) 0 0 0
1984 {
1985 0 push( @body, <<JS_RUNTIME );
1986 if(matchCount < $min)
1987 {
1988 _jsv_err(ctx, path, 'minContains', $qmsg_min, $qsp_contains);
1989 }
1990 JS_RUNTIME
1991 }
1992  
1993 0 ▶ if( $have_max )
Branch: 1 branch
BranchTrueFalseTotal
if ($have_max) 0 0 0
1994 {
1995 0 push( @body, <<JS_RUNTIME );
1996 if(matchCount > $max)
1997 {
1998 _jsv_err(ctx, path, 'maxContains', $qmsg_max, $qsp_contains);
1999 }
2000 JS_RUNTIME
2001 }
2002  
2003 # Plain "contains" only if no min/max are present
2004 0 ▶ if( !$have_min )
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_min) 0 0 0
2005 {
2006 0 push( @body, <<JS_RUNTIME );
2007 if(matchCount === 0)
2008 {
2009 _jsv_err(ctx, path, 'contains', $qmsg_cont, $qsp_contains);
2010 }
2011 JS_RUNTIME
2012 }
2013  
2014 0 push( @body, <<JS_RUNTIME );
2015 }
2016 JS_RUNTIME
2017 }
2018  
2019 0 ▶ if( $has_ref )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ref) 0 0 0
2020 {
2021 0 push( @body, <<JS_RUNTIME );
2022 }
2023 finally
2024 {
2025 delete active[guard];
2026 }
2027 JS_RUNTIME
2028 }
2029 0 push( @body, <<JS_RUNTIME );
2030 }
2031 JS_RUNTIME
2032  
2033 0 push( @$funcs, join( '', @body ) );
2034  
2035 0 return( $fn );
2036 }
2037  
2038 sub _compile_node
2039 444 ▶{
Subroutine _compile_node: called CC=29 SCAR=34.3
2040 444 my( $root, $ptr, $S ) = @_;
2041  
2042 # Boolean subschemas use the same semantics as the interpreter.
2043 444 ▶ if( JSON::is_bool( $S ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool($S)) 22 422 444
2044 {
2045 return sub
2046 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2047 0 my( $ctx, $inst ) = @_;
2048 0 return( _v_node( $ctx, $ptr, $S, $inst ) );
2049 22 };
2050 }
2051  
2052 # Non-hash, non-boolean schemas retain the existing fallback behaviour.
2053 return sub
2054 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2055 0 my( $ctx, $inst ) = @_;
2056 0 return( { ok => 1, props => {}, items => {} } );
2057 422 ▶ } unless( ref( $S ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $S eq "HASH" 0 422 422
2058  
2059 # NOTE: $ref / $dynamicRef must not be compiled inline.
2060 # Delegate reference-bearing nodes to _v_node(), which evaluates the references
2061 # and their sibling keywords together. It bypasses the compiled closure for
2062 # these nodes to prevent recursive delegation.
2063 422 ▶ if( exists( $S->{'$ref'} ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"\$ref"} or exists $S->{"\$dynamicRef"}) 10 412 422
Condition100%3/3
exists $S->{"\$ref"} or exists $S->{"\$dynamicRef"}
l!l&&r!l&&!r
46412
MC/DC100%2/2
exists $S->{"\$ref"} or exists $S->{"\$dynamicRef"}
exists $S->{"\$ref"} exists $S->{"\$dynamicRef"}
Truth table100%3/3
A or B
A= exists $S->{"\$ref"}B= exists $S->{"\$dynamicRef"}
ABresult
1X1
011
000
2064 exists( $S->{'$dynamicRef'} ) )
2065 {
2066 return sub
2067 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2068 0 my( $ctx, $inst ) = @_;
2069 0 return( _v_node( $ctx, $ptr, $S, $inst ) );
2070 10 };
2071 }
2072  
2073 # Capture presence and values so runtime avoids hash lookups
2074 412 my $has_type = exists( $S->{type} );
2075 412 my $type_spec = $S->{type};
2076  
2077 412 my $has_const = exists( $S->{const} );
2078 412 my $const_val = $S->{const};
2079  
2080 412 my $has_enum = exists( $S->{enum} );
2081 412 my $enum_vals = $S->{enum};
2082  
2083 412 my %numk = map{ $_ => $S->{ $_ } } grep{ exists( $S->{ $_ } ) }
2084 qw( multipleOf minimum maximum exclusiveMinimum exclusiveMaximum );
2085  
2086 412 ▶ my $has_strlen = ( exists( $S->{minLength} ) || exists( $S->{maxLength} ) || exists( $S->{pattern} ) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $S->{'minLength'} || exists $S->{'maxLength'} || exists $S->{'pattern'} ? : 7 405 412
Condition83%5/6
exists $S->{"minLength"} or exists $S->{"maxLength"} or exists $S->{"pattern"}
l!l&&r!l&&!r
70405
exists $S->{"minLength"} or exists $S->{"maxLength"}
l!l&&r!l&&!r
43405
MC/DC66%2/3
exists $S->{"minLength"} or exists $S->{"maxLength"} or exists $S->{"pattern"}
exists $S->{"minLength"} exists $S->{"maxLength"} exists $S->{"pattern"}
Truth table75%3/4
A or B or C
A= exists $S->{"minLength"}B= exists $S->{"maxLength"}C= exists $S->{"pattern"}
ABCresult
1XX1
01X1
0011
0000
2087 412 my $has_format = exists( $S->{format} );
2088 412 my $format_name = $S->{format};
2089  
2090 my $has_unique_keys =
2091 exists( $S->{uniqueKeys} ) &&
2092 412 ▶ ref( $S->{uniqueKeys} ) eq 'ARRAY';
Condition33%1/3
exists $S->{'uniqueKeys'} && ref $S->{'uniqueKeys'} eq 'ARRAY'
!ll&&!rl&&r
41200
MC/DC0%0/2
exists $S->{'uniqueKeys'} && ref $S->{'uniqueKeys'} eq 'ARRAY'
exists $S->{'uniqueKeys'} ref $S->{'uniqueKeys'} eq 'ARRAY'
Truth table33%1/3
A && B
A= exists $S->{'uniqueKeys'}B= ref $S->{'uniqueKeys'} eq 'ARRAY'
ABresult
0X0
100
111
2093  
2094 # Precompile child closures (same structure our interpreter walks)
2095 412 my %child;
2096  
2097 # Arrays
2098 412 ▶ if( ref( $S->{prefixItems} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S->{"prefixItems"} eq "ARRAY") 7 405 412
2099 {
2100 7 for my $i ( 0 .. $#{ $S->{prefixItems} } )
2101 {
2102 7 my $cp = _join_ptr( $ptr, "prefixItems/$i" );
2103 7 $child{ "prefix:$i" } = _compile_node( $root, $cp, $S->{prefixItems}->[$i] );
2104 }
2105 }
2106 412 ▶ if( ref( $S->{items} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S->{"items"} eq "HASH") 4 408 412
2107 {
2108 4 $child{ "items" } = _compile_node( $root, _join_ptr( $ptr, "items" ), $S->{items} );
2109 }
2110 412 ▶ if( exists( $S->{contains} ) && ref( $S->{contains} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"contains"} and ref $S->{"contains"}) 8 404 412
Condition66%2/3
exists $S->{"contains"} and ref $S->{"contains"}
!ll&&!rl&&r
40408
MC/DC50%1/2
exists $S->{"contains"} and ref $S->{"contains"}
exists $S->{"contains"} ref $S->{"contains"}
Truth table66%2/3
A and B
A= exists $S->{"contains"}B= ref $S->{"contains"}
ABresult
0X0
100
111
2111 {
2112 8 $child{ "contains" } = _compile_node( $root, _join_ptr( $ptr, "contains" ), $S->{contains} );
2113 }
2114 412 ▶ if( exists( $S->{unevaluatedItems} ) && ref( $S->{unevaluatedItems} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"unevaluatedItems"} and ref $S->{"unevaluatedItems"} eq "HASH") 0 412 412
Condition66%2/3
exists $S->{"unevaluatedItems"} and ref $S->{"unevaluatedItems"} eq "HASH"
!ll&&!rl&&r
40390
MC/DC0%0/2
exists $S->{"unevaluatedItems"} and ref $S->{"unevaluatedItems"} eq "HASH"
exists $S->{"unevaluatedItems"} ref $S->{"unevaluatedItems"} eq "HASH"
Truth table66%2/3
A and B
A= exists $S->{"unevaluatedItems"}B= ref $S->{"unevaluatedItems"} eq "HASH"
ABresult
0X0
100
111
2115 {
2116 0 $child{ "unevaluatedItems" } = _compile_node( $root, _join_ptr( $ptr, "unevaluatedItems" ), $S->{unevaluatedItems} );
2117 }
2118  
2119 # Objects
2120 412 ▶ if( ref( $S->{properties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S->{"properties"} eq "HASH") 68 344 412
2121 {
2122 68 for my $k ( keys( %{$S->{properties}} ) )
2123 {
2124 74 my $cp = _join_ptr( $ptr, "properties/$k" );
2125 74 $child{ "prop:$k" } = _compile_node( $root, $cp, $S->{properties}->{ $k } );
2126 }
2127 }
2128 412 ▶ if( ref( $S->{patternProperties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $S->{"patternProperties"} eq "HASH") 4 408 412
2129 {
2130 4 for my $re ( keys( %{ $S->{patternProperties} } ) )
2131 {
2132 6 my $cp = _join_ptr( $ptr, "patternProperties/$re" );
2133 6 $child{ "pat:$re" } = _compile_node( $root, $cp, $S->{patternProperties}->{ $re } );
2134 }
2135 }
2136 412 ▶ if( exists( $S->{additionalProperties} ) && ref( $S->{additionalProperties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"additionalProperties"} and ref $S->{"additionalProperties"} eq "HASH") 0 412 412
Condition66%2/3
exists $S->{"additionalProperties"} and ref $S->{"additionalProperties"} eq "HASH"
!ll&&!rl&&r
399130
MC/DC0%0/2
exists $S->{"additionalProperties"} and ref $S->{"additionalProperties"} eq "HASH"
exists $S->{"additionalProperties"} ref $S->{"additionalProperties"} eq "HASH"
Truth table66%2/3
A and B
A= exists $S->{"additionalProperties"}B= ref $S->{"additionalProperties"} eq "HASH"
ABresult
0X0
100
111
2137 {
2138 0 $child{ "additional" } = _compile_node( $root, _join_ptr( $ptr, "additionalProperties" ), $S->{additionalProperties} );
2139 }
2140 412 ▶ if( exists( $S->{propertyNames} ) && ref( $S->{propertyNames} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH") 0 412 412
Condition66%2/3
exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH"
!ll&&!rl&&r
41020
MC/DC0%0/2
exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH"
exists $S->{"propertyNames"} ref $S->{"propertyNames"} eq "HASH"
Truth table66%2/3
A and B
A= exists $S->{"propertyNames"}B= ref $S->{"propertyNames"} eq "HASH"
ABresult
0X0
100
111
2141 {
2142 0 $child{ "propnames" } = _compile_node( $root, _join_ptr( $ptr, "propertyNames" ), $S->{propertyNames} );
2143 }
2144 412 ▶ if( exists( $S->{dependentSchemas} ) && ref( $S->{dependentSchemas} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"dependentSchemas"} and ref $S->{"dependentSchemas"} eq "HASH") 3 409 412
Condition66%2/3
exists $S->{"dependentSchemas"} and ref $S->{"dependentSchemas"} eq "HASH"
!ll&&!rl&&r
40903
MC/DC50%1/2
exists $S->{"dependentSchemas"} and ref $S->{"dependentSchemas"} eq "HASH"
exists $S->{"dependentSchemas"} ref $S->{"dependentSchemas"} eq "HASH"
Truth table66%2/3
A and B
A= exists $S->{"dependentSchemas"}B= ref $S->{"dependentSchemas"} eq "HASH"
ABresult
0X0
100
111
2145 {
2146 3 for my $k ( keys( %{$S->{dependentSchemas}} ) )
2147 {
2148 3 my $cp = _join_ptr( $ptr, "dependentSchemas/$k" );
2149 3 $child{ "deps:$k" } = _compile_node( $root, $cp, $S->{dependentSchemas}->{ $k } );
2150 }
2151 }
2152 412 ▶ if( exists( $S->{unevaluatedProperties} ) && ref( $S->{unevaluatedProperties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"unevaluatedProperties"} and ref $S->{"unevaluatedProperties"} eq "HASH") 2 410 412
Condition100%3/3
exists $S->{"unevaluatedProperties"} and ref $S->{"unevaluatedProperties"} eq "HASH"
!ll&&!rl&&r
386242
MC/DC100%2/2
exists $S->{"unevaluatedProperties"} and ref $S->{"unevaluatedProperties"} eq "HASH"
exists $S->{"unevaluatedProperties"} ref $S->{"unevaluatedProperties"} eq "HASH"
Truth table100%3/3
A and B
A= exists $S->{"unevaluatedProperties"}B= ref $S->{"unevaluatedProperties"} eq "HASH"
ABresult
0X0
100
111
2153 {
2154 2 $child{ "ueprops" } = _compile_node( $root, _join_ptr( $ptr, "unevaluatedProperties" ), $S->{unevaluatedProperties} );
2155 }
2156  
2157 # Return specialized validator
2158 return sub
2159 409 ▶ {
Subroutine __ANON__: called CC=37 SCAR=36.3
2160 409 my( $ctx, $inst ) = @_;
2161  
2162 # Parity with interpreter: trace node visit
2163 409 ▶ _t( $ctx, $ptr, 'node', undef, 'visit' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 12 397 409
2164  
2165 # Type / const / enum
2166 409 ▶ if( $has_type ) { _k_type( $ctx, $inst, $type_spec, $ptr ) or return( _fail() ); }
Branch: 2 branches
BranchTrueFalseTotal
if ($has_type) 163 246 409
_k_type($ctx, $inst, $type_spec, $ptr) or return _fail() 127 36 163
2167 373 ▶ if( $has_const ) { _k_const( $ctx, $inst, $const_val, $ptr ) or return( _fail() ); }
Branch: 2 branches
BranchTrueFalseTotal
if ($has_const) 22 351 373
_k_const($ctx, $inst, $const_val, $ptr) or return _fail() 9 13 22
2168 360 ▶ if( $has_enum ) { _k_enum( $ctx, $inst, $enum_vals, $ptr ) or return( _fail() ); }
Branch: 2 branches
BranchTrueFalseTotal
if ($has_enum) 21 339 360
_k_enum($ctx, $inst, $enum_vals, $ptr) or return _fail() 8 13 21
2169  
2170 # uniqueKeys extension (compiled path)
2171 347 ▶ if( $ctx->{unique_keys} && $has_unique_keys && ref( $inst ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"unique_keys"} and $has_unique_keys and ref $inst eq "ARRAY") 0 347 347
Condition33%2/6
$ctx->{"unique_keys"} and $has_unique_keys and ref $inst eq "ARRAY"
!ll&&!rl&&r
34700
$ctx->{"unique_keys"} and $has_unique_keys
!ll&&!rl&&r
34700
MC/DC0%0/3
$ctx->{"unique_keys"} and $has_unique_keys and ref $inst eq "ARRAY"
$ctx->{"unique_keys"} $has_unique_keys ref $inst eq "ARRAY"
Truth table25%1/4
A and B and C
A= $ctx->{"unique_keys"}B= $has_unique_keysC= ref $inst eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
2172 {
2173 0 my $r = _k_unique_keys( $ctx, $ptr, $S->{uniqueKeys}, $inst );
2174 0 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 0 0 0
2175 }
2176  
2177 # Numbers
2178 347 ▶ if( _is_number( $inst ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_number($inst)) 164 183 347
2179 {
2180 164 for my $k ( qw( multipleOf minimum maximum exclusiveMinimum exclusiveMaximum ) )
2181 {
2182 685 ▶ next unless( exists( $numk{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $numk{$k} 612 73 685
2183 73 ▶ _k_number( $ctx, $inst, $k, $numk{$k}, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_number($ctx, $inst, $k, $numk{$k}, $ptr) or return _fail() 38 35 73
2184 }
2185 }
2186  
2187 # Strings
2188 312 ▶ if( _match_type( $inst, 'string' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_match_type($inst, "string")) 46 266 312
2189 {
2190 46 ▶ if( $has_strlen ) { _k_string( $ctx, $inst, $S, $ptr ) or return( _fail() ); }
Branch: 2 branches
BranchTrueFalseTotal
if ($has_strlen) 6 40 46
_k_string($ctx, $inst, $S, $ptr) or return _fail() 4 2 6
2191 44 ▶ if( $has_format ) { _k_format( $ctx, $inst, $format_name, $ptr ) or return( _fail() ); }
Branch: 2 branches
BranchTrueFalseTotal
if ($has_format) 2 42 44
_k_format($ctx, $inst, $format_name, $ptr) or return _fail() 1 1 2
2192  
2193 # contentEncoding / contentMediaType / contentSchema (compiled path)
2194 43 ▶ if( $ctx->{content_assert} && ( exists( $S->{contentEncoding} ) || exists( $S->{contentMediaType} ) || exists( $S->{contentSchema} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"content_assert"} and exists $S->{"contentEncoding"} || exists $S->{"contentMediaType"} || exists $S->{"contentSchema"}) 14 29 43
Condition88%8/9
$ctx->{"content_assert"} and exists $S->{"contentEncoding"} || exists $S->{"contentMediaType"} || exists $S->{"contentSchema"}
!ll&&!rl&&r
24514
exists $S->{"contentEncoding"} or exists $S->{"contentMediaType"} or exists $S->{"contentSchema"}
l!l&&r!l&&!r
3115
exists $S->{"contentEncoding"} or exists $S->{"contentMediaType"}
l!l&&r!l&&!r
3016
MC/DC75%3/4
$ctx->{"content_assert"} and exists $S->{"contentEncoding"} || exists $S->{"contentMediaType"} || exists $S->{"contentSchema"}
$ctx->{"content_assert"} exists $S->{"contentEncoding"} exists $S->{"contentMediaType"} exists $S->{"contentSchema"}
Truth table80%4/5
A and B or C or D
A= $ctx->{"content_assert"}B= exists $S->{"contentEncoding"}C= exists $S->{"contentMediaType"}D= exists $S->{"contentSchema"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2195 {
2196 14 my $r = _validate_content( $ctx, $ptr, $S, $inst );
2197 14 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 9 5 14
2198 }
2199 }
2200  
2201 300 my %ann_props;
2202 my %ann_items;
2203  
2204 # Arrays
2205 300 ▶ if( ref( $inst ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inst eq "ARRAY") 22 278 300
2206 {
2207 22 my $r = _k_array_all( $ctx, $ptr, $S, $inst );
2208 22 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 6 16 22
2209 16 %ann_items = ( %ann_items, %{$r->{items}} );
2210 }
2211  
2212 # Objects
2213 294 ▶ if( ref( $inst ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inst eq "HASH") 113 181 294
2214 {
2215 113 my $r = _k_object_all( $ctx, $ptr, $S, $inst );
2216 113 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 33 80 113
2217 80 %ann_props = ( %ann_props, %{$r->{props}} );
2218 }
2219  
2220 # Combinators and conditionals share the interpreter's annotation semantics.
2221 261 for my $comb ( qw( allOf anyOf oneOf not ) )
2222 {
2223 997 ▶ next unless( exists( $S->{ $comb } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $S->{$comb} 955 42 997
2224 42 my $r = _k_combinator( $ctx, $ptr, $S, $inst, $comb );
2225 42 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 20 22 42
2226 22 %ann_props = ( %ann_props, %{$r->{props}} );
2227 22 %ann_items = ( %ann_items, %{$r->{items}} );
2228 }
2229 241 ▶ if( exists( $S->{if} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"if"}) 5 236 241
2230 {
2231 5 my $r = _k_if_then_else( $ctx, $ptr, $S, $inst );
2232 5 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 3 2 5
2233 2 %ann_props = ( %ann_props, %{$r->{props}} );
2234 2 %ann_items = ( %ann_items, %{$r->{items}} );
2235 }
2236  
2237 238 my $r = _k_unevaluated( $ctx, $ptr, $S, $inst, \%ann_props, \%ann_items );
2238 238 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 11 227 238
2239  
2240 227 ▶ _t( $ctx, $ptr, 'node', undef, 'pass' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 8 219 227
2241 227 return( { ok => 1, props => \%ann_props, items => \%ann_items } );
2242 412 };
2243 }
2244  
2245 # Compilation / Indexing
2246 sub _compile_root
2247 540 ▶{
Subroutine _compile_root: called CC=1 SCAR=0.0
2248 540 my( $schema ) = @_;
2249 # Retain the original schema and index each resource relative to its containing resource.
2250 540 my $index = { resources => {}, nodes => {} };
2251 540 my $base = _index_schema_202012( $schema, '', '#', $index, 1 );
2252 return({
2253 540 schema => $schema,
2254 base => $base,
2255 resource_index => $index,
2256 fn_index => {},
2257 });
2258 }
2259  
2260 sub _content_decode
2261 12 ▶{
Subroutine _content_decode: called CC=5 SCAR=16.7
2262 12 my( $ctx, $enc, $s ) = @_;
2263 12 ▶ $enc = lc( $enc // '' );
Condition50%1/2
$enc // ""
l!l
120
MC/DC0%0/1
$enc // ""
$enc
Truth table50%1/2
A
A= $enc
Aresult
11
00
2264  
2265 12 ▶ if( my $cb = $ctx->{content_decoders}->{ $enc } )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $ctx->{"content_decoders"}{$enc}) 2 10 12
2266 {
2267 2 my( $ok, $msg, $out ) = _safe_invoke( $cb, $s );
2268 2 ▶ return( $ok ? $out : undef );
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 2 0 2
2269 }
2270  
2271 10 ▶ if( $enc eq 'base64' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "base64") 10 0 10
2272 {
2273 10 my $out = _strict_base64_decode( $s );
2274 # undef on failure is exactly what we want
2275 10 return( $out );
2276 }
2277  
2278 # Unknown encoding (annotation only unless assert)
2279 0 return;
2280 }
2281  
2282 # Errors, utils, pointers, URIs
2283 sub _copy_resource_index
2284 718 ▶{
Subroutine _copy_resource_index: called CC=2 SCAR=6.9
2285 718 my( $index ) = @_;
2286 718 my %resources;
2287 718 for my $uri ( keys( %{$index->{resources}} ) )
2288 {
2289 732 my $entry = $index->{resources}->{ $uri };
2290 732 $resources{ $uri } = { %$entry, anchors => { %{$entry->{anchors}} } };
2291 }
2292 718 return( { resources => \%resources, nodes => { %{$index->{nodes}} } } );
2293 }
2294  
2295 sub _decode_fragment
2296 201 ▶{
Subroutine _decode_fragment: called CC=2 SCAR=7.0
2297 201 my( $fragment ) = @_;
2298 201 my $bytes = Encode::encode( 'UTF-8', $fragment );
2299 201 ▶ die( 'Invalid URI percent escape' ) if( $bytes =~ /%(?![0-9A-Fa-f]{2})/ );
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /%(?![0-9A-Fa-f]{2})/u 0 201 201
2300 201 $bytes =~ s/%([0-9A-Fa-f]{2})/chr( hex( $1 ) )/eg;
2301 201 return( Encode::decode( 'UTF-8', $bytes, Encode::FB_CROAK ) );
2302 }
2303  
2304 sub _decimal_multiple
2305 122 ▶{
Subroutine _decimal_multiple: called CC=20 SCAR=30.2
2306 122 my( $value, $divisor ) = @_;
2307 122 my( $vs, $v, $ve ) = _decimal_parts( $value );
2308 122 my( $ds, $d, $de ) = _decimal_parts( $divisor );
2309 122 ▶ return(0) unless( defined( $v ) && defined( $d ) && $ds eq '' && $d ne '0' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and defined $d and $ds eq "" and $d ne 0 0 122 122
Condition33%3/9
defined $v and defined $d and $ds eq "" and $d ne 0
!ll&&!rl&&r
00122
defined $v and defined $d and $ds eq ""
!ll&&!rl&&r
00122
defined $v and defined $d
!ll&&!rl&&r
00122
MC/DC0%0/4
defined $v and defined $d and $ds eq "" and $d ne 0
defined $v defined $d $ds eq "" $d ne 0
Truth table20%1/5
A and B and C and D
A= defined $vB= defined $dC= $ds eq ""D= $d ne 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2310 122 ▶ return(1) if( $v eq '0' );
Branch: 1 branch
BranchTrueFalseTotal
if $v eq 0 10 112 122
2311  
2312 # Neither coefficient has trailing zeroes, so a negative exponent difference cannot yield an integer quotient.
2313 112 my $shift = $ve - $de;
2314 112 ▶ return(0) if( $shift < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $shift < 0 32 80 112
2315 80 ▶ return(1) if( $d eq '1' );
Branch: 1 branch
BranchTrueFalseTotal
if $d eq 1 24 56 80
2316 56 my $digits = $v . ( '0' x $shift );
2317  
2318 # Keep the intermediate remainder within the signed 32-bit integer range for the common case.
2319 56 ▶ if( length( $d ) <= 8 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $d <= 8) 24 32 56
2320 {
2321 24 my $rem = 0;
2322 24 $rem = ( $rem * 10 + $_ ) % $d for( split( //, $digits ) );
2323 24 ▶ return( $rem == 0 ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rem == 0 ? : 15 9 24
2324 }
2325  
2326 # Decimal long division avoids native integer overflow for larger coefficients.
2327 32 my $rem = '0';
2328 32 for my $digit ( split( //, $digits ) )
2329 {
2330 672 ▶ $rem = $rem eq '0' ? $digit : $rem . $digit;
Branch: 1 branch
BranchTrueFalseTotal
$rem eq '0' ? : 72 600 672
2331 672 ▶ while( length( $rem ) > length( $d ) || ( length( $rem ) == length( $d ) && $rem ge $d ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $rem > length $d or length $rem == length $d and $rem ge $d) 1260 672 1932
Condition100%6/6
length $rem > length $d or length $rem == length $d and $rem ge $d
l!l&&r!l&&!r
1401120672
length $rem == length $d and $rem ge $d
!ll&&!rl&&r
596761120
MC/DC100%3/3
length $rem > length $d or length $rem == length $d and $rem ge $d
length $rem > length $d length $rem == length $d $rem ge $d
Truth table100%4/4
A or B and C
A= length $rem > length $dB= length $rem == length $dC= $rem ge $d
ABCresult
1XX1
0111
00X0
0100
2332 {
2333 1260 my $borrow = 0;
2334 1260 my $j = length( $d ) - 1;
2335 1260 ▶ for( my $i = length( $rem ) - 1; $i >= 0; $i-- )
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 13496 1260 14756
2336 {
2337 13496 ▶ my $n = substr( $rem, $i, 1 ) - $borrow - ( $j >= 0 ? substr( $d, $j--, 1 ) : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$j >= 0 ? : 13356 140 13496
2338 13496 ▶ $borrow = $n < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 3088 10408 13496
2339 13496 substr( $rem, $i, 1 ) = $n + 10 * $borrow;
2340 }
2341 1260 $rem =~ s/\A0+(?=[0-9])//;
2342 }
2343 }
2344 32 ▶ return( $rem eq '0' ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$rem eq '0' ? : 16 16 32
2345 }
2346  
2347 sub _decimal_parts
2348 364 ▶{
Subroutine _decimal_parts: called CC=6 SCAR=17.9
2349 364 my( $value ) = @_;
2350 364 my $json = _canon( $value );
2351 364 ▶ return unless( $json =~ /\A(-?)(0|[1-9][0-9]*)(?:\.([0-9]+))?(?:[eE]([+-]?[0-9]+))?\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $json =~ /\A(-?)(0|[1-9][0-9]*)(?:\.([0-9]+))?(?:[eE]([+-]?[0-9]+))?\z/u 0 364 364
2352 364 ▶ my( $sign, $whole, $fraction, $exponent ) = ( $1, $2, $3 // '', $4 // 0 );
Condition100%4/4
$3 // ""
l!l
99265
$4 // 0
l!l
66298
MC/DC100%1/1
$3 // ""
$3
MC/DC100%1/1
$4 // 0
$4
Truth table100%2/2
A
A= $3
Aresult
11
00
Truth table100%2/2
A
A= $4
Aresult
11
00
2353 364 my $digits = $whole . $fraction;
2354 364 $digits =~ s/\A0+//;
2355 364 ▶ return( '', '0', 0 ) unless( length( $digits ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $digits 26 338 364
2356 338 $exponent -= length( $fraction );
2357 338 ▶ if( $digits =~ s/(0+)\z// )
Branch: 1 branch
BranchTrueFalseTotal
if ($digits =~ s/(0+)\z//u) 28 310 338
2358 {
2359 28 $exponent += length( $1 );
2360 }
2361 338 return( $sign, $digits, $exponent );
2362 }
2363  
2364 sub _decode_json_content
2365 34 ▶{
Subroutine _decode_json_content: called CC=1 SCAR=0.0
2366 34 my( $text ) = @_;
2367 34 return( JSON->new->allow_nonref(1)->decode( $text ) );
2368 }
2369  
2370 # Errors, utils, pointers, URIs
2371 sub _err
2372 508 ▶{
Subroutine _err: called CC=6 SCAR=18.4
2373 508 my( $ctx, $schema_ptr, $msg, $keyword ) = @_;
2374 508 ▶ return(0) if( $ctx->{max_errors} && $ctx->{error_count} >= $ctx->{max_errors} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"} 2 506 508
Condition100%3/3
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
!ll&&!rl&&r
84982
MC/DC100%2/2
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
$ctx->{"max_errors"} $ctx->{"error_count"} >= $ctx->{"max_errors"}
Truth table100%3/3
A and B
A= $ctx->{"max_errors"}B= $ctx->{"error_count"} >= $ctx->{"max_errors"}
ABresult
0X0
100
111
2375  
2376 # Instance path: use current ptr_stack top if available, else '#'
2377 506 ▶ my $inst_path = $ctx->{ptr_stack} && @{$ctx->{ptr_stack}}
Condition50%1/2
$ctx->{"ptr_stack"}[-1] // "#"
l!l
5060
MC/DC0%0/1
$ctx->{"ptr_stack"}[-1] // "#"
$ctx->{"ptr_stack"}[-1]
Truth table50%1/2
A
A= $ctx->{"ptr_stack"}[-1]
Aresult
11
00
2378 506 ▶ ? ( $ctx->{ptr_stack}->[-1] // '#' )
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{'ptr_stack'} && @{$ctx->{'ptr_stack'};} ? : 506 0 506
Condition33%1/3
$ctx->{"ptr_stack"} and @{$ctx->{"ptr_stack"};}
!ll&&!rl&&r
00506
MC/DC0%0/2
$ctx->{"ptr_stack"} and @{$ctx->{"ptr_stack"};}
$ctx->{"ptr_stack"} @{$ctx->{"ptr_stack"};}
Truth table33%1/3
A and B
A= $ctx->{"ptr_stack"}B= @{$ctx->{"ptr_stack"};}
ABresult
0X0
100
111
2379 : '#';
2380  
2381 506 push( @{$ctx->{errors}}, JSON::Schema::Validate::Error->new(
2382 path => $inst_path,
2383 message => $msg,
2384 keyword => $keyword, # may be undef (back-compat)
2385 schema_pointer => $schema_ptr, # where in the schema this came from
2386 ));
2387 506 $ctx->{error_count}++;
2388 506 return(0);
2389 }
2390  
2391 sub _err_res
2392 219 ▶{
Subroutine _err_res: called CC=1 SCAR=0.0
2393 219 my( $ctx, $schema_ptr, $msg, $keyword ) = @_;
2394 219 _err( $ctx, $schema_ptr, $msg, $keyword );
2395 219 return( { ok => 0, props => {}, items => {} } );
2396 }
2397  
2398 # Used by _prune* methods
2399 sub _extract_array_shape
2400 1 ▶{
Subroutine _extract_array_shape: called CC=10 SCAR=28.2
2401 1 my( $self, $schema, $out ) = @_;
2402  
2403 1 ▶ return unless( ref( $schema ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 1 1
2404  
2405 1 ▶ if( ref( $schema->{prefixItems} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"prefixItems"} eq "ARRAY") 1 0 1
2406 {
2407 1 my $src = $schema->{prefixItems};
2408 1 for my $i ( 0 .. $#$src )
2409 {
2410 # First win: do not override an existing prefix schema at this index
2411 $out->{prefix_items}->[ $i ] = $src->[ $i ]
2412 1 ▶ unless( defined( $out->{prefix_items}->[ $i ] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out->{"prefix_items"}[$i] 1 0 1
2413 }
2414 }
2415  
2416 1 ▶ if( exists( $schema->{items} ) && ref( $schema->{items} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"items"} and ref $schema->{"items"} eq "HASH") 1 0 1
Condition33%1/3
exists $schema->{"items"} and ref $schema->{"items"} eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $schema->{"items"} and ref $schema->{"items"} eq "HASH"
exists $schema->{"items"} ref $schema->{"items"} eq "HASH"
Truth table33%1/3
A and B
A= exists $schema->{"items"}B= ref $schema->{"items"} eq "HASH"
ABresult
0X0
100
111
2417 {
2418 # Again, first win: if we already have items from another branch, keep it.
2419 1 ▶ $out->{items} = $schema->{items} unless( $out->{items} );
Branch: 1 branch
BranchTrueFalseTotal
unless $out->{"items"} 1 0 1
2420 }
2421  
2422 # allOf mixins for arrays as well
2423 1 ▶ if( ref( $schema->{allOf} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"allOf"} eq "ARRAY") 0 1 1
2424 {
2425 0 foreach my $sub ( @{$schema->{allOf}} )
2426 {
2427 0 $self->_extract_array_shape( $sub, $out );
2428 }
2429 }
2430  
2431 # anyOf / oneOf / not ignored for same reason as objects.
2432 }
2433  
2434 # Used by _prune* methods
2435 sub _extract_object_shape
2436 8 ▶{
Subroutine _extract_object_shape: called CC=17 SCAR=31.1
2437 8 my( $self, $schema, $out ) = @_;
2438  
2439 8 ▶ return unless( ref( $schema ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 8 8
2440  
2441 # Direct properties
2442 8 ▶ if( ref( $schema->{properties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"properties"} eq "HASH") 6 2 8
2443 {
2444 6 foreach my $k ( keys( %{$schema->{properties}} ) )
2445 {
2446 # First win: do not override an already-collected subschema
2447 $out->{props}->{ $k } = $schema->{properties}->{ $k }
2448 7 ▶ unless( exists( $out->{props}->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out->{"props"}{$k} 7 0 7
2449 }
2450 }
2451  
2452 # patternProperties
2453 8 ▶ if( ref( $schema->{patternProperties} ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"patternProperties"} eq "HASH") 1 7 8
2454 {
2455 1 foreach my $re ( keys( %{$schema->{patternProperties}} ) )
2456 {
2457 1 push( @{$out->{patterns}}, [ $re, $schema->{patternProperties}->{ $re } ] );
2458 }
2459 }
2460  
2461 # additionalProperties
2462 8 ▶ if( exists( $schema->{additionalProperties} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"additionalProperties"}) 7 1 8
2463 {
2464 7 my $ap = $schema->{additionalProperties};
2465  
2466 # JSON booleans or plain scalars
2467 7 ▶ if( !ref( $ap ) || ( blessed( $ap ) && $ap->isa( 'JSON::PP::Boolean' ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $ap or builtin::blessed($ap) and $ap->isa('JSON::PP::Boolean')) { } 7 0 7
elsif (ref $ap eq 'HASH') { } 0 0 0
Condition33%2/6
not ref $ap or builtin::blessed($ap) and $ap->isa("JSON::PP::Boolean")
l!l&&r!l&&!r
070
builtin::blessed($ap) and $ap->isa("JSON::PP::Boolean")
!ll&&!rl&&r
007
MC/DC0%0/3
not ref $ap or builtin::blessed($ap) and $ap->isa("JSON::PP::Boolean")
not ref $ap builtin::blessed($ap) $ap->isa("JSON::PP::Boolean")
Truth table25%1/4
not(A) or B and C
A= not ref $apB= builtin::blessed($ap)C= $ap->isa("JSON::PP::Boolean")
ABCresult
1XX1
0111
00X0
0100
2468 {
2469 7 ▶ if( $ap )
Branch: 1 branch
BranchTrueFalseTotal
if ($ap) { } 1 6 7
2470 {
2471 # true: additionalProperties allowed; keep any stricter setting we might already have
2472 1 ▶ $out->{allow_ap} = 1 unless( defined( $out->{allow_ap} ) && !$out->{allow_ap} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out->{"allow_ap"} and not $out->{"allow_ap"} 1 0 1
Condition33%1/3
defined $out->{"allow_ap"} and not $out->{"allow_ap"}
!ll&&!rl&&r
010
MC/DC0%0/2
defined $out->{"allow_ap"} and not $out->{"allow_ap"}
defined $out->{"allow_ap"} not $out->{"allow_ap"}
Truth table33%1/3
A and not(B)
A= defined $out->{"allow_ap"}B= not $out->{"allow_ap"}
ABresult
0X0
100
111
2473 # do not touch ap_schema here
2474 }
2475 else
2476 {
2477 # false: forbidden regardless of earlier "true"
2478 6 $out->{allow_ap} = 0;
2479 6 $out->{ap_schema} = undef;
2480 }
2481 }
2482 elsif( ref( $ap ) eq 'HASH' )
2483 {
2484 # Schema for additionals
2485 0 $out->{allow_ap} = 1;
2486 0 $out->{ap_schema} = $ap;
2487 }
2488 }
2489  
2490 # allOf mixins: merge their object keywords as well.
2491 8 ▶ if( ref( $schema->{allOf} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema->{"allOf"} eq "ARRAY") 1 7 8
2492 {
2493 1 foreach my $sub ( @{$schema->{allOf}} )
2494 {
2495 2 $self->_extract_object_shape( $sub, $out );
2496 }
2497 }
2498  
2499 # NOTE:
2500 # We intentionally ignore anyOf / oneOf / not for pruning.
2501 # Without performing full validation against each branch, we cannot
2502 # safely decide which properties are truly forbidden, so we err on
2503 # the side of *keeping* more rather than over-pruning.
2504 }
2505  
2506 289 ▶sub _fail { return( { ok => 0, props => {}, items => {} } ); }
Subroutine _fail: called CC=1 SCAR=0.0
2507  
2508 sub _first_error_text
2509 0 ▶{
Subroutine _first_error_text: not called CC=2 SCAR=17.9
2510 0 my( $errs ) = @_;
2511 0 ▶ return( '' ) unless( @$errs );
Branch: 1 branch
BranchTrueFalseTotal
unless @$errs 0 0 0
2512 0 my $e = $errs->[0];
2513 0 return( "$e" );
2514 }
2515  
2516 sub _get_args_as_hash
2517 1227 ▶{
Subroutine _get_args_as_hash: called CC=7 SCAR=20.2
2518 1227 my $self = shift( @_ );
2519 1227 ▶ return( {} ) if( !scalar( @_ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 778 449 1227
2520 449 my $ref = {};
2521 449 ▶ if( scalar( @_ ) == 1 &&
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and defined $_[0] and (ref $_[0] || '') eq 'HASH') { } 2 447 449
elsif (not scalar(@_) % 2) { } 447 0 447
Condition62%5/8
scalar @_ == 1 and defined $_[0] and (ref $_[0] || "") eq "HASH"
!ll&&!rl&&r
44702
scalar @_ == 1 and defined $_[0]
!ll&&!rl&&r
44702
ref $_[0] || ''
l!l
20
MC/DC33%1/3
scalar @_ == 1 and defined $_[0] and (ref $_[0] || "") eq "HASH"
scalar @_ == 1 defined $_[0] ref $_[0]
Truth table50%2/4
A and B and C
A= scalar @_ == 1B= defined $_[0]C= ref $_[0]
ABCresult
0XX0
10X0
1100
1111
2522 defined( $_[0] ) &&
2523 ( ref( $_[0] ) || '' ) eq 'HASH' )
2524 {
2525 2 $ref = shift( @_ );
2526 }
2527 elsif( !( scalar( @_ ) % 2 ) )
2528 {
2529 447 $ref = { @_ };
2530 }
2531 else
2532 {
2533 0 die( "Uneven number of parameters provided: '", join( "', '", map( overload::StrVal( $_ ), @_ ) ), "'" );
2534 }
2535 449 return( $ref );
2536 }
2537  
2538 sub _index_schema_202012
2539 1693 ▶{
Subroutine _index_schema_202012: called CC=23 SCAR=31.7
2540 1693 my( $node, $base, $ptr, $index, $resource_root ) = @_;
2541 1693 ▶ if( ref( $node ) eq 'HASH' && defined( $node->{'$id'} ) && !ref( $node->{'$id'} ) && length( $node->{'$id'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node eq "HASH" and defined $node->{"\$id"} and not ref $node->{"\$id"} and length $node->{"\$id"}) 61 1632 1693
Condition77%7/9
ref $node eq "HASH" and defined $node->{"\$id"} and not ref $node->{"\$id"} and length $node->{"\$id"}
!ll&&!rl&&r
1632061
ref $node eq "HASH" and defined $node->{"\$id"} and not ref $node->{"\$id"}
!ll&&!rl&&r
1632061
ref $node eq "HASH" and defined $node->{"\$id"}
!ll&&!rl&&r
292134061
MC/DC50%2/4
ref $node eq "HASH" and defined $node->{"\$id"} and not ref $node->{"\$id"} and length $node->{"\$id"}
ref $node eq "HASH" defined $node->{"\$id"} not ref $node->{"\$id"} length $node->{"\$id"}
Truth table60%3/5
A and B and not(C) and D
A= ref $node eq "HASH"B= defined $node->{"\$id"}C= not ref $node->{"\$id"}D= length $node->{"\$id"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2542 {
2543 61 $base = _resolve_uri( $base, $node->{'$id'} );
2544 61 $base =~ s/#$//;
2545 61 $resource_root = 1;
2546 }
2547 1693 ▶ if( $resource_root )
Branch: 1 branch
BranchTrueFalseTotal
if ($resource_root) 590 1103 1693
2548 {
2549 590 ▶ die( "Duplicate schema resource identifier: $base" ) if( exists( $index->{resources}->{ $base } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{"resources"}{$base} 0 590 590
2550 590 $index->{resources}->{ $base } = { schema => $node, pointer => $ptr, base => $base, anchors => {} };
2551 }
2552 1693 ▶ return( $base ) unless( ref( $node ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 292 1401 1693
2553 1401 $index->{nodes}->{ refaddr( $node ) } = { base => $base, pointer => $ptr };
2554 1401 my $resource = $index->{resources}->{ $base };
2555 1401 for my $kw ( '$anchor', '$dynamicAnchor' )
2556 {
2557 2802 ▶ next unless( defined( $node->{ $kw } ) && !ref( $node->{ $kw } ) && length( $node->{ $kw } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{$kw} and not ref $node->{$kw} and length $node->{$kw} 2771 31 2802
Condition66%4/6
defined $node->{$kw} and not ref $node->{$kw} and length $node->{$kw}
!ll&&!rl&&r
2771031
defined $node->{$kw} and not ref $node->{$kw}
!ll&&!rl&&r
2771031
MC/DC33%1/3
defined $node->{$kw} and not ref $node->{$kw} and length $node->{$kw}
defined $node->{$kw} not ref $node->{$kw} length $node->{$kw}
Truth table50%2/4
A and not(B) and C
A= defined $node->{$kw}B= not ref $node->{$kw}C= length $node->{$kw}
ABCresult
0XX0
10X0
1100
1111
2558 31 my $name = $node->{ $kw };
2559 31 my $existing = $resource->{anchors}->{ $name };
2560 31 ▶ die( "Duplicate schema anchor: $base#$name" ) if( $existing && refaddr( $existing->{schema} ) != refaddr( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
if $existing and builtin::refaddr($existing->{"schema"}) != builtin::refaddr($node) 0 31 31
Condition33%1/3
$existing and builtin::refaddr($existing->{"schema"}) != builtin::refaddr($node)
!ll&&!rl&&r
3100
MC/DC0%0/2
$existing and builtin::refaddr($existing->{"schema"}) != builtin::refaddr($node)
$existing builtin::refaddr($existing->{"schema"}) != builtin::refaddr($node)
Truth table33%1/3
A and B
A= $existingB= builtin::refaddr($existing->{"schema"}) != builtin::refaddr($node)
ABresult
0X0
100
111
2561 31 ▶ $resource->{anchors}->{ $name } = { schema => $node, pointer => $ptr, dynamic => ( $kw eq '$dynamicAnchor' ? 1 : 0 ) };
Branch: 1 branch
BranchTrueFalseTotal
$kw eq '$dynamicAnchor' ? : 20 11 31
2562 }
2563  
2564 # Only schema-bearing keywords may declare resources or anchors. Literal values and property names cannot do so.
2565 1401 for my $kw ( qw( properties patternProperties dependentSchemas definitions $defs ) )
2566 {
2567 7005 ▶ next unless( ref( $node->{ $kw } ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node->{$kw} eq "HASH" 6597 408 7005
2568 408 for my $key ( sort( keys( %{$node->{ $kw }} ) ) )
2569 {
2570 530 _index_schema_202012( $node->{ $kw }->{ $key }, $base, _join_ptr( $ptr, $kw, $key ), $index, 0 );
2571 }
2572 }
2573 1401 for my $kw ( qw( allOf anyOf oneOf prefixItems type ) )
2574 {
2575 7005 ▶ next unless( ref( $node->{ $kw } ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node->{$kw} eq "ARRAY" 6889 116 7005
2576 116 for my $i ( 0 .. $#{$node->{ $kw }} )
2577 {
2578 214 _index_schema_202012( $node->{ $kw }->[$i], $base, _join_ptr( $ptr, $kw, $i ), $index, 0 );
2579 }
2580 }
2581 1401 for my $kw ( qw( not if then else items contains propertyNames additionalProperties unevaluatedProperties unevaluatedItems contentSchema ) )
2582 {
2583 15411 ▶ next unless( exists( $node->{ $kw } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $node->{$kw} 15042 369 15411
2584 369 _index_schema_202012( $node->{ $kw }, $base, _join_ptr( $ptr, $kw ), $index, 0 );
2585 }
2586 1401 return( $base );
2587 }
2588  
2589 sub _inst_addr
2590 1735 ▶{
Subroutine _inst_addr: called CC=2 SCAR=6.9
2591 1735 my( $inst, $ptr ) = @_;
2592 # Decoded content can contain a different scalar at the same instance path.
2593 # Preserve both its JSON type and value when identifying an active evaluation.
2594 1735 ▶ return( 'SCALAR:' . _canon( [$ptr, $inst] ) ) unless( ref( $inst ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inst 1049 686 1735
2595 686 return( ref( $inst ) . ':' . refaddr( $inst ) );
2596 }
2597  
2598 # Keyword groups
2599 # NOTE: instance pointer base
2600 # The pointer stack tracks the location of the failing value inside the *instance*,
2601 # which is what a user interface needs in order to highlight the offending field.
2602 # It must therefore be built from the current instance pointer, never from the
2603 # schema pointer, otherwise the reported path drifts into the schema as soon as
2604 # validation descends through a $ref or a combinator.
2605 sub _inst_ptr
2606 928 ▶{
Subroutine _inst_ptr: called CC=4 SCAR=15.6
2607 928 my( $ctx, $token ) = @_;
2608 928 ▶ my $base = ( $ctx->{ptr_stack} && @{$ctx->{ptr_stack}} )
Condition50%1/2
$ctx->{"ptr_stack"}[-1] // "#"
l!l
9280
MC/DC0%0/1
$ctx->{"ptr_stack"}[-1] // "#"
$ctx->{"ptr_stack"}[-1]
Truth table50%1/2
A
A= $ctx->{"ptr_stack"}[-1]
Aresult
11
00
2609 928 ▶ ? ( $ctx->{ptr_stack}->[-1] // '#' )
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{'ptr_stack'} && @{$ctx->{'ptr_stack'};} ? : 928 0 928
Condition33%1/3
$ctx->{"ptr_stack"} and @{$ctx->{"ptr_stack"};}
!ll&&!rl&&r
00928
MC/DC0%0/2
$ctx->{"ptr_stack"} and @{$ctx->{"ptr_stack"};}
$ctx->{"ptr_stack"} @{$ctx->{"ptr_stack"};}
Truth table33%1/3
A and B
A= $ctx->{"ptr_stack"}B= @{$ctx->{"ptr_stack"};}
ABresult
0X0
100
111
2610 : '#';
2611 928 return( _join_ptr( $base, $token ) );
2612 }
2613  
2614 # truthy helpers
2615 48 ▶sub _is_hash { my $v = shift; return ref($v) eq 'HASH' ? 1 : 0; }
Subroutine _is_hash: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 9 39 48
2616  
2617 sub _is_number
2618 2690 ▶{
Subroutine _is_number: called CC=6 SCAR=18.0
2619 2690 my( $v ) = @_;
2620  
2621 2690 ▶ return(0) if( ref( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 647 2043 2690
2622 2043 ▶ return(0) unless( defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 16 2027 2043
2623  
2624 # Strict JSON typing: accept only scalars that actually carry numeric flags.
2625 # JSON marks numbers with IOK/NOK; plain strings (even "12") will not have them.
2626 2027 my $sv = B::svref_2object( \$v );
2627 2027 my $flags = $sv->FLAGS;
2628  
2629 2027 local $@;
2630 # SVf_IOK = 0x02000000, SVf_NOK = 0x04000000 on most builds;
2631 # we do not hardcode constants-B::SV’s FLAGS is stable to test with these bitmasks.
2632 # Use string eval to avoid importing platform-specific constants.
2633 2027 ▶ my $SVf_IOK = eval{ B::SVf_IOK() } || 0x02000000;
Condition50%1/2
eval { do { 256 } } || 33554432
l!l
20270
MC/DC0%0/1
eval { do { 256 } } || 33554432
eval { do { 256 } }
Truth table50%1/2
A
A= eval { do { 256 } }
Aresult
11
00
2634 2027 ▶ my $SVf_NOK = eval{ B::SVf_NOK() } || 0x04000000;
Condition50%1/2
eval { do { 512 } } || 67108864
l!l
20270
MC/DC0%0/1
eval { do { 512 } } || 67108864
eval { do { 512 } }
Truth table50%1/2
A
A= eval { do { 512 } }
Aresult
11
00
2635  
2636 2027 ▶ return( ( $flags & ( $SVf_IOK | $SVf_NOK ) ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$flags & ($SVf_IOK | $SVf_NOK) ? : 1333 694 2027
2637 }
2638  
2639 52 ▶sub _is_true { my $v = shift( @_ ); return( ref( $v ) eq 'HASH' ? 0 : $v ? 1 : 0 ); }
Subroutine _is_true: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 9 43 52
$v ? : 4 39 43
2640  
2641 sub _join_ptr
2642 3084 ▶{
Subroutine _join_ptr: called CC=6 SCAR=18.4
2643 3084 my( $base, @tokens ) = @_;
2644  
2645 # Default base to '#' if not provided
2646 3084 ▶ $base = '#' unless( defined( $base ) && length( $base ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $base and length $base 0 3084 3084
Condition33%1/3
defined $base and length $base
!ll&&!rl&&r
003084
MC/DC0%0/2
defined $base and length $base
defined $base length $base
Truth table33%1/3
A and B
A= defined $baseB= length $base
ABresult
0X0
100
111
2647  
2648 3084 my $ptr = $base;
2649  
2650 3084 for my $token ( @tokens )
2651 {
2652 3828 ▶ next unless( defined( $token ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 3828 3828
2653  
2654 # Proper rfc6901 JSON Pointer escaping
2655 3828 $token =~ s/~/~0/g;
2656 3828 $token =~ s/\//~1/g;
2657  
2658 3828 ▶ if( $ptr eq '#' )
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr eq '#') { } 1948 1880 3828
2659 {
2660 1948 $ptr = "#/$token";
2661 }
2662 else
2663 {
2664 1880 $ptr .= "/$token";
2665 }
2666 }
2667  
2668 3084 return( $ptr );
2669 }
2670  
2671 sub _js_comment
2672 0 ▶{
Subroutine _js_comment: not called CC=1 SCAR=6.9
2673 0 my( $value ) = @_;
2674 0 $value =~ s/([\r\n\x{2028}\x{2029}])/sprintf( '\\u%04X', ord( $1 ) )/eg;
2675 0 return( $value );
2676 }
2677  
2678 sub _js_literal
2679 0 ▶{
Subroutine _js_literal: not called CC=1 SCAR=6.9
2680 0 my( $value ) = @_;
2681 0 return( JSON->new->allow_nonref(1)->ascii(1)->canonical(1)->encode( $value ) );
2682 }
2683  
2684 sub _js_quote
2685 0 ▶{
Subroutine _js_quote: not called CC=2 SCAR=17.9
2686 0 my( $value ) = @_;
2687 0 ▶ $value = '' unless( defined( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
2688 0 return( JSON->new->allow_nonref(1)->ascii(1)->encode( "$value" ) );
2689 }
2690  
2691 sub _js_reference_target
2692 0 ▶{
Subroutine _js_reference_target: not called CC=11 SCAR=48.8
2693 0 my( $schema, $opts ) = @_;
2694 0 my $index = $opts->{_resource_index};
2695 0 my $meta = $index->{nodes}->{ refaddr( $schema ) };
2696 0 ▶ my $base = $meta ? $meta->{base} : $opts->{_resource_base};
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 0 0 0
2697 0 my( $uri, $fragment ) = split( /#/, _resolve_uri( $base, $schema->{'$ref'} ), 2 );
2698 0 ▶ $uri //= '';
Condition0%0/2
$uri //= ""
l!l
00
MC/DC0%0/1
$uri //= ""
$uri
Truth table0%0/2
A
A= $uri
Aresult
11
00
2699 0 ▶ my $resource = $index->{resources}->{ $uri } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $resource = $index->{"resources"}{$uri} or return 0 0 0
2700 0 ▶ $fragment = eval{ _decode_fragment( $fragment // '' ) };
Condition0%0/2
$fragment // ""
l!l
00
MC/DC0%0/1
$fragment // ""
$fragment
Truth table0%0/2
A
A= $fragment
Aresult
11
00
2701 0 ▶ return unless( defined( $fragment ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment 0 0 0
2702 0 my( $target, $ptr );
2703 0 ▶ if( $fragment eq '' )
Branch: 2 branches
BranchTrueFalseTotal
if ($fragment eq '') { } 0 0 0
elsif ($fragment =~ m[^/]u) { } 0 0 0
2704 {
2705 0 ( $target, $ptr ) = @$resource{qw( schema pointer )};
2706 }
2707 elsif( $fragment =~ m{^/} )
2708 {
2709 0 $target = _jsv_resolve_internal_ref( $resource->{schema}, '#' . $fragment );
2710 0 $ptr = $resource->{pointer} . $fragment;
2711 }
2712 else
2713 {
2714 0 ▶ my $entry = $resource->{anchors}->{ $fragment } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $resource->{"anchors"}{$fragment} or return 0 0 0
2715 0 ( $target, $ptr ) = @$entry{qw( schema pointer )};
2716 }
2717 # Use the indexed canonical pointer for schemas reached through an alias.
2718 0 ▶ if( ref( $target ) eq 'HASH' && ( my $entry = $index->{nodes}->{ refaddr( $target ) } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $target eq "HASH" and my $entry = $index->{"nodes"}{builtin::refaddr($target)}) 0 0 0
Condition0%0/3
ref $target eq "HASH" and my $entry = $index->{"nodes"}{builtin::refaddr($target)}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $target eq "HASH" and my $entry = $index->{"nodes"}{builtin::refaddr($target)}
ref $target eq "HASH" my $entry = $index->{"nodes"}{builtin::refaddr($target)}
Truth table0%0/3
A and B
A= ref $target eq "HASH"B= my $entry = $index->{"nodes"}{builtin::refaddr($target)}
ABresult
0X0
100
111
2719 {
2720 0 $ptr = $entry->{pointer};
2721 }
2722 0 return( $target, $ptr );
2723 }
2724  
2725 sub _json_equal
2726 107 ▶{
Subroutine _json_equal: called CC=8 SCAR=21.2
2727 107 my( $a, $b ) = @_;
2728 107 ▶ if( _is_number( $a ) && _is_number( $b ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_number($a) and _is_number($b)) 60 47 107
Condition100%3/3
_is_number($a) and _is_number($b)
!ll&&!rl&&r
45260
MC/DC100%2/2
_is_number($a) and _is_number($b)
_is_number($a) _is_number($b)
Truth table100%3/3
A and B
A= _is_number($a)B= _is_number($b)
ABresult
0X0
100
111
2729 {
2730 60 my( $as, $ad, $ae ) = _decimal_parts( $a );
2731 60 my( $bs, $bd, $be ) = _decimal_parts( $b );
2732 60 ▶ return( $as eq $bs && $ad eq $bd && $ae == $be ? 1 : 0 ) if( defined( $ad ) && defined( $bd ) );
Branch: 2 branches
BranchTrueFalseTotal
if defined $ad and defined $bd 60 0 60
$as eq $bs && $ad eq $bd && $ae == $be ? : 34 26 60
Condition66%6/9
defined $ad and defined $bd
!ll&&!rl&&r
0060
$as eq $bs and $ad eq $bd and $ae == $be
!ll&&!rl&&r
26034
$as eq $bs and $ad eq $bd
!ll&&!rl&&r
81834
MC/DC0%0/2
defined $ad and defined $bd
defined $ad defined $bd
MC/DC66%2/3
$as eq $bs and $ad eq $bd and $ae == $be
$as eq $bs $ad eq $bd $ae == $be
Truth table33%1/3
A and B
A= defined $adB= defined $bd
ABresult
0X0
100
111
Truth table75%3/4
A and B and C
A= $as eq $bsB= $ad eq $bdC= $ae == $be
ABCresult
0XX0
10X0
1100
1111
2733 }
2734 47 return( _canon( $a ) eq _canon( $b ) );
2735 }
2736  
2737 # Very small JSON Pointer resolver for internal refs ("#/...") for JS compile
2738 sub _jsv_resolve_internal_ref
2739 101 ▶{
Subroutine _jsv_resolve_internal_ref: called CC=20 SCAR=36.5
2740 101 my( $root, $ptr ) = @_;
2741  
2742 101 ▶ return( $root ) if( !defined( $ptr ) || $ptr eq '' || $ptr eq '#' );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ptr or $ptr eq "" or $ptr eq "#" 0 101 101
Condition33%2/6
not defined $ptr or $ptr eq "" or $ptr eq "#"
l!l&&r!l&&!r
00101
not defined $ptr or $ptr eq ""
l!l&&r!l&&!r
00101
MC/DC0%0/3
not defined $ptr or $ptr eq "" or $ptr eq "#"
not defined $ptr $ptr eq "" $ptr eq "#"
Truth table25%1/4
not(A) or B or C
A= not defined $ptrB= $ptr eq ""C= $ptr eq "#"
ABCresult
1XX1
01X1
0011
0000
2743  
2744 # Expect something like "#/definitions/address"
2745 101 $ptr =~ s/^#//;
2746 101 ▶ return unless( $ptr =~ m{^/} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr =~ m[^/]u 0 101 101
2747  
2748 101 my @tokens = split( /\//, $ptr, -1 );
2749 101 ▶ shift( @tokens ) if( @tokens && $tokens[0] eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if @tokens and $tokens[0] eq "" 101 0 101
Condition33%1/3
@tokens and $tokens[0] eq ""
!ll&&!rl&&r
00101
MC/DC0%0/2
@tokens and $tokens[0] eq ""
@tokens $tokens[0] eq ""
Truth table33%1/3
A and B
A= @tokensB= $tokens[0] eq ""
ABresult
0X0
100
111
2750  
2751 101 my $node = $root;
2752  
2753 TOKEN:
2754 101 for my $tok ( @tokens )
2755 {
2756 # rfc6901 JSON Pointer unescaping
2757 194 ▶ return if( $tok =~ /~(?![01])/ );
Branch: 1 branch
BranchTrueFalseTotal
if $tok =~ /~(?![01])/u 0 194 194
2758 194 $tok =~ s/~1/\//g;
2759 194 $tok =~ s/~0/~/g;
2760  
2761 194 ▶ if( ref( $node ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'HASH') { } 192 2 194
elsif (ref $node eq 'ARRAY') { } 0 2 2
2762 {
2763 # Draft 2020-12 alias: allow "definitions" to hit "$defs" if needed
2764 192 ▶ if( $tok eq 'definitions' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($tok eq "definitions" and not exists $node->{"definitions"} and exists $node->{"\$defs"}) 4 188 192
Condition100%6/6
$tok eq "definitions" and not exists $node->{"definitions"} and exists $node->{"\$defs"}
!ll&&!rl&&r
18714
$tok eq "definitions" and not exists $node->{"definitions"}
!ll&&!rl&&r
163245
MC/DC100%3/3
$tok eq "definitions" and not exists $node->{"definitions"} and exists $node->{"\$defs"}
$tok eq "definitions" not exists $node->{"definitions"} exists $node->{"\$defs"}
Truth table100%4/4
A and not(B) and C
A= $tok eq "definitions"B= not exists $node->{"definitions"}C= exists $node->{"\$defs"}
ABCresult
0XX0
10X0
1100
1111
2765 !exists( $node->{definitions} ) &&
2766 exists( $node->{'$defs'} ) )
2767 {
2768 4 $tok = '$defs';
2769 }
2770  
2771 192 ▶ unless( exists( $node->{ $tok } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $node->{$tok}) 3 189 192
2772 {
2773 # Optional: help debug resolution problems
2774 3 ▶ warn( "_jsv_resolve_internal_ref: token '$tok' not found in current hash for pointer '$ptr'\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Schema::Validate::DEBUG 0 3 3
2775 if( $JSON::Schema::Validate::DEBUG );
2776 3 return;
2777 }
2778 189 $node = $node->{ $tok };
2779 }
2780 elsif( ref( $node ) eq 'ARRAY' )
2781 {
2782 0 ▶ unless( $tok =~ /\A(?:0|[1-9][0-9]*)\z/ && $tok < @$node )
Branch: 1 branch
BranchTrueFalseTotal
unless ($tok =~ /\A(?:0|[1-9][0-9]*)\z/u and $tok < @$node) 0 0 0
Condition0%0/3
$tok =~ /\A(?:0|[1-9][0-9]*)\z/u and $tok < @$node
!ll&&!rl&&r
000
MC/DC0%0/2
$tok =~ /\A(?:0|[1-9][0-9]*)\z/u and $tok < @$node
$tok =~ /\A(?:0|[1-9][0-9]*)\z/u $tok < @$node
Truth table0%0/3
A and B
A= $tok =~ /\A(?:0|[1-9][0-9]*)\z/uB= $tok < @$node
ABresult
0X0
100
111
2783 {
2784 0 ▶ warn( "_jsv_resolve_internal_ref: array index '$tok' out of range for pointer '$ptr'\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Schema::Validate::DEBUG 0 0 0
2785 if( $JSON::Schema::Validate::DEBUG );
2786 0 return;
2787 }
2788 0 $node = $node->[ $tok ];
2789 }
2790 else
2791 {
2792 2 ▶ warn( "_jsv_resolve_internal_ref: reached non-container node while resolving '$ptr'\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Schema::Validate::DEBUG 0 2 2
2793 if( $JSON::Schema::Validate::DEBUG );
2794 2 return;
2795 }
2796 }
2797  
2798 96 return( $node );
2799 }
2800  
2801 sub _k_array_all
2802 78 ▶{
Subroutine _k_array_all: called CC=27 SCAR=33.9
2803 78 my( $ctx, $sp, $S, $A ) = @_;
2804  
2805 78 ▶ if( exists( $S->{minItems} ) && @$A < $S->{minItems} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"minItems"} and @$A < $S->{"minItems"}) 0 78 78
Condition66%2/3
exists $S->{"minItems"} and @$A < $S->{"minItems"}
!ll&&!rl&&r
7350
MC/DC0%0/2
exists $S->{"minItems"} and @$A < $S->{"minItems"}
exists $S->{"minItems"} @$A < $S->{"minItems"}
Truth table66%2/3
A and B
A= exists $S->{"minItems"}B= @$A < $S->{"minItems"}
ABresult
0X0
100
111
2806 {
2807 0 return( _err_res( $ctx, $sp, "array has fewer than minItems $S->{minItems}", 'minItems' ) );
2808 }
2809 78 ▶ if( exists( $S->{maxItems} ) && @$A > $S->{maxItems} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"maxItems"} and @$A > $S->{"maxItems"}) 0 78 78
Condition33%1/3
exists $S->{"maxItems"} and @$A > $S->{"maxItems"}
!ll&&!rl&&r
7800
MC/DC0%0/2
exists $S->{"maxItems"} and @$A > $S->{"maxItems"}
exists $S->{"maxItems"} @$A > $S->{"maxItems"}
Truth table33%1/3
A and B
A= exists $S->{"maxItems"}B= @$A > $S->{"maxItems"}
ABresult
0X0
100
111
2810 {
2811 0 return( _err_res( $ctx, $sp, "array has more than maxItems $S->{maxItems}", 'maxItems' ) );
2812 }
2813  
2814 78 ▶ if( $S->{uniqueItems} )
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{"uniqueItems"}) 0 78 78
2815 {
2816 0 my %seen;
2817 0 for my $i ( 0 .. $#$A )
2818 {
2819 0 my $k = _canon( $A->[$i] );
2820 0 ▶ if( $seen{ $k }++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$k}++) 0 0 0
2821 {
2822 0 return( _err_res( $ctx, _join_ptr( $sp, $i ), "array items not unique", 'uniqueItems' ) );
2823 }
2824 }
2825 }
2826  
2827 78 my %items_ann;
2828  
2829 78 ▶ if( ref( $S->{prefixItems} ) eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $S->{'prefixItems'} eq 'ARRAY') { } 19 59 78
elsif (ref $S->{'items'} eq 'HASH' or &JSON::is_bool($S->{'items'})) { } 24 35 59
2830 {
2831 19 my $tuple = $S->{prefixItems};
2832 19 for my $i ( 0 .. $#$A )
2833 {
2834 38 push( @{$ctx->{ptr_stack}}, _inst_ptr( $ctx, $i ) );
2835  
2836 38 ▶ if( $i <= $#$tuple )
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $tuple->$#*) { } 24 14 38
elsif (exists $S->{'items'} and ref $S->{'items'} eq 'HASH' || &JSON::is_bool($S->{'items'})) { } 9 10 19
2837 {
2838 24 my $r = _v( $ctx, _join_ptr( $sp, "prefixItems/$i" ), $tuple->[$i], $A->[$i] );
2839 24 ▶ unless( $r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 2 22 24
2840 {
2841 2 pop( @{$ctx->{ptr_stack}} );
2842 2 return( $r );
2843 }
2844 22 $items_ann{ $i } = 1;
2845 }
2846 ▶ elsif( exists( $S->{items} ) && ( ref( $S->{items} ) eq 'HASH' || JSON::is_bool( $S->{items} ) ) )
Condition66%4/6
exists $S->{"items"} and ref $S->{"items"} eq "HASH" || &JSON::is_bool($S->{"items"})
!ll&&!rl&&r
509
ref $S->{"items"} eq "HASH" or &JSON::is_bool($S->{"items"})
l!l&&r!l&&!r
540
MC/DC33%1/3
exists $S->{"items"} and ref $S->{"items"} eq "HASH" || &JSON::is_bool($S->{"items"})
exists $S->{"items"} ref $S->{"items"} eq "HASH" &JSON::is_bool($S->{"items"})
Truth table75%3/4
A and B or C
A= exists $S->{"items"}B= ref $S->{"items"} eq "HASH"C= &JSON::is_bool($S->{"items"})
ABCresult
0XX0
1000
11X1
1011
2847 {
2848 9 my $r = _v( $ctx, _join_ptr( $sp, "items" ), $S->{items}, $A->[$i] );
2849 9 ▶ unless( $r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 3 6 9
2850 {
2851 3 pop( @{$ctx->{ptr_stack}} );
2852 3 return( $r );
2853 }
2854 6 $items_ann{ $i } = 1;
2855 }
2856  
2857 33 pop( @{$ctx->{ptr_stack}} );
2858 }
2859 }
2860 ▶ elsif( ref( $S->{items} ) eq 'HASH' || JSON::is_bool( $S->{items} ) )
Condition100%3/3
ref $S->{"items"} eq "HASH" or &JSON::is_bool($S->{"items"})
l!l&&r!l&&!r
18635
MC/DC100%2/2
ref $S->{"items"} eq "HASH" or &JSON::is_bool($S->{"items"})
ref $S->{"items"} eq "HASH" &JSON::is_bool($S->{"items"})
Truth table100%3/3
A or B
A= ref $S->{"items"} eq "HASH"B= &JSON::is_bool($S->{"items"})
ABresult
1X1
011
000
2861 {
2862 24 for my $i ( 0 .. $#$A )
2863 {
2864 35 push( @{$ctx->{ptr_stack}}, _inst_ptr( $ctx, $i ) );
2865 35 my $r = _v( $ctx, _join_ptr( $sp, "items" ), $S->{items}, $A->[$i] );
2866 # NOTE: the instance pointer must be popped on every exit path
2867 # Returning early without popping leaves the failing item's pointer on the
2868 # stack, and every later error in the same document inherits it as a prefix.
2869 35 ▶ unless( $r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 6 29 35
2870 {
2871 6 pop( @{$ctx->{ptr_stack}} );
2872 6 return( $r );
2873 }
2874 29 $items_ann{ $i } = 1;
2875 29 pop( @{$ctx->{ptr_stack}} );
2876 }
2877 }
2878  
2879 67 ▶ if( exists( $S->{contains} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"contains"}) 26 41 67
2880 {
2881 26 my $matches = 0;
2882  
2883 # Quiet sub-context to avoid emitting errors for non-matching items
2884 26 for my $i ( 0 .. $#$A )
2885 {
2886 55 my %shadow = %$ctx;
2887 55 my @errs;
2888 55 $shadow{errors} = \@errs;
2889 55 $shadow{error_count} = 0;
2890  
2891 55 push( @{$ctx->{ptr_stack}}, _inst_ptr( $ctx, $i ) );
2892 55 my $tmp = _v( \%shadow, _join_ptr( $sp, "contains" ), $S->{contains}, $A->[$i] );
2893 55 pop( @{$ctx->{ptr_stack}} );
2894 55 ▶ if( $tmp->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp->{"ok"}) 37 18 55
2895 {
2896 37 $matches++;
2897 37 $items_ann{ $i } = 1;
2898 }
2899 }
2900  
2901 26 ▶ my $minc = defined( $S->{minContains} ) ? $S->{minContains} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $S->{'minContains'} ? : 14 12 26
2902 26 ▶ my $maxc = defined( $S->{maxContains} ) ? $S->{maxContains} : ( 2**31 - 1 );
Branch: 1 branch
BranchTrueFalseTotal
defined $S->{'maxContains'} ? : 10 16 26
2903  
2904 26 ▶ return( _err_res( $ctx, $sp, "contains matched $matches < minContains $minc", 'minContains' ) ) if( $matches < $minc );
Branch: 1 branch
BranchTrueFalseTotal
if $matches < $minc 5 21 26
2905 21 ▶ return( _err_res( $ctx, $sp, "contains matched $matches > maxContains $maxc", 'maxContains' ) ) if( $matches > $maxc );
Branch: 1 branch
BranchTrueFalseTotal
if $matches > $maxc 4 17 21
2906 }
2907  
2908 58 return( { ok => 1, props => {}, items => \%items_ann } );
2909 }
2910  
2911 sub _k_combinator
2912 109 ▶{
Subroutine _k_combinator: called CC=24 SCAR=31.9
2913 109 my( $ctx, $sp, $S, $inst, $kw ) = @_;
2914  
2915  
2916 # allOf: all subschemas must pass, we merge their annotations
2917 109 ▶ if( $kw eq 'allOf' )
Branch: 1 branch
BranchTrueFalseTotal
if ($kw eq "allOf") 48 61 109
2918 {
2919 48 my %props;
2920 my %items;
2921  
2922 48 for my $i ( 0 .. $#{$S->{allOf}} )
2923 {
2924 72 my $r = _v( $ctx, _join_ptr( $sp, "allOf/$i" ), $S->{allOf}->[ $i ], $inst );
2925 72 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 23 49 72
2926  
2927 49 %props = ( %props, %{$r->{props}} );
2928 49 %items = ( %items, %{$r->{items}} );
2929 }
2930  
2931 25 return( { ok => 1, props => \%props, items => \%items } );
2932 }
2933  
2934 # anyOf: at least one subschema must pass.
2935 # Errors from failing branches must NOT leak into the main context.
2936 61 ▶ if( $kw eq 'anyOf' )
Branch: 1 branch
BranchTrueFalseTotal
if ($kw eq "anyOf") 34 27 61
2937 {
2938 34 my @branch_errs;
2939 34 my( %props, %items );
2940 34 my $matched = 0;
2941  
2942 34 for my $i ( 0 .. $#{$S->{anyOf}} )
2943 {
2944 # Shadow context: isolate errors for this branch
2945 72 my %shadow = %$ctx;
2946 72 my @errs;
2947 72 $shadow{errors} = \@errs;
2948 72 $shadow{error_count} = 0;
2949  
2950 72 my $r = _v( \%shadow, _join_ptr( $sp, "anyOf/$i" ), $S->{anyOf}->[ $i ], $inst );
2951  
2952 72 ▶ if( $r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ok"}) 23 49 72
2953 {
2954 # All successful branches contribute annotations.
2955 # Ignore errors from unsuccessful branches if any branch matches.
2956 23 $matched = 1;
2957 23 %props = ( %props, %{$r->{props}} );
2958 23 %items = ( %items, %{$r->{items}} );
2959 23 next;
2960 }
2961  
2962 49 push( @branch_errs, \@errs );
2963 }
2964  
2965 34 ▶ return( { ok => 1, props => \%props, items => \%items } ) if( $matched );
Branch: 1 branch
BranchTrueFalseTotal
if $matched 19 15 34
2966  
2967 # No branch matched: merge collected branch errors into main context
2968 15 for my $aref ( @branch_errs )
2969 {
2970 34 ▶ next unless( @$aref );
Branch: 1 branch
BranchTrueFalseTotal
unless @$aref 0 34 34
2971 34 for my $error ( @$aref )
2972 {
2973 46 ▶ last if( $ctx->{max_errors} && $ctx->{error_count} >= $ctx->{max_errors} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"} 4 42 46
Condition100%3/3
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
!ll&&!rl&&r
6364
MC/DC100%2/2
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
$ctx->{"max_errors"} $ctx->{"error_count"} >= $ctx->{"max_errors"}
Truth table100%3/3
A and B
A= $ctx->{"max_errors"}B= $ctx->{"error_count"} >= $ctx->{"max_errors"}
ABresult
0X0
100
111
2974 42 push( @{$ctx->{errors}}, $error );
2975 42 $ctx->{error_count}++;
2976 }
2977 }
2978  
2979 15 return( _err_res( $ctx, $sp, "instance does not satisfy anyOf", 'anyOf' ) );
2980 }
2981  
2982 # oneOf: exactly one subschema must pass.
2983 # Again, do not leak errors from non-selected branches.
2984 27 ▶ if( $kw eq 'oneOf' )
Branch: 1 branch
BranchTrueFalseTotal
if ($kw eq "oneOf") 23 4 27
2985 {
2986 23 my @ok_results;
2987 my @branch_errs;
2988  
2989 23 for my $i ( 0 .. $#{$S->{oneOf}} )
2990 {
2991 46 my %shadow = %$ctx;
2992 46 my @errs;
2993 46 $shadow{errors} = \@errs;
2994 46 $shadow{error_count} = 0;
2995  
2996 46 my $r = _v( \%shadow, _join_ptr( $sp, "oneOf/$i" ), $S->{oneOf}->[$i], $inst );
2997  
2998 46 ▶ if( $r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'ok'}) { } 20 26 46
2999 {
3000 20 push( @ok_results, $r );
3001 }
3002 else
3003 {
3004 26 push( @branch_errs, \@errs );
3005 }
3006 }
3007  
3008 23 ▶ if( @ok_results == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@ok_results == 1) 10 13 23
3009 {
3010 # Exactly one branch matched: its annotations contribute to the parent.
3011 10 return( $ok_results[0] );
3012 }
3013  
3014 # Zero or >1 matched -> failure; merge branch errors
3015 13 for my $aref ( @branch_errs )
3016 {
3017 16 ▶ next unless( @$aref );
Branch: 1 branch
BranchTrueFalseTotal
unless @$aref 0 16 16
3018 16 for my $error ( @$aref )
3019 {
3020 20 ▶ last if( $ctx->{max_errors} && $ctx->{error_count} >= $ctx->{max_errors} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"} 0 20 20
Condition33%1/3
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
!ll&&!rl&&r
0200
MC/DC0%0/2
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
$ctx->{"max_errors"} $ctx->{"error_count"} >= $ctx->{"max_errors"}
Truth table33%1/3
A and B
A= $ctx->{"max_errors"}B= $ctx->{"error_count"} >= $ctx->{"max_errors"}
ABresult
0X0
100
111
3021 20 push( @{$ctx->{errors}}, $error );
3022 20 $ctx->{error_count}++;
3023 }
3024 }
3025  
3026 return(
3027 13 _err_res(
3028 $ctx,
3029 $sp,
3030 "instance satisfies " . scalar( @ok_results ) . " schemas in oneOf (expected exactly 1)",
3031 'oneOf'
3032 )
3033 );
3034 }
3035  
3036 # not: subschema must NOT validate.
3037 # Any errors from validating the inner schema are irrelevant on success.
3038 4 ▶ if( $kw eq 'not' )
Branch: 1 branch
BranchTrueFalseTotal
if ($kw eq "not") 4 0 4
3039 {
3040 4 my %shadow = %$ctx;
3041 4 my @errs;
3042 4 $shadow{errors} = \@errs;
3043 4 $shadow{error_count} = 0;
3044  
3045 4 my $r = _v( \%shadow, _join_ptr( $sp, "not" ), $S->{not}, $inst );
3046  
3047 # If inner schema passes, then "not" fails
3048 return( _err_res( $ctx, $sp, "instance matches forbidden not-schema", 'not' ) )
3049 4 ▶ if( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 1 3 4
3050  
3051 # Otherwise, "not" is satisfied; ignore inner errors entirely
3052 3 return( { ok => 1, props => {}, items => {} } );
3053 }
3054  
3055 # Unknown / unsupported combinator (defensive default)
3056 0 return( { ok => 1, props => {}, items => {} } );
3057 }
3058  
3059 sub _k_const
3060 50 ▶{
Subroutine _k_const: called CC=2 SCAR=6.9
3061 50 my( $ctx, $v, $const, $ptr ) = @_;
3062 50 ▶ return(1) if( _json_equal( $v, $const ) );
Branch: 1 branch
BranchTrueFalseTotal
if _json_equal($v, $const) 24 26 50
3063 26 return( _err( $ctx, $ptr, "const mismatch", 'const' ) );
3064 }
3065  
3066 sub _k_enum
3067 50 ▶{
Subroutine _k_enum: called CC=3 SCAR=11.0
3068 50 my( $ctx, $v, $arr, $ptr ) = @_;
3069 50 for my $e ( @$arr )
3070 {
3071 57 ▶ return(1) if( _json_equal( $v, $e ) );
Branch: 1 branch
BranchTrueFalseTotal
if _json_equal($v, $e) 20 37 57
3072 }
3073 30 return( _err( $ctx, $ptr, "value not in enum", 'enum' ) );
3074 }
3075  
3076 sub _k_format
3077 55 ▶{
Subroutine _k_format: called CC=4 SCAR=13.9
3078 55 my( $ctx, $s, $fmt, $ptr ) = @_;
3079 55 my $cb = $ctx->{formats}->{ $fmt };
3080 55 ▶ return(1) unless( $cb );
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 3 52 55
3081 52 local $@;
3082 52 ▶ my $ok = eval{ $cb->( $s ) ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
&$cb($s) ? : 34 18 52
3083 52 ▶ return( $ok ? 1 : _err( $ctx, $ptr, "string fails format '$fmt'", 'format' ) );
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 34 18 52
3084 }
3085  
3086 sub _k_if_then_else
3087 26 ▶{
Subroutine _k_if_then_else: called CC=7 SCAR=19.5
3088 26 my( $ctx, $sp, $S, $inst ) = @_;
3089  
3090 # Evaluate "if" in a shadow context so its errors do NOT leak
3091 26 my %shadow = %$ctx;
3092 26 my @errs;
3093 26 $shadow{errors} = \@errs;
3094 26 $shadow{error_count} = 0;
3095  
3096 26 my $cond = _v( \%shadow, _join_ptr( $sp, 'if' ), $S->{if}, $inst );
3097  
3098 26 ▶ if( $cond->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
if ($cond->{'ok'}) { } 12 14 26
3099 {
3100 12 ▶ _t( $ctx, $sp, 'if', undef, 'pass', 'then' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 12 12
3101 12 ▶ return( $cond ) unless( exists( $S->{then} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $S->{"then"} 0 12 12
3102  
3103 # Apply "then" against the real context and retain annotations from the successful condition.
3104 12 my $r = _v( $ctx, _join_ptr( $sp, 'then' ), $S->{then}, $inst );
3105 12 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 7 5 12
3106 5 return( { ok => 1, props => { %{$cond->{props}}, %{$r->{props}} }, items => { %{$cond->{items}}, %{$r->{items}} } } );
3107 }
3108 else
3109 {
3110 14 ▶ _t( $ctx, $sp, 'if', undef, 'pass', 'else' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 14 14
3111 return( { ok => 1, props => {}, items => {} } )
3112 14 ▶ unless( exists( $S->{else} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $S->{"else"} 10 4 14
3113  
3114 # Apply "else" against the REAL context
3115 4 return( _v( $ctx, _join_ptr( $sp, 'else' ), $S->{else}, $inst ) );
3116 }
3117 }
3118  
3119 sub _k_number
3120 231 ▶{
Subroutine _k_number: called CC=24 SCAR=35.5
3121 231 my( $ctx, $v, $kw, $arg, $ptr ) = @_;
3122 231 ▶ if( $kw eq 'multipleOf' )
Branch: 5 branches
BranchTrueFalseTotal
if ($kw eq 'multipleOf') { } 131 100 231
elsif ($kw eq 'minimum') { } 87 13 100
elsif ($kw eq 'maximum') { } 6 7 13
elsif ($kw eq 'exclusiveMinimum') { } 5 2 7
elsif ($kw eq 'exclusiveMaximum') { } 2 0 2
3123 {
3124 # Guard per spec: multipleOf must be > 0
3125 131 ▶ if( !defined( $arg ) || $arg <= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $arg or $arg <= 0) 9 122 131
Condition66%2/3
not defined $arg or $arg <= 0
l!l&&r!l&&!r
09122
MC/DC50%1/2
not defined $arg or $arg <= 0
not defined $arg $arg <= 0
Truth table66%2/3
not(A) or B
A= not defined $argB= $arg <= 0
ABresult
1X1
011
000
3126 {
3127 9 ▶ _t( $ctx, $ptr, 'multipleOf', undef, 'fail', 'arg<=0' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 9 9
3128 9 return( _err( $ctx, $ptr, "multipleOf must be > 0", 'multipleOf' ) );
3129 }
3130 # Test divisibility of the decimal JSON representations without rounding the quotient.
3131 122 my $ok = _decimal_multiple( $v, $arg );
3132 122 ▶ _t( $ctx, $ptr, 'multipleOf', undef, $ok ? 'pass' : 'fail', "$v mod $arg" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 122 122
$ok ? : 0 0 0
3133 122 ▶ return( $ok ? 1 : _err( $ctx, $ptr, "number not multipleOf $arg", 'multipleOf' ) );
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 65 57 122
3134 }
3135 elsif( $kw eq 'minimum' )
3136 {
3137 87 ▶ _t( $ctx, $ptr, 'minimum', undef, $v >= $arg ? 'pass' : 'fail', "$v >= $arg" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 1 86 87
$v >= $arg ? : 0 1 1
3138 87 ▶ return( $v >= $arg ? 1 : _err( $ctx, $ptr, "number less than minimum $arg", 'minimum' ) );
Branch: 1 branch
BranchTrueFalseTotal
$v >= $arg ? : 64 23 87
3139 }
3140 elsif( $kw eq 'maximum' )
3141 {
3142 6 ▶ _t( $ctx, $ptr, 'maximum', undef, $v <= $arg ? 'pass' : 'fail', "$v <= $arg" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 6 6
$v <= $arg ? : 0 0 0
3143 6 ▶ return( $v <= $arg ? 1 : _err( $ctx, $ptr, "number greater than maximum $arg", 'maximum' ) );
Branch: 1 branch
BranchTrueFalseTotal
$v <= $arg ? : 5 1 6
3144 }
3145 elsif( $kw eq 'exclusiveMinimum' )
3146 {
3147 5 ▶ _t( $ctx, $ptr, 'exclusiveMinimum', undef, $v > $arg ? 'pass' : 'fail', "$v > $arg" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 5 5
$v > $arg ? : 0 0 0
3148 5 ▶ return( $v > $arg ? 1 : _err( $ctx, $ptr, "number not greater than exclusiveMinimum $arg", 'exclusiveMinimum' ) );
Branch: 1 branch
BranchTrueFalseTotal
$v > $arg ? : 3 2 5
3149 }
3150 elsif( $kw eq 'exclusiveMaximum' )
3151 {
3152 2 ▶ _t( $ctx, $ptr, 'exclusiveMaximum', undef, $v < $arg ? 'pass' : 'fail', "$v < $arg" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 2 2
$v < $arg ? : 0 0 0
3153 2 ▶ return( $v < $arg ? 1 : _err( $ctx, $ptr, "number not less than exclusiveMaximum $arg", 'exclusiveMaximum' ) );
Branch: 1 branch
BranchTrueFalseTotal
$v < $arg ? : 1 1 2
3154 }
3155 0 return(1);
3156 }
3157  
3158 sub _k_object_all
3159 523 ▶{
Subroutine _k_object_all: called CC=64 SCAR=42.4
3160 523 my( $ctx, $sp, $S, $H ) = @_;
3161  
3162 523 my $ok = 1;
3163  
3164 my $bail_if_max = sub
3165 198 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.1
3166 198 ▶ return( $ctx->{max_errors} && $ctx->{error_count} >= $ctx->{max_errors} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{'max_errors'} && $ctx->{'error_count'} >= $ctx->{'max_errors'} ? : 6 192 198
Condition66%2/3
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
!ll&&!rl&&r
01926
MC/DC50%1/2
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
$ctx->{"max_errors"} $ctx->{"error_count"} >= $ctx->{"max_errors"}
Truth table66%2/3
A and B
A= $ctx->{"max_errors"}B= $ctx->{"error_count"} >= $ctx->{"max_errors"}
ABresult
0X0
100
111
3167 523 };
3168  
3169 523 ▶ if( exists( $S->{minProperties} ) && ( scalar( keys( %$H ) ) ) < $S->{minProperties} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"minProperties"} and scalar keys %$H < $S->{"minProperties"}) 2 521 523
Condition100%3/3
exists $S->{"minProperties"} and scalar keys %$H < $S->{"minProperties"}
!ll&&!rl&&r
51922
MC/DC100%2/2
exists $S->{"minProperties"} and scalar keys %$H < $S->{"minProperties"}
exists $S->{"minProperties"} scalar keys %$H < $S->{"minProperties"}
Truth table100%3/3
A and B
A= exists $S->{"minProperties"}B= scalar keys %$H < $S->{"minProperties"}
ABresult
0X0
100
111
3170 {
3171 2 _err_res( $ctx, $sp, "object has fewer than minProperties $S->{minProperties}", 'minProperties' );
3172 2 $ok = 0;
3173 2 ▶ return( { ok => 0, props => {}, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 2 2
3174 }
3175 523 ▶ if( exists( $S->{maxProperties} ) && ( scalar( keys( %$H ) ) ) > $S->{maxProperties} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"maxProperties"} and scalar keys %$H > $S->{"maxProperties"}) 0 523 523
Condition33%1/3
exists $S->{"maxProperties"} and scalar keys %$H > $S->{"maxProperties"}
!ll&&!rl&&r
52300
MC/DC0%0/2
exists $S->{"maxProperties"} and scalar keys %$H > $S->{"maxProperties"}
exists $S->{"maxProperties"} scalar keys %$H > $S->{"maxProperties"}
Truth table33%1/3
A and B
A= exists $S->{"maxProperties"}B= scalar keys %$H > $S->{"maxProperties"}
ABresult
0X0
100
111
3176 {
3177 0 _err_res( $ctx, $sp, "object has more than maxProperties $S->{maxProperties}", 'maxProperties' );
3178 0 $ok = 0;
3179 0 ▶ return( { ok => 0, props => {}, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 0 0
3180 }
3181  
3182 # Merge required from:
3183 # - top-level "required" (array only)
3184 # - property-level { required => 1 } or { optional => 0 }
3185 523 my %required;
3186  
3187 523 ▶ if( exists( $S->{required} ) && ref( $S->{required} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"required"} and ref $S->{"required"} eq "ARRAY") 242 281 523
Condition100%3/3
exists $S->{"required"} and ref $S->{"required"} eq "ARRAY"
!ll&&!rl&&r
2783242
MC/DC100%2/2
exists $S->{"required"} and ref $S->{"required"} eq "ARRAY"
exists $S->{"required"} ref $S->{"required"} eq "ARRAY"
Truth table100%3/3
A and B
A= exists $S->{"required"}B= ref $S->{"required"} eq "ARRAY"
ABresult
0X0
100
111
3188 {
3189 242 $required{ $_ } = 1 for( @{ $S->{required} } );
3190 }
3191  
3192 523 ▶ if( my $P = $S->{properties} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $P = $S->{"properties"}) 391 132 523
3193 {
3194 391 for my $k ( sort( keys( %$P ) ) )
3195 {
3196 540 my $pd = $P->{ $k };
3197 540 ▶ next unless( ref( $pd ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pd eq "HASH" 24 516 540
3198  
3199 516 ▶ if( exists( $pd->{required} ) && ( !ref( $pd->{required} ) || JSON::is_bool( $pd->{required} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pd->{"required"} and !ref($pd->{"required"}) || &JSON::is_bool($pd->{"required"})) 52 464 516
Condition100%6/6
exists $pd->{"required"} and !ref($pd->{"required"}) || &JSON::is_bool($pd->{"required"})
!ll&&!rl&&r
460452
not ref $pd->{"required"} or &JSON::is_bool($pd->{"required"})
l!l&&r!l&&!r
5024
MC/DC100%3/3
exists $pd->{"required"} and !ref($pd->{"required"}) || &JSON::is_bool($pd->{"required"})
exists $pd->{"required"} not ref $pd->{"required"} &JSON::is_bool($pd->{"required"})
Truth table100%4/4
A and not(B) or C
A= exists $pd->{"required"}B= not ref $pd->{"required"}C= &JSON::is_bool($pd->{"required"})
ABCresult
0XX0
1000
11X1
1011
3200 {
3201 52 ▶ $required{ $k } = $pd->{required} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pd->{'required'} ? : 50 2 52
3202 }
3203 516 ▶ if( exists( $pd->{optional} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pd->{"optional"}) 4 512 516
3204 {
3205 4 ▶ $required{ $k } = $pd->{optional} ? 0 : 1; # optional => 0 means required
Branch: 1 branch
BranchTrueFalseTotal
$pd->{'optional'} ? : 2 2 4
3206 }
3207 }
3208 }
3209  
3210 523 for my $rq ( grep{ $required{ $_ } } sort( keys( %required ) ) )
3211 {
3212 339 ▶ next if( exists( $H->{ $rq } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $H->{$rq} 279 60 339
3213 60 ▶ _t( $ctx,$sp, 'required', undef, 'fail', $rq ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 1 59 60
3214  
3215 60 my @need = sort grep { $required{ $_ } } keys %required;
3216 60 my @have = sort keys %$H;
3217  
3218 60 ▶ my $need_str = @need ? join( ', ', @need ) : '(none)';
Branch: 1 branch
BranchTrueFalseTotal
@need ? : 60 0 60
3219 60 ▶ my $have_str = @have ? join( ', ', @have ) : '(none)';
Branch: 1 branch
BranchTrueFalseTotal
@have ? : 52 8 60
3220  
3221 60 my $msg = "required property '$rq' is missing "
3222 . "(required: $need_str; present: $have_str)";
3223  
3224 60 _err_res(
3225 $ctx,
3226 _join_ptr( $sp, $rq ),
3227 $msg,
3228 'required'
3229 );
3230  
3231 60 $ok = 0;
3232 60 ▶ return( { ok => 0, props => {}, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 1 59 60
3233 }
3234  
3235 522 ▶ if( exists( $S->{propertyNames} ) && ( ref( $S->{propertyNames} ) eq 'HASH' || JSON::is_bool( $S->{propertyNames} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH" || &JSON::is_bool($S->{"propertyNames"})) 10 512 522
Condition66%4/6
exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH" || &JSON::is_bool($S->{"propertyNames"})
!ll&&!rl&&r
512010
ref $S->{"propertyNames"} eq "HASH" or &JSON::is_bool($S->{"propertyNames"})
l!l&&r!l&&!r
640
MC/DC33%1/3
exists $S->{"propertyNames"} and ref $S->{"propertyNames"} eq "HASH" || &JSON::is_bool($S->{"propertyNames"})
exists $S->{"propertyNames"} ref $S->{"propertyNames"} eq "HASH" &JSON::is_bool($S->{"propertyNames"})
Truth table75%3/4
A and B or C
A= exists $S->{"propertyNames"}B= ref $S->{"propertyNames"} eq "HASH"C= &JSON::is_bool($S->{"propertyNames"})
ABCresult
0XX0
1000
11X1
1011
3236 {
3237 10 for my $k ( keys( %$H ) )
3238 {
3239 16 my $r = _v( $ctx, _join_ptr( $sp, "propertyNames" ), $S->{propertyNames}, $k );
3240 16 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 4 12 16
3241 {
3242 4 $ok = 0;
3243 4 ▶ return( { ok => 0, props => {}, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 4 4
3244 }
3245 }
3246 }
3247  
3248 522 ▶ my $props = $S->{properties} || {};
Condition100%2/2
$S->{'properties'} || {}
l!l
390132
MC/DC100%1/1
$S->{'properties'} || {}
$S->{'properties'}
Truth table100%2/2
A
A= $S->{'properties'}
Aresult
11
00
3249 522 ▶ my $patprops = $S->{patternProperties} || {};
Condition100%2/2
$S->{'patternProperties'} || {}
l!l
20502
MC/DC100%1/1
$S->{'patternProperties'} || {}
$S->{'patternProperties'}
Truth table100%2/2
A
A= $S->{'patternProperties'}
Aresult
11
00
3250 522 my $addl_set = exists( $S->{additionalProperties} );
3251 522 ▶ my $addl = $addl_set ? $S->{additionalProperties} : JSON::true;
Branch: 1 branch
BranchTrueFalseTotal
$addl_set ? : 178 344 522
3252  
3253 522 my %ann;
3254  
3255 522 for my $k ( sort( keys( %$H ) ) )
3256 {
3257 788 my $v = $H->{ $k };
3258 788 my $matched = 0;
3259  
3260 788 my $child_path = _inst_ptr( $ctx, $k );
3261 788 push( @{$ctx->{ptr_stack}}, $child_path );
3262  
3263 788 ▶ if( exists( $props->{ $k } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $props->{$k}) 400 388 788
3264 {
3265 400 my $r = _v( $ctx, _join_ptr( $sp, "properties/$k" ), $props->{ $k }, $v );
3266 400 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 100 300 400
3267 {
3268 100 $ok = 0;
3269 100 pop( @{$ctx->{ptr_stack}} );
3270 100 ▶ return( { ok => 0, props => \%ann, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 3 97 100
3271 97 next;
3272 }
3273 300 $ann{ $k } = 1;
3274 300 $matched = 1;
3275 }
3276  
3277 # Every matching pattern applies, even when the property has an explicit schema.
3278 # Keep the instance path on the stack until all matching patterns have been checked.
3279 {
3280 688 local $@;
3281 688 for my $re ( sort( keys( %$patprops ) ) )
3282 {
3283 29 my $re_ok = eval{ $k =~ /$re/ };
3284 29 ▶ next unless( $re_ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $re_ok 13 16 29
3285 16 $matched = 1;
3286  
3287 16 my $r = _v( $ctx, _join_ptr( $sp, "patternProperties/$re" ), $patprops->{ $re }, $v );
3288 16 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 10 6 16
3289 {
3290 10 $ok = 0;
3291 10 ▶ if( $bail_if_max->() )
Branch: 1 branch
BranchTrueFalseTotal
if (&$bail_if_max()) 2 8 10
3292 {
3293 2 pop( @{$ctx->{ptr_stack}} );
3294 2 return( { ok => 0, props => \%ann, items => {} } );
3295 }
3296 8 next;
3297 }
3298 6 $ann{ $k } = 1;
3299 }
3300 }
3301  
3302 686 ▶ unless( $matched )
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 382 304 686
3303 {
3304 382 ▶ if( $addl_set && !_is_true( $addl ) && !_is_hash( $addl ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($addl_set and not _is_true($addl) and not _is_hash($addl)) { } 16 366 382
elsif (ref $addl eq 'HASH') { } 5 361 366
elsif ($addl_set) { } 2 359 361
Condition100%6/6
$addl_set and not _is_true($addl) and not _is_hash($addl)
!ll&&!rl&&r
361516
$addl_set and not _is_true($addl)
!ll&&!rl&&r
359221
MC/DC100%3/3
$addl_set and not _is_true($addl) and not _is_hash($addl)
$addl_set not _is_true($addl) not _is_hash($addl)
Truth table100%4/4
A and not(B) and not(C)
A= $addl_setB= not _is_true($addl)C= not _is_hash($addl)
ABCresult
0XX0
10X0
1100
1111
3305 {
3306 16 _err_res( $ctx, _join_ptr( $sp, $k ), "additionalProperties not allowed: '$k'", 'additionalProperties' );
3307 16 $ok = 0;
3308 16 pop( @{$ctx->{ptr_stack}} );
3309 16 ▶ return( { ok => 0, props => \%ann, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 16 16
3310 16 next;
3311 }
3312 elsif( ref( $addl ) eq 'HASH' )
3313 {
3314 5 my $r = _v( $ctx, _join_ptr( $sp, "additionalProperties" ), $addl, $v );
3315 5 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 0 5 5
3316 {
3317 0 $ok = 0;
3318 0 pop( @{$ctx->{ptr_stack}} );
3319 0 ▶ return( { ok => 0, props => \%ann, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 0 0
3320 0 next;
3321 }
3322 5 $ann{ $k } = 1;
3323 }
3324 elsif( $addl_set )
3325 {
3326 2 $ann{ $k } = 1;
3327 }
3328 }
3329  
3330 670 pop( @{$ctx->{ptr_stack}} );
3331 }
3332  
3333 517 ▶ if( my $depR = $S->{dependentRequired} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $depR = $S->{"dependentRequired"}) 8 509 517
3334 {
3335 8 for my $k ( sort( keys( %$depR ) ) )
3336 {
3337 8 ▶ next unless( exists( $H->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $H->{$k} 2 6 8
3338 6 ▶ for my $need ( @{$depR->{ $k } || []} )
Condition50%1/2
$depR->{$k} || []
l!l
60
MC/DC0%0/1
$depR->{$k} || []
$depR->{$k}
Truth table50%1/2
A
A= $depR->{$k}
Aresult
11
00
3339 {
3340 6 ▶ next if( exists( $H->{ $need } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $H->{$need} 4 2 6
3341 2 _err_res( $ctx, _join_ptr( $sp, $need ), "dependentRequired: '$need' required when '$k' is present", 'dependentRequired' );
3342 2 $ok = 0;
3343 2 ▶ return( { ok => 0, props => \%ann, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 2 2
3344 }
3345 }
3346 }
3347  
3348 517 ▶ if( my $depS = $S->{dependentSchemas} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $depS = $S->{"dependentSchemas"}) 14 503 517
3349 {
3350 14 for my $k ( sort( keys( %$depS ) ) )
3351 {
3352 14 ▶ next unless( exists( $H->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $H->{$k} 3 11 14
3353 11 my $r = _v( $ctx, _join_ptr( $sp, "dependentSchemas/$k" ), $depS->{ $k }, $H );
3354 11 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $r->{'ok'}) { } 4 7 11
3355 {
3356 4 $ok = 0;
3357 4 ▶ return( { ok => 0, props => \%ann, items => {} } ) if( $bail_if_max->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$bail_if_max() 0 4 4
3358 }
3359 else
3360 {
3361 7 %ann = ( %ann, %{$r->{props}} );
3362 }
3363 }
3364 }
3365  
3366 517 ▶ return( { ok => ( $ok ? 1 : 0 ), props => \%ann, items => {} } );
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 344 173 517
3367 }
3368  
3369 sub _k_string
3370 57 ▶{
Subroutine _k_string: called CC=15 SCAR=27.7
3371 57 my( $ctx, $s, $S, $ptr ) = @_;
3372 57 my $len = _strlen( $s );
3373  
3374 57 ▶ if( exists( $S->{minLength} ) && $len < $S->{minLength} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"minLength"} and $len < $S->{"minLength"}) 7 50 57
Condition100%3/3
exists $S->{"minLength"} and $len < $S->{"minLength"}
!ll&&!rl&&r
24267
MC/DC100%2/2
exists $S->{"minLength"} and $len < $S->{"minLength"}
exists $S->{"minLength"} $len < $S->{"minLength"}
Truth table100%3/3
A and B
A= exists $S->{"minLength"}B= $len < $S->{"minLength"}
ABresult
0X0
100
111
3375 {
3376 7 ▶ _t( $ctx, $ptr, 'minLength', undef, 'fail', "len=$len < $S->{minLength}" ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 1 6 7
3377 7 return( _err( $ctx, $ptr, "string shorter than minLength $S->{minLength}", 'minLength' ) );
3378 }
3379 50 ▶ _t( $ctx, $ptr, 'minLength', undef, 'pass', "len=$len >= $S->{minLength}" ) if( exists( $S->{minLength} ) && $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $S->{"minLength"} and $ctx->{"trace_on"} 0 50 50
Condition66%2/3
exists $S->{"minLength"} and $ctx->{"trace_on"}
!ll&&!rl&&r
24260
MC/DC0%0/2
exists $S->{"minLength"} and $ctx->{"trace_on"}
exists $S->{"minLength"} $ctx->{"trace_on"}
Truth table66%2/3
A and B
A= exists $S->{"minLength"}B= $ctx->{"trace_on"}
ABresult
0X0
100
111
3380  
3381 50 ▶ if( exists( $S->{maxLength} ) && $len > $S->{maxLength} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"maxLength"} and $len > $S->{"maxLength"}) 3 47 50
Condition100%3/3
exists $S->{"maxLength"} and $len > $S->{"maxLength"}
!ll&&!rl&&r
3983
MC/DC100%2/2
exists $S->{"maxLength"} and $len > $S->{"maxLength"}
exists $S->{"maxLength"} $len > $S->{"maxLength"}
Truth table100%3/3
A and B
A= exists $S->{"maxLength"}B= $len > $S->{"maxLength"}
ABresult
0X0
100
111
3382 {
3383 3 ▶ _t( $ctx, $ptr, 'maxLength', undef, 'fail', "len=$len > $S->{maxLength}" ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 3 3
3384 3 return( _err( $ctx, $ptr, "string longer than maxLength $S->{maxLength}", 'maxLength' ) );
3385 }
3386 47 ▶ _t( $ctx, $ptr, 'maxLength', undef, 'pass', "len=$len <= $S->{maxLength}" ) if( exists( $S->{maxLength} ) && $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $S->{"maxLength"} and $ctx->{"trace_on"} 0 47 47
Condition66%2/3
exists $S->{"maxLength"} and $ctx->{"trace_on"}
!ll&&!rl&&r
3980
MC/DC0%0/2
exists $S->{"maxLength"} and $ctx->{"trace_on"}
exists $S->{"maxLength"} $ctx->{"trace_on"}
Truth table66%2/3
A and B
A= exists $S->{"maxLength"}B= $ctx->{"trace_on"}
ABresult
0X0
100
111
3387  
3388 47 ▶ if( exists( $S->{pattern} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $S->{"pattern"}) 18 29 47
3389 {
3390 18 my $re = $S->{pattern};
3391 18 local $@;
3392 18 my $ok = eval{ $s =~ /$re/ };
3393 18 ▶ _t( $ctx, $ptr, 'pattern', undef, $ok ? 'pass' : 'fail', "/$re/" ) if( $ctx->{trace_on} );
Branch: 2 branches
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 18 18
$ok ? : 0 0 0
3394 18 ▶ return( _err( $ctx, $ptr, "string does not match pattern /$re/", 'pattern' ) ) unless( $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 16 18
3395 }
3396 45 return(1);
3397 }
3398  
3399 # Primitive keyword helpers
3400 sub _k_type
3401 839 ▶{
Subroutine _k_type: called CC=9 SCAR=22.0
3402 839 my( $ctx, $v, $type_kw, $ptr ) = @_;
3403  
3404 839 ▶ my @alts = ref( $type_kw ) eq 'ARRAY' ? @$type_kw : ( $type_kw );
Branch: 1 branch
BranchTrueFalseTotal
ref $type_kw eq 'ARRAY' ? : 28 811 839
3405  
3406 # First, allow inline schemas in the union
3407 839 my $i = 0;
3408 839 for my $alt ( @alts )
3409 {
3410 901 ▶ if( ref( $alt ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $alt eq "HASH") 22 879 901
3411 {
3412 22 my %shadow = %$ctx;
3413 22 $shadow{errors} = [];
3414 22 $shadow{error_count} = 0;
3415 22 my $r = _v( \%shadow, _join_ptr( $ptr, "type/$i" ), $alt, $v );
3416 22 ▶ return(1) if( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"ok"} 3 19 22
3417 }
3418 898 $i++;
3419 }
3420  
3421 # Then, try string type names
3422 836 for my $t ( @alts )
3423 {
3424 869 ▶ next if( ref( $t ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $t 17 852 869
3425 852 ▶ return(1) if( _match_type( $v, $t ) );
Branch: 1 branch
BranchTrueFalseTotal
if _match_type($v, $t) 726 126 852
3426 }
3427  
3428 110 ▶ my $exp = join( '|', map{ ref( $_ ) ? 'schema' : $_ } @alts );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 15 108 123
3429 110 return( _err( $ctx, $ptr, "type mismatch: expected $exp", 'type' ) );
3430 }
3431  
3432 # Evaluate remaining properties or items only after all adjacent applicators have contributed annotations.
3433 sub _k_unevaluated
3434 963 ▶{
Subroutine _k_unevaluated: called CC=15 SCAR=27.1
3435 963 my( $ctx, $sp, $S, $inst, $props, $items ) = @_;
3436  
3437 963 my( $kw, $ann, @unknown );
3438 963 ▶ if( ref( $inst ) eq 'HASH' && exists( $S->{unevaluatedProperties} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $inst eq 'HASH' and exists $S->{'unevaluatedProperties'}) { } 51 912 963
elsif (ref $inst eq 'ARRAY' and exists $S->{'unevaluatedItems'}) { } 21 891 912
Condition100%3/3
ref $inst eq "HASH" and exists $S->{"unevaluatedProperties"}
!ll&&!rl&&r
64726551
MC/DC100%2/2
ref $inst eq "HASH" and exists $S->{"unevaluatedProperties"}
ref $inst eq "HASH" exists $S->{"unevaluatedProperties"}
Truth table100%3/3
A and B
A= ref $inst eq "HASH"B= exists $S->{"unevaluatedProperties"}
ABresult
0X0
100
111
3439 {
3440 51 $kw = 'unevaluatedProperties';
3441 51 $ann = $props;
3442 51 @unknown = grep { !$ann->{ $_ } } sort( keys( %$inst ) );
3443 }
3444 ▶ elsif( ref( $inst ) eq 'ARRAY' && exists( $S->{unevaluatedItems} ) )
Condition100%3/3
ref $inst eq "ARRAY" and exists $S->{"unevaluatedItems"}
!ll&&!rl&&r
8543721
MC/DC100%2/2
ref $inst eq "ARRAY" and exists $S->{"unevaluatedItems"}
ref $inst eq "ARRAY" exists $S->{"unevaluatedItems"}
Truth table100%3/3
A and B
A= ref $inst eq "ARRAY"B= exists $S->{"unevaluatedItems"}
ABresult
0X0
100
111
3445 {
3446 21 $kw = 'unevaluatedItems';
3447 21 $ann = $items;
3448 21 @unknown = grep { !$ann->{ $_ } } ( 0 .. $#$inst );
3449 }
3450 else
3451 {
3452 891 return( { ok => 1, props => $props, items => $items } );
3453 }
3454  
3455 72 my $UE = $S->{ $kw };
3456 72 ▶ if( @unknown && !_is_true( $UE ) && !_is_hash( $UE ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown and not _is_true($UE) and not _is_hash($UE)) 23 49 72
Condition100%6/6
@unknown and not _is_true($UE) and not _is_hash($UE)
!ll&&!rl&&r
45423
@unknown and not _is_true($UE)
!ll&&!rl&&r
43227
MC/DC100%3/3
@unknown and not _is_true($UE) and not _is_hash($UE)
@unknown not _is_true($UE) not _is_hash($UE)
Truth table100%4/4
A and not(B) and not(C)
A= @unknownB= not _is_true($UE)C= not _is_hash($UE)
ABCresult
0XX0
10X0
1100
1111
3457 {
3458 23 ▶ my $msg = $kw eq 'unevaluatedItems' ? 'unevaluatedItems not allowed at indices: ' : 'unevaluatedProperties not allowed: ';
Branch: 1 branch
BranchTrueFalseTotal
$kw eq 'unevaluatedItems' ? : 7 16 23
3459 23 return( _err_res( $ctx, $sp, $msg . join( ',', @unknown ), $kw ) );
3460 }
3461 49 my $ok = 1;
3462 49 for my $k ( @unknown )
3463 {
3464 6 ▶ if( ref( $UE ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $UE eq "HASH") 4 2 6
3465 {
3466 4 ▶ my $value = ref( $inst ) eq 'HASH' ? $inst->{ $k } : $inst->[$k];
Branch: 1 branch
BranchTrueFalseTotal
ref $inst eq 'HASH' ? : 4 0 4
3467 4 push( @{$ctx->{ptr_stack}}, _inst_ptr( $ctx, $k ) );
3468 4 my $r = _v( $ctx, _join_ptr( $sp, $kw ), $UE, $value );
3469 4 pop( @{$ctx->{ptr_stack}} );
3470 4 ▶ if( !$r->{ok} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"ok"}) 2 2 4
3471 {
3472 2 $ok = 0;
3473 2 ▶ last if( $ctx->{max_errors} && $ctx->{error_count} >= $ctx->{max_errors} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"} 0 2 2
Condition33%1/3
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
!ll&&!rl&&r
020
MC/DC0%0/2
$ctx->{"max_errors"} and $ctx->{"error_count"} >= $ctx->{"max_errors"}
$ctx->{"max_errors"} $ctx->{"error_count"} >= $ctx->{"max_errors"}
Truth table33%1/3
A and B
A= $ctx->{"max_errors"}B= $ctx->{"error_count"} >= $ctx->{"max_errors"}
ABresult
0X0
100
111
3474 2 next;
3475 }
3476 }
3477 4 $ann->{ $k } = 1;
3478 }
3479 49 return( { ok => $ok, props => $props, items => $items } );
3480 }
3481  
3482 sub _k_unique_keys
3483 17 ▶{
Subroutine _k_unique_keys: called CC=12 SCAR=25.2
3484 17 my( $ctx, $sp, $uk_def, $array ) = @_;
3485 17 ▶ unless( ref( $uk_def ) eq 'ARRAY' && @$uk_def )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $uk_def eq "ARRAY" and @$uk_def) 0 17 17
Condition33%1/3
ref $uk_def eq "ARRAY" and @$uk_def
!ll&&!rl&&r
0017
MC/DC0%0/2
ref $uk_def eq "ARRAY" and @$uk_def
ref $uk_def eq "ARRAY" @$uk_def
Truth table33%1/3
A and B
A= ref $uk_def eq "ARRAY"B= @$uk_def
ABresult
0X0
100
111
3486 {
3487 0 return( { ok => 1, props => {}, items => {} } );
3488 }
3489  
3490 17 for my $key_set ( @$uk_def )
3491 {
3492 19 ▶ next unless( ref( $key_set ) eq 'ARRAY' && @$key_set );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key_set eq "ARRAY" and @$key_set 0 19 19
Condition33%1/3
ref $key_set eq "ARRAY" and @$key_set
!ll&&!rl&&r
0019
MC/DC0%0/2
ref $key_set eq "ARRAY" and @$key_set
ref $key_set eq "ARRAY" @$key_set
Truth table33%1/3
A and B
A= ref $key_set eq "ARRAY"B= @$key_set
ABresult
0X0
100
111
3493  
3494 19 my %seen;
3495 19 for my $i ( 0 .. $#$array )
3496 {
3497 42 my $item = $array->[$i];
3498 42 ▶ next unless( ref( $item ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 2 40 42
3499  
3500 40 my @key_vals;
3501 40 my $all_present = 1;
3502 40 for my $key ( @$key_set )
3503 {
3504 47 ▶ if( exists( $item->{ $key } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $item->{$key}) { } 44 3 47
3505 {
3506 44 push( @key_vals, _canon( $item->{ $key } ) );
3507 }
3508 else
3509 {
3510 3 $all_present = 0;
3511 3 last;
3512 }
3513 }
3514  
3515 # Skip items that do not have *all* keys in this key set
3516 40 ▶ next unless( $all_present );
Branch: 1 branch
BranchTrueFalseTotal
unless $all_present 3 37 40
3517  
3518 37 my $composite = join( "\0", @key_vals );
3519 37 ▶ if( exists( $seen{ $composite } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$composite}) 8 29 37
3520 {
3521 8 my $prev_i = $seen{ $composite };
3522 8 my $keys = join( ', ', map { "'$_'" } @$key_set );
3523 8 push( @{$ctx->{ptr_stack}}, _inst_ptr( $ctx, $i ) );
3524 8 my $res = _err_res(
3525 $ctx,
3526 $sp,
3527 "uniqueKeys violation: items[$prev_i] and items[$i] have identical values for key(s) [$keys]",
3528 'uniqueKeys',
3529 );
3530 8 pop( @{$ctx->{ptr_stack}} );
3531  
3532 8 return( $res );
3533 }
3534 29 $seen{ $composite } = $i;
3535 }
3536 }
3537  
3538 9 return( { ok => 1, props => {}, items => {} } );
3539 }
3540  
3541 sub _match_type
3542 2127 ▶{
Subroutine _match_type: called CC=22 SCAR=31.0
3543 2127 my( $v, $t ) = @_;
3544  
3545 2127 ▶ return(1) if( $t eq 'null' && !defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "null" and not defined $v 3 2124 2127
Condition66%2/3
$t eq "null" and not defined $v
!ll&&!rl&&r
212403
MC/DC50%1/2
$t eq "null" and not defined $v
$t eq "null" not defined $v
Truth table66%2/3
A and not(B)
A= $t eq "null"B= not defined $v
ABresult
0X0
100
111
3546  
3547 2124 ▶ if( $t eq 'boolean' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "boolean") 16 2108 2124
3548 {
3549 16 ▶ return(0) if( !defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 14 16
3550 14 ▶ if( blessed( $v ) && ( ref( $v ) =~ /Boolean/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and ref($v) =~ /Boolean/u) 8 6 14
Condition66%2/3
builtin::blessed($v) and ref($v) =~ /Boolean/u
!ll&&!rl&&r
608
MC/DC50%1/2
builtin::blessed($v) and ref($v) =~ /Boolean/u
builtin::blessed($v) ref($v) =~ /Boolean/u
Truth table66%2/3
A and B
A= builtin::blessed($v)B= ref($v) =~ /Boolean/u
ABresult
0X0
100
111
3551 {
3552 8 my $s = "$v";
3553 8 ▶ return( ( $s eq '0' || $s eq '1' ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$s eq '0' || $s eq '1' ? : 8 0 8
Condition66%2/3
$s eq 0 or $s eq 1
l!l&&r!l&&!r
170
MC/DC0%0/2
$s eq 0 or $s eq 1
$s eq 0 $s eq 1
Truth table66%2/3
A or B
A= $s eq 0B= $s eq 1
ABresult
1X1
011
000
3554 }
3555 6 ▶ return( ( $v =~ /\A(?:0|1|true|false)\z/i ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$v =~ /\A(?:0|1|true|false)\z/iu ? : 2 4 6
3556 }
3557  
3558 2108 ▶ if( $t eq 'integer' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "integer") 280 1828 2108
3559 {
3560 280 ▶ return(0) unless( _is_number( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_number($v) 54 226 280
3561 226 ▶ return( $v == int( $v ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$v == int $v ? : 223 3 226
3562 }
3563  
3564 1828 ▶ if( $t eq 'number' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "number") 35 1793 1828
3565 {
3566 35 return( _is_number( $v ) );
3567 }
3568  
3569 1793 ▶ if( $t eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "string") 1466 327 1793
3570 {
3571 # Must be a scalar, defined, and NOT a numeric SV under strict typing.
3572 1466 ▶ return( (!ref( $v ) && defined( $v ) && !_is_number( $v )) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
!ref($v) && defined $v && !_is_number($v) ? : 395 1071 1466
Condition100%6/6
not ref $v and defined $v and not _is_number($v)
!ll&&!rl&&r
628443395
not ref $v and defined $v
!ll&&!rl&&r
6217838
MC/DC100%3/3
not ref $v and defined $v and not _is_number($v)
not ref $v defined $v not _is_number($v)
Truth table100%4/4
not(A) and B and not(C)
A= not ref $vB= defined $vC= not _is_number($v)
ABCresult
0XX0
10X0
1100
1111
3573 }
3574  
3575 327 ▶ return(1) if( $t eq 'array' && ref( $v ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "array" and ref $v eq "ARRAY" 41 286 327
Condition100%3/3
$t eq "array" and ref $v eq "ARRAY"
!ll&&!rl&&r
277941
MC/DC100%2/2
$t eq "array" and ref $v eq "ARRAY"
$t eq "array" ref $v eq "ARRAY"
Truth table100%3/3
A and B
A= $t eq "array"B= ref $v eq "ARRAY"
ABresult
0X0
100
111
3576 286 ▶ return(1) if( $t eq 'object' && ref( $v ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "object" and ref $v eq "HASH" 259 27 286
Condition100%3/3
$t eq "object" and ref $v eq "HASH"
!ll&&!rl&&r
918259
MC/DC100%2/2
$t eq "object" and ref $v eq "HASH"
$t eq "object" ref $v eq "HASH"
Truth table100%3/3
A and B
A= $t eq "object"B= ref $v eq "HASH"
ABresult
0X0
100
111
3577  
3578 27 return(0);
3579 }
3580  
3581 sub _parse_media_type
3582 10 ▶{
Subroutine _parse_media_type: called CC=5 SCAR=16.3
3583 10 my( $s ) = @_;
3584 10 my( $type, @rest ) = split( /;/, "$s" );
3585 10 ▶ $type ||= '';
Condition50%1/2
$type ||= ""
l!l
100
MC/DC0%0/1
$type ||= ""
$type
Truth table50%1/2
A
A= $type
Aresult
11
00
3586 10 $type =~ s/\s+//g;
3587 10 my %p;
3588 10 for my $kv ( @rest )
3589 {
3590 4 my( $k, $v ) = split( /=/, $kv, 2 );
3591 4 ▶ next unless( defined( $k ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 4 4
3592 4 $k =~ s/\s+//g;
3593 4 ▶ $v = '' unless( defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 4 4
3594 4 $v =~ s/^\s+|\s+$//g;
3595 # Allow single or double quote, but be consistent
3596 4 $v =~ s/^(?<quote>["'])(.*)\g{quote}$/$2/;
3597 4 $p{ lc( $k ) } = $v;
3598 }
3599 10 return( lc( $type ), \%p );
3600 }
3601  
3602 sub _prune_array_with_schema
3603 1 ▶{
Subroutine _prune_array_with_schema: called CC=6 SCAR=18.0
3604 1 my( $self, $schema, $data ) = @_;
3605  
3606 1 my @out;
3607  
3608 1 my $shape =
3609 {
3610 prefix_items => [], # index => subschema
3611 items => undef # subschema for additional items
3612 };
3613  
3614 1 $self->_extract_array_shape( $schema, $shape );
3615  
3616 1 for my $i ( 0 .. $#$data )
3617 {
3618 3 my $item = $data->[ $i ];
3619 3 my $item_schema;
3620  
3621 3 ▶ if( defined( $shape->{prefix_items}->[ $i ] ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $shape->{'prefix_items'}[$i]) { } 1 2 3
elsif ($shape->{'items'}) { } 2 0 2
3622 {
3623 1 $item_schema = $shape->{prefix_items}->[ $i ];
3624 }
3625 elsif( $shape->{items} )
3626 {
3627 2 $item_schema = $shape->{items};
3628 }
3629  
3630 3 ▶ if( $item_schema && ref( $item ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($item_schema and ref $item) { } 2 1 3
Condition66%2/3
$item_schema and ref $item
!ll&&!rl&&r
012
MC/DC50%1/2
$item_schema and ref $item
$item_schema ref $item
Truth table66%2/3
A and B
A= $item_schemaB= ref $item
ABresult
0X0
100
111
3631 {
3632 2 $out[ $i ] = $self->_prune_with_schema( $item_schema, $item );
3633 }
3634 else
3635 {
3636 # No structural knowledge or scalar item: keep as-is
3637 1 $out[ $i ] = $item;
3638 }
3639 }
3640  
3641 1 return( \@out );
3642 }
3643  
3644 sub _prune_object_with_schema
3645 6 ▶{
Subroutine _prune_object_with_schema: called CC=9 SCAR=22.9
3646 6 my( $self, $schema, $data ) = @_;
3647  
3648 # Collect effective object shape from this schema and any allOf mixins.
3649 6 my $shape =
3650 {
3651 props => {}, # property name => subschema
3652 patterns => [], # [ regex, subschema ] ...
3653 allow_ap => 1, # additionalProperties allowed?
3654 ap_schema => undef # subschema for additionalProperties, if any
3655 };
3656  
3657 6 $self->_extract_object_shape( $schema, $shape );
3658  
3659 6 my %clean;
3660  
3661 KEY:
3662 6 foreach my $key ( keys( %$data ) )
3663 {
3664 16 my $val = $data->{ $key };
3665  
3666 # 1) Direct properties
3667 16 ▶ if( exists( $shape->{props}->{ $key } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $shape->{"props"}{$key}) 7 9 16
3668 {
3669 7 my $sub = $shape->{props}->{ $key };
3670 7 $clean{ $key } = $self->_prune_with_schema( $sub, $val );
3671 7 next KEY;
3672 }
3673  
3674 # 2) patternProperties
3675 9 foreach my $pair ( @{$shape->{patterns}} )
3676 {
3677 3 my( $re, $pschema ) = @$pair;
3678 3 my $ok;
3679  
3680 {
3681 3 local $@;
3682 3 ▶ $ok = eval{ $key =~ /$re/ ? 1 : 0; };
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /$re/u ? : 2 1 3
3683 }
3684  
3685 3 ▶ next unless( $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 2 3
3686  
3687 2 $clean{ $key } = $self->_prune_with_schema( $pschema, $val );
3688 2 next KEY;
3689 }
3690  
3691 # 3) additionalProperties
3692 7 ▶ if( $shape->{allow_ap} )
Branch: 1 branch
BranchTrueFalseTotal
if ($shape->{'allow_ap'}) { } 0 7 7
3693 {
3694 0 ▶ if( $shape->{ap_schema} && ref( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($shape->{'ap_schema'} and ref $val) { } 0 0 0
Condition0%0/3
$shape->{"ap_schema"} and ref $val
!ll&&!rl&&r
000
MC/DC0%0/2
$shape->{"ap_schema"} and ref $val
$shape->{"ap_schema"} ref $val
Truth table0%0/3
A and B
A= $shape->{"ap_schema"}B= ref $val
ABresult
0X0
100
111
3695 {
3696 0 $clean{ $key } = $self->_prune_with_schema( $shape->{ap_schema}, $val );
3697 }
3698 else
3699 {
3700 # allowed, but no further structure known
3701 0 $clean{ $key } = $val;
3702 }
3703 }
3704 else
3705 {
3706 # additionalProperties is false (or equivalent) => drop unknown key
3707 7 next KEY;
3708 }
3709 }
3710  
3711 6 return( \%clean );
3712 }
3713  
3714 sub _prune_with_schema
3715 17 ▶{
Subroutine _prune_with_schema: called CC=4 SCAR=13.9
3716 17 my( $self, $schema, $data ) = @_;
3717  
3718 # Boolean schemas and non-hash schemas: do not attempt pruning.
3719 17 ▶ return( $data ) unless( ref( $schema ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 17 17
3720  
3721 # Only prune structured values; scalars we leave untouched.
3722 17 ▶ if( ref( $data ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 6 11 17
elsif (ref $data eq 'ARRAY') { } 1 10 11
3723 {
3724 6 return( $self->_prune_object_with_schema( $schema, $data ) );
3725 }
3726 elsif( ref( $data ) eq 'ARRAY' )
3727 {
3728 1 return( $self->_prune_array_with_schema( $schema, $data ) );
3729 }
3730  
3731 10 return( $data );
3732 }
3733  
3734 52 my %SCRIPT_LIKE = map{ $_ => 1 } qw(
3735 Hiragana Katakana Han Hangul Latin Cyrillic Greek
3736 Hebrew Thai Armenian Georgian
3737 Arabic Devanagari Bengali Gurmukhi Gujarati Oriya
3738 Tamil Telugu Kannada Malayalam Sinhala
3739 Lao Tibetan Myanmar Khmer
3740 );
3741 sub _re_to_js
3742 0 ▶{
Subroutine _re_to_js: not called CC=7 SCAR=40.3
3743 0 my $re = shift( @_ );
3744 0 my %opt = @_;
3745  
3746 # style: 'literal' => for use in /.../u
3747 # 'string' => for use in new RegExp("...", "u")
3748 0 ▶ my $style = $opt{style} || 'string';
Condition0%0/2
$opt{'style'} || 'string'
l!l
00
MC/DC0%0/1
$opt{'style'} || 'string'
$opt{'style'}
Truth table0%0/2
A
A= $opt{'style'}
Aresult
11
00
3749  
3750 0 ▶ return if( !defined( $re ) || !length( $re ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $re and length $re 0 0 0
Condition0%0/3
defined $re and length $re
!ll&&!rl&&r
000
MC/DC0%0/2
defined $re and length $re
defined $re length $re
Truth table0%0/3
A and B
A= defined $reB= length $re
ABresult
0X0
100
111
3751  
3752 #
3753 # 1) Convert \x{...} (Perl) to \u.... or \u{....} (JS)
3754 # - 1–4 hex digits -> \uHHHH
3755 # - 5–6 hex digits -> \u{HHHHH} (requires /u in JS)
3756 #
3757 0 $re =~ s{
3758 \\x\{([0-9A-Fa-f]{1,6})\}
3759 }{
3760 0 my $hex = uc( $1 );
3761 0 ▶ if( length( $hex ) <= 4 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $hex <= 4) { } 0 0 0
3762 {
3763 0 "\\u$hex";
3764 }
3765 else
3766 {
3767 0 "\\u\{$hex\}";
3768 }
3769 }egx;
3770  
3771 # Script eXtended
3772 #
3773 # 2) Convert \p{Katakana} / \P{Katakana}
3774 # to \p{scx=Katakana} / \P{scx=Katakana}
3775 # (Script_Extensions so it covers half-width too)
3776 #
3777 0 $re =~ s[
3778 \\([pP])\{([^}]+)\}
3779 ][
3780 0 my( $p, $name ) = ( $1, $2 );
3781  
3782 0 my $norm = $name;
3783 0 $norm =~ s/^\s+|\s+$//g;
3784 0 $norm = ucfirst( lc( $norm ) );
3785  
3786 0 ▶ if( exists( $SCRIPT_LIKE{ $norm } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SCRIPT_LIKE{$norm}) { } 0 0 0
3787 {
3788 0 "\\$p\{scx=$norm\}";
3789 }
3790 else
3791 {
3792 # Pass other properties through unchanged:
3793 # \p{L}, \p{Letter}, \p{Nd}, etc.
3794 0 "\\$p\{$name\}";
3795 }
3796 ]egx;
3797  
3798 #
3799 # 3) Optionally escape backslashes for JS string literal
3800 #
3801 0 ▶ if( $style eq 'string' )
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "string") 0 0 0
3802 {
3803 0 $re =~ s{\\}{\\\\}g; # existing
3804 0 $re =~ s{"}{\\"}g; # escape "
3805 0 $re =~ s{\n}{\\n}g;
3806 0 $re =~ s{\r}{\\r}g;
3807 0 $re =~ s{\x{2028}}{\\u2028}g;
3808 0 $re =~ s{\x{2029}}{\\u2029}g;
3809 0 $re =~ s{/}{\/}g;
3810 }
3811  
3812 # For 'literal', we leave backslashes as-is, for use in /.../u
3813  
3814 0 return( $re );
3815 }
3816  
3817 sub _register_builtin_media_validators
3818 18 ▶{
Subroutine _register_builtin_media_validators: called CC=2 SCAR=6.9
3819 18 my( $self ) = @_;
3820 18 ▶ $self->register_media_validator( 'application/json' => \&_builtin_json_media ) unless( exists( $self->{media_validators}->{'application/json'} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"media_validators"}{"application/json"} 16 2 18
3821 18 return( $self );
3822 }
3823  
3824 sub _resolve_uri
3825 289 ▶{
Subroutine _resolve_uri: called CC=28 SCAR=38.3
3826 289 my( $base, $ref ) = @_;
3827 289 ▶ $base //= '';
Condition50%1/2
$base //= ""
l!l
2890
MC/DC0%0/1
$base //= ""
$base
Truth table50%1/2
A
A= $base
Aresult
11
00
3828 289 my( $bs, $ba, $bp, $bq ) = $base =~ m{\A(?:([A-Za-z][A-Za-z0-9+.-]*):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?};
3829 289 my( $rs, $ra, $rp, $rq, $rf ) = $ref =~ m{\A(?:([A-Za-z][A-Za-z0-9+.-]*):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?\z}s;
3830 289 ▶ return( $ref ) unless( defined( $rp ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rp 0 289 289
3831 289 my( $scheme, $authority, $path, $query ) = ( $rs, $ra, $rp, $rq );
3832 289 ▶ if( !defined( $rs ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rs) 187 102 289
3833 {
3834 187 $scheme = $bs;
3835 187 ▶ if( !defined( $ra ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ra) 187 0 187
3836 {
3837 187 $authority = $ba;
3838 187 ▶ if( $rp eq '' )
Branch: 2 branches
BranchTrueFalseTotal
if ($rp eq '') { } 150 37 187
elsif (not $rp =~ m[^/]u) { } 37 0 37
3839 {
3840 150 ▶ $path = $bp // '';
Condition50%1/2
$bp // ""
l!l
1500
MC/DC0%0/1
$bp // ""
$bp
Truth table50%1/2
A
A= $bp
Aresult
11
00
3841 150 ▶ $query = $bq unless( defined( $rq ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rq 150 0 150
3842 }
3843 elsif( $rp !~ m{^/} )
3844 {
3845 37 ▶ my $dir = $bp // '';
Condition50%1/2
$bp // ""
l!l
370
MC/DC0%0/1
$bp // ""
$bp
Truth table50%1/2
A
A= $bp
Aresult
11
00
3846 37 $dir =~ s{[^/]*$}{};
3847 37 ▶ $dir = '/' if( $dir eq '' && defined( $ba ) );
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq "" and defined $ba 0 37 37
Condition66%2/3
$dir eq "" and defined $ba
!ll&&!rl&&r
3520
MC/DC0%0/2
$dir eq "" and defined $ba
$dir eq "" defined $ba
Truth table66%2/3
A and B
A= $dir eq ""B= defined $ba
ABresult
0X0
100
111
3848 37 $path = $dir . $rp;
3849 }
3850 }
3851 }
3852 # Remove dot segments while retaining leading relative parent segments when there is no absolute base.
3853 289 ▶ if( $path =~ m{(?:^|/)\.\.?(?:/|$)} )
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[(?:^|/)\.\.?(?:/|$)]u) 6 283 289
3854 {
3855 6 my @parts;
3856 6 my $absolute = $path =~ m{^/};
3857 6 for my $part ( split( m{/}, $path, -1 ) )
3858 {
3859 24 ▶ next if( $part eq '.' );
Branch: 1 branch
BranchTrueFalseTotal
if $part eq "." 0 24 24
3860 24 ▶ if( $part eq '..' )
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq '..') { } 6 18 24
3861 {
3862 6 ▶ if( @parts && $parts[-1] ne '..' && !( $absolute && @parts == 1 ) ) { pop( @parts ); }
Branch: 2 branches
BranchTrueFalseTotal
if (@parts and $parts[-1] ne '..' and not $absolute && @parts == 1) { } 6 0 6
elsif (not $absolute) { } 0 0 0
Condition33%3/9
@parts and $parts[-1] ne ".." and not $absolute && @parts == 1
!ll&&!rl&&r
006
@parts and $parts[-1] ne ".."
!ll&&!rl&&r
006
$absolute && @parts == 1
!ll&&!rl&&r
060
MC/DC0%0/4
@parts and $parts[-1] ne ".." and not $absolute && @parts == 1
@parts $parts[-1] ne ".." $absolute @parts == 1
Truth table20%1/5
A and B and not(C && D)
A= @partsB= $parts[-1] ne ".."C= $absoluteD= @parts == 1
ABCDresult
0XXX0
10XX0
11110
110X1
11101
3863 0 elsif( !$absolute ) { push( @parts, $part ); }
3864 }
3865 18 else { push( @parts, $part ); }
3866 }
3867 6 ▶ push( @parts, '' ) if( $path =~ m{/(?:\.|\.\.)$} );
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/(?:\.|\.\.)$]u 0 6 6
3868 6 $path = join( '/', @parts );
3869 6 ▶ $path = '/' if( $absolute && $path eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if $absolute and $path eq "" 0 6 6
Condition33%1/3
$absolute and $path eq ""
!ll&&!rl&&r
060
MC/DC0%0/2
$absolute and $path eq ""
$absolute $path eq ""
Truth table33%1/3
A and B
A= $absoluteB= $path eq ""
ABresult
0X0
100
111
3870 }
3871 289 ▶ return( ( defined( $scheme ) ? "$scheme:" : '' ) . ( defined( $authority ) ? "//$authority" : '' ) . $path . ( defined( $query ) ? "?$query" : '' ) . ( defined( $rf ) ? "#$rf" : '' ) );
Branch: 4 branches
BranchTrueFalseTotal
defined $scheme ? : 189 100 289
defined $authority ? : 189 100 289
defined $query ? : 0 289 289
defined $rf ? : 167 122 289
3872 }
3873  
3874 sub _safe_invoke
3875 12 ▶{
Subroutine _safe_invoke: called CC=9 SCAR=32.3
3876 12 my( $cb, @args ) = @_;
3877 12 local $@;
3878 # Force list context to preserve (ok, msg, out) style returns
3879 12 my @ret = eval{ $cb->( @args ) };
3880 12 ▶ return( 0, ( $@ || 'callback failed' ), undef ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
Condition0%0/2
$@ || 'callback failed'
l!l
00
MC/DC0%0/1
$@ || 'callback failed'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
3881  
3882 # If callback returns (ok, msg, out) or (ok, msg)
3883 12 ▶ if( @ret >= 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (@ret >= 2) 12 0 12
3884 {
3885 12 ▶ my( $ok, $msg, $out ) = ( $ret[0] ? 1 : 0, $ret[1], $ret[2] );
Branch: 1 branch
BranchTrueFalseTotal
$ret[0] ? : 11 1 12
3886 12 return( $ok, $msg, $out );
3887 }
3888  
3889 # If callback returns a single value
3890 0 ▶ if( @ret == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@ret == 1) 0 0 0
3891 {
3892 0 my $v = $ret[0];
3893 # Reference => treat as decoded structure (success)
3894 0 ▶ return( 1, undef, $v ) if( ref( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 0 0
3895 # Defined scalar => truthiness decides; scalar can be treated as decoded bytes
3896 0 ▶ return( $v ? 1 : 0, undef, ( defined( $v ) ? $v : undef ) );
Branch: 2 branches
BranchTrueFalseTotal
$v ? : 0 0 0
defined $v ? : 0 0 0
3897 }
3898  
3899 # Empty list => treat as failure (safer default)
3900 0 return( 0, 'callback returned no value', undef );
3901 }
3902  
3903 # Strict base64: validates alphabet, padding, length, and round-trips
3904 sub _strict_base64_decode
3905 10 ▶{
Subroutine _strict_base64_decode: called CC=6 SCAR=18.2
3906 10 my( $s ) = @_;
3907 10 ▶ return unless( defined( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 10 10
3908  
3909 # strip ASCII whitespace per RFC 4648 §3.3 (tests commonly include raw)
3910 10 ( my $norm = "$s" ) =~ s/\s+//g;
3911  
3912 # valid alphabet + padding only
3913 10 ▶ return unless( $norm =~ /\A[A-Za-z0-9+\/]*={0,2}\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $norm =~ m[\A[A-Za-z0-9+/]*={0,2}\z]u 2 8 10
3914  
3915 # length must be a multiple of 4
3916 8 ▶ return unless( ( length( $norm ) % 4 ) == 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless length($norm) % 4 == 0 0 8 8
3917  
3918 8 local $@;
3919 8 ▶ return unless( eval{ require MIME::Base64; 1 } );
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require MIME::Base64; 1 } } 0 8 8
3920  
3921 8 my $out = MIME::Base64::decode_base64( $norm );
3922  
3923 # re-encode and compare to ensure no silent salvage
3924 8 my $re = MIME::Base64::encode_base64( $out, '' );
3925 # RFC allows omitting trailing '=' if not needed; normalize both
3926 8 $re =~ s/\s+//g;
3927 8 $norm =~ s/\s+//g;
3928 8 ▶ return unless( $re eq $norm );
Branch: 1 branch
BranchTrueFalseTotal
unless $re eq $norm 0 8 8
3929 8 return( $out );
3930 }
3931  
3932 sub _strlen
3933 57 ▶{
Subroutine _strlen: called CC=1 SCAR=0.0
3934 57 my( $s ) = @_;
3935 # The input is a decoded Perl string, not an encoded UTF-8 byte sequence.
3936 57 return( length( $s ) );
3937 }
3938  
3939 sub _t
3940 44 ▶{
Subroutine _t: called CC=7 SCAR=21.5
3941 44 my( $ctx, $schema_ptr, $keyword, $inst_path, $outcome, $note ) = @_;
3942 44 ▶ return unless( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->{"trace_on"} 0 44 44
3943  
3944 44 ▶ if( $ctx->{trace_limit} && @{$ctx->{trace}} >= $ctx->{trace_limit} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"trace_limit"} and @{$ctx->{"trace"};} >= $ctx->{"trace_limit"}) 0 44 44
Condition33%1/3
$ctx->{"trace_limit"} and @{$ctx->{"trace"};} >= $ctx->{"trace_limit"}
!ll&&!rl&&r
0440
MC/DC0%0/2
$ctx->{"trace_limit"} and @{$ctx->{"trace"};} >= $ctx->{"trace_limit"}
$ctx->{"trace_limit"} @{$ctx->{"trace"};} >= $ctx->{"trace_limit"}
Truth table33%1/3
A and B
A= $ctx->{"trace_limit"}B= @{$ctx->{"trace"};} >= $ctx->{"trace_limit"}
ABresult
0X0
100
111
3945 {
3946 0 return;
3947 }
3948 44 ▶ if( $ctx->{trace_sample} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"trace_sample"}) 8 36 44
3949 {
3950 8 ▶ return if( int( rand(100) ) >= $ctx->{trace_sample} );
Branch: 1 branch
BranchTrueFalseTotal
if int rand 100 >= $ctx->{"trace_sample"} 8 0 8
3951 }
3952  
3953 36 ▶ push( @{$ctx->{trace}},
Condition50%1/2
$ctx->{"ptr_stack"}[-1] // "#"
l!l
360
MC/DC0%0/1
$ctx->{"ptr_stack"}[-1] // "#"
$ctx->{"ptr_stack"}[-1]
Truth table50%1/2
A
A= $ctx->{"ptr_stack"}[-1]
Aresult
11
00
3954 {
3955 schema_ptr => $schema_ptr,
3956 keyword => $keyword,
3957 36 inst_path => ( $ctx->{ptr_stack}->[-1] // '#' ),
3958 outcome => $outcome, # 'pass' | 'fail'
3959 note => $note, # short string
3960 });
3961 }
3962  
3963 # Validation core with annotation + recursion
3964 # _v returns { ok => 0|1, props => {k=>1,...}, items => {i=>1,...} }
3965 sub _v
3966 1735 ▶{
Subroutine _v: called CC=6 SCAR=17.9
3967 1735 my( $ctx, $schema_ptr, $schema, $inst ) = @_;
3968  
3969 # The indexed resource, rather than the calling reference, determines the base URI.
3970 1735 ▶ my $meta = ref( $schema ) eq 'HASH' ? $ctx->{resource_index}->{nodes}->{ refaddr( $schema ) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' ? : 1639 96 1735
3971 1735 ▶ my $resource = $meta ? $meta->{base} : $ctx->{id_stack}->[-1];
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 1639 96 1735
3972 1735 ▶ local $ctx->{id_stack} = [ @{$ctx->{id_stack}}, $resource ] if( $resource ne $ctx->{id_stack}->[-1] );
Branch: 1 branch
BranchTrueFalseTotal
if $resource ne $ctx->{"id_stack"}[-1] 56 1679 1735
3973  
3974 # Guard active recursion by schema identity and instance identity, including scalar values.
3975 1735 my $inst_addr = _inst_addr( $inst, $ctx->{ptr_stack}->[-1] );
3976 1735 ▶ my $identity = ref( $schema ) eq 'HASH' ? refaddr( $schema ) : $schema_ptr;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' ? : 1639 96 1735
3977 1735 my $vkey = _canon( [$resource, $identity, $inst_addr] );
3978 1735 ▶ return( { ok => 1, props => {}, items => {} } ) if( $ctx->{visited}->{ $vkey } );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"visited"}{$vkey} 8 1727 1735
3979 1727 local $ctx->{visited}->{ $vkey } = 1;
3980 1727 return( _v_node( $ctx, $schema_ptr, $schema, $inst ) );
3981 }
3982  
3983 sub _v_node
3984 1727 ▶{
Subroutine _v_node: called CC=67 SCAR=42.3
3985 1727 my( $ctx, $schema_ptr, $schema, $inst ) = @_;
3986  
3987 # Boolean subschemas must be resolved before the compiled or interpreted path.
3988 1727 ▶ if( JSON::is_bool( $schema ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool($schema)) 96 1631 1727
3989 {
3990 96 ▶ return( $schema ? { ok => 1, props => {}, items => {} } :
Branch: 1 branch
BranchTrueFalseTotal
$schema ? : 40 56 96
3991 _err_res( $ctx, $schema_ptr, "instance does not satisfy false schema", 'false' ) );
3992 }
3993  
3994 # References are applicators. Evaluate each reference before its sibling keywords.
3995 1631 my @ref_results;
3996 1631 ▶ if( ref( $schema ) eq 'HASH' && exists( $schema->{'$ref'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and exists $schema->{"\$ref"}) 178 1453 1631
Condition66%2/3
ref $schema eq "HASH" and exists $schema->{"\$ref"}
!ll&&!rl&&r
01453178
MC/DC50%1/2
ref $schema eq "HASH" and exists $schema->{"\$ref"}
ref $schema eq "HASH" exists $schema->{"\$ref"}
Truth table66%2/3
A and B
A= ref $schema eq "HASH"B= exists $schema->{"\$ref"}
ABresult
0X0
100
111
3997 {
3998 178 my $r = _apply_ref( $ctx, $schema_ptr, $schema->{'$ref'}, $inst );
3999 178 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 79 99 178
4000 99 push( @ref_results, $r );
4001 }
4002 1552 ▶ if( ref( $schema ) eq 'HASH' && exists( $schema->{'$dynamicRef'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and exists $schema->{"\$dynamicRef"}) 29 1523 1552
Condition66%2/3
ref $schema eq "HASH" and exists $schema->{"\$dynamicRef"}
!ll&&!rl&&r
0152329
MC/DC50%1/2
ref $schema eq "HASH" and exists $schema->{"\$dynamicRef"}
ref $schema eq "HASH" exists $schema->{"\$dynamicRef"}
Truth table66%2/3
A and B
A= ref $schema eq "HASH"B= exists $schema->{"\$dynamicRef"}
ABresult
0X0
100
111
4003 {
4004 29 my $r = _apply_dynamic_ref( $ctx, $schema_ptr, $schema->{'$dynamicRef'}, $inst );
4005 29 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 10 19 29
4006 19 push( @ref_results, $r );
4007 }
4008 1542 ▶ if( ref( $schema ) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH" and exists $schema->{"\$comment"} and defined $schema->{"\$comment"}) 9 1533 1542
Condition66%4/6
ref $schema eq "HASH" and exists $schema->{"\$comment"} and defined $schema->{"\$comment"}
!ll&&!rl&&r
153309
ref $schema eq "HASH" and exists $schema->{"\$comment"}
!ll&&!rl&&r
015339
MC/DC33%1/3
ref $schema eq "HASH" and exists $schema->{"\$comment"} and defined $schema->{"\$comment"}
ref $schema eq "HASH" exists $schema->{"\$comment"} defined $schema->{"\$comment"}
Truth table50%2/4
A and B and C
A= ref $schema eq "HASH"B= exists $schema->{"\$comment"}C= defined $schema->{"\$comment"}
ABCresult
0XX0
10X0
1100
1111
4009 exists( $schema->{'$comment'} ) &&
4010 defined( $schema->{'$comment'} ) )
4011 {
4012 9 my $c = $schema->{'$comment'};
4013 9 ▶ if( my $cb = $ctx->{comment_handler} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $ctx->{"comment_handler"}) 4 5 9
4014 {
4015 4 local $@;
4016 4 eval{ $cb->( $schema_ptr, "$c" ) };
4017 # ignore callback errors to keep validation resilient
4018 }
4019 9 ▶ _t( $ctx, $schema_ptr, '$comment', undef, 'visit', "$c" ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 9 0 9
4020 }
4021  
4022 1542 ▶ _t( $ctx, $schema_ptr, 'node', undef, 'visit' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 12 1530 1542
4023  
4024 # Reference-bearing nodes use the interpreter to avoid re-entering the delegate closure.
4025 1542 ▶ if( $ctx->{compile_on} && !@ref_results )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"compile_on"} and not @ref_results) 409 1133 1542
Condition100%3/3
$ctx->{"compile_on"} and not @ref_results
!ll&&!rl&&r
108350409
MC/DC100%2/2
$ctx->{"compile_on"} and not @ref_results
$ctx->{"compile_on"} not @ref_results
Truth table100%3/3
A and not(B)
A= $ctx->{"compile_on"}B= not @ref_results
ABresult
0X0
100
111
4026 {
4027 409 my $resource = $ctx->{id_stack}->[-1];
4028 409 ▶ my $identity = ref( $schema ) eq 'HASH' ? refaddr( $schema ) : $schema_ptr;
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' ? : 409 0 409
4029 409 my $key = _canon( [$resource, $identity, $schema_ptr] );
4030 409 ▶ my $cache = ref( $schema ) eq 'HASH' && exists( $ctx->{root}->{resource_index}->{nodes}->{ $identity } ) ? $ctx->{root}->{fn_index} : $ctx->{external_fn_index};
Branch: 1 branch
BranchTrueFalseTotal
ref $schema eq 'HASH' && exists $ctx->{'root'}{'resource_index'}{'nodes'}{$identity} ? : 394 15 409
Condition66%2/3
ref $schema eq "HASH" and exists $ctx->{"root"}{"resource_index"}{"nodes"}{$identity}
!ll&&!rl&&r
015394
MC/DC50%1/2
ref $schema eq "HASH" and exists $ctx->{"root"}{"resource_index"}{"nodes"}{$identity}
ref $schema eq "HASH" exists $ctx->{"root"}{"resource_index"}{"nodes"}{$identity}
Truth table66%2/3
A and B
A= ref $schema eq "HASH"B= exists $ctx->{"root"}{"resource_index"}{"nodes"}{$identity}
ABresult
0X0
100
111
4031 409 my $fn = $cache->{ $key };
4032 409 ▶ unless( $fn )
Branch: 1 branch
BranchTrueFalseTotal
unless ($fn) 340 69 409
4033 {
4034 340 $fn = _compile_node( $ctx->{root}, $schema_ptr, $schema );
4035 340 $cache->{ $key } = $fn;
4036 }
4037 409 return( $fn->( $ctx, $inst ) );
4038 }
4039  
4040 1133 ▶ return( { ok => 1, props => {}, items => {} } ) unless( ref( $schema ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 1133 1133
4041  
4042 1133 my $ptr = $schema_ptr;
4043  
4044 # Types / const / enum
4045 1133 ▶ if( exists( $schema->{type} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"type"}) 676 457 1133
4046 {
4047 676 ▶ _k_type( $ctx, $inst, $schema->{type}, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_type($ctx, $inst, $schema->{"type"}, $ptr) or return _fail() 602 74 676
4048 }
4049 1059 ▶ if( exists( $schema->{const} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"const"}) 28 1031 1059
4050 {
4051 28 ▶ _k_const( $ctx, $inst, $schema->{const}, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_const($ctx, $inst, $schema->{"const"}, $ptr) or return _fail() 15 13 28
4052 }
4053 1046 ▶ if( exists( $schema->{enum} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"enum"}) 29 1017 1046
4054 {
4055 29 ▶ _k_enum( $ctx, $inst, $schema->{enum}, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_enum($ctx, $inst, $schema->{"enum"}, $ptr) or return _fail() 12 17 29
4056 }
4057 1029 ▶ _t( $ctx, $schema_ptr, 'type/const/enum', undef, 'pass', '' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 1029 1029
4058  
4059 1029 ▶ if( $ctx->{unique_keys} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"unique_keys"} and exists $schema->{"uniqueKeys"} and ref $schema->{"uniqueKeys"} eq "ARRAY" and ref $inst eq "ARRAY") 17 1012 1029
Condition77%7/9
$ctx->{"unique_keys"} and exists $schema->{"uniqueKeys"} and ref $schema->{"uniqueKeys"} eq "ARRAY" and ref $inst eq "ARRAY"
!ll&&!rl&&r
1012017
$ctx->{"unique_keys"} and exists $schema->{"uniqueKeys"} and ref $schema->{"uniqueKeys"} eq "ARRAY"
!ll&&!rl&&r
1012017
$ctx->{"unique_keys"} and exists $schema->{"uniqueKeys"}
!ll&&!rl&&r
9535917
MC/DC50%2/4
$ctx->{"unique_keys"} and exists $schema->{"uniqueKeys"} and ref $schema->{"uniqueKeys"} eq "ARRAY" and ref $inst eq "ARRAY"
$ctx->{"unique_keys"} exists $schema->{"uniqueKeys"} ref $schema->{"uniqueKeys"} eq "ARRAY" ref $inst eq "ARRAY"
Truth table60%3/5
A and B and C and D
A= $ctx->{"unique_keys"}B= exists $schema->{"uniqueKeys"}C= ref $schema->{"uniqueKeys"} eq "ARRAY"D= ref $inst eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
4060 exists( $schema->{uniqueKeys} ) &&
4061 ref( $schema->{uniqueKeys} ) eq 'ARRAY' &&
4062 ref( $inst ) eq 'ARRAY' )
4063 {
4064 17 my $r = _k_unique_keys( $ctx, $ptr, $schema->{uniqueKeys}, $inst );
4065 17 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 8 9 17
4066 }
4067  
4068 # Numbers
4069 1021 ▶ if( _is_number( $inst ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_number($inst)) 347 674 1021
4070 {
4071 347 for my $k ( qw( multipleOf minimum maximum exclusiveMinimum exclusiveMaximum ) )
4072 {
4073 1533 ▶ next unless( exists( $schema->{ $k } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{$k} 1375 158 1533
4074 158 ▶ _k_number( $ctx, $inst, $k, $schema->{ $k }, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_number($ctx, $inst, $k, $schema->{$k}, $ptr) or return _fail() 100 58 158
4075 }
4076 }
4077  
4078 # Strings
4079 963 ▶ if( _match_type( $inst, 'string' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_match_type($inst, "string")) 190 773 963
4080 {
4081 190 ▶ if( exists( $schema->{minLength} ) || exists( $schema->{maxLength} ) || exists( $schema->{pattern} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"minLength"} or exists $schema->{"maxLength"} or exists $schema->{"pattern"}) 51 139 190
Condition100%6/6
exists $schema->{"minLength"} or exists $schema->{"maxLength"} or exists $schema->{"pattern"}
l!l&&r!l&&!r
3318139
exists $schema->{"minLength"} or exists $schema->{"maxLength"}
l!l&&r!l&&!r
303157
MC/DC100%3/3
exists $schema->{"minLength"} or exists $schema->{"maxLength"} or exists $schema->{"pattern"}
exists $schema->{"minLength"} exists $schema->{"maxLength"} exists $schema->{"pattern"}
Truth table100%4/4
A or B or C
A= exists $schema->{"minLength"}B= exists $schema->{"maxLength"}C= exists $schema->{"pattern"}
ABCresult
1XX1
01X1
0011
0000
4082 {
4083 51 ▶ _k_string( $ctx, $inst, $schema, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_string($ctx, $inst, $schema, $ptr) or return _fail() 41 10 51
4084 }
4085 180 ▶ if( exists( $schema->{format} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"format"}) 53 127 180
4086 {
4087 53 ▶ _k_format( $ctx, $inst, $schema->{format}, $ptr ) or return( _fail() );
Branch: 1 branch
BranchTrueFalseTotal
_k_format($ctx, $inst, $schema->{"format"}, $ptr) or return _fail() 36 17 53
4088 }
4089  
4090 # contentEncoding / contentMediaType / contentSchema
4091 163 ▶ if( $ctx->{content_assert} && ( exists( $schema->{contentEncoding} ) || exists( $schema->{contentMediaType} ) || exists( $schema->{contentSchema} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->{"content_assert"} and exists $schema->{"contentEncoding"} || exists $schema->{"contentMediaType"} || exists $schema->{"contentSchema"}) 20 143 163
Condition88%8/9
$ctx->{"content_assert"} and exists $schema->{"contentEncoding"} || exists $schema->{"contentMediaType"} || exists $schema->{"contentSchema"}
!ll&&!rl&&r
134920
exists $schema->{"contentEncoding"} or exists $schema->{"contentMediaType"} or exists $schema->{"contentSchema"}
l!l&&r!l&&!r
9119
exists $schema->{"contentEncoding"} or exists $schema->{"contentMediaType"}
l!l&&r!l&&!r
9020
MC/DC75%3/4
$ctx->{"content_assert"} and exists $schema->{"contentEncoding"} || exists $schema->{"contentMediaType"} || exists $schema->{"contentSchema"}
$ctx->{"content_assert"} exists $schema->{"contentEncoding"} exists $schema->{"contentMediaType"} exists $schema->{"contentSchema"}
Truth table80%4/5
A and B or C or D
A= $ctx->{"content_assert"}B= exists $schema->{"contentEncoding"}C= exists $schema->{"contentMediaType"}D= exists $schema->{"contentSchema"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4092 {
4093 20 my $r = _validate_content( $ctx, $ptr, $schema, $inst );
4094 20 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 13 7 20
4095 }
4096 }
4097  
4098 923 my %ann_props;
4099 my %ann_items;
4100  
4101 # Arrays
4102 923 ▶ if( ref( $inst ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inst eq "ARRAY") 56 867 923
4103 {
4104 56 my $r = _k_array_all( $ctx, $schema_ptr, $schema, $inst );
4105 56 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 14 42 56
4106 42 %ann_items = ( %ann_items, %{ $r->{items} } );
4107 }
4108 909 ▶ _t( $ctx, $schema_ptr, 'array', undef, 'pass', '' ) if( ref( $inst ) eq 'ARRAY' && $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $inst eq "ARRAY" and $ctx->{"trace_on"} 0 909 909
Condition66%2/3
ref $inst eq "ARRAY" and $ctx->{"trace_on"}
!ll&&!rl&&r
867420
MC/DC0%0/2
ref $inst eq "ARRAY" and $ctx->{"trace_on"}
ref $inst eq "ARRAY" $ctx->{"trace_on"}
Truth table66%2/3
A and B
A= ref $inst eq "ARRAY"B= $ctx->{"trace_on"}
ABresult
0X0
100
111
4109  
4110 # Objects
4111 909 ▶ if( ref( $inst ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inst eq "HASH") 410 499 909
4112 {
4113 410 my $r = _k_object_all( $ctx, $schema_ptr, $schema, $inst );
4114 410 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 146 264 410
4115 264 %ann_props = ( %ann_props, %{ $r->{props} } );
4116 }
4117 763 ▶ _t( $ctx, $schema_ptr, 'object', undef, 'pass', '' ) if( ref( $inst ) eq 'HASH' && $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $inst eq "HASH" and $ctx->{"trace_on"} 0 763 763
Condition66%2/3
ref $inst eq "HASH" and $ctx->{"trace_on"}
!ll&&!rl&&r
4992640
MC/DC0%0/2
ref $inst eq "HASH" and $ctx->{"trace_on"}
ref $inst eq "HASH" $ctx->{"trace_on"}
Truth table66%2/3
A and B
A= ref $inst eq "HASH"B= $ctx->{"trace_on"}
ABresult
0X0
100
111
4118  
4119 # Combinators
4120 763 for my $comb ( qw( allOf anyOf oneOf not ) )
4121 {
4122 2987 ▶ next unless( exists( $schema->{ $comb } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $schema->{$comb} 2920 67 2987
4123 67 my $r = _k_combinator( $ctx, $schema_ptr, $schema, $inst, $comb );
4124 67 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 32 35 67
4125 35 %ann_props = ( %ann_props, %{ $r->{props} } );
4126 35 %ann_items = ( %ann_items, %{ $r->{items} } );
4127 35 ▶ _t( $ctx, $schema_ptr, $comb, undef, 'pass', '' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 35 35
4128 }
4129  
4130 # Conditionals
4131 731 ▶ if( exists( $schema->{if} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"if"}) 21 710 731
4132 {
4133 21 my $r = _k_if_then_else( $ctx, $schema_ptr, $schema, $inst );
4134 21 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 6 15 21
4135 15 %ann_props = ( %ann_props, %{ $r->{props} } );
4136 15 %ann_items = ( %ann_items, %{ $r->{items} } );
4137 }
4138  
4139 # Include annotations from every successful reference before evaluating unevaluated keywords.
4140 725 for my $r ( @ref_results )
4141 {
4142 104 %ann_props = ( %ann_props, %{$r->{props}} );
4143 104 %ann_items = ( %ann_items, %{$r->{items}} );
4144 }
4145  
4146 725 my $r = _k_unevaluated( $ctx, $schema_ptr, $schema, $inst, \%ann_props, \%ann_items );
4147 725 ▶ return( $r ) unless( $r->{ok} );
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 14 711 725
4148  
4149 711 ▶ _t( $ctx, $schema_ptr, 'node', undef, 'pass' ) if( $ctx->{trace_on} );
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"trace_on"} 0 711 711
4150 711 return( { ok => 1, props => \%ann_props, items => \%ann_items } );
4151 }
4152  
4153 sub _validate_content
4154 34 ▶{
Subroutine _validate_content: called CC=25 SCAR=34.1
4155 34 my( $ctx, $ptr, $schema, $inst ) = @_;
4156 34 my $text = "$inst";
4157 34 my $octets = 0;
4158 34 ▶ if( exists( $schema->{contentEncoding} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"contentEncoding"}) 12 22 34
4159 {
4160 12 $text = _content_decode( $ctx, $schema->{contentEncoding}, $text );
4161 12 ▶ return( _err_res( $ctx, $ptr, "contentEncoding '$schema->{contentEncoding}' decode failed", 'contentEncoding' ) ) unless( defined( $text ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 2 10 12
4162 10 $octets = !utf8::is_utf8( $text );
4163 }
4164 32 my $decoded;
4165 32 ▶ if( exists( $schema->{contentMediaType} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"contentMediaType"}) 10 22 32
4166 {
4167 10 my( $mt, $params ) = _parse_media_type( $schema->{contentMediaType} );
4168 10 my $cb = $ctx->{media_validators}->{ $mt };
4169 10 ▶ $cb ||= \&_builtin_json_media if( $mt eq 'application/json' );
Branch: 1 branch
BranchTrueFalseTotal
if $mt eq "application/json" 8 2 10
Condition50%1/2
$cb ||= \&JSON::Schema::Validate::_builtin_json_media
l!l
80
MC/DC0%0/1
$cb ||= \&JSON::Schema::Validate::_builtin_json_media
$cb
Truth table50%1/2
A
A= $cb
Aresult
11
00
4170 # Only the built-in JSON validator needs character text. Custom callbacks retain their original input contract.
4171 10 ▶ my $builtin_json = $cb && refaddr( $cb ) == refaddr( \&_builtin_json_media );
Condition66%2/3
$cb && builtin::refaddr($cb) == builtin::refaddr(\&JSON::Schema::Validate::_builtin_json_media)
!ll&&!rl&&r
046
MC/DC50%1/2
$cb && builtin::refaddr($cb) == builtin::refaddr(\&JSON::Schema::Validate::_builtin_json_media)
$cb builtin::refaddr($cb) == builtin::refaddr(\&JSON::Schema::Validate::_builtin_json_media)
Truth table66%2/3
A && B
A= $cbB= builtin::refaddr($cb) == builtin::refaddr(\&JSON::Schema::Validate::_builtin_json_media)
ABresult
0X0
100
111
4172 10 ▶ if( $octets && ( $builtin_json || ( !$cb && $mt =~ m{^text/} && ( $params->{charset} // '' ) =~ /\Autf-?8\z/i ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($octets and $builtin_json || !$cb && $mt =~ m[^text/]u && ($params->{"charset"} // "") =~ /\Autf-?8\z/iu) 6 4 10
Condition42%6/14
$octets and $builtin_json || !$cb && $mt =~ m[^text/]u && ($params->{"charset"} // "") =~ /\Autf-?8\z/iu
!ll&&!rl&&r
046
$builtin_json or not $cb and $mt =~ m[^text/]u and ($params->{"charset"} // "") =~ /\Autf-?8\z/iu
l!l&&r!l&&!r
604
not $cb and $mt =~ m[^text/]u and ($params->{"charset"} // "") =~ /\Autf-?8\z/iu
!ll&&!rl&&r
400
not $cb and $mt =~ m[^text/]u
!ll&&!rl&&r
400
$params->{"charset"} // ""
l!l
00
MC/DC20%1/5
$octets and $builtin_json || !$cb && $mt =~ m[^text/]u && ($params->{"charset"} // "") =~ /\Autf-?8\z/iu
$octets $builtin_json not $cb $mt =~ m[^text/]u $params->{"charset"}
Truth table33%2/6
A and B or not(C) and D and E
A= $octetsB= $builtin_jsonC= not $cbD= $mt =~ m[^text/]uE= $params->{"charset"}
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
4173 {
4174 6 local $@;
4175 6 my $chars = eval{ Encode::decode( 'UTF-8', $text, Encode::FB_CROAK | Encode::LEAVE_SRC ) };
4176 6 ▶ return( _err_res( $ctx, $ptr, "contentMediaType '$mt' invalid UTF-8", 'contentMediaType' ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
4177 6 $text = $chars;
4178 6 $octets = 0;
4179 }
4180 10 ▶ if( $cb )
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 10 0 10
4181 {
4182 10 my( $ok, $msg, $out ) = _safe_invoke( $cb, $text, $params );
4183 10 ▶ return( _err_res( $ctx, $ptr, ( $msg || "contentMediaType '$mt' validation failed" ), 'contentMediaType' ) ) unless( $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 9 10
Condition50%1/2
$msg || "contentMediaType '${mt}' validation failed"
l!l
10
MC/DC0%0/1
$msg || "contentMediaType '${mt}' validation failed"
$msg
Truth table50%1/2
A
A= $msg
Aresult
11
00
4184 9 ▶ $decoded = $out if( ref( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $out 0 9 9
4185 9 ▶ $text = $out if( defined( $out ) && !ref( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and not ref $out 1 8 9
Condition66%2/3
defined $out and not ref $out
!ll&&!rl&&r
801
MC/DC50%1/2
defined $out and not ref $out
defined $out not ref $out
Truth table66%2/3
A and not(B)
A= defined $outB= not ref $out
ABresult
0X0
100
111
4186 9 ▶ $octets = 0 if( utf8::is_utf8( $text ) );
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($text) 6 3 9
4187 }
4188 }
4189 31 ▶ if( exists( $schema->{contentSchema} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"contentSchema"}) 28 3 31
4190 {
4191 28 my $val = $decoded;
4192 28 ▶ unless( ref( $decoded ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $decoded) 28 0 28
4193 {
4194 28 local $@;
4195 28 ▶ $val = eval{ $octets ? JSON->new->allow_nonref(1)->utf8(1)->decode( $text ) : _decode_json_content( $text ) };
Branch: 1 branch
BranchTrueFalseTotal
$octets ? : 0 28 28
4196 28 ▶ return( _err_res( $ctx, $ptr, 'contentSchema present but payload not JSON-decodable', 'contentSchema' ) ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 28 28
4197 }
4198 28 return( _v( $ctx, _join_ptr( $ptr, 'contentSchema' ), $schema->{contentSchema}, $val ) );
4199 }
4200 3 return( { ok => 1, props => {}, items => {} } );
4201 }
4202  
4203 # NOTE: JSON::Schema::Validate::Error
4204 package JSON::Schema::Validate::Error;
4205 BEGIN
4206 52 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
4207 52 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4208 52 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4209 52 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
4210 52 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
4211 '""' => 'as_string',
4212 2 ▶ 'eq' => sub{ _obj_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
4213 3 ▶ 'ne' => sub{ !_obj_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
4214 1 ▶ '==' => sub{ _obj_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
4215 0 ▶ '!=' => sub{ !_obj_eq(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
4216 5 ▶ bool => sub{1},
Subroutine __ANON__: called CC=1 SCAR=0.0
4217 52 fallback => 1,
4218 52 );
4219 52 our $VERSION = 'v0.1.0';
4220 };
4221  
4222 52 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4223 52 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4224 52 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4225  
4226 sub new
4227 510 ▶{
Subroutine new: called CC=6 SCAR=18.6
4228 510 my $that = shift( @_ );
4229 510 my $ref = {};
4230 # Legacy instantiation
4231 # We make sure this is not one single option that was provided to us.
4232 510 ▶ if( @_ == 2 && $_[0] !~ /^(?:path|message|keyword|schema_pointer)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and not $_[0] =~ /^(?:path|message|keyword|schema_pointer)$/u) { } 4 506 510
Condition66%2/3
@_ == 2 and not $_[0] =~ /^(?:path|message|keyword|schema_pointer)$/u
!ll&&!rl&&r
50604
MC/DC50%1/2
@_ == 2 and not $_[0] =~ /^(?:path|message|keyword|schema_pointer)$/u
@_ == 2 not $_[0] =~ /^(?:path|message|keyword|schema_pointer)$/u
Truth table66%2/3
A and not(B)
A= @_ == 2B= not $_[0] =~ /^(?:path|message|keyword|schema_pointer)$/u
ABresult
0X0
100
111
4233 {
4234 4 @$ref{qw( path message )} = @_;
4235 }
4236 else
4237 {
4238 506 my $args = { @_ };
4239 506 for( qw( path message keyword schema_pointer ) )
4240 {
4241 2024 ▶ $ref->{ $_ } = $args->{ $_ } if( exists( $args->{ $_ } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$_} 2024 0 2024
4242 }
4243 }
4244 510 ▶ return( bless( $ref => ( ref( $that ) || $that ) ) );
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
05100
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
4245 }
4246  
4247 sub as_hash
4248 0 ▶{
Subroutine as_hash: not called CC=1 SCAR=6.9
4249 0 my $self = shift( @_ );
4250 0 my $ref = {};
4251 0 my @keys = qw( keyword message path schema_pointer );
4252 0 @$ref{ @keys } = @$self{ @keys };
4253 0 return( $ref );
4254 }
4255  
4256 sub as_string
4257 11 ▶{
Subroutine as_string: called CC=5 SCAR=16.3
4258 11 my $self = shift( @_ );
4259 11 ▶ my $sp = $self->schema_pointer // '';
Condition100%2/2
$self->schema_pointer // ""
l!l
101
MC/DC100%1/1
$self->schema_pointer // ""
$self->schema_pointer
Truth table100%2/2
A
A= $self->schema_pointer
Aresult
11
00
4260 11 ▶ my $path = $self->path // '';
Condition50%1/2
$self->path // ""
l!l
110
MC/DC0%0/1
$self->path // ""
$self->path
Truth table50%1/2
A
A= $self->path
Aresult
11
00
4261 11 ▶ my $msg = $self->message // '';
Condition50%1/2
$self->message // ""
l!l
110
MC/DC0%0/1
$self->message // ""
$self->message
Truth table50%1/2
A
A= $self->message
Aresult
11
00
4262 # Dual-path if avail
4263 11 ▶ return( $sp ? "${sp} → ${path}: ${msg}" : "${path}: ${msg}" );
Branch: 1 branch
BranchTrueFalseTotal
$sp ? : 10 1 11
4264 }
4265  
4266 sub keyword
4267 20 ▶{
Subroutine keyword: called CC=2 SCAR=7.1
4268 20 my $self = shift( @_ );
4269 20 ▶ $self->{keyword} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 20 20
4270 20 return( $self->{keyword} );
4271 }
4272  
4273 sub message
4274 31 ▶{
Subroutine message: called CC=2 SCAR=7.1
4275 31 my $self = shift( @_ );
4276 31 ▶ $self->{message} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 31 31
4277 31 return( $self->{message} );
4278 }
4279  
4280 sub path
4281 29 ▶{
Subroutine path: called CC=2 SCAR=7.1
4282 29 my $self = shift( @_ );
4283 29 ▶ $self->{path} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 29 29
4284 29 return( $self->{path} );
4285 }
4286  
4287 sub schema_pointer
4288 12 ▶{
Subroutine schema_pointer: called CC=2 SCAR=7.1
4289 12 my $self = shift( @_ );
4290 12 ▶ $self->{schema_pointer} = shift( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12 12
4291 12 return( $self->{schema_pointer} );
4292 }
4293  
4294 sub _obj_eq
4295 6 ▶{
Subroutine _obj_eq: called CC=12 SCAR=26.3
4296 52 ▶ no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
4297 6 my $self = shift( @_ );
4298 6 my $other = shift( @_ );
4299 6 my $me;
4300 6 ▶ if( defined( $other ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (defined $other and builtin::blessed($other) and $other->isa('JSON::Schema::Validate::Error')) { } 4 2 6
elsif (not ref $other) { } 2 0 2
Condition66%4/6
defined $other and builtin::blessed($other) and $other->isa("JSON::Schema::Validate::Error")
!ll&&!rl&&r
204
defined $other and builtin::blessed($other)
!ll&&!rl&&r
024
MC/DC33%1/3
defined $other and builtin::blessed($other) and $other->isa("JSON::Schema::Validate::Error")
defined $other builtin::blessed($other) $other->isa("JSON::Schema::Validate::Error")
Truth table50%2/4
A and B and C
A= defined $otherB= builtin::blessed($other)C= $other->isa("JSON::Schema::Validate::Error")
ABCresult
0XX0
10X0
1100
1111
4301 Scalar::Util::blessed( $other ) &&
4302 $other->isa( 'JSON::Schema::Validate::Error' ) )
4303 {
4304 4 ▶ if( ( $self->message // '' ) eq ( $other->message // '' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (($self->message // '') eq ($other->message // '') and ($self->path // '') eq ($other->path // '')) { } 2 2 4
Condition63%7/11
($self->message // "") eq ($other->message // "") and ($self->path // "") eq ($other->path // "")
!ll&&!rl&&r
112
$self->message // ""
l!l
40
$other->message // ""
l!l
40
$self->path // ""
l!l
30
$other->path // ""
l!l
30
MC/DC0%0/2
($self->message // "") eq ($other->message // "") and ($self->path // "") eq ($other->path // "")
$self->message $self->path
MC/DC0%0/1
$other->message // ""
$other->message
MC/DC0%0/1
$other->path // ""
$other->path
Truth table33%1/3
A and B
A= $self->messageB= $self->path
ABresult
0X0
100
111
Truth table50%1/2
A
A= $other->message
Aresult
11
00
Truth table50%1/2
A
A= $other->path
Aresult
11
00
4305 ( $self->path // '' ) eq ( $other->path // '' ) )
4306 {
4307 2 return(1);
4308 }
4309 else
4310 {
4311 2 return(0);
4312 }
4313 }
4314 # Compare error message
4315 elsif( !ref( $other ) )
4316 {
4317 2 ▶ my $me = $self->message // '';
Condition50%1/2
$self->message // ""
l!l
20
MC/DC0%0/1
$self->message // ""
$self->message
Truth table50%1/2
A
A= $self->message
Aresult
11
00
4318 2 return( $me eq $other );
4319 }
4320 # Otherwise some reference data to which we cannot compare
4321 0 return(0) ;
4322 }
4323  
4324 52 1;
4325 # NOTE: POD
4326 __END__
↑ Summary