-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate-against-schema.pl
executable file
·82 lines (55 loc) · 1.5 KB
/
validate-against-schema.pl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
=encoding utf-8
=head1 NAME
validate-against-schema.pl -- Validates YAML files against a JSON schema in YAML format.
=head1 SYNOPSIS
validate-against-schema.pl [-v] -s=<SCHEMA> file.yml [ ... ]
=head1 OPTIONS
=over 4
=item B<< -s <SCHEMA> >>
JSON schema file (in YAML format).
=item B<-v>
Verbose output to C<STDERR>.
=item B<-?>, B<-help>, B<-h>, B<-man>
Print help and exit.
=back
=head1 SEE ALSO
JSON schema specifiation: L<https://json-schema.org/specification.html>.
=head1 AUTHOR
Frank Wiegand, L<mailto:[email protected]>, 2022.
=cut
use 5.012;
use warnings;
use Getopt::Long;
use JSON::PP;
use JSON::Schema::Modern;
use Pod::Usage;
use YAML::PP;
my ($verbose, $man, $help, $schema_file);
GetOptions(
'schema=s' => \$schema_file,
'v+' => \$verbose,
'vv' => sub { $verbose += 2 },
'help|?' => \$help,
'man|h' => \$man,
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitval => 0, -verbose => 2 ) if $man;
die '-s ... missing' unless $schema_file;
die 'no files to check' unless @ARGV;
my $ypp = YAML::PP->new(
schema => [qw(Core Merge)],
boolean => 'JSON::PP',
);
my $yaml_schema = $ypp->load_file( $schema_file );
my $js = JSON::Schema::Modern->new(
output_format => 'flag',
validate_formats => 1,
);
foreach my $file ( @ARGV ) {
my $yaml_config = $ypp->load_file( $file );
my $result = $js->evaluate( $yaml_config, $yaml_schema );
my @errors = $result->errors;
say sprintf "%s: %s", $file, $result;
}
__END__