forked from SPORE/api-description
/
validator.pl
51 lines (36 loc) · 878 Bytes
/
validator.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
#!/usr/bin/env perl
use strict;
use warnings;
use JSON;
use IO::All;
use Data::Rx;
use Getopt::Long;
use Pod::Usage;
GetOptions(
'help|?' => \my $help,
'schema=s' => \my $schema,
'description=s' => \my $desc,
) or pod2usage(1);
pod2usage(1) if $help;
pod2usage(1) unless $schema && $desc;
my $rx = Data::Rx->new;
my $schema_val = $rx->make_schema( read_from_json($schema) );
my $res = $schema_val->check(read_from_json($desc));
if ($res) {
print "ok - $desc is a valid description\n";
}
else{
print "nok - $desc is not a valid description\n";
}
sub read_from_json {
my ($file) = @_;
my $schema < io $file;
my $schema_json = JSON::decode_json($schema);
$schema_json;
}
__END__
=head1 NAME
validator - valid JSON against Rx schema
=head1 SYNOPSIS
validator.pl --schema spore_validation.rx --description api.json
=cut