-
Notifications
You must be signed in to change notification settings - Fork 3
/
update-schemas
56 lines (44 loc) · 1.93 KB
/
update-schemas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env perl
use strict;
use warnings;
use 5.020;
no autovivification warn => qw(fetch store exists delete);
use if "$]" >= 5.022, experimental => 're_strict';
no if "$]" >= 5.031009, feature => 'indirect';
no if "$]" >= 5.033001, feature => 'multidimensional';
no if "$]" >= 5.033006, feature => 'bareword_filehandles';
use Path::Tiny;
use HTTP::Tiny;
use YAML::PP;
use JSON::Schema::Modern;
my %files = (
# metaschema for json schemas contained within openapi documents
# TODO: switch to /latest, when it is available
'oas/dialect/base.schema.json' => 'https://spec.openapis.org/oas/3.1/dialect/base',
# vocabulary definition
# TODO: switch to /latest, when it is available
'oas/meta/base.schema.json' => 'https://spec.openapis.org/oas/3.1/meta/base',
# openapi document schema + custom json schema dialect
'oas/schema-base.json' => 'https://spec.openapis.org/oas/3.1/schema-base/2022-10-07',
# the main openapi document schema
'oas/schema.json' => 'https://spec.openapis.org/oas/3.1/schema/2022-10-07',
'oas/LICENSE' => 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/LICENSE',
);
foreach my $target (keys %files) {
my $source = $files{$target};
$target = path('share', $target);
$target->parent->mkpath;
my $response = HTTP::Tiny->new->get($source);
die "Failed to fetch $source: $response->{status} $response->{reason}" if not $response->{success};
$target->spew($response->{content});
}
my $yaml = YAML::PP->new(boolean => 'JSON::PP');
my $js = JSON::Schema::Modern->new(validate_formats => 1);
$js->add_schema($files{$_} => $yaml->load_file('share/'.$_)) foreach grep /json$/, keys %files;
foreach my $uri (values %files) {
next if $uri =~ /LICENSE$/;
print "# validating $uri\n" if $ENV{DEBUG};
my $document = $js->_fetch_from_uri($uri)->{document};
my $result = $js->evaluate($document->schema, $document->metaschema_uri);
die $js->_json_decoder->pretty->encode($result) if not $result;
}