Debian Codegraph @

script/json-schema-eval

raw
1 #!/usr/bin/perl
2 # vim: set ts=8 sts=2 sw=2 tw=100 et :
3 # PODNAME: json-schema-eval
4 # ABSTRACT: A command-line interface to JSON::Schema::Modern::evaluate()
5 use 5.020; # for fc, unicode_strings features
6 use strictures 2;
7 use stable 0.031 'postderef';
8 use experimental 'signatures';
9 no autovivification warn => qw(fetch store exists delete);
10 use if "$]" >= 5.022, experimental => 're_strict';
11 no if "$]" >= 5.031009, feature => 'indirect';
12 no if "$]" >= 5.033001, feature => 'multidimensional';
13 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
14 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
15
16 use Getopt::Long::Descriptive;
17 use Mojo::File 'path';
18 use Feature::Compat::Try;
19 use List::Util 'max';
20 use builtin::compat 'blessed';
21 use JSON::Schema::Modern;
22
23 my ($opt, $usage) = Getopt::Long::Descriptive::describe_options(
24 "$0 %o",
25 ['help|usage|?|h', 'print usage information and exit', { shortcircuit => 1 } ],
26 [],
27 ['specification-version|version=s', 'which version of the JSON Schema specification to use'],
28 ['output-format=s', 'output format (flag, basic, terse)'],
29 ['short-circuit', 'return early in any execution path as soon as the outcome can be determined'],
30 ['max-traversal-depth=i', 'the maximum number of levels deep a schema traversal may go'],
31 ['validate-formats', 'treat the "format" keyword as an assertion, not merely an annotation'],
32 ['validate-content-schemas', 'treat the "contentMediaType" and "contentSchema" keywords as assertions'],
33 ['collect-annotations', 'collect annotations'],
34 ['strict', 'disallow unknown keywords'],
35 # scalarref-booleans, stringy-numbers make no sense in json-encoded data
36 ['dump-identifiers', 'print a list of all identifiers found in the schema'],
37 ['with-defaults', 'include list of defaults for missing data'],
38 [],
39 ['validate-schema:s@', 'validate the provided schema against its meta-schema and the specification. do not provide --data or --schema; can be used more than once' ],
40 ['add-schema=s@', 'the filename of an extra schema to load, so it can be used by $ref; can be used more than once' ],
41 ['data=s@', 'the filename to use for the instance data (if not provided, STDIN is used); can be used more than once'],
42 ['schema=s', 'the filename to use for the schema (if not provided, STDIN is used)'],
43 );
44
45 print($usage->text), exit if $opt->help;
46
47 die '--validate-schema and --data should not be used together' if defined $opt->data and defined $opt->validate_schema;
48 die '--validate-schema and --schema should not be used together' if defined $opt->schema and defined $opt->validate_schema;
49
50 my $js = JSON::Schema::Modern->new(%$opt);
51
52
53 foreach my $add_schema_file (($opt->add_schema//[])->@*) {
54 try {
55 my $schema = parse_input(path($add_schema_file)->slurp('UTF-8'));
56 die "this looks like an OpenAPI file: try 'openapi-validate' (install OpenAPI::Modern), instead\n"
57 if ref $schema eq 'HASH' and $schema->{openapi};
58 $js->add_schema('file://'.$add_schema_file => $schema);
59 }
60 catch ($e) {
61 say blessed($e) && $e->isa('JSON::Schema::Modern::Result') ? $e->dump: '"'.$e.'"';
62 exit 2;
63 }
64 }
65
66 my $exit_val = 0;
67
68 if (defined $opt->validate_schema) {
69 my $encoded_schema;
70
71 foreach my $schema_filename ($opt->validate_schema->@*) {
72 # boolean flag is passed as ''; some other value = filename
73 if (length $schema_filename) {
74 $encoded_schema = path($schema_filename)->slurp('UTF-8');
75 }
76 else {
77 say 'enter schema, followed by ^D:';
78 local $/;
79 $encoded_schema = <STDIN>;
80 say '';
81 }
82
83 my $schema = parse_input($encoded_schema);
84 die "this looks like an OpenAPI file: try 'openapi-validate' (install OpenAPI::Modern), instead\n"
85 if ref $schema eq 'HASH' and $schema->{openapi};
86
87 my $result = $js->validate_schema($schema);
88 $js->add_schema($schema) if $opt->dump_identifiers;
89
90 $exit_val = max($exit_val, $result->valid ? 0 : $result->exception ? 2 : 1);
91
92 # possible FIXME: can output_format ever be data_only here? it does not jsonify
93 say encode($opt->validate_schema->@* > 1 ? { $schema_filename => $result } : $result);
94 }
95 }
96 else {
97 my @encoded_data;
98 foreach my $data_filename (($opt->data//[''])->@*) {
99 if (length $data_filename) {
100 push @encoded_data, [ $data_filename => path($data_filename)->slurp('UTF-8') ];
101 }
102 else {
103 say 'enter data instance, followed by ^D:';
104 local $/;
105 push @encoded_data, [ '' => <STDIN> ];
106 STDIN->clearerr;
107 }
108 }
109
110 my ($schema_filename, $encoded_schema);
111 if (defined $opt->schema) {
112 $encoded_schema = path($schema_filename = $opt->schema)->slurp('UTF-8');
113 }
114 else {
115 say 'enter schema, followed by ^D:';
116 local $/;
117 $encoded_schema = <STDIN>;
118 say '';
119 }
120
121 # if there is no $id within the document, the filename will be used instead
122 my $schema = parse_input($encoded_schema);
123 die "this looks like an OpenAPI file: try 'openapi-validate' (install OpenAPI::Modern), instead\n"
124 if ref $schema eq 'HASH' and $schema->{openapi};
125 $js->add_schema(my $uri = 'file://'.$schema_filename, $schema) if length $schema_filename;
126
127 foreach my $encoded_data (@encoded_data) { # [ filename, data string ]
128 my $data = parse_input($encoded_data->[1]);
129
130 my $result = $js->evaluate($data, $uri // $schema);
131 $exit_val = max($exit_val, $result->valid ? 0 : $result->exception ? 2 : 1);
132
133 # possible FIXME: can output_format ever be data_only here? it does not jsonify
134 say encode(($opt->data//[])->@* > 1 ? { $encoded_data->[0] => $result } : $result);
135 }
136 }
137
138
139 if ($opt->dump_identifiers) {
140 my %identifiers = map +(
141 $_->[0] => {
142 canonical_uri => $_->[1]{canonical_uri},
143 document_base => $_->[1]{document}->canonical_uri,
144 document_path => $_->[1]{path},
145 }
146 ),
147 grep $_->[0] !~ m{^https?://json-schema.org/},
148 $js->_resource_pairs;
149
150 say encode({identifiers => \%identifiers });
151 }
152
153 exit $exit_val;
154
155 ### END
156
157 sub parse_input ($input) {
158 if ($input =~ /^(\{|\[\|["0-9]|true\b|false\b|null\b)/) {
159 # this looks like json
160 state $json_decoder = JSON::Schema::Modern::_JSON_BACKEND()->new->allow_nonref(1)->utf8(0);
161 return $json_decoder->decode($input);
162 }
163 else {
164 # well I suppose it must be yaml
165 require YAML::PP;
166 state $yaml_decoder = YAML::PP->new(boolean => 'JSON::PP');
167 return $yaml_decoder->load_string($input);
168 }
169 }
170
171 sub encode ($input) {
172 my $encoder = JSON::Schema::Modern::_JSON_BACKEND()->new
173 ->convert_blessed(1)
174 ->utf8(0)
175 ->canonical(1)
176 ->pretty(1)
177 ->space_before(0);
178 $encoder->indent_length(2) if $encoder->can('indent_length');
179 $encoder->encode($input);
180 }
181
182 __END__
183
184 =pod
185
186 =encoding UTF-8
187
188 =head1 NAME
189
190 json-schema-eval - A command-line interface to JSON::Schema::Modern::evaluate()
191
192 =head1 VERSION
193
194 version 0.641
195
196 =head1 SYNOPSIS
197
198 json-schema-eval --help
199
200 json-schema-eval \
201 [ --specification-version|version <version> ] \
202 [ --output-format <format> ] \
203 [ --short-circuit ] \
204 [ --max-traversal-depth <depth> ] \
205 [ --validate-formats ] \
206 [ --validate-content-schemas ] \
207 [ --collect-annotations ] \
208 [ --strict ] \
209 [ --dump-identifiers ] \
210 [ --with-defaults ] \
211 [ --validate-schema [ <filename> ] [ ... ] ] \
212 [ --add-schema [ <filename> ] [ ... ] ] \
213 [ --data [ <filename> ] [ ... ] ] \
214 [ --schema [ <filename> ] ]
215
216 =head1 DESCRIPTION
217
218 A command-line interface to L<JSON::Schema::Modern/evaluate>.
219
220 F<data.json> contains:
221
222 {"hello": 42.1}
223
224 F<schema.json> contains:
225
226 {"properties": {"hello": {"type": ["string", "integer"]}}}
227
228 Run:
229
230 json-schema-eval --data data.json --schema schema.json
231
232 produces output:
233
234 {
235 "errors" : [
236 {
237 "error" : "got number, not one of string, integer",
238 "instanceLocation" : "/hello",
239 "keywordLocation" : "/properties/hello/type"
240 },
241 {
242 "error" : "not all properties are valid",
243 "instanceLocation" : "",
244 "keywordLocation" : "/properties"
245 }
246 ],
247 "valid" : false
248 }
249
250 Or run:
251
252 json-schema-eval --validate-schema schema.json
253
254 produces output:
255
256 {
257 "valid": true
258 }
259
260 The exit value (C<$?>) is 0 when the result is valid, 1 when it is invalid,
261 and some other non-zero value if an exception occurred.
262
263 =head1 OPTIONS
264
265 =for stopwords schemas
266
267 All boolean and string options used as L<constructors to
268 JSON::Schema::Modern|JSON::Schema::Modern/CONFIGURATION OPTIONS> are available.
269
270 Additionally, C<--data> is used to provide the filename containing a JSON- or YAML-encoded data
271 instance, and C<--schema> provides the filename containing a JSON- or YAML-encoded schema.
272 You can use the C<--data> option more than once to validate multiple data instances against the same
273 schema.
274
275 If either or both of these are not provided, STDIN is used as input.
276
277 Both JSON- and YAML-encoded data and schemas are supported, using heuristics based on the
278 content of the first line of the data.
279
280 Alternatively, you can use C<--validate-schema> and either provide a filename containing a
281 JSON-encoded schema, or omit the argument to read a schema from STDIN. The schema
282 will be evaluated against its meta-schema for the corresponding specification version.
283 You can use this option more than once to validate multiple schemas in the same runtime instance.
284
285 Additional schemas, that you wish to use via the C<$ref> keyword, can be added with
286 C<< --add-schema <filename> >>. The actual filename is insignificant: Make sure you use an C<$id>
287 keyword within that schema that matches the value you use in the C<$ref>. This option can be used
288 more than once.
289
290 =for stopwords OpenAPI
291
292 =head1 AVAILABILITY
293
294 This executable is available on modern Debian versions (via C<apt-get>) as the
295 C<libjson-schema-modern-perl> package.
296
297 =head1 GIVING THANKS
298
299 =for stopwords MetaCPAN GitHub
300
301 If you found this module to be useful, please show your appreciation by
302 adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern>
303 and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>.
304
305 =head1 SUPPORT
306
307 Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>.
308
309 I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
310
311 =for stopwords OpenAPI
312
313 You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack
314 server|https://open-api.slack.com>, which are also great resources for finding help.
315
316 =head1 AUTHOR
317
318 Karen Etheridge <[email protected]>
319
320 =head1 COPYRIGHT AND LICENCE
321
322 This software is copyright (c) 2020 by Karen Etheridge.
323
324 This is free software; you can redistribute it and/or modify it under
325 the same terms as the Perl 5 programming language system itself.
326
327 Some schema files have their own licence, in share/LICENSE.
328
329 =cut