| 1 | package JSV::Context; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 45 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 45 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | use Class::Accessor::Lite ( | ||||||||||||||||||||||||||||
| 7 | 45 | 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 | 45 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 45 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 45 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 45 | ▶ | use JSV::Keyword qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 45 | ▶ | use JSV::Util::Type qw(detect_instance_type detect_instance_type_loose); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 45 | ▶ | use JSV::Result; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 45 | ▶ | use Clone qw(clone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub validate { | ||||||||||||||||||||||||||||
| 41 | 1260 | ▶ | my ($self, $schema, $instance) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=26 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1260 | ▶ | local $self->{current_type} = !$self->loose_type ? detect_instance_type($instance) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | : detect_instance_type_loose($instance); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1260 | local $self->{cleanup_callbacks} = []; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1260 | my $rv; | |||||||||||||||||||||||||||
| 49 | 1260 | eval { | |||||||||||||||||||||||||||
| 50 | 1260 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ANY()} }) { | |||||||||||||||||||||||||||
| 51 | 10080 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 944 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1260 | ▶ | if ($self->is_matched_types(qw/integer_or_string number_or_string/)) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 56 | 165 | for my $keyword ( | |||||||||||||||||||||||||||
| 57 | 165 | @{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }, | |||||||||||||||||||||||||||
| 58 | 165 | @{ $self->keywords->{INSTANCE_TYPE_STRING()} } | |||||||||||||||||||||||||||
| 59 | ) { | ||||||||||||||||||||||||||||
| 60 | 1155 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 54 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ($self->is_matched_types(qw/integer number/)) { | ||||||||||||||||||||||||||||
| 65 | 209 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }) { | |||||||||||||||||||||||||||
| 66 | 627 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 56 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif ($self->is_matched_types(qw/string/)) { | ||||||||||||||||||||||||||||
| 71 | 411 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_STRING()} }) { | |||||||||||||||||||||||||||
| 72 | 1644 | ▶ | 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 | 141 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ARRAY()} }) { | |||||||||||||||||||||||||||
| 78 | 564 | ▶ | next unless exists $schema->{$keyword->keyword}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 125 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | elsif ($self->current_type eq "object") { | ||||||||||||||||||||||||||||
| 83 | 292 | for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_OBJECT()} }) { | |||||||||||||||||||||||||||
| 84 | ### for addtionalProperties, patternProperties keyword without properties keyword | ||||||||||||||||||||||||||||
| 85 | 1460 | ▶ | next unless ( ( grep { defined $_ && exists $schema->{$_} } ($keyword->keyword, @{$keyword->additional_keywords}) ) > 0 ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | # next unless exists $schema->{$_->keyword}; | ||||||||||||||||||||||||||||
| 87 | 378 | $self->apply_keyword($keyword, $schema, $instance); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1260 | ▶ | $rv = JSV::Result->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | ($self->enable_history ? (history => $self->history) : ()), | ||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | 1260 | ▶ | if ( my $e = $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->log_error(sprintf("Unexpected error: %s", $e)); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1260 | ▶ | while (my $cb = pop @{ $self->{cleanup_callbacks} }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 8 | $cb->(); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1260 | ▶ | if ( scalar @{ $self->errors } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 504 | ▶ | $rv = JSV::Result->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | errors => $self->errors, | ||||||||||||||||||||||||||||
| 106 | ($self->enable_history ? (history => $self->history) : ()), | ||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||
| 108 | 504 | ▶ | if ( $self->throw_error ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | croak $rv; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1260 | return $rv; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub apply_keyword { | ||||||||||||||||||||||||||||
| 117 | 1665 | ▶ | my ($self, $keyword, $schema, $instance) = @_; | ||||||||||||||||||||||||||
|
Subroutine
apply_keyword: called CC=3 SCAR=12.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1665 | local $self->{current_keyword} = $keyword->keyword; | |||||||||||||||||||||||||||
| 120 | 1665 | local $self->{current_schema} = $schema; | |||||||||||||||||||||||||||
| 121 | 1665 | local $self->{current_instance} = $instance; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1665 | $keyword->validate($self, $schema, $instance); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1665 | ▶ | 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 | sub log_error { | ||||||||||||||||||||||||||||
| 136 | 365 | ▶ | my ($self, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
log_error: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 365 | 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 | 365 | ▶ | 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 | 365 | ▶ | 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 | 365 | push @{ $self->{errors} }, $error; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub resolve_current_instance { | ||||||||||||||||||||||||||||
| 166 | 365 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
resolve_current_instance: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 365 | my $instance; | |||||||||||||||||||||||||||
| 169 | 365 | ▶ | if ( JSON::is_bool($self->current_instance) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 9 | ▶ | if ( $self->current_instance == JSON::true ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 7 | $instance = "true"; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | else { | ||||||||||||||||||||||||||||
| 174 | 2 | $instance = "false"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | else { | ||||||||||||||||||||||||||||
| 178 | 356 | $instance = $self->current_instance; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 365 | return $instance; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub is_matched_types { | ||||||||||||||||||||||||||||
| 185 | 3241 | ▶ | my ($self, @types) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_matched_types: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 3241 | ▶ | return (grep { $self->{current_type} eq $_ } @types) > 0 ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub register_cleanup_callback { | ||||||||||||||||||||||||||||
| 191 | 8 | ▶ | my ($self, $cb) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register_cleanup_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 8 | push @{ $self->{cleanup_callbacks} }, $cb; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 45 | 1; | |||||||||||||||||||||||||||