| 1 | package JSONSchema::Validator; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Validator for JSON Schema Draft4/Draft6/Draft7 and OpenAPI Specification 3.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use URI::file; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Cwd; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use JSONSchema::Validator::Draft4; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use JSONSchema::Validator::Draft6; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use JSONSchema::Validator::Draft7; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use JSONSchema::Validator::OAS30; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use JSONSchema::Validator::Util qw(get_resource decode_content read_file); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | our $VERSION = '0.011'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | my $SPECIFICATIONS = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | JSONSchema::Validator::OAS30::ID => JSONSchema::Validator::OAS30::SPECIFICATION, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | JSONSchema::Validator::Draft4::ID => JSONSchema::Validator::Draft4::SPECIFICATION, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | JSONSchema::Validator::Draft6::ID => JSONSchema::Validator::Draft6::SPECIFICATION, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | JSONSchema::Validator::Draft7::ID => JSONSchema::Validator::Draft7::SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 7 | our $JSON_SCHEMA_VALIDATORS = ['JSONSchema::Validator::Draft4', 'JSONSchema::Validator::Draft6', 'JSONSchema::Validator::Draft7']; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | our $OAS_VALIDATORS = ['JSONSchema::Validator::OAS30']; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=26.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 25 | my ($class, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 25 | my $resource = delete $params{resource}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 25 | ▶ | my $validate_schema = delete($params{validate_schema}) // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $params{"validate_schema"} // 1
MC/DC0%0/1
delete $params{"validate_schema"} // 1
delete $params{"validate_schema"}
Truth table50%1/2
A
A= delete $params{"validate_schema"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 25 | my $schema = delete $params{schema}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 25 | my $base_uri = delete $params{base_uri}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 25 | my $specification = delete $params{specification}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 25 | ▶ | $schema = resource_schema($resource, \%params) if !$schema && $resource; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $schema and $resource
MC/DC50%1/2
not $schema and $resource
not $schema
$resource
Truth table66%2/3
not(A) and B
A= not $schemaB= $resource
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 25 | ▶ | croak 'resource or schema must be specified' unless defined $schema; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | ▶ | my $validator_class = find_validator($specification // schema_specification($schema)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$specification // schema_specification($schema)
MC/DC100%2/2
$specification // schema_specification($schema)
$specification
schema_specification($schema)
Truth table100%3/3
A // B
A= $specificationB= schema_specification($schema)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 25 | ▶ | croak 'unknown specification' unless $validator_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 19 | ▶ | if ($validate_schema) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 19 | my ($result, $errors) = $class->validate_resource_schema($schema, $validator_class->SPECIFICATION); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 19 | ▶ | croak "invalid schema:\n" . join "\n", @$errors unless $result; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # schema may be boolean value according to json schema draft6 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 15 | ▶ | if (ref $schema eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 15 | ▶ | $base_uri //= $resource || $schema->{'$id'} || $schema->{id}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
$base_uri //= $resource || $schema->{"\$id"} || $schema->{"id"}
$resource || $schema->{'$id'} || $schema->{'id'}
$resource or $schema->{"\$id"}
MC/DC0%0/4
$base_uri //= $resource || $schema->{"\$id"} || $schema->{"id"}
$base_uri
$resource
$schema->{"\$id"}
$schema->{'id'}
Truth table0%0/5
A //= B or C || D
A= $base_uriB= $resourceC= $schema->{"\$id"}D= $schema->{'id'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 15 | return $validator_class->new(schema => $schema, base_uri => $base_uri, %params); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub validate_paths { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_paths: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | my ($class, $globs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | my $results = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | for my $glob (@$globs) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | ▶ | my @resources = map { Cwd::abs_path($_) } glob $glob; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | for my $resource (@resources) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | my $uri = URI::file->new($resource)->as_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 6 | my ($result, $errors) = $class->validate_resource($uri); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | $results->{$resource} = [$result, $errors]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | return $results; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | sub validate_resource { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_resource: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | my ($class, $resource, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | my $schema_to_validate = resource_schema($resource, \%params); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 6 | my $validator_class = find_validator(schema_specification($schema_to_validate)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | ▶ | croak "unknown specification of resource $resource" unless $validator_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | return $class->validate_resource_schema($schema_to_validate, $validator_class->SPECIFICATION); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 25 | ▶ | sub validate_resource_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_resource_schema: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 25 | my ($class, $schema_to_validate, $schema_specification) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 25 | my $schema = read_specification($schema_specification); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 25 | my $meta_schema = $schema->{'$schema'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 25 | ▶ | my $meta_schema_specification = $SPECIFICATIONS->{$meta_schema} // $SPECIFICATIONS->{$meta_schema . '#'}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$SPECIFICATIONS->{$meta_schema} // $$SPECIFICATIONS{$meta_schema . '#'}
MC/DC0%0/2
$SPECIFICATIONS->{$meta_schema} // $$SPECIFICATIONS{$meta_schema . '#'}
$SPECIFICATIONS->{$meta_schema}
$$SPECIFICATIONS{$meta_schema . '#'}
Truth table33%1/3
A // B
A= $SPECIFICATIONS->{$meta_schema}B= $$SPECIFICATIONS{$meta_schema . '#'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 25 | ▶ | croak "unknown meta schema: $meta_schema" unless $meta_schema_specification; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 25 | my $validator_class = find_validator($meta_schema_specification); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 25 | ▶ | croak "can't find validator by meta schema: $meta_schema" unless $validator_class; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | my $validator = $validator_class->new(schema => $schema); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 25 | my ($result, $errors) = $validator->validate_schema($schema_to_validate); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 25 | return ($result, $errors); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 25 | ▶ | sub read_specification { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_specification: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 25 | my $filename = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 25 | my $curret_filepath = __FILE__; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 25 | my $schema_filepath = ($curret_filepath =~ s/\.pm$//r) . '/schemas/' . lc($filename) . '.json'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 25 | my ($content, $mime_type) = read_file($schema_filepath); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 25 | return decode_content($content, $mime_type, $schema_filepath); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 20 | ▶ | sub resource_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resource_schema: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 20 | my ($resource, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 20 | my ($response, $mime_type) = get_resource($params->{scheme_handlers}, $resource); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 20 | my $schema = decode_content($response, $mime_type, $resource); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 20 | return $schema; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 56 | ▶ | sub find_validator { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_validator: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 56 | my $specification = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 56 | ▶ | my ($validator_class) = grep { lc($_->SPECIFICATION) eq lc($specification // '') } @$JSON_SCHEMA_VALIDATORS, @$OAS_VALIDATORS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$specification // ""
MC/DC100%1/1
$specification // ""
$specification
Truth table100%2/2
A
A= $specification
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 56 | return $validator_class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 24 | ▶ | sub schema_specification { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema_specification: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 24 | my $schema = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 24 | ▶ | return if ref $schema ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 24 | my $meta_schema = $schema->{'$schema'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 24 | ▶ | my $specification = $meta_schema ? $SPECIFICATIONS->{$meta_schema} // $SPECIFICATIONS->{$meta_schema . '#'} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$SPECIFICATIONS->{$meta_schema} // $$SPECIFICATIONS{$meta_schema . '#'}
MC/DC0%0/2
$SPECIFICATIONS->{$meta_schema} // $$SPECIFICATIONS{$meta_schema . '#'}
$SPECIFICATIONS->{$meta_schema}
$$SPECIFICATIONS{$meta_schema . '#'}
Truth table66%2/3
A // B
A= $SPECIFICATIONS->{$meta_schema}B= $$SPECIFICATIONS{$meta_schema . '#'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 24 | ▶ | if (!$specification && $schema->{openapi}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $specification and $schema->{"openapi"}
MC/DC100%2/2
not $specification and $schema->{"openapi"}
not $specification
$schema->{"openapi"}
Truth table100%3/3
not(A) and B
A= not $specificationB= $schema->{"openapi"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 3 | my @vers = split /\./, $schema->{openapi}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | $specification = 'OAS' . $vers[0] . $vers[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 24 | return $specification; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||