line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Convert::Pheno::Schema; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
45
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
183
|
|
4
|
6
|
|
|
6
|
|
27
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
134
|
|
5
|
6
|
|
|
6
|
|
26
|
use autodie; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
35
|
|
6
|
6
|
|
|
6
|
|
32422
|
use feature qw(say); |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
452
|
|
7
|
6
|
|
|
6
|
|
394
|
use Moo; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
87
|
|
8
|
6
|
|
|
6
|
|
4434
|
use File::Spec::Functions qw(catfile); |
|
6
|
|
|
|
|
22
|
|
|
6
|
|
|
|
|
390
|
|
9
|
6
|
|
|
6
|
|
3421
|
use JSON::Validator; |
|
6
|
|
|
|
|
2452767
|
|
|
6
|
|
|
|
|
53
|
|
10
|
6
|
|
|
6
|
|
4800
|
use Term::ANSIColor qw(:constants); |
|
6
|
|
|
|
|
59716
|
|
|
6
|
|
|
|
|
8205
|
|
11
|
6
|
|
|
6
|
|
62
|
use Convert::Pheno::IO; |
|
6
|
|
|
|
|
15
|
|
|
6
|
|
|
|
|
3411
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Declate attribute classes |
14
|
|
|
|
|
|
|
has data => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
required => 1 |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
has debug => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
required => 1 |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
has schema_file => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
required => 1 |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# The BUILD method is called after an object is created |
28
|
|
|
|
|
|
|
sub BUILD { |
29
|
|
|
|
|
|
|
|
30
|
7
|
|
|
7
|
0
|
6425
|
my $self = shift; |
31
|
|
|
|
|
|
|
$self->{schema} = |
32
|
7
|
|
|
|
|
54
|
io_yaml_or_json( { filepath => $self->{schema_file}, mode => 'read' } ) |
33
|
|
|
|
|
|
|
; # setter |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
######################### |
37
|
|
|
|
|
|
|
######################### |
38
|
|
|
|
|
|
|
# SCHEMA VALIDATION # |
39
|
|
|
|
|
|
|
######################### |
40
|
|
|
|
|
|
|
######################### |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub json_validate { |
43
|
|
|
|
|
|
|
|
44
|
7
|
|
|
7
|
0
|
16
|
my $self = shift; |
45
|
7
|
|
|
|
|
18
|
my $data = $self->{data}; |
46
|
7
|
|
|
|
|
16
|
my $schema = $self->{schema}; |
47
|
7
|
|
|
|
|
16
|
my $debug = $self->{debug}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# DEBUG => we self-validate the schema |
50
|
7
|
100
|
|
|
|
26
|
self_validate($schema) if $debug; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Create object and load schema |
53
|
6
|
|
|
|
|
2225
|
my $jv = JSON::Validator->new; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Load schema in object |
56
|
6
|
|
|
|
|
198
|
$jv->schema($schema); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Validate data |
59
|
6
|
|
|
|
|
109626
|
my @errors = $jv->validate($data); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# Show error if any |
62
|
6
|
50
|
0
|
|
|
100947
|
say_errors( \@errors ) and die if @errors; |
63
|
6
|
|
|
|
|
185
|
return 1; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub self_validate { |
67
|
|
|
|
|
|
|
|
68
|
2
|
|
|
2
|
0
|
27
|
my $validator = JSON::Validator::Schema->new(shift); |
69
|
2
|
|
|
|
|
32784
|
my @errors = $validator->is_invalid; |
70
|
2
|
100
|
50
|
|
|
1528010
|
say BOLD RED |
71
|
|
|
|
|
|
|
"ERROR: The schema does not follow JSON Schema specification\nSee https://json-schema.org/draft/2020-12/schema" |
72
|
|
|
|
|
|
|
and die |
73
|
|
|
|
|
|
|
if $validator->is_invalid; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub say_errors { |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
0
|
0
|
|
my $errors = shift; |
79
|
0
|
0
|
|
|
|
|
if ( @{$errors} ) { |
|
0
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
say BOLD RED( join "\n", @{$errors} ), RESET; |
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#else { |
84
|
|
|
|
|
|
|
# say BOLD GREEN 'Hurray! No errors found', RESET; |
85
|
|
|
|
|
|
|
#} |
86
|
0
|
|
|
|
|
|
return 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
1; |