Debian Codegraph @

update-schemas

raw
1 #!/usr/bin/env perl
2 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
3 use strict;
4 use warnings;
5
6 use 5.020;
7 no autovivification warn => qw(fetch store exists delete);
8 use if "$]" >= 5.022, experimental => 're_strict';
9 no if "$]" >= 5.031009, feature => 'indirect';
10 no if "$]" >= 5.033001, feature => 'multidimensional';
11 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
12 use Mojo::File 'path';
13 use Mojo::UserAgent;
14 use Digest::MD5 'md5_hex';
15 use Test::File::ShareDir -share => { -dist => { 'JSON-Schema-Modern' => 'share' } };
16 use lib 'lib';
17
18 # ATTENTION DISTRO REPACKAGERS: do NOT use fresh copies of these files
19 # from their source; it is important to include the original versions
20 # of the files as they were packaged with this cpan distribution, or
21 # surprising behaviour may occur.
22
23 my %files = (
24 'draft2020-12/meta/applicator.json' => 'https://json-schema.org/draft/2020-12/meta/applicator',
25 'draft2020-12/meta/content.json' => 'https://json-schema.org/draft/2020-12/meta/content',
26 'draft2020-12/meta/core.json' => 'https://json-schema.org/draft/2020-12/meta/core',
27 'draft2020-12/meta/format-annotation.json' => 'https://json-schema.org/draft/2020-12/meta/format-annotation',
28 'draft2020-12/meta/format-assertion.json' => 'https://json-schema.org/draft/2020-12/meta/format-assertion',
29 'draft2020-12/meta/meta-data.json' => 'https://json-schema.org/draft/2020-12/meta/meta-data',
30 'draft2020-12/meta/unevaluated.json' => 'https://json-schema.org/draft/2020-12/meta/unevaluated',
31 'draft2020-12/meta/validation.json' => 'https://json-schema.org/draft/2020-12/meta/validation',
32 'draft2020-12/output/schema.json' => 'https://json-schema.org/draft/2020-12/output/schema',
33 'draft2020-12/schema.json' => 'https://json-schema.org/draft/2020-12/schema',
34
35 'draft2019-09/meta/applicator.json' => 'https://json-schema.org/draft/2019-09/meta/applicator',
36 'draft2019-09/meta/content.json' => 'https://json-schema.org/draft/2019-09/meta/content',
37 'draft2019-09/meta/core.json' => 'https://json-schema.org/draft/2019-09/meta/core',
38 'draft2019-09/meta/format.json' => 'https://json-schema.org/draft/2019-09/meta/format',
39 'draft2019-09/meta/meta-data.json' => 'https://json-schema.org/draft/2019-09/meta/meta-data',
40 'draft2019-09/meta/validation.json' => 'https://json-schema.org/draft/2019-09/meta/validation',
41 'draft2019-09/output/schema.json' => 'https://json-schema.org/draft/2019-09/output/schema',
42 'draft2019-09/schema.json' => 'https://json-schema.org/draft/2019-09/schema',
43
44 'draft7/schema.json' => 'http://json-schema.org/draft-07/schema',
45 'draft6/schema.json' => 'http://json-schema.org/draft-06/schema',
46 'draft4/schema.json' => 'http://json-schema.org/draft-04/schema',
47
48 'LICENSE' => 'https://raw.githubusercontent.com/json-schema-org/json-schema-spec/main/LICENSE',
49 );
50
51 my $ua = Mojo::UserAgent->new(max_redirects => 3);
52 my %checksums;
53
54 foreach my $target (sort keys %files) {
55 my $uri = $files{$target};
56
57 say "# fetching $uri -> share/$target" if $ENV{DEBUG};
58 my $res = $ua->get($uri)->result;
59 die "Failed to fetch $uri", $res->code, " ", $res->message if $res->is_error;
60
61 $target = path('share', $target);
62 $target->dirname->make_path;
63 $target->spew(my $content = $res->body);
64 $checksums{$target} = md5_hex($content);
65 }
66
67 # lazy-load this to make sure we load the files we just downloaded
68 require JSON::Schema::Modern;
69
70 my $json_decoder = JSON::Schema::Modern::_JSON_BACKEND()->new->utf8(1);
71
72 # all files must be updated before any of them can be validated, since they depend on each other via
73 # the '$schema' and '$vocabulary' keywords
74 foreach my $target (sort keys %files) {
75 $target = path('share', $target);
76 next if $target->basename eq 'LICENSE';
77
78 my $schema = $json_decoder->decode($target->slurp);
79 say '# validating ', $schema->{'$id'}//$schema->{id}, ' -> ', $target if $ENV{DEBUG};
80
81 my $result = JSON::Schema::Modern::Document->validate(schema => $schema);
82 die $result->dump if not $result->valid;
83 }
84
85 my $checksums_file = path('t/checksums.t');
86 my $content = $checksums_file->slurp;
87 $content =~ m/^__DATA__$/mg;
88 $checksums_file->spew(substr($content, 0, pos($content)+1).join("\n", map $_.' '.$checksums{$_}, sort keys %checksums)."\n");