File Coverage

blib/lib/BenchmarkAnything/Schema.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 33 34 97.0


line stmt bran cond sub pod time code
1 2     2   1080 use strict;
  2         2  
  2         47  
2 2     2   9 use warnings;
  2         3  
  2         90  
3             package BenchmarkAnything::Schema;
4             # git description: 985d419
5              
6             BEGIN {
7 2     2   400 $BenchmarkAnything::Schema::AUTHORITY = 'cpan:SCHWIGON';
8             }
9             # ABSTRACT: Tooling to handle the "BenchmarkAnything" schema
10             $BenchmarkAnything::Schema::VERSION = '0.001';
11              
12              
13             sub valid_json_schema {
14 18     18 1 6401525 my ($data_or_json) = @_;
15              
16 18         295 require File::Slurp;
17 18         1708 require File::ShareDir;
18 18         400080 require JSON::MaybeXS;
19 18         97 require JSON::Schema;
20 18         91 require Scalar::Util;
21              
22             # decode JSON unless already given a HASH or ARRAY reference
23 18         39 my $data;
24 18         109 my $ref = Scalar::Util::reftype($data_or_json);
25 18 100 66     187 if ($ref and $ref =~ /^HASH|ARRAY$/) {
26 9         17 $data = $data_or_json;
27             } else {
28 9         160 $data = JSON::MaybeXS::decode_json($data_or_json);
29             }
30              
31 18         145 my $schema_file = File::ShareDir::dist_file('BenchmarkAnything-Schema', 'benchmark-anything-schema.json');
32 18         6550 my $schema_json = File::Slurp::read_file($schema_file);
33 18         3237 my $schema = JSON::MaybeXS::decode_json($schema_json);
34 18         207 my $validator = JSON::Schema->new($schema);
35 18         353 my $result = $validator->validate($data);
36              
37 18         46733 return $result;
38             }
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding utf-8
47              
48             =head1 NAME
49              
50             BenchmarkAnything::Schema - Tooling to handle the "BenchmarkAnything" schema
51              
52             =head1 SYNOPSIS
53              
54             require BenchmarkAnything::Schema;
55              
56             if (my $result = BenchmarkAnything::Schema::valid_json_schema($input_data__or__raw_json_text))
57             {
58             print "Data structure is valid BenchmarkAnything data.\n";
59             } else {
60             print STDERR "JSON schema errors:\n";
61             print STDERR " - $_\n" foreach $result->errors;
62             }
63              
64             =head2 valid_json_schema($data_or_json)
65              
66             Validate if $data_or_json conforms to the BenchmarkAnything schema.
67              
68             Returns a L<JSON::Schema::Result|JSON::Schema::Result> object which is
69             overloaded to behave and stringify sensibly, and also provides error
70             details.
71              
72             =head1 AUTHOR
73              
74             Steffen Schwigon <ss5@renormalist.net>
75              
76             =head1 COPYRIGHT AND LICENSE
77              
78             This software is copyright (c) 2015 by Steffen Schwigon.
79              
80             This is free software; you can redistribute it and/or modify it under
81             the same terms as the Perl 5 programming language system itself.
82              
83             =cut