| 1 | package Dist::Conformance::Schema 0.004; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Parse YAML, JSON and INI, and validate the result against a schema | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use 5.040; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use re '/aasx'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Clone (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use Cpanel::JSON::XS(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use JSON::Validator::Schema::Troglodyne(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use YAML::XS(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 19 | our $NAME = { type => 'string', pattern => '\A[A-Za-z0-9][A-Za-z0-9._-]*\z' }; | |||||||||||||||||||||||||||
| 16 | 19 | our $PATH = { type => 'string', pattern => '\A(?!/)(?!(?:.*/)?[.][.](?:/|\z)).+\z' }; | |||||||||||||||||||||||||||
| 17 | 19 | our $STATUS = { type => 'string', enum => [qw{pass info fail}] }; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub parse_yaml ( $text, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_yaml: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 0 | my $data = eval { YAML::XS::Load($text) }; | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | die "$where does not parse as YAML: $@" if !defined $data && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $data and $@
MC/DC0%0/2
not defined $data and $@
not defined $data
$@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | die "$where does not hold a mapping at its top level\n" if ref $data ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | return $data; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub parse_json ( $bytes, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_json: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 0 | my $data = eval { Cpanel::JSON::XS->new->utf8->decode($bytes) }; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | die( "$where does not parse as JSON: " . first_line($@), "\n" ) if !defined $data && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $data and $@
MC/DC0%0/2
not defined $data and $@
not defined $data
$@
Truth table0%0/3
not(A) and B
A= not defined $dataB= $@
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | die "$where does not hold an object at its top level\n" if ref $data ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | return $data; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub encode_json ($data) { | ||||||||||||||||||||||||||
|
Subroutine
encode_json: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | return Cpanel::JSON::XS->new->utf8->pretty->space_before(0)->canonical->indent_length(2)->encode($data); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub validate ( $data, $schema, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
validate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 0 | my $copy = Clone::clone($data); | |||||||||||||||||||||||||||
| 43 | 0 | my @errors = errors( $copy, $schema ); | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | refuse( $where, @errors ) if @errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | return $copy; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub refuse ( $where, @errors ) { | ||||||||||||||||||||||||||
|
Subroutine
refuse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 0 | die join( "\n", "$where is not valid:", map { " $_" } @errors ), "\n"; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub errors ( $data, $schema ) { | ||||||||||||||||||||||||||
|
Subroutine
errors: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 0 | my $validator = JSON::Validator::Schema::Troglodyne->new; | |||||||||||||||||||||||||||
| 56 | 0 | $validator->coerce( { %{ $validator->coerce }, defaults => 1, booleans => 1 } ); | |||||||||||||||||||||||||||
| 57 | 0 | my @errors = map { "$_" } $validator->validate( $data, $schema ); | |||||||||||||||||||||||||||
| 58 | 0 | return @errors; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub matches ( $fragment, $value ) { | ||||||||||||||||||||||||||
|
Subroutine
matches: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # (?^:) gives the pattern the default flags that JSON::Validator matches it with, not the /s of this file. | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return defined $value && $value =~ m/(?^:$fragment->{pattern})/; | ||||||||||||||||||||||||||
|
Condition0%0/3
defined $value && $value =~ /(?^:$fragment->{'pattern'})/aasx
MC/DC0%0/2
defined $value && $value =~ /(?^:$fragment->{'pattern'})/aasx
defined $value
$value =~ /(?^:$fragment->{'pattern'})/aasx
Truth table0%0/3
A && B
A= defined $valueB= $value =~ /(?^:$fragment->{'pattern'})/aasx
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub relative_path ( $path, $inside ) { | ||||||||||||||||||||||||||
|
Subroutine
relative_path: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | die( ( $path // 'undef' ) . " is not a relative path inside $inside\n" ) if !matches( $PATH, $path ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$path // "undef"
MC/DC0%0/1
$path // "undef"
$path
Truth table0%0/2
A
A= $path
| |||||||||||||||||||||||||||||
| 72 | 0 | return $path; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub parse_ini ( $text, $where ) { | ||||||||||||||||||||||||||
|
Subroutine
parse_ini: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | my $data = eval { Dist::Conformance::Schema::INIReader->read_string($text) }; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | die( "$where does not parse as INI: " . first_line($@), "\n" ) if !defined $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | return $data; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub first_line ($text) { | ||||||||||||||||||||||||||
|
Subroutine
first_line: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my ($line) = split /\n/, $text // q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table0%0/2
A
A= $text
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $line //= q{}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$line //= ""
MC/DC0%0/1
$line //= ""
$line
Truth table0%0/2
A
A= $line
| |||||||||||||||||||||||||||||
| 86 | 0 | $line =~ s/\ at\ \S+\ line\ \d+[.]?\z//; | |||||||||||||||||||||||||||
| 87 | 0 | return $line; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Config::INI::Reader keeps the last value of a repeated key, so it collects each repeat into a list. | ||||||||||||||||||||||||||||
| 91 | 19 | package Dist::Conformance::Schema::INIReader 0.004 { | |||||||||||||||||||||||||||
| 92 | 19 | ▶ | use parent 'Config::INI::Reader'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub set_value ( $self, $name, $value ) { | ||||||||||||||||||||||||||
|
Subroutine
set_value: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $values = $self->{ __PACKAGE__ . '/values' }{ $self->current_section }{$name} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
MC/DC0%0/1
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name} //= []
$self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
Truth table0%0/2
A
A= $self->{"Dist::Conformance::Schema::INIReader/values"}{$self->current_section}{$name}
| |||||||||||||||||||||||||||||
| 96 | 0 | push @$values, $value; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->SUPER::set_value( $name, @$values == 1 ? $value : [@$values] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||