lib/ClarID/Tools/Validator.pm

filter: Statementstmt 92.0% 46 / 50 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 1 totaltotal 85.1% 63 / 74 CC 10 SCAR 12.6
CC
10
Cov
85%
_self_validate
6.0
_validate_unique_stub_codes
5.0
validate_codebook
5.0
CRAP 3.5
« bin/clarid-tools ↑ Summary lib/ClarID/Tools/Util.pm »
1 package ClarID::Tools::Validator;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use JSON::Validator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 our @EXPORT_OK = qw(validate_codebook _self_validate);
10  
11 4 ▶sub validate_codebook {
Subroutine validate_codebook: called CC=5 SCAR=16.1
Pod undocumented
12 4 my ($data, $schema, $debug) = @_;
13  
14 4 ▶ ClarID::Tools::Validator::_self_validate($schema) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
15  
16 4 my $jv = JSON::Validator->new;
17 4 $jv->schema($schema);
18  
19 4 my @errors = $jv->validate($data);
20 4 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 1 3 4
21 1 _say_errors(\@errors);
22 1 die "Codebook validation failed\n";
23 }
24  
25 # Additional stub_code uniqueness validation (for all relevant entities/categories)
26 _validate_unique_stub_codes($data, 'biosample', $_)
27 3 for qw(project species tissue sample_type assay timepoint);
28  
29 _validate_unique_stub_codes($data, 'subject', $_)
30 2 for qw(study type sex age_group);
31  
32 2 return 1;
33 }
34  
35 23 ▶sub _validate_unique_stub_codes {
Subroutine _validate_unique_stub_codes: called CC=5 SCAR=16.2
36 23 my ($data, $entity, $category) = @_;
37  
38 23 ▶ my $defs = $data->{entities}{$entity}{$category}
Branch: 1 branch
BranchTrueFalseTotal
my $defs = $data->{"entities"}{$entity}{$category} or die "Missing category '${category}' in entity '${entity}'" 23 0 23
39 or die "Missing category '$category' in entity '$entity'";
40  
41 23 my %seen;
42 23 for my $key (keys %$defs) {
43 230 my $def = $defs->{$key};
44 230 ▶ next unless defined $def->{stub_code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def->{"stub_code"} 0 230 230
45 230 my $sc = $def->{stub_code};
46  
47 230 ▶ if (exists $seen{$sc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$sc}) 1 229 230
48 die sprintf(
49 "Duplicate stub_code '%s' in category '%s' (entity: '%s') for keys '%s' and '%s'\n",
50 1 $sc, $category, $entity, $key, $seen{$sc}
51 );
52 }
53 229 $seen{$sc} = $key;
54 }
55 }
56  
57 0 ▶sub _self_validate {
Subroutine _self_validate: not called CC=2 SCAR=17.9
58 0 my ($schema) = @_;
59 0 my $validator = JSON::Validator::Schema->new($schema);
60 0 ▶ die "Invalid JSON Schema\nSee https://json-schema.org/" if $validator->is_invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $validator->is_invalid 0 0 0
61 0 print "Codebook Schema is OK\n";
62 }
63  
64 1 ▶sub _say_errors {
Subroutine _say_errors: called CC=1 SCAR=0.0
65 1 my ($errors) = @_;
66 1 print BOLD RED (join "\n", @$errors) , RESET "\n";
67 }
68  
69 6 1;
« bin/clarid-tools ↑ Summary lib/ClarID/Tools/Util.pm »