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