File Coverage

blib/lib/BenchmarkAnything/Schema.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 5 5 100.0
pod 1 1 100.0
total 36 37 97.3


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