line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
36
|
|
|
36
|
|
269937
|
use strict; |
|
36
|
|
|
|
|
118
|
|
|
36
|
|
|
|
|
1269
|
|
2
|
36
|
|
|
36
|
|
239
|
use warnings; |
|
36
|
|
|
|
|
110
|
|
|
36
|
|
|
|
|
1940
|
|
3
|
|
|
|
|
|
|
package JSON::Schema::Modern::Utilities; |
4
|
|
|
|
|
|
|
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
5
|
|
|
|
|
|
|
# ABSTRACT: Internal utilities for JSON::Schema::Modern |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.570'; |
8
|
|
|
|
|
|
|
|
9
|
36
|
|
|
36
|
|
844
|
use 5.020; |
|
36
|
|
|
|
|
161
|
|
10
|
36
|
|
|
36
|
|
301
|
use strictures 2; |
|
36
|
|
|
|
|
325
|
|
|
36
|
|
|
|
|
1527
|
|
11
|
36
|
|
|
36
|
|
7270
|
use stable 0.031 'postderef'; |
|
36
|
|
|
|
|
651
|
|
|
36
|
|
|
|
|
288
|
|
12
|
36
|
|
|
36
|
|
5803
|
use experimental 'signatures'; |
|
36
|
|
|
|
|
119
|
|
|
36
|
|
|
|
|
209
|
|
13
|
36
|
|
|
36
|
|
3347
|
use if "$]" >= 5.022, experimental => 're_strict'; |
|
36
|
|
|
|
|
139
|
|
|
36
|
|
|
|
|
449
|
|
14
|
36
|
|
|
36
|
|
3655
|
no if "$]" >= 5.031009, feature => 'indirect'; |
|
36
|
|
|
|
|
181
|
|
|
36
|
|
|
|
|
440
|
|
15
|
36
|
|
|
36
|
|
1980
|
no if "$]" >= 5.033001, feature => 'multidimensional'; |
|
36
|
|
|
|
|
133
|
|
|
36
|
|
|
|
|
323
|
|
16
|
36
|
|
|
36
|
|
1992
|
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
|
36
|
|
|
|
|
128
|
|
|
36
|
|
|
|
|
263
|
|
17
|
36
|
|
|
36
|
|
1504
|
use B; |
|
36
|
|
|
|
|
161
|
|
|
36
|
|
|
|
|
2170
|
|
18
|
36
|
|
|
36
|
|
390
|
use Carp 'croak'; |
|
36
|
|
|
|
|
124
|
|
|
36
|
|
|
|
|
2577
|
|
19
|
36
|
|
|
36
|
|
1345
|
use JSON::MaybeXS 1.004004 'is_bool'; |
|
36
|
|
|
|
|
17111
|
|
|
36
|
|
|
|
|
2995
|
|
20
|
36
|
|
|
36
|
|
1360
|
use Ref::Util 0.100 qw(is_ref is_plain_arrayref is_plain_hashref); |
|
36
|
|
|
|
|
4020
|
|
|
36
|
|
|
|
|
2431
|
|
21
|
36
|
|
|
36
|
|
359
|
use Scalar::Util 'blessed'; |
|
36
|
|
|
|
|
129
|
|
|
36
|
|
|
|
|
2046
|
|
22
|
36
|
|
|
36
|
|
1745
|
use Storable 'dclone'; |
|
36
|
|
|
|
|
6989
|
|
|
36
|
|
|
|
|
2183
|
|
23
|
36
|
|
|
36
|
|
1278
|
use Feature::Compat::Try; |
|
36
|
|
|
|
|
769
|
|
|
36
|
|
|
|
|
560
|
|
24
|
36
|
|
|
36
|
|
11850
|
use JSON::Schema::Modern::Error; |
|
36
|
|
|
|
|
134
|
|
|
36
|
|
|
|
|
1405
|
|
25
|
36
|
|
|
36
|
|
1325
|
use JSON::Schema::Modern::Annotation; |
|
36
|
|
|
|
|
88
|
|
|
36
|
|
|
|
|
1195
|
|
26
|
36
|
|
|
36
|
|
264
|
use namespace::clean; |
|
36
|
|
|
|
|
156
|
|
|
36
|
|
|
|
|
491
|
|
27
|
|
|
|
|
|
|
|
28
|
36
|
|
|
36
|
|
12010
|
use Exporter 'import'; |
|
36
|
|
|
|
|
96
|
|
|
36
|
|
|
|
|
3026
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
31
|
|
|
|
|
|
|
is_type |
32
|
|
|
|
|
|
|
get_type |
33
|
|
|
|
|
|
|
is_equal |
34
|
|
|
|
|
|
|
is_elements_unique |
35
|
|
|
|
|
|
|
jsonp |
36
|
|
|
|
|
|
|
unjsonp |
37
|
|
|
|
|
|
|
local_annotations |
38
|
|
|
|
|
|
|
canonical_uri |
39
|
|
|
|
|
|
|
E |
40
|
|
|
|
|
|
|
A |
41
|
|
|
|
|
|
|
abort |
42
|
|
|
|
|
|
|
assert_keyword_exists |
43
|
|
|
|
|
|
|
assert_keyword_type |
44
|
|
|
|
|
|
|
assert_pattern |
45
|
|
|
|
|
|
|
assert_uri_reference |
46
|
|
|
|
|
|
|
assert_uri |
47
|
|
|
|
|
|
|
annotate_self |
48
|
|
|
|
|
|
|
sprintf_num |
49
|
|
|
|
|
|
|
true |
50
|
|
|
|
|
|
|
false |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
36
|
|
|
36
|
|
299
|
use JSON::PP (); |
|
36
|
|
|
|
|
113
|
|
|
36
|
|
|
|
|
2040
|
|
54
|
36
|
|
|
36
|
|
299
|
use constant { true => JSON::PP::true, false => JSON::PP::false }; |
|
36
|
|
|
|
|
125
|
|
|
36
|
|
|
|
|
1510
|
|
55
|
|
|
|
|
|
|
|
56
|
71555
|
|
|
71555
|
0
|
479709
|
sub is_type ($type, $value) { |
|
71555
|
|
|
|
|
105488
|
|
|
71555
|
|
|
|
|
104773
|
|
|
71555
|
|
|
|
|
100649
|
|
57
|
71555
|
100
|
|
|
|
145280
|
if ($type eq 'null') { |
58
|
71
|
|
|
|
|
334
|
return !(defined $value); |
59
|
|
|
|
|
|
|
} |
60
|
71484
|
100
|
|
|
|
133769
|
if ($type eq 'boolean') { |
61
|
8127
|
|
|
|
|
24973
|
return is_bool($value); |
62
|
|
|
|
|
|
|
} |
63
|
63357
|
100
|
|
|
|
123141
|
if ($type eq 'object') { |
64
|
17947
|
|
|
|
|
83420
|
return is_plain_hashref($value); |
65
|
|
|
|
|
|
|
} |
66
|
45410
|
100
|
|
|
|
87846
|
if ($type eq 'array') { |
67
|
11066
|
|
|
|
|
50861
|
return is_plain_arrayref($value); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
34344
|
100
|
100
|
|
|
101816
|
if ($type eq 'string' or $type eq 'number' or $type eq 'integer') { |
|
|
|
100
|
|
|
|
|
71
|
34334
|
100
|
|
|
|
68459
|
return 0 if not defined $value; |
72
|
34320
|
|
|
|
|
153156
|
my $flags = B::svref_2object(\$value)->FLAGS; |
73
|
|
|
|
|
|
|
|
74
|
34320
|
100
|
|
|
|
90843
|
if ($type eq 'string') { |
75
|
23940
|
|
66
|
|
|
211074
|
return !is_ref($value) && $flags & B::SVf_POK && !($flags & (B::SVf_IOK | B::SVf_NOK)); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
10380
|
100
|
|
|
|
22935
|
if ($type eq 'number') { |
79
|
6892
|
|
100
|
|
|
55888
|
return ref($value) =~ /^Math::Big(?:Int|Float)$/ |
80
|
|
|
|
|
|
|
|| !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK)); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
3488
|
50
|
|
|
|
7700
|
if ($type eq 'integer') { |
84
|
3488
|
|
100
|
|
|
34559
|
return ref($value) =~ /^Math::Big(?:Int|Float)$/ && $value->is_int |
85
|
|
|
|
|
|
|
|| !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK)) && int($value) == $value; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
10
|
100
|
|
|
|
54
|
if ($type =~ /^reference to (.+)$/) { |
90
|
6
|
|
33
|
|
|
63
|
return !blessed($value) && ref($value) eq $1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
4
|
|
|
|
|
25
|
return ref($value) eq $type; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
76862
|
|
|
76862
|
0
|
176799
|
sub get_type ($value) { |
|
76862
|
|
|
|
|
122744
|
|
|
76862
|
|
|
|
|
105473
|
|
97
|
76862
|
100
|
|
|
|
237708
|
return 'object' if is_plain_hashref($value); |
98
|
21499
|
100
|
|
|
|
58793
|
return 'boolean' if is_bool($value); |
99
|
12736
|
100
|
|
|
|
90081
|
return 'null' if not defined $value; |
100
|
12423
|
100
|
|
|
|
26760
|
return 'array' if is_plain_arrayref($value); |
101
|
|
|
|
|
|
|
|
102
|
11183
|
100
|
|
|
|
25224
|
return ref($value) =~ /^Math::Big(?:Int|Float)$/ ? ($value->is_int ? 'integer' : 'number') |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
103
|
|
|
|
|
|
|
: (blessed($value) ? '' : 'reference to ').ref($value) |
104
|
|
|
|
|
|
|
if is_ref($value); |
105
|
|
|
|
|
|
|
|
106
|
10732
|
|
|
|
|
40467
|
my $flags = B::svref_2object(\$value)->FLAGS; |
107
|
10732
|
100
|
100
|
|
|
46161
|
return 'string' if $flags & B::SVf_POK && !($flags & (B::SVf_IOK | B::SVf_NOK)); |
108
|
4720
|
100
|
66
|
|
|
28941
|
return int($value) == $value ? 'integer' : 'number' |
|
|
100
|
|
|
|
|
|
109
|
|
|
|
|
|
|
if !($flags & B::SVf_POK) && ($flags & (B::SVf_IOK | B::SVf_NOK)); |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
11
|
return 'ambiguous type'; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# compares two arbitrary data payloads for equality, as per |
115
|
|
|
|
|
|
|
# https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.4.2.2 |
116
|
|
|
|
|
|
|
# if provided with a state hashref with a 'path' key, any differences are recorded within |
117
|
4405
|
|
|
4405
|
0
|
6718
|
sub is_equal ($x, $y, $state = undef) { |
|
4405
|
|
|
|
|
7604
|
|
|
4405
|
|
|
|
|
6882
|
|
|
4405
|
|
|
|
|
6428
|
|
|
4405
|
|
|
|
|
5750
|
|
118
|
4405
|
|
100
|
|
|
17923
|
$state->{path} //= ''; |
119
|
|
|
|
|
|
|
|
120
|
4405
|
|
|
|
|
9770
|
my @types = map get_type($_), $x, $y; |
121
|
|
|
|
|
|
|
|
122
|
4405
|
100
|
|
|
|
15042
|
if ($state->{scalarref_booleans}) { |
123
|
1568
|
100
|
|
|
|
3610
|
($x, $types[0]) = (0+!!$$x, 'boolean') if $types[0] eq 'reference to SCALAR'; |
124
|
1568
|
100
|
|
|
|
3239
|
($y, $types[1]) = (0+!!$$y, 'boolean') if $types[1] eq 'reference to SCALAR'; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
4405
|
100
|
|
|
|
11302
|
return 0 if $types[0] ne $types[1]; |
128
|
3512
|
100
|
|
|
|
7399
|
return 1 if $types[0] eq 'null'; |
129
|
3498
|
100
|
|
|
|
13808
|
return $x eq $y if $types[0] eq 'string'; |
130
|
1613
|
100
|
|
|
|
8285
|
return $x == $y if grep $types[0] eq $_, qw(boolean number integer); |
131
|
|
|
|
|
|
|
|
132
|
530
|
|
|
|
|
1034
|
my $path = $state->{path}; |
133
|
530
|
100
|
|
|
|
1203
|
if ($types[0] eq 'object') { |
134
|
208
|
100
|
|
|
|
705
|
return 0 if keys %$x != keys %$y; |
135
|
192
|
100
|
|
|
|
892
|
return 0 if not is_equal([ sort keys %$x ], [ sort keys %$y ]); |
136
|
186
|
|
|
|
|
776
|
foreach my $property (sort keys %$x) { |
137
|
218
|
|
|
|
|
505
|
$state->{path} = jsonp($path, $property); |
138
|
218
|
100
|
|
|
|
630
|
return 0 if not is_equal($x->{$property}, $y->{$property}, $state); |
139
|
|
|
|
|
|
|
} |
140
|
100
|
|
|
|
|
735
|
return 1; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
322
|
50
|
|
|
|
803
|
if ($types[0] eq 'array') { |
144
|
322
|
100
|
|
|
|
870
|
return 0 if @$x != @$y; |
145
|
314
|
|
|
|
|
912
|
foreach my $idx (0 .. $x->$#*) { |
146
|
352
|
|
|
|
|
1048
|
$state->{path} = $path.'/'.$idx; |
147
|
352
|
100
|
|
|
|
941
|
return 0 if not is_equal($x->[$idx], $y->[$idx], $state); |
148
|
|
|
|
|
|
|
} |
149
|
226
|
|
|
|
|
1020
|
return 1; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
return 0; # should never get here |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# checks array elements for uniqueness. short-circuits on first pair of matching elements |
156
|
|
|
|
|
|
|
# if second arrayref is provided, it is populated with the indices of identical items |
157
|
2839
|
|
|
2839
|
0
|
5135
|
sub is_elements_unique ($array, $equal_indices = undef) { |
|
2839
|
|
|
|
|
4565
|
|
|
2839
|
|
|
|
|
4918
|
|
|
2839
|
|
|
|
|
4260
|
|
158
|
2839
|
|
|
|
|
9262
|
foreach my $idx0 (0 .. $array->$#*-1) { |
159
|
983
|
|
|
|
|
2607
|
foreach my $idx1 ($idx0+1 .. $array->$#*) { |
160
|
1388
|
100
|
|
|
|
4844
|
if (is_equal($array->[$idx0], $array->[$idx1], { scalarref_booleans => 1 })) { |
161
|
205
|
50
|
|
|
|
2114
|
push @$equal_indices, $idx0, $idx1 if defined $equal_indices; |
162
|
205
|
|
|
|
|
822
|
return 0; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
2634
|
|
|
|
|
8914
|
return 1; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# shorthand for creating and appending json pointers |
170
|
|
|
|
|
|
|
# the first argument is a a json pointer; remaining arguments are path segments to be encoded and |
171
|
|
|
|
|
|
|
# appended |
172
|
|
|
|
|
|
|
sub jsonp { |
173
|
44838
|
100
|
|
44838
|
0
|
554968
|
return join('/', shift, map s/~/~0/gr =~ s!/!~1!gr, map +(is_plain_arrayref($_) ? @$_ : $_), grep defined, @_); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# splits a json pointer apart into its path segments |
177
|
0
|
|
|
0
|
0
|
0
|
sub unjsonp ($path) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
178
|
0
|
|
|
|
|
0
|
return map s!~0!~!gr =~ s!~1!/!gr, split m!/!, $path; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# get all annotations produced for the current instance data location (that are visible to this |
182
|
|
|
|
|
|
|
# schema location) - remember these are hashrefs, not Annotation objects |
183
|
1184
|
|
|
1184
|
0
|
2062
|
sub local_annotations ($state) { |
|
1184
|
|
|
|
|
1852
|
|
|
1184
|
|
|
|
|
9386
|
|
184
|
1184
|
|
|
|
|
4594
|
grep $_->{instance_location} eq $state->{data_path}, $state->{annotations}->@*; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# shorthand for finding the canonical uri of the present schema location |
188
|
|
|
|
|
|
|
# last argument can be an arrayref, usually coming from $state->{_schema_path_suffix} |
189
|
40805
|
|
|
40805
|
0
|
66059
|
sub canonical_uri ($state, @extra_path) { |
|
40805
|
|
|
|
|
61111
|
|
|
40805
|
|
|
|
|
77255
|
|
|
40805
|
|
|
|
|
59573
|
|
190
|
40805
|
100
|
100
|
|
|
170687
|
return $state->{initial_schema_uri} if not @extra_path and not length($state->{schema_path}); |
191
|
23596
|
100
|
100
|
|
|
83242
|
splice(@extra_path, -1, 1, $extra_path[-1]->@*) if @extra_path and is_plain_arrayref($extra_path[-1]); |
192
|
23596
|
|
|
|
|
80920
|
my $uri = $state->{initial_schema_uri}->clone; |
193
|
23596
|
100
|
100
|
|
|
1812965
|
my $fragment = ($uri->fragment//'').(@extra_path ? jsonp($state->{schema_path}, @extra_path) : $state->{schema_path}); |
194
|
23596
|
100
|
|
|
|
126324
|
undef $fragment if not length($fragment); |
195
|
23596
|
|
|
|
|
66578
|
$uri->fragment($fragment); |
196
|
23596
|
|
|
|
|
170073
|
$uri; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
# shorthand for creating error objects |
200
|
|
|
|
|
|
|
# uses these keys from $state: |
201
|
|
|
|
|
|
|
# - initial_schema_uri |
202
|
|
|
|
|
|
|
# - keyword |
203
|
|
|
|
|
|
|
# - data_path |
204
|
|
|
|
|
|
|
# - traversed_schema_path |
205
|
|
|
|
|
|
|
# - schema_path |
206
|
|
|
|
|
|
|
# - _schema_path_suffix |
207
|
|
|
|
|
|
|
# - errors |
208
|
11090
|
|
|
11090
|
0
|
43080
|
sub E ($state, $error_string, @args) { |
|
11090
|
|
|
|
|
19168
|
|
|
11090
|
|
|
|
|
18429
|
|
|
11090
|
|
|
|
|
19674
|
|
|
11090
|
|
|
|
|
16247
|
|
209
|
11090
|
50
|
|
|
|
26164
|
croak 'E called in void context' if not defined wantarray; |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# sometimes the keyword shouldn't be at the very end of the schema path |
212
|
|
|
|
|
|
|
my $uri = canonical_uri($state, $state->{keyword}, $state->{_schema_path_suffix}) |
213
|
11090
|
|
|
|
|
38704
|
->to_abs($state->{effective_base_uri}); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
my $keyword_location = $state->{traversed_schema_path} |
216
|
11090
|
|
|
|
|
2691577
|
.jsonp($state->{schema_path}, $state->{keyword}, delete $state->{_schema_path_suffix}); |
217
|
|
|
|
|
|
|
|
218
|
11090
|
100
|
100
|
|
|
42194
|
undef $uri if $uri eq '' and $keyword_location eq '' |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
219
|
|
|
|
|
|
|
or ($uri->fragment // '') eq $keyword_location and $uri->clone->fragment(undef) eq ''; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
push $state->{errors}->@*, JSON::Schema::Modern::Error->new( |
222
|
|
|
|
|
|
|
keyword => $state->{keyword}, |
223
|
|
|
|
|
|
|
instance_location => $state->{data_path}, |
224
|
|
|
|
|
|
|
keyword_location => $keyword_location, |
225
|
|
|
|
|
|
|
defined $uri ? ( absolute_keyword_location => $uri ) : (), |
226
|
|
|
|
|
|
|
error => @args ? sprintf($error_string, @args) : $error_string, |
227
|
11090
|
100
|
|
|
|
4429329
|
$state->{exception} ? ( exception => $state->{exception} ) : (), |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
228
|
|
|
|
|
|
|
); |
229
|
|
|
|
|
|
|
|
230
|
11090
|
|
|
|
|
765035
|
return 0; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
# shorthand for creating annotations |
234
|
|
|
|
|
|
|
# uses these keys from $state: |
235
|
|
|
|
|
|
|
# - initial_schema_uri |
236
|
|
|
|
|
|
|
# - keyword |
237
|
|
|
|
|
|
|
# - data_path |
238
|
|
|
|
|
|
|
# - traversed_schema_path |
239
|
|
|
|
|
|
|
# - schema_path |
240
|
|
|
|
|
|
|
# - _schema_path_suffix |
241
|
|
|
|
|
|
|
# - annotations |
242
|
|
|
|
|
|
|
# - collect_annotations |
243
|
10665
|
|
|
10665
|
0
|
17005
|
sub A ($state, $annotation) { |
|
10665
|
|
|
|
|
17054
|
|
|
10665
|
|
|
|
|
17372
|
|
|
10665
|
|
|
|
|
15085
|
|
244
|
10665
|
100
|
100
|
|
|
41526
|
return 1 if not $state->{collect_annotations} or $state->{spec_version} eq 'draft7'; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# we store the absolute uri in unresolved form until needed, |
247
|
|
|
|
|
|
|
# and perform the rest of the calculations later. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
my $uri = [ canonical_uri($state, $state->{keyword}, $state->{_schema_path_suffix}), |
250
|
2438
|
|
|
|
|
8588
|
$state->{effective_base_uri} ]; |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
my $keyword_location = $state->{traversed_schema_path} |
253
|
2438
|
|
|
|
|
8405
|
.jsonp($state->{schema_path}, $state->{keyword}, delete $state->{_schema_path_suffix}); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
push $state->{annotations}->@*, { |
256
|
|
|
|
|
|
|
keyword => $state->{keyword}, |
257
|
|
|
|
|
|
|
instance_location => $state->{data_path}, |
258
|
|
|
|
|
|
|
keyword_location => $keyword_location, |
259
|
|
|
|
|
|
|
# we calculate absolute_keyword_location when instantiating the Annotation object for Result |
260
|
|
|
|
|
|
|
_uri => $uri, |
261
|
|
|
|
|
|
|
annotation => $annotation, |
262
|
2438
|
100
|
|
|
|
14997
|
$state->{_unknown} ? ( unknown => 1 ) : (), |
263
|
|
|
|
|
|
|
}; |
264
|
|
|
|
|
|
|
|
265
|
2438
|
|
|
|
|
6698
|
return 1; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# creates an error object, but also aborts evaluation immediately |
269
|
|
|
|
|
|
|
# only this error is returned, because other errors on the stack might not actually be "real" |
270
|
|
|
|
|
|
|
# errors (consider if we were in the middle of evaluating a "not" or "if"). |
271
|
|
|
|
|
|
|
# Therefore this is only appropriate during the evaluation phase, not the traverse phase. |
272
|
55
|
|
|
55
|
0
|
293
|
sub abort ($state, $error_string, @args) { |
|
55
|
|
|
|
|
118
|
|
|
55
|
|
|
|
|
116
|
|
|
55
|
|
|
|
|
122
|
|
|
55
|
|
|
|
|
89
|
|
273
|
55
|
|
|
|
|
780
|
()= E({ %$state, exception => 1 }, $error_string, @args); |
274
|
55
|
50
|
|
|
|
409
|
croak 'abort() called during traverse' if $state->{traverse}; |
275
|
55
|
|
|
|
|
709
|
die pop $state->{errors}->@*; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
0
|
|
|
0
|
0
|
0
|
sub assert_keyword_exists ($state, $schema) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
279
|
0
|
0
|
|
|
|
0
|
croak 'assert_keyword_exists called in void context' if not defined wantarray; |
280
|
0
|
0
|
|
|
|
0
|
return E($state, '%s keyword is required', $state->{keyword}) if not exists $schema->{$state->{keyword}}; |
281
|
0
|
|
|
|
|
0
|
return 1; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
39287
|
|
|
39287
|
0
|
62325
|
sub assert_keyword_type ($state, $schema, $type) { |
|
39287
|
|
|
|
|
59327
|
|
|
39287
|
|
|
|
|
55851
|
|
|
39287
|
|
|
|
|
58700
|
|
|
39287
|
|
|
|
|
54024
|
|
285
|
39287
|
50
|
|
|
|
82466
|
croak 'assert_keyword_type called in void context' if not defined wantarray; |
286
|
39287
|
|
|
|
|
81474
|
my $value = $schema->{$state->{keyword}}; |
287
|
39287
|
|
|
|
|
60189
|
my $thing = 'value'; |
288
|
|
|
|
|
|
|
($value, $thing) = is_plain_hashref($value) ? ($value->{$state->{_schema_path_suffix}}, 'value at "'.$state->{_schema_path_suffix}.'"') |
289
|
|
|
|
|
|
|
: is_plain_arrayref($value) ? ($value->[$state->{_schema_path_suffix}], 'item '.$state->{_schema_path_suffix}) |
290
|
|
|
|
|
|
|
: die 'unknown type' |
291
|
39287
|
0
|
|
|
|
84160
|
if exists $state->{_schema_path_suffix}; |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
292
|
39287
|
100
|
|
|
|
81166
|
return 1 if is_type($type, $value); |
293
|
18
|
100
|
|
|
|
158
|
E($state, '%s %s is not a%s %s', $state->{keyword}, $thing, ($type =~ /^[aeiou]/ ? 'n' : ''), $type); |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
2285
|
|
|
2285
|
0
|
4153
|
sub assert_pattern ($state, $pattern) { |
|
2285
|
|
|
|
|
3692
|
|
|
2285
|
|
|
|
|
3696
|
|
|
2285
|
|
|
|
|
3367
|
|
297
|
2285
|
50
|
|
|
|
5245
|
croak 'assert_pattern called in void context' if not defined wantarray; |
298
|
|
|
|
|
|
|
try { |
299
|
0
|
|
|
0
|
|
0
|
local $SIG{__WARN__} = sub { die @_ }; |
300
|
|
|
|
|
|
|
qr/$pattern/; |
301
|
|
|
|
|
|
|
} |
302
|
2285
|
|
|
|
|
5179
|
catch ($e) { return E($state, $e); }; |
303
|
2282
|
|
|
|
|
21326
|
return 1; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
# this is only suitable for checking URIs within schemas themselves |
307
|
4424
|
|
|
4424
|
0
|
8038
|
sub assert_uri_reference ($state, $schema) { |
|
4424
|
|
|
|
|
6638
|
|
|
4424
|
|
|
|
|
6844
|
|
|
4424
|
|
|
|
|
6744
|
|
308
|
4424
|
50
|
|
|
|
9614
|
croak 'assert_uri_reference called in void context' if not defined wantarray; |
309
|
|
|
|
|
|
|
|
310
|
4424
|
|
|
|
|
9227
|
my $string = $schema->{$state->{keyword}}; |
311
|
|
|
|
|
|
|
return E($state, '%s value is not a valid URI reference', $state->{keyword}) |
312
|
|
|
|
|
|
|
# see also uri-reference format sub |
313
|
4424
|
100
|
66
|
|
|
15306
|
if fc(Mojo::URL->new($string)->to_unsafe_string) ne fc($string) |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
314
|
|
|
|
|
|
|
or $string =~ /[^[:ascii:]]/ |
315
|
|
|
|
|
|
|
or $string =~ /#/ |
316
|
|
|
|
|
|
|
and $string !~ m{#$} # empty fragment |
317
|
|
|
|
|
|
|
and $string !~ m{#[A-Za-z][A-Za-z0-9_:.-]*$} # plain-name fragment |
318
|
|
|
|
|
|
|
and $string !~ m{#/(?:[^~]|~[01])*$}; # json pointer fragment |
319
|
|
|
|
|
|
|
|
320
|
4394
|
|
|
|
|
1562629
|
return 1; |
321
|
|
|
|
|
|
|
} |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
# this is only suitable for checking URIs within schemas themselves |
324
|
5737
|
|
|
5737
|
0
|
10694
|
sub assert_uri ($state, $schema, $override = undef) { |
|
5737
|
|
|
|
|
8587
|
|
|
5737
|
|
|
|
|
8802
|
|
|
5737
|
|
|
|
|
9105
|
|
|
5737
|
|
|
|
|
8224
|
|
325
|
5737
|
50
|
|
|
|
12187
|
croak 'assert_uri called in void context' if not defined wantarray; |
326
|
|
|
|
|
|
|
|
327
|
5737
|
|
66
|
|
|
20793
|
my $string = $override // $schema->{$state->{keyword}}; |
328
|
5737
|
|
|
|
|
19484
|
my $uri = Mojo::URL->new($string); |
329
|
|
|
|
|
|
|
|
330
|
5737
|
50
|
66
|
|
|
494255
|
return E($state, '"%s" is not a valid URI', $string) |
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
331
|
|
|
|
|
|
|
# see also uri format sub |
332
|
|
|
|
|
|
|
if fc($uri->to_unsafe_string) ne fc($string) |
333
|
|
|
|
|
|
|
or $string =~ /[^[:ascii:]]/ |
334
|
|
|
|
|
|
|
or not $uri->is_abs |
335
|
|
|
|
|
|
|
or $string =~ /#/ |
336
|
|
|
|
|
|
|
and $string !~ m{#$} # empty fragment |
337
|
|
|
|
|
|
|
and $string !~ m{#[A-Za-z][A-Za-z0-9_:.-]*$} # plain-name fragment |
338
|
|
|
|
|
|
|
and $string !~ m{#/(?:[^~]|~[01])*$}; # json pointer fragment |
339
|
|
|
|
|
|
|
|
340
|
5729
|
|
|
|
|
1163296
|
return 1; |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
# produces an annotation whose value is the same as that of the current keyword |
344
|
1608
|
|
|
1608
|
0
|
2480
|
sub annotate_self ($state, $schema) { |
|
1608
|
|
|
|
|
2469
|
|
|
1608
|
|
|
|
|
2405
|
|
|
1608
|
|
|
|
|
2333
|
|
345
|
|
|
|
|
|
|
A($state, is_ref($schema->{$state->{keyword}}) ? dclone($schema->{$state->{keyword}}) |
346
|
1608
|
100
|
|
|
|
10857
|
: $schema->{$state->{keyword}}); |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
1101
|
|
|
1101
|
0
|
1987
|
sub sprintf_num ($value) { |
|
1101
|
|
|
|
|
2083
|
|
|
1101
|
|
|
|
|
1770
|
|
350
|
|
|
|
|
|
|
# use original value as stored in the NV, without losing precision |
351
|
1101
|
100
|
|
|
|
6762
|
ref($value) =~ /^Math::Big(?:Int|Float)$/ ? $value->bstr : sprintf('%s', $value); |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
1; |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
__END__ |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=pod |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
=encoding UTF-8 |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
=head1 NAME |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
JSON::Schema::Modern::Utilities - Internal utilities for JSON::Schema::Modern |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head1 VERSION |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
version 0.570 |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=head1 SYNOPSIS |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
use JSON::Schema::Modern::Utilities qw(func1 func2..); |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=head1 DESCRIPTION |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
This class contains internal utilities to be used by L<JSON::Schema::Modern>. |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=for Pod::Coverage is_type get_type is_equal is_elements_unique jsonp unjsonp local_annotations |
379
|
|
|
|
|
|
|
canonical_uri E A abort assert_keyword_exists assert_keyword_type assert_pattern assert_uri_reference assert_uri |
380
|
|
|
|
|
|
|
annotate_self sprintf_num |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=for stopwords OpenAPI |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
=head1 SUPPORT |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
391
|
|
|
|
|
|
|
server|https://open-api.slack.com>, which are also great resources for finding help. |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
=head1 AUTHOR |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENCE |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
This software is copyright (c) 2020 by Karen Etheridge. |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
402
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=cut |