Debian Codegraph @

t/specification_version.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
14
15 use lib 't/lib';
16 use Helper;
17 use Test2::Warnings qw(warnings :no_end_test had_no_warnings);
18
19 {
20 like(
21 dies { ()= JSON::Schema::Modern->new(specification_version => 'ohhai')->evaluate(true, true) },
22 qr/^Value "ohhai" did not pass type constraint/,
23 'unrecognized specification_version',
24 );
25 }
26
27 subtest 'specification aliases' => sub {
28 is(
29 JSON::Schema::Modern->new(specification_version => '2020-12')->specification_version,
30 'draft2020-12',
31 '2020-12 is an alias for draft2020-12',
32 );
33
34 is(
35 JSON::Schema::Modern->new(specification_version => '2019-09')->specification_version,
36 'draft2019-09',
37 '2019-09 is an alias for draft2019-09',
38 );
39
40 is(
41 JSON::Schema::Modern->new(specification_version => '7')->specification_version,
42 'draft7',
43 '7 is an alias for draft7',
44 );
45 };
46
47 subtest '$ref and older specification versions' => sub {
48 is_equal(
49 JSON::Schema::Modern->new->evaluate(
50 true,
51 {
52 '$schema' => 'http://json-schema.org/draft-07/schema#',
53 '$ref' => '#/definitions/foo',
54 }
55 )->TO_JSON,
56 {
57 valid => false,
58 errors => [
59 {
60 keywordLocation => '/$schema',
61 error => '$schema and $ref cannot be used together in older drafts',
62 },
63 ],
64 },
65 '$schema and $ref cannot be used together, when $schema is too old',
66 );
67 };
68
69 subtest '<= draft7: $ref in combination with any other keyword causes the other keywords to be ignored' => sub {
70 is_equal(
71 JSON::Schema::Modern->new(
72 specification_version => 'draft7',
73 )->evaluate(
74 1,
75 {
76 allOf => [
77 true,
78 {
79 '$ref' => '#/allOf/0',
80 maximum => 0,
81 },
82 ],
83 }
84 )->TO_JSON,
85 { valid => true },
86 'keywords adjacent to $ref are not evaluated',
87 );
88 };
89
90 subtest '$ref adjacent to a path used in a $ref' => sub {
91 my $js = JSON::Schema::Modern->new(specification_version => 'draft7');
92
93 $js->add_schema('http://example.com/mydoc', {
94 '$ref' => 'http://example.com/otherdoc#/allOf/1/anyOf/1',
95 });
96
97 $js->add_schema('http://example.com/otherdoc', {
98 allOf => [
99 true,
100 {
101 anyOf => [ false, true ],
102 '$ref' => '#/allOf/0',
103 },
104 ],
105 });
106
107 is_equal(
108 $js->evaluate(true, { '$ref' => 'http://example.com/mydoc' })->TO_JSON,
109 {
110 valid => false,
111 errors => [
112 {
113 instanceLocation => '',
114 keywordLocation => '/$ref/$ref',
115 absoluteKeywordLocation => 'http://example.com/mydoc#/$ref',
116 error => 'EXCEPTION: bad reference to "http://example.com/otherdoc#/allOf/1/anyOf/1": not a schema',
117 },
118 ],
119 },
120 'the presence of $ref also blocks the use of other $refs to adjacent locations',
121 );
122 };
123
124 subtest '$defs support' => sub {
125 my $js = JSON::Schema::Modern->new(specification_version => 'draft7');
126
127 $js->add_schema('http://example.com/mydoc', {
128 allOf => [ { '$ref' => 'http://example.com/otherdoc#/$defs/foo' } ],
129 });
130
131 $js->add_schema('http://example.com/otherdoc', { '$defs' => { foo => true } });
132
133 is_equal(
134 $js->evaluate(1, 'http://example.com/mydoc')->TO_JSON,
135 {
136 valid => false,
137 errors => [
138 {
139 instanceLocation => '',
140 keywordLocation => '/allOf/0/$ref',
141 absoluteKeywordLocation => 'http://example.com/mydoc#/allOf/0/$ref',
142 error => 'EXCEPTION: bad reference to "http://example.com/otherdoc#/$defs/foo": not a schema',
143 },
144 ],
145 },
146 '$defs is not recognized in <= draft7',
147 );
148
149 is_equal(
150 JSON::Schema::Modern->new(specification_version => 'draft2019-09')
151 ->evaluate(1, {'$defs' => 1})->TO_JSON,
152 {
153 valid => false,
154 errors => [
155 {
156 keywordLocation => '/$defs',
157 error => '$defs value is not an object',
158 },
159 ],
160 },
161 '$defs is supported in > draft7',
162 );
163 };
164
165 subtest 'definitions support' => sub {
166 my $js = JSON::Schema::Modern->new(specification_version => 'draft2019-09');
167
168 $js->add_schema('http://example.com/mydoc', {
169 allOf => [ { '$ref' => 'http://example.com/otherdoc#/definitions/foo' } ],
170 });
171
172 my $schema;
173 my @warnings = warnings {
174 $js->add_schema('http://example.com/otherdoc', $schema = { definitions => 1 });
175 };
176 cmp_result(
177 \@warnings,
178 [ re(qr/^no-longer-supported "definitions" keyword present/) ],
179 'warned when using no-longer-supported keyword',
180 );
181
182 is_equal(
183 $js->evaluate(1, 'http://example.com/mydoc')->TO_JSON,
184 {
185 valid => false,
186 errors => [
187 {
188 instanceLocation => '',
189 keywordLocation => '/allOf/0/$ref',
190 absoluteKeywordLocation => 'http://example.com/mydoc#/allOf/0/$ref',
191 error => 'EXCEPTION: unable to find resource "http://example.com/otherdoc#/definitions/foo"',
192 },
193 ],
194 },
195 'definitions is not recognized in >= draft2019-09',
196 );
197
198 is_equal(
199 JSON::Schema::Modern->new(specification_version => 'draft7')->evaluate(1, $schema)->TO_JSON,
200 {
201 valid => false,
202 errors => [
203 {
204 keywordLocation => '/definitions',
205 error => 'definitions value is not an object',
206 },
207 ],
208 },
209 'definitions is supported in <= draft7',
210 );
211 };
212
213 subtest 'dependencies, dependentRequired, dependentSchemas' => sub {
214 my $js = JSON::Schema::Modern->new(specification_version => 'draft2019-09');
215 my $dependencies_schema;
216 my @warnings = warnings {
217 is_equal(
218 $js->evaluate(
219 { alpha => 1, beta => 2 },
220 $dependencies_schema = {
221 dependencies => {
222 alpha => [ qw(a b c) ],
223 beta => false,
224 },
225 }
226 )->TO_JSON,
227 { valid => true },
228 'dependencies is not recognized in >= draft2019-09',
229 );
230 };
231 cmp_result(
232 \@warnings,
233 [ re(qr/^no-longer-supported "dependencies" keyword present/) ],
234 'warned when using no-longer-supported keyword',
235 );
236
237 is_equal(
238 $js->evaluate(
239 { alpha => 1, beta => 2 },
240 my $dependentRequired_schema = {
241 dependentRequired => {
242 alpha => [ qw(a b c) ],
243 },
244 },
245 )->TO_JSON,
246 {
247 valid => false,
248 errors => [
249 {
250 instanceLocation => '',
251 keywordLocation => '/dependentRequired/alpha',
252 error => 'object is missing properties: a, b, c',
253 },
254 {
255 instanceLocation => '',
256 keywordLocation => '/dependentRequired',
257 error => 'not all dependencies are satisfied',
258 },
259 ],
260 },
261 'dependentRequired is supported in >= draft2019-09',
262 );
263
264 is_equal(
265 $js->evaluate(
266 { alpha => 1, beta => 2 },
267 my $dependentSchemas_schema = {
268 dependentSchemas => {
269 beta => false,
270 },
271 },
272 )->TO_JSON,
273 {
274 valid => false,
275 errors => [
276 {
277 instanceLocation => '',
278 keywordLocation => '/dependentSchemas/beta',
279 error => 'subschema is false',
280 },
281 {
282 instanceLocation => '',
283 keywordLocation => '/dependentSchemas',
284 error => 'not all dependencies are satisfied',
285 },
286 ],
287 },
288 'dependentSchemas is supported in >= draft2019-09',
289 );
290
291 $js = JSON::Schema::Modern->new(specification_version => 'draft7');
292 is_equal(
293 $js->evaluate(
294 { alpha => 1, beta => 2 },
295 $dependencies_schema,
296 )->TO_JSON,
297 {
298 valid => false,
299 errors => [
300 {
301 instanceLocation => '',
302 keywordLocation => '/dependencies/alpha',
303 error => 'object is missing properties: a, b, c',
304 },
305 {
306 instanceLocation => '',
307 keywordLocation => '/dependencies/beta',
308 error => 'subschema is false',
309 },
310 {
311 instanceLocation => '',
312 keywordLocation => '/dependencies',
313 error => 'not all dependencies are satisfied',
314 },
315 ],
316 },
317 'dependencies is supported in <= draft7',
318 );
319
320 is_equal(
321 $js->evaluate(
322 { alpha => 1, beta => 2 },
323 $dependentRequired_schema,
324 )->TO_JSON,
325 { valid => true },
326 'dependentRequired is not recognized in <= draft7',
327 );
328
329 is_equal(
330 $js->evaluate(
331 { alpha => 1, beta => 2 },
332 $dependentSchemas_schema,
333 )->TO_JSON,
334 { valid => true },
335 'dependentSchemas is not recognized in <= draft7',
336 );
337 };
338
339 subtest 'prefixItems, items and additionalItems' => sub {
340 my $js = JSON::Schema::Modern->new;
341 is_equal(
342 $js->evaluate(
343 [ 1, 2 ],
344 {
345 prefixItems => [ { maximum => 0 } ],
346 items => { maximum => 1 },
347 }
348 )->TO_JSON,
349 {
350 valid => false,
351 errors => [
352 {
353 instanceLocation => '/0',
354 keywordLocation => '/prefixItems/0/maximum',
355 error => 'value is greater than 0',
356 },
357 {
358 instanceLocation => '',
359 keywordLocation => '/prefixItems',
360 error => 'not all items are valid',
361 },
362 {
363 instanceLocation => '/1',
364 keywordLocation => '/items/maximum',
365 error => 'value is greater than 1',
366 },
367 {
368 instanceLocation => '',
369 keywordLocation => '/items',
370 error => 'subschema is not valid against all items',
371 },
372 ],
373 },
374 'prefixitems+items works when specification_version >= draft2020-12',
375 );
376
377 is_equal(
378 $js->evaluate(
379 [ 1 ],
380 {
381 items => [ { maximum => 0 } ],
382 },
383 )->TO_JSON,
384 {
385 valid => false,
386 errors => [
387 {
388 keywordLocation => '/items',
389 error => 'array form of "items" not supported in draft2020-12',
390 },
391 ],
392 },
393 'array form of items not supported when specification_version >= draft2020-12',
394 );
395
396 my @warnings = warnings {
397 is_equal(
398 $js->evaluate(
399 [ 1 ],
400 { additionalItems => false },
401 )->TO_JSON,
402 { valid => true },
403 'additionalitems not recognized when specification_version >= draft2020-12',
404 );
405 };
406 cmp_result(
407 \@warnings,
408 [ re(qr/^no-longer-supported "additionalItems" keyword present/) ],
409 'warned when using no-longer-supported keyword',
410 );
411
412 is_equal(
413 JSON::Schema::Modern->new(specification_version => 'draft2019-09')->evaluate(
414 [ 1 ],
415 { prefixItems => [ { maximum => 0 } ] }
416 )->TO_JSON,
417 { valid => true },
418 'prefixitems not supported when specification_version specifies other than draft2020-12',
419 );
420
421 is_equal(
422 $js->evaluate(
423 [ 1, 2, 3 ],
424 {
425 prefixItems => [ { maximum => 0 } ],
426 items => [ { maximum => 1 } ],
427 },
428 )->TO_JSON,
429 {
430 valid => false,
431 errors => [
432 {
433 keywordLocation => '/items',
434 error => "array form of \"items\" not supported in draft2020-12",
435 },
436 ],
437 },
438 'array-based items in >= draft2020-12',
439 );
440
441 @warnings = warnings {
442 is_equal(
443 $js->evaluate(
444 [ 1, 2, 3 ],
445 {
446 prefixItems => [ { maximum => 0 } ],
447 additionalItems => { maximum => 1 },
448 },
449 )->TO_JSON,
450 {
451 valid => false,
452 errors => [
453 {
454 instanceLocation => '/0',
455 keywordLocation => '/prefixItems/0/maximum',
456 error => 'value is greater than 0',
457 },
458 {
459 instanceLocation => '',
460 keywordLocation => '/prefixItems',
461 error => 'not all items are valid',
462 },
463 ],
464 },
465 'prefixItems + additionalItems',
466 );
467 };
468 cmp_result(
469 \@warnings,
470 [ re(qr/^no-longer-supported "additionalItems" keyword present/) ],
471 'warned when using no-longer-supported keyword',
472 );
473
474 is_equal(
475 $js->evaluate(
476 [ 1, 2, 3 ],
477 {
478 prefixItems => [ { maximum => 0 } ],
479 items => { maximum => 1 },
480 }
481 )->TO_JSON,
482 {
483 valid => false,
484 errors => [
485 {
486 instanceLocation => '/0',
487 keywordLocation => '/prefixItems/0/maximum',
488 error => 'value is greater than 0',
489 },
490 {
491 instanceLocation => '',
492 keywordLocation => '/prefixItems',
493 error => 'not all items are valid',
494 },
495 {
496 instanceLocation => '/1',
497 keywordLocation => '/items/maximum',
498 error => 'value is greater than 1',
499 },
500 {
501 instanceLocation => '/2',
502 keywordLocation => '/items/maximum',
503 error => 'value is greater than 1',
504 },
505 {
506 instanceLocation => '',
507 keywordLocation => '/items',
508 error => 'subschema is not valid against all items',
509 },
510 ],
511 },
512 'prefixItems + schema-based items',
513 );
514
515 @warnings = warnings {
516 is_equal(
517 $js->evaluate(
518 [ 1, 2, 3 ],
519 {
520 items => { maximum => 0 },
521 additionalItems => { maximum => 1 },
522 }
523 )->TO_JSON,
524 {
525 valid => false,
526 errors => [
527 (map +{
528 instanceLocation => '/'.$_,
529 keywordLocation => '/items/maximum',
530 error => 'value is greater than 0',
531 }, (0..2)),
532 {
533 instanceLocation => '',
534 keywordLocation => '/items',
535 error => 'subschema is not valid against all items',
536 },
537 ],
538 },
539 'schema-based items + additionalItems, failure case',
540 );
541 };
542 cmp_result(
543 \@warnings,
544 [ re(qr/^no-longer-supported "additionalItems" keyword present/) ],
545 'warned when using no-longer-supported keyword',
546 );
547
548 @warnings = warnings {
549 is_equal(
550 $js->evaluate(
551 [ 1, 2, 3 ],
552 {
553 items => { maximum => 5 },
554 additionalItems => { maximum => 0 },
555 }
556 )->TO_JSON,
557 { valid => true },
558 'schema-based items + additionalItems, passing case',
559 );
560 };
561 cmp_result(
562 \@warnings,
563 [ re(qr/^no-longer-supported "additionalItems" keyword present/) ],
564 'warned when using no-longer-supported keyword',
565 );
566 };
567
568 had_no_warnings() if $ENV{AUTHOR_TESTING};
569 done_testing;