Debian Codegraph @

lib/JSON/Schema/Modern/Vocabulary.pm

raw
1 use strict;
2 use warnings;
3 package JSON::Schema::Modern::Vocabulary;
4 # vim: set ts=8 sts=2 sw=2 tw=100 et :
5 # ABSTRACT: Base role for JSON Schema vocabulary classes
6
7 our $VERSION = '0.650';
8
9 use 5.020;
10 use Moo::Role;
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 JSON::Schema::Modern::Utilities qw(jsonp assert_keyword_type abort);
22 use Carp ();
23 use namespace::clean;
24
25 our @CARP_NOT = qw(JSON::Schema::Modern);
26
27 requires qw(vocabulary keywords);
28
29 sub evaluation_order { 999 } # override, if needed
30
31 sub BUILD { die 'these classes are never instantiated' }
32
33 sub traverse ($class, $schema, $state) {
34 $state->{evaluator}->_traverse_subschema($schema, $state);
35 }
36
37 sub traverse_subschema ($class, $schema, $state) {
38 $state->{evaluator}->_traverse_subschema($schema->{$state->{keyword}},
39 +{ %$state, keyword_path => $state->{keyword_path}.'/'.$state->{keyword} });
40 }
41
42 sub traverse_array_schemas ($class, $schema, $state) {
43 return if not assert_keyword_type($state, $schema, 'array');
44 return E($state, '%s array is empty', $state->{keyword}) if not $schema->{$state->{keyword}}->@*;
45
46 my $valid = 1;
47 foreach my $idx (0 .. $schema->{$state->{keyword}}->$#*) {
48 $valid = 0 if not $state->{evaluator}->_traverse_subschema($schema->{$state->{keyword}}[$idx],
49 +{ %$state, keyword_path => $state->{keyword_path}.'/'.$state->{keyword}.'/'.$idx });
50 }
51 return $valid;
52 }
53
54 sub traverse_object_schemas ($class, $schema, $state) {
55 return if not assert_keyword_type($state, $schema, 'object');
56
57 my $valid = 1;
58 foreach my $property (sort keys $schema->{$state->{keyword}}->%*) {
59 $valid = 0 if not $state->{evaluator}->_traverse_subschema($schema->{$state->{keyword}}{$property},
60 +{ %$state, keyword_path => jsonp($state->{keyword_path}, $state->{keyword}, $property) });
61 }
62 return $valid;
63 }
64
65 sub traverse_property_schema ($class, $schema, $state, $property) {
66 return if not assert_keyword_type($state, $schema, 'object');
67
68 $state->{evaluator}->_traverse_subschema($schema->{$state->{keyword}}{$property},
69 +{ %$state, keyword_path => jsonp($state->{keyword_path}, $state->{keyword}, $property) });
70 }
71
72 sub eval ($class, $data, $schema, $state) {
73 $state->{evaluator}->_evaluate_subschema($data, $schema, $state);
74 }
75
76 sub eval_subschema_at_uri ($class, $data, $schema, $state, $uri) {
77 my $schema_info = $state->{evaluator}->_fetch_from_uri($uri);
78 abort($state, 'EXCEPTION: unable to find resource "%s"', $uri) if not $schema_info;
79 abort($state, 'EXCEPTION: bad reference to "%s": not a schema', $schema_info->{canonical_uri})
80 if $schema_info->{document}->get_entity_at_location($schema_info->{document_path}) ne 'schema';
81
82 my $scope_uri = $schema_info->{canonical_uri}->clone->fragment(undef);
83 push $state->{dynamic_scope}->@*, $scope_uri if $state->{dynamic_scope}->[-1] ne $scope_uri;
84
85 return $state->{evaluator}->_evaluate_subschema($data, $schema_info->{schema},
86 +{
87 %$state,
88 # keyword is assumed to be json pointer-encoded (if a suffix path is needed), so we just concat
89 traversed_keyword_path => $state->{traversed_keyword_path}.$state->{keyword_path}.'/'.$state->{keyword},
90 initial_schema_uri => $schema_info->{canonical_uri},
91 $schema_info->%{qw(document specification_version vocabularies)},
92 keyword_path => '',
93 });
94 }
95
96 1;
97
98 __END__
99
100 =pod
101
102 =encoding UTF-8
103
104 =head1 NAME
105
106 JSON::Schema::Modern::Vocabulary - Base role for JSON Schema vocabulary classes
107
108 =head1 VERSION
109
110 version 0.650
111
112 I use a linearly-increasing version numbering scheme. No meaning should be
113 presumed or inferred from the version being less than 1.0.
114
115 =head1 SYNOPSIS
116
117 package MyApp::Vocabulary::Awesome;
118 use Moo;
119 with 'JSON::Schema::Modern::Vocabulary';
120
121 =head1 DESCRIPTION
122
123 This package is the role which all all vocabulary classes for L<JSON::Schema::Modern>
124 must compose, describing the basic structure expected of a vocabulary class and defining some basic
125 methods which are useful for traversing or evaluating various keywords.
126
127 =head1 ATTRIBUTES
128
129 =head1 METHODS
130
131 =for Pod::Coverage BUILD
132
133 =for stopwords schema subschema
134
135 =head2 vocabulary
136
137 Returns the canonical URI(s) describing the vocabulary for each draft specification version, as described in
138 L<JSON Schema Core Meta-specification, section 8.1.2|https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.1.2>.
139 Must be implemented by the composing class.
140
141 =head2 evaluation_order
142
143 Returns a positive integer, used as a sort key for determining the evaluation order of this vocabulary. If
144 not overridden in a custom vocabulary class, its evaluation order will be after all built-in
145 vocabularies. You probably don't need to define this.
146
147 =head2 keywords
148
149 Returns the list of keywords defined by the vocabulary. Must be implemented by the composing class.
150
151 =head2 traverse
152
153 Traverses a subschema. Callers are expected to establish a new C<$state> scope.
154
155 =head2 traverse_subschema
156
157 Recursively traverses the schema at the current keyword, as in the C<not> keyword.
158
159 =head2 traverse_array_schemas
160
161 Recursively traverses the list of subschemas at the current keyword, as in the C<allOf> keyword.
162
163 =head2 traverse_object_schemas
164
165 Recursively traverses the (subschema) values of the object at the current keyword, as in the C<$defs> keyword.
166
167 =head2 traverse_property_schema
168
169 Recursively traverses the subschema under one property of the object at the current keyword.
170
171 =head2 eval
172
173 Evaluates a subschema. Callers are expected to establish a new C<$state> scope.
174
175 =head2 eval_subschema_at_uri
176
177 Resolves a URI to a subschema, then evaluates that subschema (essentially the C<$ref> keyword).
178
179 =head1 GIVING THANKS
180
181 =for stopwords MetaCPAN GitHub
182
183 If you found this module to be useful, please show your appreciation by
184 adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern>
185 and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>.
186
187 =head1 SUPPORT
188
189 Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>.
190
191 I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
192
193 =for stopwords OpenAPI
194
195 You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack
196 server|https://open-api.slack.com>, which are also great resources for finding help.
197
198 =head1 AUTHOR
199
200 Karen Etheridge <[email protected]>
201
202 =head1 COPYRIGHT AND LICENCE
203
204 This software is copyright (c) 2020 by Karen Etheridge.
205
206 This is free software; you can redistribute it and/or modify it under
207 the same terms as the Perl 5 programming language system itself.
208
209 Some schema files have their own licence, in share/LICENSE.
210
211 =cut