Debian Codegraph @

lib/JSON/Schema/Modern/Vocabulary/Unevaluated.pm

raw
1 use strict;
2 use warnings;
3 package JSON::Schema::Modern::Vocabulary::Unevaluated;
4 # vim: set ts=8 sts=2 sw=2 tw=100 et :
5 # ABSTRACT: Implementation of the JSON Schema Unevaluated vocabulary
6
7 our $VERSION = '0.650';
8
9 use 5.020;
10 use Moo;
11 use strictures 2;
12 use stable 0.031 'postderef';
13 use experimental 'signatures';
14 no autovivification warn => qw(fetch store exists delete);
15 use if "$]" >= 5.022, experimental => 're_strict';
16 no if "$]" >= 5.031009, feature => 'indirect';
17 no if "$]" >= 5.033001, feature => 'multidimensional';
18 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
19 no if "$]" >= 5.041009, feature => 'smartmatch';
20 no feature 'switch';
21 use List::Util 'max';
22 use if "$]" < 5.041010, 'List::Util' => 'any';
23 use if "$]" >= 5.041010, experimental => 'keyword_any';
24 use JSON::Schema::Modern::Utilities qw(is_type jsonp local_annotations E A abort true);
25 use namespace::clean;
26
27 with 'JSON::Schema::Modern::Vocabulary';
28
29 sub vocabulary ($class) {
30 'https://json-schema.org/draft/2020-12/vocab/unevaluated' => 'draft2020-12';
31 }
32
33 sub evaluation_order ($class) { 7 }
34
35 # This vocabulary should be evaluated after the Applicator vocabulary.
36 sub keywords ($class, $spec_version) {
37 die 'Unevaluated not implemented in '.$spec_version if $spec_version =~ /^draft(?:[467]|2019-09)\z/;
38 qw(unevaluatedItems unevaluatedProperties);
39 }
40
41 sub _traverse_keyword_unevaluatedItems ($class, $schema, $state) {
42 $class->traverse_subschema($schema, $state);
43 }
44
45 sub _eval_keyword_unevaluatedItems ($class, $data, $schema, $state) {
46 return 1 if not is_type('array', $data);
47
48 # these should never happen
49 die '"unevaluatedItems" keyword present, but annotation collection is disabled'
50 if not $state->{collect_annotations};
51
52 my @annotations = local_annotations($state);
53
54 # a relevant keyword already produced a 'true' annotation at this location
55 my @boolean_annotation_keywords =
56 $state->{specification_version} eq 'draft2019-09' ? qw(items additionalItems unevaluatedItems)
57 : qw(prefixItems items contains unevaluatedItems);
58 my %bools; @bools{@boolean_annotation_keywords} = (1)x@boolean_annotation_keywords;
59 return 1
60 if any { $bools{$_->{keyword}} && is_type('boolean', $_->{annotation}) && $_->{annotation} }
61 @annotations;
62
63 # otherwise, evaluate at every instance item greater than the max of all 'prefixItems'/numeric
64 # 'items' annotations that isn't in a 'contains' annotation
65 my $max_index_annotation_keyword = $state->{specification_version} eq 'draft2019-09' ? 'items' : 'prefixItems';
66 my $last_index = max(-1, grep is_type('integer', $_),
67 map +($_->{keyword} eq $max_index_annotation_keyword ? $_->{annotation} : ()), @annotations);
68
69 return 1 if $last_index == $data->$#*;
70
71 my @contains_annotation_indexes = $state->{specification_version} eq 'draft2019-09' ? ()
72 : map +($_->{keyword} eq 'contains' ? $_->{annotation}->@* : ()), @annotations;
73
74 my $valid = 1;
75 foreach my $idx ($last_index+1 .. $data->$#*) {
76 next if any { $idx == $_ } @contains_annotation_indexes;
77 if (is_type('boolean', $schema->{unevaluatedItems})) {
78 next if $schema->{unevaluatedItems};
79 $valid = E({ %$state, data_path => $state->{data_path}.'/'.$idx },
80 'additional item not permitted')
81 }
82 else {
83 if ($class->eval($data->[$idx], $schema->{unevaluatedItems},
84 +{ %$state, data_path => $state->{data_path}.'/'.$idx,
85 keyword_path => $state->{keyword_path}.'/unevaluatedItems',
86 collect_annotations => $state->{collect_annotations} & ~1 })) {
87 next;
88 }
89
90 $valid = 0;
91 }
92 last if $state->{short_circuit};
93 }
94
95 A($state, true);
96 return E($state, 'subschema is not valid against all additional items') if not $valid;
97 return 1;
98 }
99
100 sub _traverse_keyword_unevaluatedProperties ($class, $schema, $state) {
101 $class->traverse_subschema($schema, $state);
102 }
103
104 sub _eval_keyword_unevaluatedProperties ($class, $data, $schema, $state) {
105 return 1 if not is_type('object', $data);
106
107 # these should never happen
108 die '"unevaluatedProperties" keyword present, but annotation collection is disabled'
109 if not $state->{collect_annotations};
110
111 my @evaluated_properties = map do {
112 my $keyword = $_->{keyword};
113 (grep $keyword eq $_, qw(properties additionalProperties patternProperties unevaluatedProperties))
114 ? $_->{annotation}->@* : ();
115 }, local_annotations($state);
116
117 my $valid = 1;
118 my @properties;
119 foreach my $property (sort keys %$data) {
120 next if any { $_ eq $property } @evaluated_properties;
121 push @properties, $property;
122
123 if (is_type('boolean', $schema->{unevaluatedProperties})) {
124 next if $schema->{unevaluatedProperties};
125 $valid = E({ %$state, data_path => jsonp($state->{data_path}, $property) },
126 'additional property not permitted');
127 }
128 else {
129 if ($class->eval($data->{$property}, $schema->{unevaluatedProperties},
130 +{ %$state, data_path => jsonp($state->{data_path}, $property),
131 keyword_path => $state->{keyword_path}.'/unevaluatedProperties',
132 collect_annotations => $state->{collect_annotations} & ~1 })) {
133 next;
134 }
135
136 $valid = 0;
137 }
138 last if $state->{short_circuit};
139 }
140
141 A($state, \@properties);
142 return E($state, 'not all additional properties are valid') if not $valid;
143 return 1;
144 }
145
146 1;
147
148 __END__
149
150 =pod
151
152 =encoding UTF-8
153
154 =head1 NAME
155
156 JSON::Schema::Modern::Vocabulary::Unevaluated - Implementation of the JSON Schema Unevaluated vocabulary
157
158 =head1 VERSION
159
160 version 0.650
161
162 I use a linearly-increasing version numbering scheme. No meaning should be
163 presumed or inferred from the version being less than 1.0.
164
165 =head1 DESCRIPTION
166
167 =for Pod::Coverage vocabulary evaluation_order keywords
168
169 =for stopwords metaschema
170
171 Implementation of the JSON Schema Draft 2020-12 "Unevaluated" vocabulary, indicated in metaschemas
172 with the URI C<https://json-schema.org/draft/2020-12/vocab/unevaluated> and formally specified in
173 L<https://json-schema.org/draft/2020-12/json-schema-core.html#section-11>.
174
175 Support is also provided for the equivalent Draft 2019-09 keywords in the
176 JSON Schema Draft 2019-09 "Applicator" vocabulary, indicated in metaschemas
177 with the URI C<https://json-schema.org/draft/2019-09/vocab/applicator> and formally specified in
178 L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-02#section-9>.
179
180 =head1 GIVING THANKS
181
182 =for stopwords MetaCPAN GitHub
183
184 If you found this module to be useful, please show your appreciation by
185 adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern>
186 and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>.
187
188 =head1 SUPPORT
189
190 Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>.
191
192 I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
193
194 =for stopwords OpenAPI
195
196 You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack
197 server|https://open-api.slack.com>, which are also great resources for finding help.
198
199 =head1 AUTHOR
200
201 Karen Etheridge <[email protected]>
202
203 =head1 COPYRIGHT AND LICENCE
204
205 This software is copyright (c) 2020 by Karen Etheridge.
206
207 This is free software; you can redistribute it and/or modify it under
208 the same terms as the Perl 5 programming language system itself.
209
210 Some schema files have their own licence, in share/LICENSE.
211
212 =cut