Validator/Util.pm

filter: Statementstmt 95.3% 164 / 172 Branchbran 91.8% 90 / 98 Conditioncond 84.4% 49 / 58 MC/DCmcdc 70.9% 22 / 31 Subroutinesub 91.6% 33 / 36 Podpod 90.9% 10 / 11 totaltotal 90.6% 368 / 406 CC 78 SCAR 24.5
CC
78
Cov
92%
schema_type
27.1
data_type
14.0
negotiate_content_type
13.0
CRAP 11.6
« Validator/Schema.pm ↑ Summary Validator/Schema/OpenAPIv3.pm »
1 package JSON::Validator::Util;
2 85 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 82 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use JSON::Validator::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 82 ▶use Mojo::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 82 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 82 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 82 ▶use constant SEREAL_SUPPORT => !$ENV{JSON_VALIDATOR_NO_SEREAL} && eval 'use Sereal::Encoder 4.00;1';
Subroutine BEGIN: called CC=2 SCAR=7.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Condition33%1/3
!$ENV{'JSON_VALIDATOR_NO_SEREAL'} && eval 'use Sereal::Encoder 4.00;1'
!ll&&!rl&&r
0082
MC/DC0%0/2
!$ENV{'JSON_VALIDATOR_NO_SEREAL'} && eval 'use Sereal::Encoder 4.00;1'
!$ENV{'JSON_VALIDATOR_NO_SEREAL'} eval 'use Sereal::Encoder 4.00;1'
Truth table33%1/3
not(A) && B
A= !$ENV{'JSON_VALIDATOR_NO_SEREAL'}B= eval 'use Sereal::Encoder 4.00;1'
ABresult
0X0
100
111
16 82 ▶use constant CORE_BOOL => defined &builtin::is_bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 82 our @EXPORT_OK = (
19 qw(E data_checksum data_section data_type is_bool is_num is_type),
20 qw(negotiate_content_type prefix_errors schema_type str2data),
21 );
22  
23 5200 ▶sub E { JSON::Validator::Error->new(@_) }
Subroutine E: called CC=1 SCAR=0.0
Pod undocumented
24  
25 82 my $serializer = SEREAL_SUPPORT ? \&_sereal_encode : \&_yaml_dump;
26  
27 14931 ▶sub data_checksum {
Subroutine data_checksum: called CC=3 SCAR=11.0
Pod documented
28 14931 ▶ return Mojo::Util::md5_sum(ref $_[0] ? $serializer->($_[0]) : defined $_[0] ? do { utf8::encode(my $x = shift); qq('$x') } : 'undef');
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] ? : 3143 11788 14931
defined $_[0] ? : 11770 18 11788
29 }
30  
31 45 ▶sub data_section {
Subroutine data_section: called CC=11 SCAR=24.9
Pod documented
32 45 my ($class, $file, $params) = @_;
33 45 state $skip_re = qr{(^JSON::Validator|^Mojo::Base$|^Mojolicious$|\w+::_Dynamic)};
34  
35 45 ▶ my @classes = $class ? ([$class]) : ();
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 41 4 45
36 45 ▶ unless (@classes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@classes) 4 41 45
37 4 my $i = 0;
38 4 ▶ while ($class = caller($i++)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class = caller $i++) 26 4 30
39 26 ▶ push @classes, [$class] unless $class =~ $skip_re;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /$skip_re/u 10 16 26
40 }
41 }
42  
43 45 for my $group (@classes) {
44 82 ▶ push @$group, grep { !/$skip_re/ } do { no strict 'refs'; @{"$group->[0]\::ISA"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
45 48 for my $class (@$group) {
46 49 ▶ next unless my $text = Mojo::Loader::data_section($class, $file);
Branch: 1 branch
BranchTrueFalseTotal
unless my $text = Mojo::Loader::data_section($class, $file) 5 44 49
47 44 ▶ return Mojo::Util::encode($params->{encoding}, $text) if $params->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"encoding"} 44 0 44
48 0 return $text;
49 }
50 }
51  
52 1 ▶ return undef unless $params->{confess};
Branch: 1 branch
BranchTrueFalseTotal
unless $params->{"confess"} 1 0 1
53  
54 0 ▶ my $err = Mojo::JSON::encode_json([map { @$_ == 1 ? $_->[0] : $_ } @classes]);
Branch: 1 branch
BranchTrueFalseTotal
@$_ == 1 ? : 0 0 0
55 0 Carp::confess(qq(Could not find "$file" in __DATA__ section of $err.));
56 }
57  
58 7978 ▶sub data_type {
Subroutine data_type: called CC=14 SCAR=26.4
Pod documented
59 7978 my $ref = ref $_[0];
60 7978 my $blessed = blessed $_[0];
61 7978 ▶ return 'object' if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 7368 610 7978
62 610 ▶ return 'file' if $ref eq 'Mojo::Upload';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "Mojo::Upload" 1 609 610
63 609 ▶ return lc $ref if $ref and !$blessed;
Branch: 1 branch
BranchTrueFalseTotal
if $ref and not $blessed 231 378 609
Condition100%3/3
$ref and not $blessed
!ll&&!rl&&r
36513231
MC/DC100%2/2
$ref and not $blessed
$ref not $blessed
Truth table100%3/3
A and not(B)
A= $refB= not $blessed
ABresult
0X0
100
111
64 378 ▶ return 'null' if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 6 372 378
65 372 ▶ return 'boolean' if $blessed and ("$_[0]" eq "1" or !"$_[0]");
Branch: 1 branch
BranchTrueFalseTotal
if $blessed and "$_[0]" eq 1 || !"$_[0]" 10 362 372
Condition100%6/6
$blessed and "$_[0]" eq 1 || !"$_[0]"
!ll&&!rl&&r
359310
"$_[0]" eq 1 or not "$_[0]"
l!l&&r!l&&!r
733
MC/DC100%3/3
$blessed and "$_[0]" eq 1 || !"$_[0]"
$blessed "$_[0]" eq 1 not "$_[0]"
Truth table100%4/4
A and B or not(C)
A= $blessedB= "$_[0]" eq 1C= not "$_[0]"
ABCresult
0XX0
1000
11X1
1011
66  
67 362 ▶ if (is_num($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_num($_[0])) 81 281 362
68 81 ▶ return 'integer' if grep { ($_->{type} // '') eq 'integer' } @{$_[1] || []};
Branch: 1 branch
BranchTrueFalseTotal
if grep {($_->{"type"} // "") eq "integer";} @{$_[1] or [];} 1 80 81
Condition75%3/4
$_->{"type"} // ""
l!l
400
$_[1] || []
l!l
4041
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC100%1/1
$_[1] || []
$_[1]
Truth table50%1/2
A
A= $_->{"type"}
Aresult
11
00
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
69 80 return 'number';
70 }
71  
72 281 ▶ return $blessed || 'string';
Condition100%2/2
$blessed || 'string'
l!l
3278
MC/DC100%1/1
$blessed || 'string'
$blessed
Truth table100%2/2
A
A= $blessed
Aresult
11
00
73 }
74  
75 136946 ▶sub is_bool {
Subroutine is_bool: called CC=5 SCAR=16.2
Pod documented
76 136946 if (CORE_BOOL) {
77 82 ▶ BEGIN { warnings->unimport('experimental::builtin') if CORE_BOOL }
Subroutine BEGIN: called CC=1 SCAR=0.0
78 136946 ▶ return !!1 if builtin::is_bool $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_bool($_[0]) 0 136946 136946
79 }
80 136946 ▶ blessed $_[0] && ($_[0]->isa('JSON::PP::Boolean') || "$_[0]" eq "1" || !$_[0]);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("JSON::PP::Boolean") || "$_[0]" eq 1 || !$_[0] 7337 129609 136946
Condition88%8/9
builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || "$_[0]" eq '1' || !$_[0])
!ll&&!rl&&r
12960997328
$_[0]->isa("JSON::PP::Boolean") or "$_[0]" eq 1 or not $_[0]
l!l&&r!l&&!r
732809
$_[0]->isa("JSON::PP::Boolean") or "$_[0]" eq 1
l!l&&r!l&&!r
732629
MC/DC75%3/4
builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || "$_[0]" eq '1' || !$_[0])
builtin::blessed($_[0]) $_[0]->isa("JSON::PP::Boolean") "$_[0]" eq 1 not $_[0]
Truth table80%4/5
A && B or C or not(D)
A= builtin::blessed($_[0])B= $_[0]->isa("JSON::PP::Boolean")C= "$_[0]" eq 1D= not $_[0]
ABCDresult
0XXX0
10000
11XX1
101X1
10011
81 }
82 1055 ▶sub is_num { B::svref_2object(\$_[0])->FLAGS & (B::SVp_IOK | B::SVp_NOK) && 0 + $_[0] eq $_[0] && $_[0] * 0 == 0 }
Subroutine is_num: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
B::svref_2object(\$_[0])->FLAGS & 12288 and 0 + $_[0] eq $_[0] and $_[0] * 0 == 0 291 764 1055
Condition66%2/3
B::svref_2object(\$_[0])->FLAGS & 12288 and 0 + $_[0] eq $_[0]
!ll&&!rl&&r
7640291
MC/DC50%1/2
B::svref_2object(\$_[0])->FLAGS & 12288 and 0 + $_[0] eq $_[0]
B::svref_2object(\$_[0])->FLAGS & 12288 0 + $_[0] eq $_[0]
Truth table66%2/3
A and B
A= B::svref_2object(\$_[0])->FLAGS & 12288B= 0 + $_[0] eq $_[0]
ABresult
0X0
100
111
83 786270 ▶sub is_type { blessed $_[0] ? $_[0]->isa($_[1]) : ref $_[0] eq $_[1] }
Subroutine is_type: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 54546 731724 786270
84  
85 32 ▶sub negotiate_content_type {
Subroutine negotiate_content_type: called CC=13 SCAR=25.7
Pod documented
86 32 my ($accepts, $header) = @_;
87 32 ▶ return '' unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 2 30 32
88  
89 my %header_map = map {
90 30 ▶ /^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/i ? (lc $1, $2 // -3)
Branch: 2 branches
BranchTrueFalseTotal
/^\s*([^,; ]+)(?:\s*\;\s*q\s*=\s*(\d+(?:\.\d+)?))?\s*$/iu ? : 26 8 34
/^\s*([^,; ]+)(?:\s*\;\s*\w+\s*=\S+)?\s*$/iu ? : 7 1 8
Condition100%2/2
$2 // -3
l!l
224
MC/DC100%1/1
$2 // -3
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
91 : /^\s*([^,; ]+)(?:\s*\;\s*\w+\s*=\S+)?\s*$/i ? (lc $1, -1)
92 : (lc $_, -2);
93 } split /,/, $header;
94 30 my @headers = sort { $header_map{$b} <=> $header_map{$a} } sort keys %header_map;
95  
96 # Check for exact match
97 30 for my $ct (@$accepts) {
98 37 ▶ return $ct if exists $header_map{$ct};
Branch: 1 branch
BranchTrueFalseTotal
if exists $header_map{$ct} 14 23 37
99 }
100  
101 # Check for closest match
102 16 for my $re (map { my $re = "$_"; $re =~ s!\*!.*!g; $re = qr{$re}; [$_, $re] } grep {/\*/} @$accepts) {
103 5 for my $ct (@headers) {
104 5 ▶ return $re->[0] if $ct =~ $re->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ /$re->[1]/u 5 0 5
105 }
106 }
107 11 for my $re (map { local $_ = "$_"; s!\*!.*!g; qr{$_} } grep {/\*/} @headers) {
108 4 for my $ct (@$accepts) {
109 4 ▶ return $ct if $ct =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ /$re/u 4 0 4
110 }
111 }
112  
113 # Could not find any valid content type
114 7 return '';
115 }
116  
117 60 ▶sub prefix_errors {
Subroutine prefix_errors: called CC=2 SCAR=6.9
Pod documented
118 60 my ($type, @errors_with_index) = @_;
119 60 my @errors;
120  
121 60 for my $e (@errors_with_index) {
122 106 my $index = shift @$e;
123 push @errors, map {
124 106 my $msg = sprintf '/%s/%s %s', $type, $index, $_->message;
125 117 $msg =~ s!(\d+)\s/!$1/!g;
126 117 E +{%$_, message => $msg}; # preserve 'details', for later introspection
127 } @$e;
128 }
129  
130 60 return @errors;
131 }
132  
133 42781 ▶sub schema_type {
Subroutine schema_type: called CC=27 SCAR=33.0
Pod documented
134 42781 ▶ return '' if ref $_[0] ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "HASH" 68 42713 42781
135 42713 ▶ return $_[0]->{type} if $_[0]->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"type"} 379 42334 42713
136 42334 ▶ return _guessed_right(object => $_[1]) if $_[0]->{additionalProperties};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"additionalProperties"} 8 42326 42334
137 42326 ▶ return _guessed_right(object => $_[1]) if $_[0]->{patternProperties};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"patternProperties"} 40 42286 42326
138 42286 ▶ return _guessed_right(object => $_[1]) if $_[0]->{properties};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"properties"} 4596 37690 42286
139 37690 ▶ return _guessed_right(object => $_[1]) if exists $_[0]->{propertyNames};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"propertyNames"} 8 37682 37690
140 37682 ▶ return _guessed_right(object => $_[1]) if $_[0]->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"required"} 2656 35026 37682
141 return _guessed_right(object => $_[1])
142 if $_[0]->{dependencies}
143 or $_[0]->{dependentSchemas}
144 35026 ▶ or $_[0]->{dependentRequired};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"dependencies"} or $_[0]{"dependentSchemas"} or $_[0]{"dependentRequired"} 1 35025 35026
Condition66%4/6
$_[0]{"dependencies"} or $_[0]{"dependentSchemas"} or $_[0]{"dependentRequired"}
l!l&&r!l&&!r
1035025
$_[0]{"dependencies"} or $_[0]{"dependentSchemas"}
l!l&&r!l&&!r
1035025
MC/DC33%1/3
$_[0]{"dependencies"} or $_[0]{"dependentSchemas"} or $_[0]{"dependentRequired"}
$_[0]{"dependencies"} $_[0]{"dependentSchemas"} $_[0]{"dependentRequired"}
Truth table50%2/4
A or B or C
A= $_[0]{"dependencies"}B= $_[0]{"dependentSchemas"}C= $_[0]{"dependentRequired"}
ABCresult
1XX1
01X1
0011
0000
145 35025 ▶ return _guessed_right(object => $_[1]) if defined $_[0]->{maxProperties} or defined $_[0]->{minProperties};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"maxProperties"} or defined $_[0]{"minProperties"} 20 35005 35025
Condition100%3/3
defined $_[0]{"maxProperties"} or defined $_[0]{"minProperties"}
l!l&&r!l&&!r
16435005
MC/DC100%2/2
defined $_[0]{"maxProperties"} or defined $_[0]{"minProperties"}
defined $_[0]{"maxProperties"} defined $_[0]{"minProperties"}
Truth table100%3/3
A or B
A= defined $_[0]{"maxProperties"}B= defined $_[0]{"minProperties"}
ABresult
1X1
011
000
146  
147 # additionalItems is intentionally omitted - it requires 'items' to take effect
148 35005 ▶ return _guessed_right(array => $_[1]) if exists $_[0]->{items};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"items"} 9 34996 35005
149 34996 ▶ return _guessed_right(array => $_[1]) if $_[0]->{uniqueItems};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"uniqueItems"} 0 34996 34996
150 34996 ▶ return _guessed_right(array => $_[1]) if exists $_[0]->{contains};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"contains"} 3 34993 34996
151 34993 ▶ return _guessed_right(array => $_[1]) if exists $_[0]->{maxItems} or exists $_[0]->{minItems};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"maxItems"} or exists $_[0]{"minItems"} 3 34990 34993
Condition66%2/3
exists $_[0]{"maxItems"} or exists $_[0]{"minItems"}
l!l&&r!l&&!r
3034990
MC/DC50%1/2
exists $_[0]{"maxItems"} or exists $_[0]{"minItems"}
exists $_[0]{"maxItems"} exists $_[0]{"minItems"}
Truth table66%2/3
A or B
A= exists $_[0]{"maxItems"}B= exists $_[0]{"minItems"}
ABresult
1X1
011
000
152 34990 ▶ return _guessed_right(string => $_[1]) if $_[0]->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"pattern"} 26 34964 34990
153 34964 ▶ return _guessed_right(string => $_[1]) if exists $_[0]->{maxLength} or defined $_[0]->{minLength};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"maxLength"} or defined $_[0]{"minLength"} 19 34945 34964
Condition100%3/3
exists $_[0]{"maxLength"} or defined $_[0]{"minLength"}
l!l&&r!l&&!r
18134945
MC/DC100%2/2
exists $_[0]{"maxLength"} or defined $_[0]{"minLength"}
exists $_[0]{"maxLength"} defined $_[0]{"minLength"}
Truth table100%3/3
A or B
A= exists $_[0]{"maxLength"}B= defined $_[0]{"minLength"}
ABresult
1X1
011
000
154 34945 ▶ return _guessed_right(number => $_[1]) if $_[0]->{multipleOf};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"multipleOf"} 3 34942 34945
155 return _guessed_right(number => $_[1])
156 if defined $_[0]->{maximum}
157 or defined $_[0]->{minimum}
158 or defined $_[0]->{exclusiveMaximum}
159 34942 ▶ or defined $_[0]->{exclusiveMinimum};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"} or defined $_[0]{"exclusiveMinimum"} 37 34905 34942
Condition100%9/9
defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"} or defined $_[0]{"exclusiveMinimum"}
l!l&&r!l&&!r
33434905
defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"}
l!l&&r!l&&!r
29434909
defined $_[0]{"maximum"} or defined $_[0]{"minimum"}
l!l&&r!l&&!r
151434913
MC/DC100%4/4
defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"} or defined $_[0]{"exclusiveMinimum"}
defined $_[0]{"maximum"} defined $_[0]{"minimum"} defined $_[0]{"exclusiveMaximum"} defined $_[0]{"exclusiveMinimum"}
Truth table100%5/5
A or B or C or D
A= defined $_[0]{"maximum"}B= defined $_[0]{"minimum"}C= defined $_[0]{"exclusiveMaximum"}D= defined $_[0]{"exclusiveMinimum"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
160 34905 ▶ return 'const' if exists $_[0]->{const};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"const"} 2522 32383 34905
161 32383 return '';
162 }
163  
164 654 ▶sub str2data {
Subroutine str2data: called CC=3 SCAR=11.0
Pod documented
165 654 my $data;
166 654 ▶ eval { $data = $_[0] =~ m!^\s*\{!s ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]); 1 } // Carp::confess($@);
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\s*\{/su ? : 642 12 654
Condition50%1/2
eval { do { $data = $_[0] =~ /^\s*\{/su ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]); 1 } } // Carp::confess($@)
l!l
6540
MC/DC0%0/1
eval { do { $data = $_[0] =~ /^\s*\{/su ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]); 1 } } // Carp::confess($@)
eval { do { $data = $_[0] =~ /^\s*\{/su ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]); 1 } }
Truth table50%1/2
A
A= eval { do { $data = $_[0] =~ /^\s*\{/su ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]); 1 } }
Aresult
11
00
167 654 return $data;
168 }
169  
170 # _guessed_right($type, $data);
171 7429 ▶sub _guessed_right {
Subroutine _guessed_right: called CC=3 SCAR=11.0
172 7429 ▶ return $_[0] if !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 5 7424 7429
173 7424 ▶ return $_[0] if $_[0] eq data_type $_[1], [{type => $_[0]}];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq data_type($_[1], [{"type", $_[0]}]) 7422 2 7424
174 2 return '';
175 }
176  
177 3143 ▶sub _sereal_encode {
Subroutine _sereal_encode: called CC=1 SCAR=0.0
178 3143 state $s = Sereal::Encoder->new({canonical => 1});
179 3143 return $s->encode($_[0]);
180 }
181  
182 82 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
183 82 ▶ if (eval 'use YAML::XS 0.67;1') {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'use YAML::XS 0.67;1') { } 81 1 82
184 0 ▶ *_yaml_dump = sub { local $YAML::XS::Boolean = 'JSON::PP'; YAML::XS::Dump(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
185 81 ▶ *_yaml_load = sub { local $YAML::XS::Boolean = 'JSON::PP'; YAML::XS::Load(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
186 }
187 else {
188 1 require YAML::PP;
189 1 my $pp = YAML::PP->new(boolean => 'JSON::PP');
190 0 ▶ *_yaml_dump = sub { $pp->dump_string(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
191 0 ▶ *_yaml_load = sub { $pp->load_string(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
192 }
193 }
194  
195 82 1;
196  
197 =encoding utf8
198  
199 =head1 NAME
200  
201 JSON::Validator::Util - Utility functions for JSON::Validator
202  
203 =head1 DESCRIPTION
204  
205 L<JSON::Validator::Util> is a package containing utility functions for
206 L<JSON::Validator>. Each of the L</FUNCTIONS> can be imported.
207  
208 =head1 FUNCTIONS
209  
210 =head2 data_checksum
211  
212 $str = data_checksum $any;
213  
214 Will create a checksum for any data structure stored in C<$any>.
215  
216 =head2 data_section
217  
218 $str = data_section "Some::Module", "file.json";
219 $str = data_section "Some::Module", "file.json", {encode => 'UTF-8'};
220  
221 Same as L<Mojo::Loader/data_section>, but will also look up the file in any
222 inherited class.
223  
224 =head2 data_type
225  
226 $str = data_type $any;
227 $str = data_type $any, [@schemas];
228 $str = data_type $any, [{type => "integer", ...}];
229  
230 Returns the JSON type for C<$any>. C<$str> can be array, boolean, integer,
231 null, number object or string. Note that a list of schemas need to be provided
232 to differentiate between "integer" and "number".
233  
234 =head2 is_bool
235  
236 $bool = is_bool $any;
237  
238 Checks if C<$any> looks like a boolean.
239  
240 =head2 is_num
241  
242 $bool = is_num $any;
243  
244 Checks if C<$any> looks like a number.
245  
246 =head2 is_type
247  
248 $bool = is_type $any, $class;
249 $bool = is_type $any, $type;
250  
251 Checks if C<$any> is a, or inherits from, C<$class> or C<$type>.
252  
253 =head2 negotiate_content_type
254  
255 $content_type = negotiate_content_type($header, \@content_types);
256  
257 This method can take a "Content-Type" or "Accept" header and find the closest
258 matching content type in C<@content_types>. C<@content_types> can contain
259 wildcards, meaning "*/*" will match anything.
260  
261 =head2 prefix_errors
262  
263 @errors = prefix_errors $prefix, @errors;
264  
265 Consider this internal for now.
266  
267 =head2 schema_type
268  
269 $str = schema_type $hash_ref;
270 $str = schema_type $hash_ref, $any;
271  
272 Looks at C<$hash_ref> and tries to figure out what kind of type the schema
273 represents. C<$str> can be "array", "const", "number", "object", "string", or
274 fallback to empty string if the correct type could not be figured out.
275  
276 C<$any> can be provided to double check the type, so if C<$hash_ref> describes
277 an "object", but C<$any> is an array-ref, then C<$str> will become an empty
278 string. Example:
279  
280 # $str = "";
281 $str = schema {additionalProperties => false}, [];
282  
283 # $str = "object"
284 $str = schema {additionalProperties => false};
285 $str = schema {additionalProperties => false}, {};
286  
287 Note that this process is relatively slow, so it will make your validation
288 faster if you specify "type". Both of the two below is valid, but the one with
289 "type" will be faster.
290  
291 {"type": "object", "properties": {}} # Faster
292 {"properties": {}} # Slower
293  
294 =head2 str2data
295  
296 $any = str2data $str;
297  
298 Will try to parse C<$str> as JSON or YAML, and return a data structure.
299  
300 =head1 SEE ALSO
301  
302 L<JSON::Validator>.
303  
304 =cut
« Validator/Schema.pm ↑ Summary Validator/Schema/OpenAPIv3.pm »