File Coverage

blib/lib/App/AppSpec/Schema/Validator.pm
Criterion Covered Total %
statement 32 38 84.2
branch 1 2 50.0
condition n/a
subroutine 8 9 88.8
pod 0 3 0.0
total 41 52 78.8


line stmt bran cond sub pod time code
1 2     2   1821 use strict;
  2         6  
  2         65  
2 2     2   14 use warnings;
  2         12  
  2         112  
3             package App::AppSpec::Schema::Validator;
4              
5             our $VERSION = '0.004'; # VERSION
6              
7 2     2   974 use App::Spec;
  2         59275  
  2         71  
8 2     2   456 use File::Share qw/ dist_file /;
  2         12650  
  2         140  
9 2     2   14 use YAML::PP;
  2         10  
  2         63  
10 2     2   21 use Moo;
  2         10  
  2         9  
11              
12             sub validate_spec_file {
13 1     1 0 504 my ($self, $file) = @_;
14 1         10 my $yp = YAML::PP->new( boolean => 'JSON::PP', schema => [qw/ JSON /] );
15 1         16012 my $spec = $yp->load_file($file);
16 1         50291 return $self->validate_spec($spec);
17             }
18              
19             sub validate_spec {
20 1     1 0 4 my ($self, $spec) = @_;
21 1 50       2 eval { require JSON::Validator }
  1         771  
22             or die "JSON::Validator is needed for validating a spec file";
23 1         230051 my $schema_file = dist_file("App-Spec", "schema.yaml");
24 1         441 my $json_validator = JSON::Validator->new;
25 1         34 my $yp = YAML::PP->new( boolean => 'JSON::PP', schema => [qw/ JSON /] );
26 1         1091 my $schema = $yp->load_file($schema_file);
27 1         119028 $json_validator->schema($schema);
28 1         13303 my @errors = $json_validator->validate($spec);
29 1         15842 return @errors;
30             }
31              
32             sub format_errors {
33 0     0 0   my ($self, $errors) = @_;
34 0           my $output = '';
35 0           for my $error (@$errors) {
36 0           $output .= "Path: " . $error->path . "\n";
37 0           $output .= " Message: " . $error->message . "\n";
38             }
39 0           return $output;
40             }
41              
42             1;