| 1 | package JSON::Schema::AsType; | ||||||||||||||||||||||||||||||||||||||
| 2 | 18 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: generates Type::Tiny types out of JSON schemas | ||||||||||||||||||||||||||||||||||||||
| 4 | 18 | $JSON::Schema::AsType::VERSION = '0.4.4'; | |||||||||||||||||||||||||||||||||||||
| 5 | 18 | ▶ | use 5.14.0; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use Type::Tiny; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use Type::Tiny::Class; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Scalar::Util qw/ looks_like_number /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use List::Util qw/ reduce pairmap pairs /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | 18 | ▶ | use List::MoreUtils qw/ any all none uniq zip /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use Types::Standard qw/InstanceOf HashRef StrictNum Any Str ArrayRef Int Object slurpy Dict Optional slurpy /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 16 | 18 | ▶ | use Type::Utils; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 17 | 18 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 18 | 18 | ▶ | use Clone 'clone'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 19 | 18 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | use Moose::Util qw/ apply_all_roles /; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 18 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 18 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | use MooseX::MungeHas 'is_ro'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 28 | 18 | ▶ | use MooseX::ClassAttribute; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | 18 | our $strict_string = 1; | |||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | class_has schema_registry => ( | ||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 36 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 37 | 12 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 38 | 18 | traits => [ 'Hash' ], | |||||||||||||||||||||||||||||||||||||
| 39 | handles => { | ||||||||||||||||||||||||||||||||||||||
| 40 | all_schemas => 'elements', | ||||||||||||||||||||||||||||||||||||||
| 41 | all_schema_uris => 'keys', | ||||||||||||||||||||||||||||||||||||||
| 42 | registered_schema => 'get', | ||||||||||||||||||||||||||||||||||||||
| 43 | register_schema => 'set', | ||||||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | around register_schema => sub { | ||||||||||||||||||||||||||||||||||||||
| 48 | # TODO Use a type instead to coerce into canonical | ||||||||||||||||||||||||||||||||||||||
| 49 | 448 | ▶ | my( $orig, $self, $uri, $schema ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 50 | 448 | $uri =~ s/#$//; | |||||||||||||||||||||||||||||||||||||
| 51 | 448 | $orig->($self,$uri,$schema); | |||||||||||||||||||||||||||||||||||||
| 52 | 18 | }; | |||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | 18 | has type => ( | |||||||||||||||||||||||||||||||||||||
| 55 | is => 'rwp', | ||||||||||||||||||||||||||||||||||||||
| 56 | handles => [ qw/ check validate validate_explain / ], | ||||||||||||||||||||||||||||||||||||||
| 57 | builder => 1, | ||||||||||||||||||||||||||||||||||||||
| 58 | lazy => 1 | ||||||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | has draft_version => ( | ||||||||||||||||||||||||||||||||||||||
| 62 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 63 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 64 | default => sub { | ||||||||||||||||||||||||||||||||||||||
| 65 | $_[0]->has_specification ? $_[0]->specification =~ /(\d+)/ && $1 | ||||||||||||||||||||||||||||||||||||||
| 66 | 4457 | ▶ | : eval { $_[0]->parent_schema->draft_version } || 4; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
Branch: 1 branch
Condition60%3/5
$_[0]->specification =~ /(\d+)/u and $1
eval { or 4
MC/DC0%0/2
$_[0]->specification =~ /(\d+)/u and $1
$_[0]->specification =~ /(\d+)/u
$1
MC/DC100%1/1
eval {
do {
$_[0]->parent_schema->draft_version
}
} or 4
eval {
do {
$_[0]->parent_schema->draft_version
}
}
Truth table33%1/3
A and B
A= $_[0]->specification =~ /(\d+)/uB= $1
Truth table100%2/2
A
A= eval {
do {
$_[0]->parent_schema->draft_version
}
}
| |||||||||||||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||||||||||||
| 68 | 18 | isa => enum([ 3, 4, 6 ]), | |||||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | has spec => ( | ||||||||||||||||||||||||||||||||||||||
| 72 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 73 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 74 | default => sub { | ||||||||||||||||||||||||||||||||||||||
| 75 | 10 | ▶ | $_[0]->fetch( sprintf "https://json-schema.org/draft-%02d/schema", $_[0]->draft_version ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||||||||||||
| 77 | 18 | ); | |||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | has schema => ( | ||||||||||||||||||||||||||||||||||||||
| 80 | predicate => 'has_schema', | ||||||||||||||||||||||||||||||||||||||
| 81 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 82 | default => sub { | ||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | my $uri = $self->uri or die "schema or uri required"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | 1 | return $self->fetch($uri)->schema; | |||||||||||||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||||||||||||
| 89 | 18 | ); | |||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | 18 | has parent_schema => ( | |||||||||||||||||||||||||||||||||||||
| 92 | clearer => 1, | ||||||||||||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | has strict_string => ( | ||||||||||||||||||||||||||||||||||||||
| 96 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 97 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 98 | default => sub { | ||||||||||||||||||||||||||||||||||||||
| 99 | 2647 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 2647 | ▶ | $self->parent_schema->strict_string if $self->parent_schema; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | 2647 | return $JSON::Schema::AsType::strict_string; | |||||||||||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||||||||||||
| 105 | 18 | ); | |||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | sub fetch { | ||||||||||||||||||||||||||||||||||||||
| 108 | 447 | ▶ | my( $self, $url ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | 447 | ▶ | unless ( $url =~ m#^\w+://# ) { # doesn't look like an uri | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $id =$self->uri; | |||||||||||||||||||||||||||||||||||||
| 112 | 0 | $id =~ s#[^/]*$##; | |||||||||||||||||||||||||||||||||||||
| 113 | 0 | $url = $id . $url; | |||||||||||||||||||||||||||||||||||||
| 114 | # such that the 'id's can cascade | ||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ( my $p = $self->parent_schema ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 116 | 0 | return $p->fetch( $url ); | |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 447 | $url = URI->new($url); | |||||||||||||||||||||||||||||||||||||
| 121 | 447 | $url->path( $url->path =~ y#/#/#sr ); | |||||||||||||||||||||||||||||||||||||
| 122 | 447 | $url = $url->canonical; | |||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | 447 | ▶ | if ( my $schema = $self->registered_schema($url) ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 125 | 446 | ▶ | return $schema if $schema->has_schema; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | 1 | my $schema = eval { from_json LWP::Simple::get($url) }; | |||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | die "couldn't get schema from '$url'\n" unless ref $schema eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | 1 | return $self->register_schema( $url => $self->new( uri => $url, schema => $schema ) ); | |||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | has uri => ( | ||||||||||||||||||||||||||||||||||||||
| 136 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||
| 137 | trigger => sub { | ||||||||||||||||||||||||||||||||||||||
| 138 | 447 | ▶ | my( $self, $uri ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 139 | 447 | $self->register_schema($uri,$self); | |||||||||||||||||||||||||||||||||||||
| 140 | 447 | $self->clear_parent_schema; | |||||||||||||||||||||||||||||||||||||
| 141 | 18 | } ); | |||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||
| 143 | has references => sub { | ||||||||||||||||||||||||||||||||||||||
| 144 | +{} | ||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_references: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||
| 147 | has specification => ( | ||||||||||||||||||||||||||||||||||||||
| 148 | predicate => 1, | ||||||||||||||||||||||||||||||||||||||
| 149 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||
| 150 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||
| 151 | default => sub { | ||||||||||||||||||||||||||||||||||||||
| 152 | 4553 | ▶ | return 'draft'.$_[0]->draft_version; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | eval { $_[0]->parent_schema->specification } || 'draft4' }, | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | 18 | isa => enum 'JsonSchemaSpecification', [ qw/ draft3 draft4 draft6 / ], | |||||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||
| 157 | sub specification_schema { | ||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
specification_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $self->spec->schema; | |||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | sub validate_schema { | ||||||||||||||||||||||||||||||||||||||
| 164 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 165 | 6 | $self->spec->validate($self->schema); | |||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | sub validate_explain_schema { | ||||||||||||||||||||||||||||||||||||||
| 169 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_explain_schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 170 | 2 | $self->spec->validate_explain($self->schema); | |||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | sub root_schema { | ||||||||||||||||||||||||||||||||||||||
| 174 | 3593 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
root_schema: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 175 | 3593 | ▶ | eval { $self->parent_schema->root_schema } || $self; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | sub is_root_schema { | ||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
is_root_schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 180 | 0 | return not $self->parent_schema; | |||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | sub sub_schema { | ||||||||||||||||||||||||||||||||||||||
| 184 | 4296 | ▶ | my( $self, $subschema ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sub_schema: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 185 | 4296 | $self->new( schema => $subschema, parent_schema => $self ); | |||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | sub absolute_id { | ||||||||||||||||||||||||||||||||||||||
| 189 | 428 | ▶ | my( $self, $new_id ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
absolute_id: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | 428 | ▶ | return $new_id if $new_id =~ m#://#; # looks absolute to me | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | 25 | my $base = $self->ancestor_uri; | |||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||
| 195 | 25 | $base =~ s#[^/]+$##; | |||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | 25 | return $base . $new_id; | |||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||
| 200 | sub _build_type { | ||||||||||||||||||||||||||||||||||||||
| 201 | 4607 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_type: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | 4607 | $self->_set_type(''); | |||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||
| 205 | my @types = | ||||||||||||||||||||||||||||||||||||||
| 206 | 6702 | ▶ | grep { $_ and $_->name ne 'Any' } | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 207 | 4607 | map { $self->_process_keyword($_) } | |||||||||||||||||||||||||||||||||||||
| 208 | $self->all_keywords; | ||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||
| 210 | 4607 | ▶ | return @types ? reduce { $a & $b } @types : Any | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||
| 213 | sub all_keywords { | ||||||||||||||||||||||||||||||||||||||
| 214 | 3324 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
all_keywords: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||
| 216 | 3324 | return sort map { /^_keyword_(.*)/ } $self->meta->get_method_list; | |||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | sub _process_keyword { | ||||||||||||||||||||||||||||||||||||||
| 220 | 96283 | ▶ | my( $self, $keyword ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_keyword: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||
| 222 | 96283 | ▶ | return unless exists $self->schema->{$keyword}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||
| 224 | 7241 | my $value = $self->schema->{$keyword}; | |||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||
| 226 | 7241 | my $method = "_keyword_$keyword"; | |||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||
| 228 | 7241 | $self->$method($value); | |||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||
| 231 | # returns the first defined parent uri | ||||||||||||||||||||||||||||||||||||||
| 232 | sub ancestor_uri { | ||||||||||||||||||||||||||||||||||||||
| 233 | 1092 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ancestor_uri: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||
| 235 | 1092 | ▶ | return $self->uri || eval{ $self->parent_schema->ancestor_uri }; | ||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->uri || eval {
MC/DC100%2/2
$self->uri || eval {
do {
$self->parent_schema->ancestor_uri
}
}
$self->uri
eval {
do {
$self->parent_schema->ancestor_uri
}
}
Truth table100%3/3
A || B
A= $self->uriB= eval {
do {
$self->parent_schema->ancestor_uri
}
}
| |||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||
| 239 | sub resolve_reference { | ||||||||||||||||||||||||||||||||||||||
| 240 | 1990 | ▶ | my( $self, $ref ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve_reference: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | 1990 | ▶ | $ref = join '/', '#', map { $self->_escape_ref($_) } @$ref | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 243 | if ref $ref; | ||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||
| 245 | 1990 | ▶ | if ( $ref =~ s/^([^#]+)// ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 246 | 420 | my $base = $1; | |||||||||||||||||||||||||||||||||||||
| 247 | 420 | ▶ | unless( $base =~ m#://# ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 248 | 382 | my $base_uri = $self->ancestor_uri; | |||||||||||||||||||||||||||||||||||||
| 249 | 382 | $base_uri =~ s#[^/]+$##; | |||||||||||||||||||||||||||||||||||||
| 250 | 382 | $base = $base_uri . $base; | |||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||
| 252 | 420 | return $self->fetch($base)->resolve_reference($ref); | |||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||
| 255 | 1570 | $self = $self->root_schema; | |||||||||||||||||||||||||||||||||||||
| 256 | 1570 | ▶ | return $self if $ref eq '#'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||
| 258 | 889 | $ref =~ s/^#//; | |||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||
| 260 | # return $self->references->{$ref} if $self->references->{$ref}; | ||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||
| 262 | 889 | my $s = $self->schema; | |||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||
| 264 | 889 | my @refs = map { $self->_unescape_ref($_) } grep { length $_ } split '/', $ref; | |||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||
| 266 | 889 | ▶ | while( @refs ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 267 | 976 | my $ref = shift @refs; | |||||||||||||||||||||||||||||||||||||
| 268 | 976 | my $is_array = ref $s eq 'ARRAY'; | |||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | 976 | ▶ | $s = $is_array ? $s->[$ref] : $s->{$ref} or last; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | 973 | ▶ | if( ref $s eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 273 | 960 | ▶ | if( my $local_id = $s->{id} || $s->{'$id'} ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s->{'id'} || $s->{'$id'}
MC/DC100%2/2
$s->{'id'} || $s->{'$id'}
$s->{'id'}
$s->{'$id'}
Truth table100%3/3
A || B
A= $s->{'id'}B= $s->{'$id'}
| |||||||||||||||||||||||||||||||||||||||
| 274 | 16 | my $id = $self->absolute_id($local_id); | |||||||||||||||||||||||||||||||||||||
| 275 | 16 | $self = $self->fetch( $self->absolute_id($id) ); | |||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||
| 277 | 16 | return $self->resolve_reference(\@refs); | |||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | return ( | ||||||||||||||||||||||||||||||||||||||
| 284 | 873 | ▶ | ( ref $s eq 'HASH' or ref $s eq 'JSON::PP::Boolean' ) | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $s eq "HASH" or ref $s eq "JSON::PP::Boolean"
MC/DC0%0/2
ref $s eq "HASH" or ref $s eq "JSON::PP::Boolean"
ref $s eq "HASH"
ref $s eq "JSON::PP::Boolean"
Truth table66%2/3
A or B
A= ref $s eq "HASH"B= ref $s eq "JSON::PP::Boolean"
| |||||||||||||||||||||||||||||||||||||||
| 285 | ? $self->sub_schema($s) | ||||||||||||||||||||||||||||||||||||||
| 286 | : Any ); | ||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||
| 290 | sub _unescape_ref { | ||||||||||||||||||||||||||||||||||||||
| 291 | 992 | ▶ | my( $self, $ref ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_unescape_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||
| 293 | 992 | $ref =~ s/~0/~/g; | |||||||||||||||||||||||||||||||||||||
| 294 | 992 | $ref =~ s!~1!/!g; | |||||||||||||||||||||||||||||||||||||
| 295 | 992 | $ref =~ s!%25!%!g; | |||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||
| 297 | 992 | $ref; | |||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||
| 300 | sub _escape_ref { | ||||||||||||||||||||||||||||||||||||||
| 301 | 16 | ▶ | my( $self, $ref ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||
| 303 | 16 | $ref =~ s/~/~0/g; | |||||||||||||||||||||||||||||||||||||
| 304 | 16 | $ref =~ s!/!~1!g; | |||||||||||||||||||||||||||||||||||||
| 305 | 16 | $ref =~ s!%!%25!g; | |||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | 16 | $ref; | |||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||
| 310 | sub _add_reference { | ||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | my( $self, $path, $schema ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_reference: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | $path = join '/', '#', map { $self->_escape_ref($_) } @$path | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 314 | if ref $path; | ||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||
| 316 | 0 | $self->references->{$path} = $schema; | |||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||
| 319 | sub _add_to_type { | ||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | my( $self, $t ) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_to_type: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if( my $already = $self->type ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 323 | 0 | $t = $already & $t; | |||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | 0 | $self->_set_type( $t ); | |||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||
| 329 | sub BUILD { | ||||||||||||||||||||||||||||||||||||||
| 330 | 4654 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 331 | # TODO rename specification to draft_version | ||||||||||||||||||||||||||||||||||||||
| 332 | # and have specifications renamed to spec | ||||||||||||||||||||||||||||||||||||||
| 333 | 4654 | apply_all_roles( $self, 'JSON::Schema::AsType::' . ucfirst $self->specification ); | |||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||
| 335 | # TODO move the role into a trait, which should take care of this | ||||||||||||||||||||||||||||||||||||||
| 336 | 4654 | ▶ | $self->type if $self->has_schema; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||
| 339 | 18 | 1; | |||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||
| 341 | __END__ | ||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||
| 343 | =pod | ||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||
| 345 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||
| 347 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||
| 349 | JSON::Schema::AsType - generates Type::Tiny types out of JSON schemas | ||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||
| 351 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||
| 353 | version 0.4.4 | ||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||
| 355 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | use JSON::Schema::AsType; | ||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | my $schema = JSON::Schema::AsType->new( schema => { | ||||||||||||||||||||||||||||||||||||||
| 360 | properties => { | ||||||||||||||||||||||||||||||||||||||
| 361 | foo => { type => 'integer' }, | ||||||||||||||||||||||||||||||||||||||
| 362 | bar => { type => 'object' }, | ||||||||||||||||||||||||||||||||||||||
| 363 | }, | ||||||||||||||||||||||||||||||||||||||
| 364 | }); | ||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||
| 366 | print 'valid' if $schema->check({ foo => 1, bar => { two => 2 } }); # prints 'valid' | ||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||
| 368 | print $schema->validate_explain({ foo => 'potato', bar => { two => 2 } }); | ||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||
| 370 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||
| 372 | This module takes in a JSON Schema (L<http://json-schema.org/>) and turns it into a | ||||||||||||||||||||||||||||||||||||||
| 373 | L<Type::Tiny> type. | ||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||
| 375 | =head2 Strings and Numbers | ||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||
| 377 | By default, C<JSON::Schema::AsType> follows the | ||||||||||||||||||||||||||||||||||||||
| 378 | JSON schema specs and distinguish between strings and | ||||||||||||||||||||||||||||||||||||||
| 379 | numbers. | ||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||
| 381 | value String? Number? | ||||||||||||||||||||||||||||||||||||||
| 382 | "a" yes no | ||||||||||||||||||||||||||||||||||||||
| 383 | 1 no yes | ||||||||||||||||||||||||||||||||||||||
| 384 | "1" yes no | ||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||
| 386 | If you want the usual Perl | ||||||||||||||||||||||||||||||||||||||
| 387 | behavior and considers the JSON schema type C<String> | ||||||||||||||||||||||||||||||||||||||
| 388 | to be a superset of C<Number>. That is: | ||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||
| 390 | value String? Number? | ||||||||||||||||||||||||||||||||||||||
| 391 | "a" yes no | ||||||||||||||||||||||||||||||||||||||
| 392 | 1 yes yes | ||||||||||||||||||||||||||||||||||||||
| 393 | "1" yes yes | ||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||
| 395 | Then you can set the object's attribute C<strict_string> to C<0>. | ||||||||||||||||||||||||||||||||||||||
| 396 | Setting the global variable C<$JSON::Schema::AsType::strict_string> to C<0> | ||||||||||||||||||||||||||||||||||||||
| 397 | will work too, but that's deprecated and will eventually go away. | ||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||
| 399 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||
| 401 | =head2 new( %args ) | ||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||
| 403 | my $schema = JSON::Schema::AsType->new( schema => $my_schema ); | ||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||
| 405 | The class constructor. Accepts the following arguments. | ||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||
| 407 | =over | ||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||
| 409 | =item schema => \%schema | ||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||
| 411 | The JSON schema to compile, as a hashref. | ||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||
| 413 | If not given, will be retrieved from C<uri>. | ||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||
| 415 | An error will be thrown is neither C<schema> nor C<uri> is given. | ||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||
| 417 | =item uri => $uri | ||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||
| 419 | Optional uri associated with the schema. | ||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | If provided, the schema will also | ||||||||||||||||||||||||||||||||||||||
| 422 | be added to a schema cache. There is currently no way to prevent this. | ||||||||||||||||||||||||||||||||||||||
| 423 | If this is an issue for you, you can manipulate the cache by accessing | ||||||||||||||||||||||||||||||||||||||
| 424 | C<%JSON::Schema::AsType::EXTERNAL_SCHEMAS> directly. | ||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||
| 426 | =item draft_version => $version | ||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||
| 428 | The version of the JSON-Schema specification to use. Accepts C<3> or C<4>, | ||||||||||||||||||||||||||||||||||||||
| 429 | defaults to '4'. | ||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||
| 431 | =back | ||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||
| 433 | =head2 type | ||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||
| 435 | Returns the compiled L<Type::Tiny> type. | ||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||
| 437 | =head2 check( $struct ) | ||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||
| 439 | Returns C<true> if C<$struct> is valid as per the schema. | ||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||
| 441 | =head2 validate( $struct ) | ||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||
| 443 | Returns a short explanation if C<$struct> didn't validate, nothing otherwise. | ||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||
| 445 | =head2 validate_explain( $struct ) | ||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||
| 447 | Returns a log explanation if C<$struct> didn't validate, nothing otherwise. | ||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||
| 449 | =head2 validate_schema | ||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||
| 451 | Like C<validate>, but validates the schema itself against its specification. | ||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||
| 453 | print $schema->validate_schema; | ||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||
| 455 | # equivalent to | ||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||
| 457 | print $schema->specification_schema->validate($schema); | ||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||
| 459 | =head2 validate_explain_schema | ||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||
| 461 | Like C<validate_explain>, but validates the schema itself against its specification. | ||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||
| 463 | =head2 draft_version | ||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||
| 465 | Returns the draft version used by the object. | ||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||
| 467 | =head2 spec | ||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||
| 469 | Returns the L<JSON::Schema::AsType> object associated with the | ||||||||||||||||||||||||||||||||||||||
| 470 | specs of this object's schema. | ||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||
| 472 | I.e., if the current object is a draft4 schema, C<spec> will | ||||||||||||||||||||||||||||||||||||||
| 473 | return the schema definining draft4. | ||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||
| 475 | =head2 schema | ||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||
| 477 | Returns the JSON schema, as a hashref. | ||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||
| 479 | =head2 parent_schema | ||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||
| 481 | Returns the L<JSON::Schema::AsType> object for the parent schema, or | ||||||||||||||||||||||||||||||||||||||
| 482 | C<undef> is the current schema is the top-level one. | ||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||
| 484 | =head2 fetch( $url ) | ||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||
| 486 | Fetches the schema at the given C<$url>. If already present, it will use the schema in | ||||||||||||||||||||||||||||||||||||||
| 487 | the cache. If not, the newly fetched schema will be added to the cache. | ||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||
| 489 | =head2 uri | ||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||
| 491 | Returns the uri associated with the schema, if any. | ||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||
| 493 | =head2 specification | ||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||
| 495 | Returns the JSON Schema specification used by the object. | ||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||
| 497 | =head2 specification_schema | ||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||
| 499 | Returns the L<JSON::Schema::AsType> object representing the schema of | ||||||||||||||||||||||||||||||||||||||
| 500 | the current object's specification. | ||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||
| 502 | =head2 root_schema | ||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||
| 504 | Returns the top-level schema including this schema. | ||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||
| 506 | =head2 is_root_schema | ||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||
| 508 | Returns C<true> if this schema is a top-level | ||||||||||||||||||||||||||||||||||||||
| 509 | schema. | ||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||
| 511 | =head2 resolve_reference( $ref ) | ||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||
| 513 | my $sub_schema = $schema->resolve_reference( '#/properties/foo' ); | ||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||
| 515 | print $sub_schema->check( $struct ); | ||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||
| 517 | Returns the L<JSON::Schema::AsType> object associated with the | ||||||||||||||||||||||||||||||||||||||
| 518 | type referenced by C<$ref>. | ||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||
| 520 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||
| 522 | =over | ||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||
| 524 | =item L<JSON::Schema> | ||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||
| 526 | =item L<JSV> | ||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||
| 528 | =back | ||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||
| 530 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||
| 532 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||
| 534 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||
| 536 | This software is copyright (c) 2024 by Yanick Champoux. | ||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||
| 538 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||
| 539 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||
| 541 | =cut | ||||||||||||||||||||||||||||||||||||||