| 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'
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'
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 45 | ▶ | unless (@classes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | while ($class = caller($i++)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 26 | ▶ | push @classes, [$class] unless $class =~ $skip_re; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 44 | ▶ | return Mojo::Util::encode($params->{encoding}, $text) if $params->{encoding}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | ▶ | return undef unless $params->{confess}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $err = Mojo::JSON::encode_json([map { @$_ == 1 ? $_->[0] : $_ } @classes]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 610 | ▶ | return 'file' if $ref eq 'Mojo::Upload'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 609 | ▶ | return lc $ref if $ref and !$blessed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ref and not $blessed
MC/DC100%2/2
$ref and not $blessed
$ref
not $blessed
Truth table100%3/3
A and not(B)
A= $refB= not $blessed
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 378 | ▶ | return 'null' if !defined $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 372 | ▶ | return 'boolean' if $blessed and ("$_[0]" eq "1" or !"$_[0]"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$blessed and "$_[0]" eq 1 || !"$_[0]"
"$_[0]" eq 1 or not "$_[0]"
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]"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 362 | ▶ | if (is_num($_[0])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 81 | ▶ | return 'integer' if grep { ($_->{type} // '') eq 'integer' } @{$_[1] || []}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$_->{"type"} // ""
$_[1] || []
MC/DC0%0/1
$_->{"type"} // ""
$_->{"type"}
MC/DC100%1/1
$_[1] || []
$_[1]
Truth table50%1/2
A
A= $_->{"type"}
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 80 | return 'number'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 281 | ▶ | return $blessed || 'string'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$blessed || 'string'
MC/DC100%1/1
$blessed || 'string'
$blessed
Truth table100%2/2
A
A= $blessed
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 136946 | ▶ | blessed $_[0] && ($_[0]->isa('JSON::PP::Boolean') || "$_[0]" eq "1" || !$_[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
builtin::blessed($_[0]) && ($_[0]->isa('JSON::PP::Boolean') || "$_[0]" eq '1' || !$_[0])
$_[0]->isa("JSON::PP::Boolean") or "$_[0]" eq 1 or not $_[0]
$_[0]->isa("JSON::PP::Boolean") or "$_[0]" eq 1
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
B::svref_2object(\$_[0])->FLAGS & 12288 and 0 + $_[0] eq $_[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$2 // -3
MC/DC100%1/1
$2 // -3
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 42713 | ▶ | return $_[0]->{type} if $_[0]->{type}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 42334 | ▶ | return _guessed_right(object => $_[1]) if $_[0]->{additionalProperties}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 42326 | ▶ | return _guessed_right(object => $_[1]) if $_[0]->{patternProperties}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 42286 | ▶ | return _guessed_right(object => $_[1]) if $_[0]->{properties}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 37690 | ▶ | return _guessed_right(object => $_[1]) if exists $_[0]->{propertyNames}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 37682 | ▶ | return _guessed_right(object => $_[1]) if $_[0]->{required}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | return _guessed_right(object => $_[1]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | if $_[0]->{dependencies} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | or $_[0]->{dependentSchemas} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 35026 | ▶ | or $_[0]->{dependentRequired}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$_[0]{"dependencies"} or $_[0]{"dependentSchemas"} or $_[0]{"dependentRequired"}
$_[0]{"dependencies"} or $_[0]{"dependentSchemas"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 35025 | ▶ | return _guessed_right(object => $_[1]) if defined $_[0]->{maxProperties} or defined $_[0]->{minProperties}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $_[0]{"maxProperties"} or defined $_[0]{"minProperties"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 34996 | ▶ | return _guessed_right(array => $_[1]) if $_[0]->{uniqueItems}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 34996 | ▶ | return _guessed_right(array => $_[1]) if exists $_[0]->{contains}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 34993 | ▶ | return _guessed_right(array => $_[1]) if exists $_[0]->{maxItems} or exists $_[0]->{minItems}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $_[0]{"maxItems"} or exists $_[0]{"minItems"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 34990 | ▶ | return _guessed_right(string => $_[1]) if $_[0]->{pattern}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 34964 | ▶ | return _guessed_right(string => $_[1]) if exists $_[0]->{maxLength} or defined $_[0]->{minLength}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $_[0]{"maxLength"} or defined $_[0]{"minLength"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 34945 | ▶ | return _guessed_right(number => $_[1]) if $_[0]->{multipleOf}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%9/9
defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"} or defined $_[0]{"exclusiveMinimum"}
defined $_[0]{"maximum"} or defined $_[0]{"minimum"} or defined $_[0]{"exclusiveMaximum"}
defined $_[0]{"maximum"} or defined $_[0]{"minimum"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 34905 | ▶ | return 'const' if exists $_[0]->{const}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
eval {
do {
$data = $_[0] =~ /^\s*\{/su ? Mojo::JSON::decode_json($_[0]) : _yaml_load($_[0]);
1
}
} // Carp::confess($@)
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
}
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 7424 | ▶ | return $_[0] if $_[0] eq data_type $_[1], [{type => $_[0]}]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||