| 1 | package JSON::Validator::Schema::Troglodyne; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $JSON::Validator::Schema::Troglodyne::VERSION = '1.001'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Troglodyne extensions to the OpenAPIv3 schema validator. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use re '/aa'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use parent qw{JSON::Validator::Schema::OpenAPIv3}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Data::Validate::Email(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use List::Util qw{any}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | sub _validate_type_email { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_type_email: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | my ( $self, $input, $info ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | my $path = $self->_troglodyne_path($info); | |||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | return "wrong type, not email" if ref $input; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | return "$path ain't no email I never heard of pardner" unless Data::Validate::Email::is_email($input); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | sub _validate_type_callback { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_type_callback: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | my ( $self, $input, $info ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | my $path = $self->_troglodyne_path($info); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | return "$path is not a fully qualified sub name" unless defined $input && !ref $input; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $input and not ref $input
MC/DC50%1/2
defined $input and not ref $input
defined $input
not ref $input
Truth table66%2/3
A and not(B)
A= defined $inputB= not ref $input
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | my ($modname) = $input =~ m/^([\w:]+)::\w+$/; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | return "$path is not a fully qualified sub name" unless $modname; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | my $modpath = $modname; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | $modpath =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | $modpath .= '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | local $@; | |||||||||||||||||||||||||||||||||||||||||
| 41 | # Require so we can check the sub exists, but don't dobule-require | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | my @available = keys(%INC); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | ▶ | my $loaded = any { m/\Q$modpath\E/ } @available; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | if (!$loaded) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | eval { require $modpath; 1 } or return "$path refers to a module ($modname) which cannot be loaded"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | return "$path refers to a sub which does not exist" unless defined &{$input}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 51 | ▶ | sub _validate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 51 | my ( $self, $data, $state ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 51 | my @errors = $self->SUPER::_validate( $_[1], $state ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 51 | my $schema = $state->{schema}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 51 | ▶ | return @errors if !@errors || ref $schema ne 'HASH' || ref $schema->{'x-errorMessage'} ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not @errors or ref $schema ne "HASH" or ref $schema->{"x-errorMessage"} ne "HASH"
not @errors or ref $schema ne "HASH"
MC/DC66%2/3
not @errors or ref $schema ne "HASH" or ref $schema->{"x-errorMessage"} ne "HASH"
not @errors
ref $schema ne "HASH"
ref $schema->{"x-errorMessage"} ne "HASH"
Truth table75%3/4
not(A) or B or C
A= not @errorsB= ref $schema ne "HASH"C= ref $schema->{"x-errorMessage"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | my $messages = $schema->{'x-errorMessage'}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | my $here = JSON::Validator::Error->new( $state->{path} )->path; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | for my $error (@errors) { | |||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | my $keyword = $error->details->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | next if !defined $keyword || !defined $messages->{$keyword}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $keyword and defined $messages->{$keyword}
MC/DC50%1/2
defined $keyword and defined $messages->{$keyword}
defined $keyword
defined $messages->{$keyword}
Truth table66%2/3
A and B
A= defined $keywordB= defined $messages->{$keyword}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | my $at = $error->path; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | ▶ | if ( $keyword eq 'required' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | $at =~ s{/[^/]*\z}{}; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | $at ||= '/'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$at ||= "/"
MC/DC0%0/1
$at ||= "/"
$at
Truth table50%1/2
A
A= $at
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | $error->message( $messages->{$keyword} ) if $at eq $here; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | return @errors; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | # Build the path the module usually does for errors | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 14 | ▶ | sub _troglodyne_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_troglodyne_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 14 | my ($self, $input) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 14 | return "$input->{base_url}/".join('/', @{$input->{path}}); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||||||||||||||||