| 1 | package Convert::Pheno::Utils::Schema; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 138 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 138 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 138 | ▶ | use autodie; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 138 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 138 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 138 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 138 | ▶ | use JSON::Validator; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 138 | ▶ | use Term::ANSIColor qw(:constants); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 138 | ▶ | use Convert::Pheno::IO::FileIO; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | # Declate attribute classes | ||||||||||||||||||||||||||||||||
| 14 | 138 | has data => ( | |||||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 16 | required => 1 | ||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||
| 18 | 138 | has debug => ( | |||||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 20 | required => 1 | ||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||
| 22 | 138 | has schema_file => ( | |||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 24 | required => 1 | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | # The BUILD method is called after an object is created | ||||||||||||||||||||||||||||||||
| 28 | 63 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 29 | 63 | my $self = shift; | |||||||||||||||||||||||||||||||
| 30 | $self->{schema} = | ||||||||||||||||||||||||||||||||
| 31 | 63 | io_yaml_or_json( { filepath => $self->{schema_file}, mode => 'read' } ) | |||||||||||||||||||||||||||||||
| 32 | ; # setter | ||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | ######################### | ||||||||||||||||||||||||||||||||
| 36 | ######################### | ||||||||||||||||||||||||||||||||
| 37 | # SCHEMA VALIDATION # | ||||||||||||||||||||||||||||||||
| 38 | ######################### | ||||||||||||||||||||||||||||||||
| 39 | ######################### | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 62 | ▶ | sub json_validate { | ||||||||||||||||||||||||||||||
|
Subroutine
json_validate: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 42 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||
| 43 | 62 | my $data = $self->{data}; | |||||||||||||||||||||||||||||||
| 44 | 62 | my $schema = $self->{schema}; | |||||||||||||||||||||||||||||||
| 45 | 62 | my $debug = $self->{debug}; | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | # DEBUG => we self-validate the schema | ||||||||||||||||||||||||||||||||
| 48 | 62 | ▶ | self_validate($schema) if $debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | # Create object and load schema | ||||||||||||||||||||||||||||||||
| 51 | 62 | my $jv = JSON::Validator->new; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # Load schema in object | ||||||||||||||||||||||||||||||||
| 54 | 62 | $jv->schema($schema); | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | # Validate data | ||||||||||||||||||||||||||||||||
| 57 | 62 | my @errors = $jv->validate($data); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # Show error if any | ||||||||||||||||||||||||||||||||
| 60 | 62 | ▶ | say_errors( \@errors ) and die if @errors; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
@errors && (say_errors(\@errors) && die())
say_errors(\@errors) and die
MC/DC0%0/2
@errors && (say_errors(\@errors) && die())
@errors
say_errors(\@errors)
Truth table33%1/3
A && B
A= @errorsB= say_errors(\@errors)
| |||||||||||||||||||||||||||||||||
| 61 | 56 | return 1; | |||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | sub self_validate { | ||||||||||||||||||||||||||||||
|
Subroutine
self_validate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 65 | 0 | my $validator = JSON::Validator::Schema->new(shift); | |||||||||||||||||||||||||||||||
| 66 | 0 | my @errors = $validator->is_invalid; | |||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | die | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | "The schema does not follow JSON Schema specification\nSee https://json-schema.org/draft/2020-12/schema\n" | ||||||||||||||||||||||||||||||||
| 69 | if $validator->is_invalid; | ||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | sub say_errors { | ||||||||||||||||||||||||||||||
|
Subroutine
say_errors: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 73 | 6 | my $errors = shift; | |||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | if ( @{$errors} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 6 | say BOLD RED( join "\n", @{$errors} ), RESET; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | #else { | ||||||||||||||||||||||||||||||||
| 79 | # say BOLD GREEN 'Hurray! No errors found', RESET; | ||||||||||||||||||||||||||||||||
| 80 | #} | ||||||||||||||||||||||||||||||||
| 81 | 6 | return 1; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | 138 | 1; | |||||||||||||||||||||||||||||||