| 1 | package JSV::Context; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 47 | ▶ | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 47 | new => 1, | |||||||||||||||||||||||||||
| 8 | rw => [qw/ | ||||||||||||||||||||||||||||
| 9 | keywords | ||||||||||||||||||||||||||||
| 10 | reference | ||||||||||||||||||||||||||||
| 11 | original_schema | ||||||||||||||||||||||||||||
| 12 | throw_error | ||||||||||||||||||||||||||||
| 13 | throw_immediate | ||||||||||||||||||||||||||||
| 14 | enable_history | ||||||||||||||||||||||||||||
| 15 | enable_format | ||||||||||||||||||||||||||||
| 16 | formats | ||||||||||||||||||||||||||||
| 17 | history | ||||||||||||||||||||||||||||
| 18 | json | ||||||||||||||||||||||||||||
| 19 | loose_type | ||||||||||||||||||||||||||||
| 20 | /], | ||||||||||||||||||||||||||||
| 21 | ro => [qw/ | ||||||||||||||||||||||||||||
| 22 | errors | ||||||||||||||||||||||||||||
| 23 | current_type | ||||||||||||||||||||||||||||
| 24 | current_keyword | ||||||||||||||||||||||||||||
| 25 | current_pointer | ||||||||||||||||||||||||||||
| 26 | current_instance | ||||||||||||||||||||||||||||
| 27 | current_schema | ||||||||||||||||||||||||||||
| 28 | current_schema_pointer | ||||||||||||||||||||||||||||
| 29 | schema_pointer_history | ||||||||||||||||||||||||||||
| 30 | /], | ||||||||||||||||||||||||||||
| 31 | 47 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 47 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 47 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 47 | ▶ | use JSV::Keyword qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 47 | ▶ | use JSV::Util::Type qw(detect_instance_type detect_instance_type_loose); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 47 | ▶ | use JSV::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 47 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1421 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=26 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 1421 | my ($self, $schema, $instance) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1421 | ▶ | local $self->{current_type} = !$self->loose_type ? detect_instance_type($instance) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | : detect_instance_type_loose($instance); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1421 | local $self->{cleanup_callbacks} = []; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1421 | my $rv; | |||||||||||||||||||||||||||
| 49 | 1421 | eval { | |||||||||||||||||||||||||||
| 50 | 1421 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ANY()} }) { | |||||||||||||||||||||||||||
| 51 | 11368 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1224 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1421 | ▶ | if ($self->is_matched_types(qw/integer_or_string number_or_string/)) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 56 | 212 | for my $keyword ( | |||||||||||||||||||||||||||
| 57 | 212 | @{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }, | |||||||||||||||||||||||||||
| 58 | 212 | @{ $self->keywords->{INSTANCE_TYPE_STRING()} } | |||||||||||||||||||||||||||
| 59 | ) { | ||||||||||||||||||||||||||||
| 60 | 1484 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 85 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ($self->is_matched_types(qw/integer number/)) { | ||||||||||||||||||||||||||||
| 65 | 237 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }) { | |||||||||||||||||||||||||||
| 66 | 711 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 62 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif ($self->is_matched_types(qw/string/)) { | ||||||||||||||||||||||||||||
| 71 | 439 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_STRING()} }) { | |||||||||||||||||||||||||||
| 72 | 1756 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 108 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | elsif ($self->current_type eq "array") { | ||||||||||||||||||||||||||||
| 77 | 157 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ARRAY()} }) { | |||||||||||||||||||||||||||
| 78 | 628 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 139 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | elsif ($self->current_type eq "object") { | ||||||||||||||||||||||||||||
| 83 | 328 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_OBJECT()} }) { | |||||||||||||||||||||||||||
| 84 | ### for addtionalProperties, patternProperties keyword without properties keyword | ||||||||||||||||||||||||||||
| 85 | 1640 | ▶ | next unless ( ( grep { defined $_ && exists $schema->{$_} } ($keyword->keyword, @{$keyword->additional_keywords}) ) > 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ && exists $schema->{$_}
MC/DC50%1/2
defined $_ && exists $schema->{$_}
defined $_
exists $schema->{$_}
Truth table66%2/3
A && B
A= defined $_B= exists $schema->{$_}
| |||||||||||||||||||||||||||||
| 86 | # next unless exists $schema->{$_->keyword}; | ||||||||||||||||||||||||||||
| 87 | 418 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1421 | ▶ | $rv = JSV::Result->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | ($self->enable_history ? (history => $self->history) : ()), | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | 1421 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->log_error(sprintf("Unexpected error: %s", $e)); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1421 | ▶ | while (my $cb = pop @{ $self->{cleanup_callbacks} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 121 | $cb->(); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1421 | ▶ | if ( scalar @{ $self->errors } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 581 | ▶ | $rv = JSV::Result->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | errors => $self->errors, | ||||||||||||||||||||||||||||
| 106 | ($self->enable_history ? (history => $self->history) : ()), | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | 581 | ▶ | if ( $self->throw_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | croak $rv; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1421 | return $rv; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2036 | ▶ | sub apply_keyword { | ||||||||||||||||||||||||||
|
Subroutine
apply_keyword: called CC=3 SCAR=12.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 2036 | my ($self, $keyword, $schema, $instance) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2036 | local $self->{current_keyword} = $keyword->keyword; | |||||||||||||||||||||||||||
| 120 | 2036 | local $self->{current_schema} = $schema; | |||||||||||||||||||||||||||
| 121 | 2036 | local $self->{current_instance} = $instance; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2036 | $keyword->validate($self, $schema, $instance); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2036 | ▶ | if ( $ENV{JSV_DEBUG} || $self->enable_history ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"JSV_DEBUG"} or $self->enable_history
MC/DC0%0/2
$ENV{"JSV_DEBUG"} or $self->enable_history
$ENV{"JSV_DEBUG"}
$self->enable_history
Truth table33%1/3
A or B
A= $ENV{"JSV_DEBUG"}B= $self->enable_history
| |||||||||||||||||||||||||||||
| 126 | 0 | push @{ $self->history }, +{ | |||||||||||||||||||||||||||
| 127 | keyword => $self->current_keyword, | ||||||||||||||||||||||||||||
| 128 | pointer => $self->current_pointer, | ||||||||||||||||||||||||||||
| 129 | schema => $self->current_schema, | ||||||||||||||||||||||||||||
| 130 | instance => $self->resolve_current_instance, | ||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 422 | ▶ | sub log_error { | ||||||||||||||||||||||||||
|
Subroutine
log_error: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 422 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 422 | my $error = +{ | |||||||||||||||||||||||||||
| 139 | keyword => $self->current_keyword, | ||||||||||||||||||||||||||||
| 140 | pointer => $self->current_pointer, | ||||||||||||||||||||||||||||
| 141 | schema => $self->current_schema, | ||||||||||||||||||||||||||||
| 142 | instance => $self->resolve_current_instance, | ||||||||||||||||||||||||||||
| 143 | schema_pointer => $self->current_schema_pointer, | ||||||||||||||||||||||||||||
| 144 | schema_pointer_history => clone($self->schema_pointer_history), | ||||||||||||||||||||||||||||
| 145 | message => $message, | ||||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 422 | ▶ | if ( $ENV{JSV_DEBUG} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | require Data::Dump; | |||||||||||||||||||||||||||
| 150 | 0 | warn "history = " . Data::Dump::dump($self->history); | |||||||||||||||||||||||||||
| 151 | 0 | warn "error = " . Data::Dump::dump($error); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 422 | ▶ | if ( $self->throw_immediate ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak JSV::Result->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | error => $error, | ||||||||||||||||||||||||||||
| 157 | ($self->enable_history ? (history => $self->history) : ()), | ||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||
| 161 | 422 | push @{ $self->{errors} }, $error; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 422 | ▶ | sub resolve_current_instance { | ||||||||||||||||||||||||||
|
Subroutine
resolve_current_instance: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 422 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 422 | my $instance; | |||||||||||||||||||||||||||
| 169 | 422 | ▶ | if ( JSON::is_bool($self->current_instance) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 11 | ▶ | if ( $self->current_instance == JSON::true ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 9 | $instance = "true"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||
| 174 | 2 | $instance = "false"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 411 | $instance = $self->current_instance; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 422 | return $instance; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3602 | ▶ | sub is_matched_types { | ||||||||||||||||||||||||||
|
Subroutine
is_matched_types: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 185 | 3602 | my ($self, @types) = @_; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 3602 | ▶ | return (grep { $self->{current_type} eq $_ } @types) > 0 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 121 | ▶ | sub register_cleanup_callback { | ||||||||||||||||||||||||||
|
Subroutine
register_cleanup_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 121 | my ($self, $cb) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 121 | push @{ $self->{cleanup_callbacks} }, $cb; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 47 | 1; | |||||||||||||||||||||||||||