Validator/Store.pm

filter: Statementstmt 97.4% 151 / 155 Branchbran 81.2% 52 / 64 Conditioncond 69.2% 45 / 65 MC/DCmcdc 22.8% 8 / 35 Subroutinesub 100% 30 / 30 Podpod 100% 5 / 5 totaltotal 82.2% 291 / 354 CC 57 SCAR 20.0
CC
57
Cov
87%
resolve
16.2
_load_from_url
12.6
_load_from_app
8.4
CRAP 7.4
« Validator/URI.pm ↑ Summary Validator/Error.pm »
1 package JSON::Validator::Store;
2 82 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 82 ▶use Mojo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use Mojo::Util qw(url_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use JSON::Validator::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 82 ▶use JSON::Validator::URI qw(uri);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 82 ▶use JSON::Validator::Util qw(data_section str2data);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 82 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 82 ▶use constant DEBUG => $ENV{JSON_VALIDATOR_DEBUG} && 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'JSON_VALIDATOR_DEBUG'} && 1
!ll
820
MC/DC0%0/1
$ENV{'JSON_VALIDATOR_DEBUG'} && 1
$ENV{'JSON_VALIDATOR_DEBUG'}
Truth table50%1/2
A
A= $ENV{'JSON_VALIDATOR_DEBUG'}
Aresult
00
11
16 82 ▶use constant BUNDLED_PATH => path(path(__FILE__)->dirname, 'cache')->to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 82 ▶use constant CASE_TOLERANT => File::Spec->case_tolerant;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 82 ▶die $@ unless eval q(package JSON::Validator::Exception; use Mojo::Base 'Mojo::Exception'; 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless eval "package JSON::Validator::Exception; use Mojo::Base 'Mojo::Exception'; 1" 0 82 82
20  
21 532 ▶has cache_paths => sub { [split(/:/, $ENV{JSON_VALIDATOR_CACHE_PATH} || ''), BUNDLED_PATH] };
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition50%1/2
$ENV{'JSON_VALIDATOR_CACHE_PATH'} || ''
l!l
0532
MC/DC0%0/1
$ENV{'JSON_VALIDATOR_CACHE_PATH'} || ''
$ENV{'JSON_VALIDATOR_CACHE_PATH'}
Truth table50%1/2
A
A= $ENV{'JSON_VALIDATOR_CACHE_PATH'}
Aresult
11
00
22 616 ▶has schemas => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 2 ▶has ua => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 2 my $ua = Mojo::UserAgent->new;
26 2 $ua->proxy->detect;
27 2 return $ua->max_redirects(3);
28 82 };
29  
30 2542 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
31 2542 my ($self, $id, $schema) = @_;
32 2542 $id =~ s!(.)#$!$1!;
33 2542 $self->schemas->{$id} = $schema;
34 2542 return $id;
35 }
36  
37 2984 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.3
Pod documented
38 2984 my ($self, $id) = @_;
39 2984 ▶ return undef unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 2984 2984
40 2984 $id =~ s!(.)#$!$1!;
41 2984 ▶ return $self->schemas->{$id} && $id;
Condition66%2/3
$self->schemas->{$id} && $id
!ll&&!rl&&r
19850999
MC/DC50%1/2
$self->schemas->{$id} && $id
$self->schemas->{$id} $id
Truth table66%2/3
A && B
A= $self->schemas->{$id}B= $id
ABresult
0X0
100
111
42 }
43  
44 1307 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod documented
45 1307 my ($self, $id) = @_;
46 1307 ▶ return undef unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 1307 1307
47 1307 $id =~ s!(.)#$!$1!;
48 1307 return $self->schemas->{$id};
49 }
50  
51 699 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod documented
52 return
53 699 ▶ $_[0]->_load_from_url($_[1])
Condition88%16/18
$_[0]->_load_from_url($_[1]) || $_[0]->_load_from_data($_[1]) || $_[0]->_load_from_text($_[1]) || $_[0]->_load_from_file($_[1]) || $_[0]->_load_from_app($_[1]) || $_[0]->get($_[1]) || _raise(qq[Unable to load schema "$_[1]".])
l!l&&r!l&&!r
69403
$_[0]->_load_from_url($_[1]) or $_[0]->_load_from_data($_[1]) or $_[0]->_load_from_text($_[1]) or $_[0]->_load_from_file($_[1]) or $_[0]->_load_from_app($_[1]) or $_[0]->get($_[1])
l!l&&r!l&&!r
69403
$_[0]->_load_from_url($_[1]) or $_[0]->_load_from_data($_[1]) or $_[0]->_load_from_text($_[1]) or $_[0]->_load_from_file($_[1]) or $_[0]->_load_from_app($_[1])
l!l&&r!l&&!r
69314
$_[0]->_load_from_url($_[1]) or $_[0]->_load_from_data($_[1]) or $_[0]->_load_from_text($_[1]) or $_[0]->_load_from_file($_[1])
l!l&&r!l&&!r
611825
$_[0]->_load_from_url($_[1]) or $_[0]->_load_from_data($_[1]) or $_[0]->_load_from_text($_[1])
l!l&&r!l&&!r
610187
$_[0]->_load_from_url($_[1]) or $_[0]->_load_from_data($_[1])
l!l&&r!l&&!r
5615088
MC/DC0%0/7
$_[0]->_load_from_url($_[1]) || $_[0]->_load_from_data($_[1]) || $_[0]->_load_from_text($_[1]) || $_[0]->_load_from_file($_[1]) || $_[0]->_load_from_app($_[1]) || $_[0]->get($_[1]) || _raise(qq[Unable to load schema "$_[1]".])
$_[0]->_load_from_url($_[1]) $_[0]->_load_from_data($_[1]) $_[0]->_load_from_text($_[1]) $_[0]->_load_from_file($_[1]) $_[0]->_load_from_app($_[1]) $_[0]->get($_[1]) _raise(qq[Unable to load schema "$_[1]".])
Truth table62%5/8
A or B or C or D or E or F || G
A= $_[0]->_load_from_url($_[1])B= $_[0]->_load_from_data($_[1])C= $_[0]->_load_from_text($_[1])D= $_[0]->_load_from_file($_[1])E= $_[0]->_load_from_app($_[1])F= $_[0]->get($_[1])G= _raise(qq[Unable to load schema "$_[1]".])
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
54 || $_[0]->_load_from_data($_[1])
55 || $_[0]->_load_from_text($_[1])
56 || $_[0]->_load_from_file($_[1])
57 || $_[0]->_load_from_app($_[1])
58 || $_[0]->get($_[1])
59 || _raise(qq(Unable to load schema "$_[1]".));
60 }
61  
62 63055 ▶sub resolve {
Subroutine resolve: called CC=16 SCAR=27.8
Pod documented
63 63055 my ($self, $ref, $curr) = @_;
64 63055 ▶ $curr //= {base_url => ''};
Condition100%2/2
$curr //= {"base_url", ""}
l!l
62436619
MC/DC100%1/1
$curr //= {"base_url", ""}
$curr
Truth table100%2/2
A
A= $curr
Aresult
11
00
65  
66 63055 my ($base_url, $fragment) = split '#', $ref;
67 63055 my $abs_url = uri($base_url)->fragment($fragment);
68 63055 ▶ $abs_url = uri $abs_url, $curr->{base_url} if $curr->{base_url} and !$abs_url->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
if $curr->{"base_url"} and not $abs_url->is_abs 62121 934 63055
Condition100%3/3
$curr->{"base_url"} and not $abs_url->is_abs
!ll&&!rl&&r
61931562121
MC/DC100%2/2
$curr->{"base_url"} and not $abs_url->is_abs
$curr->{"base_url"} not $abs_url->is_abs
Truth table100%3/3
A and not(B)
A= $curr->{"base_url"}B= not $abs_url->is_abs
ABresult
0X0
100
111
69 63055 ▶ $fragment = '' unless defined $fragment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment 629 62426 63055
70 63055 ▶ $base_url ||= $curr->{base_url} || '';
Condition60%3/5
$base_url ||= $curr->{"base_url"} || ""
l!l&&r!l&&!r
1002620530
$curr->{'base_url'} || ''
l!l
620530
MC/DC0%0/2
$base_url ||= $curr->{"base_url"} || ""
$base_url $curr->{'base_url'}
Truth table66%2/3
A ||= B
A= $base_urlB= $curr->{'base_url'}
ABresult
1X1
011
000
71  
72 63055 warn "[JSON::Validator] Resolve curr: ref=$ref,@{[map qq($_=$curr->{$_}), sort keys %$curr]}\n" if DEBUG;
73  
74 63055 my $state = {base_url => $base_url, fragment => $fragment, source => 'unknown'};
75 63055 ▶ if (defined(my $schema = $self->schemas->{$abs_url})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined(my $schema = $self->schemas->{$abs_url})) { } 376 62679 63055
elsif (defined(my $root = $self->schemas->{$base_url})) { } 62066 613 62679
elsif ($base_url) { } 613 0 613
76 376 @$state{qw(base_url id root schema source)} = ($abs_url, $abs_url, $schema, $schema, 'schema/abs_url');
77 }
78 elsif (defined(my $root = $self->schemas->{$base_url})) {
79 62066 @$state{qw(base_url id root source)} = ($base_url, $base_url, $root, 'schema/base_url');
80 }
81 elsif ($base_url) {
82 613 ▶ $base_url = uri $base_url, $curr->{base_url} if $curr->{base_url};
Branch: 1 branch
BranchTrueFalseTotal
if $curr->{"base_url"} 53 560 613
83 613 my $id = $self->load($base_url);
84 610 @$state{qw(base_url id root source)} = ($id, $id, $self->get($id), 'load');
85 610 $state->{root} = $self->get($id);
86 }
87 else {
88 0 @$state{qw(id root source)} = ('', $curr->{root}, 'root');
89 }
90  
91 63052 $fragment =~ s!%2f!~1!; # /
92 63052 $fragment =~ s!%7e!~0!; # ~
93 63052 $fragment = url_unescape $fragment;
94 63052 ▶ $state->{schema} //= length $fragment ? Mojo::JSON::Pointer->new($state->{root})->get($fragment) : $state->{root};
Branch: 1 branch
BranchTrueFalseTotal
length $fragment ? : 62096 580 62676
Condition100%3/3
$state->{"schema"} //= length $fragment ? "Mojo::JSON::Pointer"->new($state->{"root"})->get($fragment) : $state->{"root"}
l!l&&r!l&&!r
376626751
MC/DC100%2/2
$state->{"schema"} //= length $fragment ? "Mojo::JSON::Pointer"->new($state->{"root"})->get($fragment) : $state->{"root"}
$state->{"schema"} length $fragment ? "Mojo::JSON::Pointer"->new($state->{"root"})->get($fragment) : $state->{"root"}
Truth table100%3/3
A //= B
A= $state->{"schema"}B= length $fragment ? "Mojo::JSON::Pointer"->new($state->{"root"})->get($fragment) : $state->{"root"}
ABresult
1X1
011
000
95 63052 ▶ _raise(qq[Unable to resolve "$ref" from "$state->{base_url}". ($state->{source})]) unless defined $state->{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state->{"schema"} 1 63051 63052
96  
97 63051 ▶ $state->{$_} //= $curr->{$_} for keys %$curr; # pass on original information
Condition66%2/3
$state->{$_} //= $curr->{$_}
l!l&&r!l&&!r
3728601530
MC/DC0%0/2
$state->{$_} //= $curr->{$_}
$state->{$_} $curr->{$_}
Truth table66%2/3
A //= B
A= $state->{$_}B= $curr->{$_}
ABresult
1X1
011
000
98 63051 warn "[JSON::Validator] Resolve state: @{[map qq($_=$state->{$_}), sort keys %$state]}\n" if DEBUG;
99 63051 return $state;
100 }
101  
102 632 ▶sub _add {
Subroutine _add: called CC=4 SCAR=14.1
103 632 my ($self, $id, $schema) = @_;
104 632 $id = $self->add($id => $schema);
105  
106 632 ▶ if (ref $schema eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $schema eq "HASH") 632 0 632
107 return
108 $schema->{'$id'} ? $self->add($schema->{'$id'} => $schema)
109 632 ▶ : $schema->{id} ? $self->add($schema->{id} => $schema)
Branch: 2 branches
BranchTrueFalseTotal
$schema->{'$id'} ? : 527 105 632
$schema->{'id'} ? : 26 79 105
110 : $id;
111 }
112  
113 0 return $id;
114 }
115  
116 5 ▶sub _load_from_app {
Subroutine _load_from_app: called CC=8 SCAR=21.3
117 5 ▶ return undef unless $_[1] =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ m[^/]u 2 3 5
118  
119 3 my ($self, $url) = @_;
120 3 my $id;
121 3 ▶ return undef unless $self->ua->server->app;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ua->server->app 1 2 3
122 2 ▶ return undef if blessed $url and !$url->can('scheme');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($url) and not $url->can("scheme") 0 2 2
Condition33%1/3
builtin::blessed($url) and not $url->can("scheme")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($url) and not $url->can("scheme")
builtin::blessed($url) not $url->can("scheme")
Truth table33%1/3
A and not(B)
A= builtin::blessed($url)B= not $url->can("scheme")
ABresult
0X0
100
111
123 2 ▶ return $id if $id = $self->exists($url);
Branch: 1 branch
BranchTrueFalseTotal
if $id = $self->exists($url) 0 2 2
124  
125 2 my $tx = $self->ua->get($url);
126 2 ▶ my $err = $tx->error && $tx->error->{message};
Condition66%2/3
$tx->error && $tx->error->{'message'}
!ll&&!rl&&r
101
MC/DC50%1/2
$tx->error && $tx->error->{'message'}
$tx->error $tx->error->{'message'}
Truth table66%2/3
A && B
A= $tx->errorB= $tx->error->{'message'}
ABresult
0X0
100
111
127 2 ▶ _raise("GET $url: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 1 2
128 1 warn "[JSON::Validator] Load from app $url\n" if DEBUG;
129 1 return $self->_add($url => str2data $tx->res->body);
130 }
131  
132 138 ▶sub _load_from_data {
Subroutine _load_from_data: called CC=4 SCAR=13.9
133 138 ▶ return undef unless $_[1] =~ m!^data://([^/]*)/(.*)!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ m[^data://([^/]*)/(.*)]u 88 50 138
134  
135 50 my ($self, $url) = @_;
136 50 my $id;
137 50 ▶ return $id if $id = $self->exists($url);
Branch: 1 branch
BranchTrueFalseTotal
if $id = $self->exists($url) 5 45 50
138  
139 45 my ($class, $file) = ($1, $2); # data://([^/]*)/(.*)
140 45 my $text = data_section $class, $file, {encoding => 'UTF-8'};
141 45 ▶ _raise("Could not find $url") unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 1 44 45
142 44 warn "[JSON::Validator] Load from data $file in $class\n" if DEBUG;
143 44 return $self->_add($url => str2data $text);
144 }
145  
146 87 ▶sub _load_from_file {
Subroutine _load_from_file: called CC=3 SCAR=11.0
147 87 my ($self, $file) = @_;
148  
149 87 $file =~ s!^file://!!;
150 87 $file =~ s!#$!!;
151 87 $file = path(split '/', url_unescape $file);
152 87 ▶ return undef unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 5 82 87
153  
154 82 $file = $file->realpath;
155 82 my $id = uri()->new->scheme('file')->host('')->path(CASE_TOLERANT ? lc $file : "$file");
156 82 warn "[JSON::Validator] Load from file $file\n" if DEBUG;
157 82 ▶ return $self->exists($id) || $self->_add($id => str2data $file->slurp);
Condition66%2/3
$self->exists($id) || $self->_add($id, str2data($file->slurp))
l!l&&r!l&&!r
50320
MC/DC0%0/2
$self->exists($id) || $self->_add($id, str2data($file->slurp))
$self->exists($id) $self->_add($id, str2data($file->slurp))
Truth table66%2/3
A || B
A= $self->exists($id)B= $self->_add($id, str2data($file->slurp))
ABresult
1X1
011
000
158 }
159  
160 88 ▶sub _load_from_text {
Subroutine _load_from_text: called CC=6 SCAR=19.1
161 88 my ($self, $text) = @_;
162 88 my $is_scalar_ref = ref $text eq 'SCALAR';
163 88 ▶ return undef unless $is_scalar_ref or $text =~ m!^\s*(?:---|\{)!s;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_scalar_ref or $text =~ /^\s*(?:---|\{)/su 87 1 88
Condition66%2/3
$is_scalar_ref or $text =~ /^\s*(?:---|\{)/su
l!l&&r!l&&!r
1087
MC/DC50%1/2
$is_scalar_ref or $text =~ /^\s*(?:---|\{)/su
$is_scalar_ref $text =~ /^\s*(?:---|\{)/su
Truth table66%2/3
A or B
A= $is_scalar_refB= $text =~ /^\s*(?:---|\{)/su
ABresult
1X1
011
000
164  
165 1 ▶ my $id = uri->from_data($is_scalar_ref ? $$text : $text);
Branch: 1 branch
BranchTrueFalseTotal
$is_scalar_ref ? : 1 0 1
166 1 warn "[JSON::Validator] Load from text $id\n" if DEBUG;
167 1 ▶ return $self->exists($id) || $self->_add($id => str2data $is_scalar_ref ? $$text : $text);
Branch: 1 branch
BranchTrueFalseTotal
$is_scalar_ref ? : 1 0 1
Condition33%1/3
$self->exists($id) || $self->_add($id, str2data($is_scalar_ref ? $$text : $text))
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->exists($id) || $self->_add($id, str2data($is_scalar_ref ? $$text : $text))
$self->exists($id) $self->_add($id, str2data($is_scalar_ref ? $$text : $text))
Truth table33%1/3
A || B
A= $self->exists($id)B= $self->_add($id, str2data($is_scalar_ref ? $$text : $text))
ABresult
1X1
011
000
168 }
169  
170 699 ▶sub _load_from_url {
Subroutine _load_from_url: called CC=11 SCAR=25.4
171 699 ▶ return undef unless $_[1] =~ m!^https?://!;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] =~ m[^https?://]u 138 561 699
172  
173 561 my ($self, $url) = @_;
174 561 my $id;
175 561 ▶ return $id if $id = $self->exists($url);
Branch: 1 branch
BranchTrueFalseTotal
if $id = $self->exists($url) 7 554 561
176  
177 554 $url = uri($url)->fragment(undef);
178 554 ▶ return $id if $id = $self->exists($url);
Branch: 1 branch
BranchTrueFalseTotal
if $id = $self->exists($url) 0 554 554
179  
180 554 my $cache_path = $self->cache_paths->[0];
181 554 my $cache_file = Mojo::Util::md5_sum("$url");
182 554 for (@{$self->cache_paths}) {
183 550 my $path = path $_, $cache_file;
184 550 warn "[JSON::Validator] Load from cache $path\n" if DEBUG and -r $path;
185 550 ▶ return $self->_add($url => str2data $path->slurp) if -r $path;
Branch: 1 branch
BranchTrueFalseTotal
if -r $path 545 5 550
186 }
187  
188 9 my $tx = $self->ua->get($url);
189 9 ▶ my $err = $tx->error && $tx->error->{message};
Condition33%1/3
$tx->error && $tx->error->{'message'}
!ll&&!rl&&r
900
MC/DC0%0/2
$tx->error && $tx->error->{'message'}
$tx->error $tx->error->{'message'}
Truth table33%1/3
A && B
A= $tx->errorB= $tx->error->{'message'}
ABresult
0X0
100
111
190 9 ▶ _raise("GET $url: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 9 9
191  
192 9 ▶ if ($cache_path and $cache_path ne BUNDLED_PATH and -w $cache_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_path and $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache" and -w $cache_path) 0 9 9
Condition50%3/6
$cache_path and $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache" and -w $cache_path
!ll&&!rl&&r
900
$cache_path and $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache"
!ll&&!rl&&r
450
MC/DC0%0/3
$cache_path and $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache" and -w $cache_path
$cache_path $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache" -w $cache_path
Truth table50%2/4
A and B and C
A= $cache_pathB= $cache_path ne "/home/cpancover/.cpan/build/JSON-Validator-5.19-0/blib/lib/JSON/Validator/cache"C= -w $cache_path
ABCresult
0XX0
10X0
1100
1111
193 0 $cache_file = path $cache_path, $cache_file;
194 0 $cache_file->spurt($tx->res->body);
195 }
196  
197 9 warn "[JSON::Validator] Load from URL $url\n" if DEBUG;
198 9 return $self->_add($url => str2data $tx->res->body);
199 }
200  
201 6 ▶sub _raise { die JSON::Validator::Exception->new(@_)->trace }
Subroutine _raise: called CC=1 SCAR=0.0
202  
203 82 1;
204  
205 =encoding utf8
206  
207 =head1 NAME
208  
209 JSON::Validator::Store - Load and caching JSON schemas
210  
211 =head1 SYNOPSIS
212  
213 use JSON::Validator;
214 my $jv = JSON::Validator->new;
215 $jv->store->add("urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" => {...});
216 $jv->store->load("http://api.example.com/my/schema.json");
217  
218 =head1 DESCRIPTION
219  
220 L<JSON::Validator::Store> is a class for loading and caching JSON-Schemas.
221  
222 =head1 ATTRIBUTES
223  
224 =head2 cache_paths
225  
226 my $store = $store->cache_paths(\@paths);
227 my $array_ref = $store->cache_paths;
228  
229 A list of directories to where cached specifications are stored. Defaults to
230 C<JSON_VALIDATOR_CACHE_PATH> environment variable and the specs that is bundled
231 with this distribution.
232  
233 C<JSON_VALIDATOR_CACHE_PATH> can be a list of directories, each separated by ":".
234  
235 See L<JSON::Validator/Bundled specifications> for more details.
236  
237 =head2 schemas
238  
239 my $hash_ref = $store->schemas;
240 my $store = $store->schemas({});
241  
242 Hold the schemas as data structures. The keys are schema "id".
243  
244 =head2 ua
245  
246 my $ua = $store->ua;
247 my $store = $store->ua(Mojo::UserAgent->new);
248  
249 Holds a L<Mojo::UserAgent> object, used by L</schema> to load a JSON schema
250 from remote location.
251  
252 The default L<Mojo::UserAgent> will detect proxy settings and have
253 L<Mojo::UserAgent/max_redirects> set to 3.
254  
255 =head1 METHODS
256  
257 =head2 add
258  
259 my $normalized_id = $store->add($id => \%schema);
260  
261 Used to add a schema data structure. Note that C<$id> might not be the same as
262 C<$normalized_id>.
263  
264 =head2 exists
265  
266 my $normalized_id = $store->exists($id);
267  
268 Returns a C<$normalized_id> if it is present in the L</schemas>.
269  
270 =head2 get
271  
272 my $schema = $store->get($normalized_id);
273  
274 Used to retrieve a C<$schema> added by L</add> or L</load>.
275  
276 =head2 load
277  
278 my $normalized_id = $store->load('https://...');
279 my $normalized_id = $store->load('data://main/foo.json');
280 my $normalized_id = $store->load('---\nid: yaml');
281 my $normalized_id = $store->load('{"id":"yaml"}');
282 my $normalized_id = $store->load(\$text);
283 my $normalized_id = $store->load('/path/to/foo.json');
284 my $normalized_id = $store->load('file:///path/to/foo.json');
285 my $normalized_id = $store->load('/load/from/ua-server-app');
286  
287 Can load a C<$schema> from many different sources. The input can be a string or
288 a string-like object, and the L</load> method will try to resolve it in the
289 order listed in above.
290  
291 Loading schemas from C<$text> will generate an C<$normalized_id> in L</schemas>
292 looking like "urn:text:$text_checksum". This might change in the future!
293  
294 Loading files from disk will result in a C<$normalized_id> that always start
295 with "file://".
296  
297 Loading can also be done with relative path, which will then load from:
298  
299 $store->ua->server->app;
300  
301 This method is EXPERIMENTAL, but unlikely to change significantly.
302  
303 =head2 resolve
304  
305 $hash_ref = $store->resolve($url, \%defaults);
306  
307 Takes a C<$url> (can also be a file, urn, ...) with or without a fragment and
308 returns this structure about the schema:
309  
310 {
311 base_url => $str, # the part before the fragment in the $url
312 fragment => $str, # fragment part of the $url
313 id => $str, # store ID
314 root => ..., # the root schema
315 schema => ..., # the schema inside "root" if fragment is present
316 }
317  
318 This method is EXPERIMENTAL and can change without warning.
319  
320 =head1 SEE ALSO
321  
322 L<JSON::Validator>.
323  
324 =cut
« Validator/URI.pm ↑ Summary Validator/Error.pm »