File Coverage

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


line stmt bran cond sub pod time code
1 2     2   811 use 5.008;
  2         4  
2 2     2   6 use strict;
  2         1  
  2         32  
3 2     2   5 use warnings;
  2         2  
  2         338  
4             package BenchmarkAnything::Schema;
5             # git description: v0.002-1-g978c3c0
6              
7             our $AUTHORITY = 'cpan:SCHWIGON';
8             # ABSTRACT: Tooling to handle the "BenchmarkAnything" schema
9             $BenchmarkAnything::Schema::VERSION = '0.004';
10              
11              
12             sub valid_json_schema {
13 14     14 1 82423 my ($data_or_json) = @_;
14              
15 14         66 require File::Slurper;
16 14         562 require File::ShareDir;
17 14         4807 require JSON::MaybeXS;
18 14         27 require JSON::Schema;
19 14         22 require Scalar::Util;
20              
21             # decode JSON unless already given a HASH or ARRAY reference
22 14         13 my $data;
23 14         31 my $ref = Scalar::Util::reftype($data_or_json);
24 14 100 66     65 if ($ref and $ref =~ /^HASH|ARRAY$/) {
25 7         8 $data = $data_or_json;
26             } else {
27 7         42 $data = JSON::MaybeXS::decode_json($data_or_json);
28             }
29              
30 14         31 my $schema_file = File::ShareDir::dist_file('BenchmarkAnything-Schema', 'benchmark-anything-schema.json');
31 14         1524 my $schema_json = File::Slurper::read_text($schema_file);
32 14         1058 my $schema = JSON::MaybeXS::decode_json($schema_json);
33 14         63 my $validator = JSON::Schema->new($schema);
34 14         123 my $result = $validator->validate($data);
35              
36 14         10685 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) 2016 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