Validator.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 88.8% 32 / 36 Conditioncond 74.2% 52 / 70 MC/DCmcdc 46.1% 18 / 39 Subroutinesub 94.4% 17 / 18 Podpod 100% 7 / 7 totaltotal 81.9% 209 / 255 CC 45 SCAR 30.4
CC
45
Cov
87%
_new_schema
37.5
coerce
6.1
schema
3.1
CRAP 20.9
↑ Summary Validator/Schema/Draft4.pm »
1 package JSON::Validator;
2 82 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 82 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 82 ▶use JSON::Validator::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 82 ▶use JSON::Validator::Util qw(E data_checksum is_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 82 ▶use Mojo::Util qw(sha1_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 82 our $VERSION = '5.19';
11  
12 82 our %SCHEMAS = (
13 'http://json-schema.org/draft-04/schema#' => '+Draft4',
14 'http://json-schema.org/draft-06/schema#' => '+Draft6',
15 'http://json-schema.org/draft-07/schema#' => '+Draft7',
16 'https://json-schema.org/draft/2019-09/schema' => '+Draft201909',
17 'http://swagger.io/v2/schema.json' => '+OpenAPIv2',
18 'https://spec.openapis.org/oas/3.0/schema/2021-09-28' => '+OpenAPIv3',
19 'https://spec.openapis.org/oas/3.1/schema/2021-05-20' => '+OpenAPIv3',
20 );
21  
22 0 ▶has formats => sub { require JSON::Validator::Schema; JSON::Validator::Schema->_build_formats };
Subroutine __ANON__: not called CC=1 SCAR=2.6
23 82 has recursive_data_protection => 1;
24  
25 616 ▶has store => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 616 my $self = shift;
27 616 my %attrs;
28 616 $attrs{$_} = delete $self->{$_} for grep { $self->{$_} } qw(cache_paths ua);
29 616 return JSON::Validator::Store->new(%attrs);
30 82 };
31  
32 # store proxy attributes
33 82 for my $method (qw(cache_paths ua)) {
34 1 ▶ Mojo::Util::monkey_patch(__PACKAGE__, $method => sub { shift->store->$method(@_) });
Subroutine cache_paths: called CC=1 SCAR=0.0
Subroutine ua: called CC=1 SCAR=0.0
35 }
36  
37 21 ▶sub bundle { shift->schema->bundle(@_)->data }
Subroutine bundle: called CC=1 SCAR=0.0
Pod documented
38  
39 651 ▶sub coerce {
Subroutine coerce: called CC=6 SCAR=18.0
Pod documented
40 651 my $self = shift;
41 651 ▶ return $self->{coerce} ||= {} unless defined(my $what = shift);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $what = shift()) 6 645 651
Condition50%1/2
$self->{"coerce"} ||= {}
l!l
60
MC/DC0%0/1
$self->{"coerce"} ||= {}
$self->{"coerce"}
Truth table50%1/2
A
A= $self->{"coerce"}
Aresult
11
00
42  
43 645 state $short = {bool => 'booleans', def => 'defaults', num => 'numbers', str => 'strings'};
44 645 ▶ $what = {map { ($_ => 1) } split /,/, $what} unless ref $what;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $what 551 94 645
45 645 $self->{coerce} = {};
46 645 ▶ $self->{coerce}{($short->{$_} || $_)} = $what->{$_} for keys %$what;
Condition66%2/3
$short->{$_} || $_
l!l&&r!l&&!r
412830
MC/DC0%0/2
$short->{$_} || $_
$short->{$_} $_
Truth table66%2/3
A || B
A= $short->{$_}B= $_
ABresult
1X1
011
000
47  
48 645 return $self;
49 }
50  
51 6 ▶sub get { shift->schema->get(@_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
52  
53 20 ▶sub load_and_validate_schema {
Subroutine load_and_validate_schema: called CC=3 SCAR=11.0
Pod documented
54 20 my ($self, $schema, $args) = @_;
55  
56 20 delete $self->{schema};
57 20 my $schema_obj = $self->_new_schema($schema, %$args);
58 confess join "\n", "Invalid JSON specification", (ref $schema eq 'HASH' ? Mojo::Util::dumper($schema) : $schema),
59 4 map {"- $_"} @{$schema_obj->errors}
60 18 ▶ if @{$schema_obj->errors};
Branch: 2 branches
BranchTrueFalseTotal
if @{$schema_obj->errors;} 4 14 18
ref $schema eq 'HASH' ? : 0 4 4
61  
62 14 $self->{schema} = $schema_obj;
63 14 return $self;
64 }
65  
66 1828 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
67 1828 my $self = shift->SUPER::new(@_);
68 1828 ▶ $self->coerce($self->{coerce}) if defined $self->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"coerce"} 95 1733 1828
69 1828 return $self;
70 }
71  
72 1332 ▶sub schema {
Subroutine schema: called CC=3 SCAR=11.3
Pod documented
73 1332 my $self = shift;
74 1332 ▶ return $self->{schema} //= $self->_new_schema({}) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 708 624 1332
Condition33%1/3
$self->{"schema"} //= $self->_new_schema({})
l!l&&r!l&&!r
70800
MC/DC0%0/2
$self->{"schema"} //= $self->_new_schema({})
$self->{"schema"} $self->_new_schema({})
Truth table33%1/3
A //= B
A= $self->{"schema"}B= $self->_new_schema({})
ABresult
1X1
011
000
75 624 $self->{schema} = $self->_new_schema(shift);
76 620 return $self;
77 }
78  
79 601 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
80 601 my ($self, $data, $schema) = @_;
81 601 ▶ return +(defined $schema ? $self->_new_schema($schema) : $self->schema)->validate($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 1 600 601
82 }
83  
84 645 ▶sub _new_schema {
Subroutine _new_schema: called CC=33 SCAR=36.2
85 645 my ($self, $source, %attrs) = @_;
86 645 ▶ return $source if blessed $source and $source->can('specification');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($source) and $source->can("specification") 0 645 645
Condition66%2/3
builtin::blessed($source) and $source->can("specification")
!ll&&!rl&&r
604410
MC/DC0%0/2
builtin::blessed($source) and $source->can("specification")
builtin::blessed($source) $source->can("specification")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("specification")
ABresult
0X0
100
111
87  
88 # Compat with load_and_validate_schema()
89 645 ▶ $attrs{specification} = delete $attrs{schema} if $attrs{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"schema"} 12 633 645
90  
91 645 ▶ my $loadable
Condition100%18/18
builtin::blessed($source) && ($source->can('scheme') || $source->isa('Mojo::File')) || !($source =~ /\n/u) && -f $source || !ref($source) && $source =~ /^\w/u
l!l&&r!l&&!r
2955561
builtin::blessed($source) and $source->can("scheme") || $source->isa("Mojo::File") or not $source =~ /\n/u and -f $source
l!l&&r!l&&!r
272616
builtin::blessed($source) and $source->can("scheme") || $source->isa("Mojo::File")
!ll&&!rl&&r
6041427
$source->can("scheme") or $source->isa("Mojo::File")
l!l&&r!l&&!r
22514
not $source =~ /\n/u and -f $source
!ll&&!rl&&r
16152
not ref $source and $source =~ /^\w/u
!ll&&!rl&&r
5362555
MC/DC100%7/7
builtin::blessed($source) && ($source->can('scheme') || $source->isa('Mojo::File')) || !($source =~ /\n/u) && -f $source || !ref($source) && $source =~ /^\w/u
builtin::blessed($source) $source->can("scheme") $source->isa("Mojo::File") not $source =~ /\n/u -f $source not ref $source $source =~ /^\w/u
Truth table50%8/16
A and B or C or not(D) and E || not(F) and G
A= builtin::blessed($source)B= $source->can("scheme")C= $source->isa("Mojo::File")D= not $source =~ /\n/uE= -f $sourceF= not ref $sourceG= $source =~ /^\w/u
ABCDEFGresult
11XXXXX1
101XXXX1
0XX11XX1
10011XX1
0XX0X111
0XX10111
1000X111
10010111
0XX0X0X0
0XX0X100
0XX100X0
0XX10100
1000X0X0
1000X100
100100X0
10010100
92 = (blessed $source && ($source->can('scheme') || $source->isa('Mojo::File')))
93 || ($source !~ /\n/ && -f $source)
94 || (!ref $source && $source =~ /^\w/);
95  
96 645 my $store = $self->store;
97 645 ▶ my $schema = $loadable ? $store->get($store->load($source)) : $source;
Branch: 1 branch
BranchTrueFalseTotal
$loadable ? : 84 561 645
98  
99 643 ▶ $attrs{recursive_data_protection} //= $self->recursive_data_protection;
Condition33%1/3
$attrs{"recursive_data_protection"} //= $self->recursive_data_protection
l!l&&r!l&&!r
06430
MC/DC0%0/2
$attrs{"recursive_data_protection"} //= $self->recursive_data_protection
$attrs{"recursive_data_protection"} $self->recursive_data_protection
Truth table33%1/3
A //= B
A= $attrs{"recursive_data_protection"}B= $self->recursive_data_protection
ABresult
1X1
011
000
100  
101 643 ▶ $attrs{coerce} ||= $self->{coerce} if $self->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"coerce"} 92 551 643
Condition33%1/3
$attrs{"coerce"} ||= $self->{"coerce"}
l!l&&r!l&&!r
0920
MC/DC0%0/2
$attrs{"coerce"} ||= $self->{"coerce"}
$attrs{"coerce"} $self->{"coerce"}
Truth table33%1/3
A ||= B
A= $attrs{"coerce"}B= $self->{"coerce"}
ABresult
1X1
011
000
102 643 ▶ $attrs{formats} ||= $self->{formats} if $self->{formats};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"formats"} 0 643 643
Condition0%0/3
$attrs{"formats"} ||= $self->{"formats"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs{"formats"} ||= $self->{"formats"}
$attrs{"formats"} $self->{"formats"}
Truth table0%0/3
A ||= B
A= $attrs{"formats"}B= $self->{"formats"}
ABresult
1X1
011
000
103 $attrs{specification} = $schema->{'$schema'}
104 if !$attrs{specification}
105 and is_type $schema, 'HASH'
106 643 ▶ and $schema->{'$schema'};
Branch: 1 branch
BranchTrueFalseTotal
if not $attrs{"specification"} and is_type($schema, "HASH") and $schema->{"\$schema"} 16 627 643
Condition100%6/6
not $attrs{"specification"} and is_type($schema, "HASH") and $schema->{"\$schema"}
!ll&&!rl&&r
5157616
not $attrs{"specification"} and is_type($schema, "HASH")
!ll&&!rl&&r
1239592
MC/DC100%3/3
not $attrs{"specification"} and is_type($schema, "HASH") and $schema->{"\$schema"}
not $attrs{"specification"} is_type($schema, "HASH") $schema->{"\$schema"}
Truth table100%4/4
not(A) and B and C
A= not $attrs{"specification"}B= is_type($schema, "HASH")C= $schema->{"\$schema"}
ABCresult
0XX0
10X0
1100
1111
107 643 $attrs{store} = $store;
108  
109 # Detect openapiv2 and v3 schemas by content, since no "$schema" is present
110 643 ▶ my $spec = $attrs{specification} || $schema;
Condition66%2/3
$attrs{'specification'} || $schema
l!l&&r!l&&!r
286150
MC/DC0%0/2
$attrs{'specification'} || $schema
$attrs{'specification'} $schema
Truth table66%2/3
A || B
A= $attrs{'specification'}B= $schema
ABresult
1X1
011
000
111 643 ▶ if (ref $spec eq 'HASH' and $spec->{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq "HASH" and $spec->{"paths"}) 32 611 643
Condition100%3/3
ref $spec eq "HASH" and $spec->{"paths"}
!ll&&!rl&&r
6754432
MC/DC100%2/2
ref $spec eq "HASH" and $spec->{"paths"}
ref $spec eq "HASH" $spec->{"paths"}
Truth table100%3/3
A and B
A= ref $spec eq "HASH"B= $spec->{"paths"}
ABresult
0X0
100
111
112 32 ▶ if ($spec->{swagger} and $spec->{swagger} eq '2.0') {
Branch: 3 branches
BranchTrueFalseTotal
if ($spec->{'swagger'} and $spec->{'swagger'} eq '2.0') { } 11 21 32
elsif ($spec->{'openapi'} and $spec->{'openapi'} =~ /^3\.1\.\d+$/u) { } 1 20 21
elsif ($spec->{'openapi'} and $spec->{'openapi'} =~ /^3\.0\.\d+$/u) { } 17 6 23
Condition66%2/3
$spec->{"swagger"} and $spec->{"swagger"} eq "2.0"
!ll&&!rl&&r
21011
MC/DC50%1/2
$spec->{"swagger"} and $spec->{"swagger"} eq "2.0"
$spec->{"swagger"} $spec->{"swagger"} eq "2.0"
Truth table66%2/3
A and B
A= $spec->{"swagger"}B= $spec->{"swagger"} eq "2.0"
ABresult
0X0
100
111
113 11 $spec = 'http://swagger.io/v2/schema.json';
114 }
115 ▶ elsif ($spec->{openapi} and $spec->{openapi} =~ m!^3\.1\.\d+$!) {
Condition100%3/3
$spec->{"openapi"} and $spec->{"openapi"} =~ /^3\.1\.\d+$/u
!ll&&!rl&&r
3171
MC/DC100%2/2
$spec->{"openapi"} and $spec->{"openapi"} =~ /^3\.1\.\d+$/u
$spec->{"openapi"} $spec->{"openapi"} =~ /^3\.1\.\d+$/u
Truth table100%3/3
A and B
A= $spec->{"openapi"}B= $spec->{"openapi"} =~ /^3\.1\.\d+$/u
ABresult
0X0
100
111
116 1 $spec = 'https://spec.openapis.org/oas/3.1/schema/2021-05-20';
117 1 ▶ $attrs{specification} ||= $spec;
Condition33%1/3
$attrs{"specification"} ||= $spec
l!l&&r!l&&!r
010
MC/DC0%0/2
$attrs{"specification"} ||= $spec
$attrs{"specification"} $spec
Truth table33%1/3
A ||= B
A= $attrs{"specification"}B= $spec
ABresult
1X1
011
000
118 }
119 ▶ elsif ($spec->{openapi} and $spec->{openapi} =~ m!^3\.0\.\d+$!) {
Condition66%2/3
$spec->{"openapi"} and $spec->{"openapi"} =~ /^3\.0\.\d+$/u
!ll&&!rl&&r
3017
MC/DC50%1/2
$spec->{"openapi"} and $spec->{"openapi"} =~ /^3\.0\.\d+$/u
$spec->{"openapi"} $spec->{"openapi"} =~ /^3\.0\.\d+$/u
Truth table66%2/3
A and B
A= $spec->{"openapi"}B= $spec->{"openapi"} =~ /^3\.0\.\d+$/u
ABresult
0X0
100
111
120 17 $spec = 'https://spec.openapis.org/oas/3.0/schema/2021-09-28';
121 }
122 }
123  
124 643 ▶ my $schema_class = $spec && $SCHEMAS{$spec} || 'JSON::Validator::Schema';
Condition100%5/5
$spec && $JSON::Validator::SCHEMAS{$spec} || 'JSON::Validator::Schema'
l!l
57586
$spec and $JSON::Validator::SCHEMAS{$spec}
!ll&&!rl&&r
757957
MC/DC100%2/2
$spec && $JSON::Validator::SCHEMAS{$spec} || 'JSON::Validator::Schema'
$spec $JSON::Validator::SCHEMAS{$spec}
Truth table100%3/3
A and B
A= $specB= $JSON::Validator::SCHEMAS{$spec}
ABresult
111
0X0
100
125 643 $schema_class =~ s!^\+(.+)$!JSON::Validator::Schema::$1!;
126 643 ▶ confess "Could not load $schema_class: $@" unless $schema_class->can('new') or eval "require $schema_class;1";
Branch: 1 branch
BranchTrueFalseTotal
unless $schema_class->can("new") or eval "require $schema_class;1" 0 643 643
Condition66%2/3
$schema_class->can("new") or eval "require $schema_class;1"
l!l&&r!l&&!r
621220
MC/DC0%0/2
$schema_class->can("new") or eval "require $schema_class;1"
$schema_class->can("new") eval "require $schema_class;1"
Truth table66%2/3
A or B
A= $schema_class->can("new")B= eval "require $schema_class;1"
ABresult
1X1
011
000
127 643 return $schema_class->new($source, %attrs);
128 }
129  
130 82 1;
131  
132 =encoding utf8
133  
134 =head1 NAME
135  
136 JSON::Validator - Validate data against a JSON schema
137  
138 =head1 SYNOPSIS
139  
140 =head2 Using a schema object
141  
142 L<JSON::Validator::Schema> or any of the sub classes can be used instead of
143 L<JSON::Validator>. The only reason to use L<JSON::Validator> directly is if
144 you don't know the schema version up front.
145  
146 =head2 Basics
147  
148 use JSON::Validator;
149 my $jv = JSON::Validator->new;
150  
151 # Define a schema - http://json-schema.org/learn/miscellaneous-examples.html
152 # You can also load schema from disk or web
153 $jv->schema({
154 type => "object",
155 required => ["firstName", "lastName"],
156 properties => {
157 firstName => {type => "string"},
158 lastName => {type => "string"},
159 age => {type => "integer", minimum => 0, description => "Age in years"}
160 }
161 });
162  
163 # Validate your data
164 my @errors = $jv->validate({firstName => "Jan Henning", lastName => "Thorsen", age => -42});
165  
166 # Do something if any errors was found
167 die "@errors" if @errors;
168  
169 =head2 Using joi
170  
171 # Use joi() to build the schema
172 use JSON::Validator::Joi 'joi';
173  
174 $jv->schema(joi->object->props({
175 firstName => joi->string->required,
176 lastName => joi->string->required,
177 age => joi->integer->min(0),
178 }));
179  
180 # joi() can also validate directly
181 my @errors = joi(
182 {firstName => "Jan Henning", lastName => "Thorsen", age => -42},
183 joi->object->props({
184 firstName => joi->string->required,
185 lastName => joi->string->required,
186 age => joi->integer->min(0),
187 }),
188 );
189  
190 =head1 DESCRIPTION
191  
192 L<JSON::Validator> is a data structure validation library based around
193 L<JSON Schema|https://json-schema.org/>. This module can be used directly with
194 a JSON schema or you can use the elegant DSL schema-builder
195 L<JSON::Validator::Joi> to define the schema programmatically.
196  
197 =head2 Supported schema formats
198  
199 L<JSON::Validator> can load JSON schemas in multiple formats: Plain perl data
200 structured (as shown in L</SYNOPSIS>), JSON or YAML. The JSON parsing is done
201 with L<Mojo::JSON>, while YAML files requires L<YAML::PP> or L<YAML::XS>.
202  
203 =head2 Resources
204  
205 Here are some resources that are related to JSON schemas and validation:
206  
207 =over 4
208  
209 =item * L<http://json-schema.org/documentation.html>
210  
211 =item * L<https://json-schema.org/understanding-json-schema/index.html>
212  
213 =item * L<https://github.com/json-schema/json-schema/>
214  
215 =back
216  
217 =head2 Bundled specifications
218  
219 This module comes with some JSON specifications bundled, so your application
220 don't have to fetch those from the web. These specifications should be up to
221 date, but please submit an issue if they are not.
222  
223 Files referenced to an URL will automatically be cached if the first element in
224 L</cache_paths> is a writable directory. Note that the cache headers for the
225 remote assets are B<not> honored, so you will manually need to remove any
226 cached file, should you need to refresh them.
227  
228 To download and cache an online asset, do this:
229  
230 JSON_VALIDATOR_CACHE_PATH=/some/writable/directory perl myapp.pl
231  
232 Here is the list of the bundled specifications:
233  
234 =over 2
235  
236 =item * JSON schema, draft 4, 6, 7, 2019-09.
237  
238 Web page: L<http://json-schema.org>
239  
240 C<$ref>: L<http://json-schema.org/draft-04/schema#>,
241 L<http://json-schema.org/draft-06/schema#>,
242 L<http://json-schema.org/draft-07/schema#>.
243  
244 =item * JSON schema for JSONPatch files
245  
246 Web page: L<http://jsonpatch.com>
247  
248 C<$ref>: L<http://json.schemastore.org/json-patch#>
249  
250 =item * Swagger / OpenAPI specification, version 2
251  
252 Web page: L<https://openapis.org>
253  
254 C<$ref>: L<http://swagger.io/v2/schema.json#>
255  
256 =item * OpenAPI specification, version 3
257  
258 Web page: L<https://openapis.org>
259  
260 C<$ref>: L<https://spec.openapis.org/oas/3.0/schema/2021-09-28|https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json>
261  
262 This specification is still EXPERIMENTAL.
263  
264 =item * Swagger Petstore
265  
266 This is used for unit tests, and should not be relied on by external users.
267  
268 =back
269  
270 =head2 Optional modules
271  
272 =over 2
273  
274 =item * Sereal::Encoder
275  
276 Installing L<Sereal::Encoder> v4.00 (or later) will make
277 L<JSON::Validator::Util/data_checksum> significantly faster. This function is
278 used both when parsing schemas and validating data.
279  
280 =item * Format validators
281  
282 See the documentation in L<JSON::Validator::Formats> for other optional modules
283 to do validation of specific "format", such as "hostname", "ipv4" and others.
284  
285 =back
286  
287 =head1 ATTRIBUTES
288  
289 =head2 cache_paths
290  
291 Proxy attribute for L<JSON::Validator::Store/cache_paths>.
292  
293 =head2 formats
294  
295 This attribute will be used as default value for
296 L<JSON::Validator::Schema/formats>. It is highly recommended to change this
297 directly on the L</schema> instead:
298  
299 $jv->formats(...); # Legacy
300 $jv->schema->formats(...); # Recommended way
301  
302 =head2 recursive_data_protection
303  
304 This attribute will be used as default value for
305 L<JSON::Validator::Schema/recursive_data_protection>. It is highly recommended
306 to change this directly on the L</schema> instead:
307  
308 $jv->recursive_data_protection(...); # Legacy
309 $jv->schema->recursive_data_protection(...); # Recommended way
310  
311 =head2 store
312  
313 $store = $jv->store;
314  
315 Holds a L<JSON::Validator::Store> object that caches the retrieved schemas.
316 This object will be shared amongst different L</schema> objects to prevent
317 a schema from having to be downloaded again.
318  
319 =head2 ua
320  
321 Proxy attribute for L<JSON::Validator::Store/ua>.
322  
323 =head1 METHODS
324  
325 =head2 bundle
326  
327 This method can be used to get a bundled version of L</schema>. It will however
328 return a data-structure instead of a new object. See
329 L<JSON::Validator::Schema/bundle> for an alternative.
330  
331 # These two lines does the same
332 $data = $jv->bundle;
333 $data = $jv->schema->bundle->data;
334  
335 # Recommended way
336 $schema = $jv->schema->bundle;
337  
338 =head2 coerce
339  
340 This attribute will be used as default value for
341 L<JSON::Validator::Schema/coerce>. It is highly recommended to change this
342 directly on the L</schema> instead:
343  
344 $jv->coerce(...); # Legacy
345 $jv->schema->coerce(...); # Recommended way
346  
347 =head2 get
348  
349 Proxy method for L<JSON::Validator::Schema/get>.
350  
351 =head2 new
352  
353 $jv = JSON::Validator->new(%attributes);
354 $jv = JSON::Validator->new(\%attributes);
355  
356 Creates a new L<JSON::Validate> object.
357  
358 =head2 load_and_validate_schema
359  
360 This method will be deprecated in the future. See
361 L<JSON::Validator::Schema/errors> and L<JSON::Validator::Schema/is_invalid>
362 instead.
363  
364 =head2 schema
365  
366 $jv = $jv->schema($json_or_yaml_string);
367 $jv = $jv->schema($url);
368 $jv = $jv->schema(\%schema);
369 $jv = $jv->schema(JSON::Validator::Joi->new);
370 $jv = $jv->schema(JSON::Validator::Schema->new);
371 $schema = $jv->schema;
372  
373 Used to set a schema from either a data structure or a URL.
374  
375 C<$schema> will be an instance of L<JSON::Validator::Schema::Draft4>,
376 L<JSON::Validator::Schema::Draft6> L<JSON::Validator::Schema::Draft7>,
377 L<JSON::Validator::Schema::Draft201909>, L<JSON::Validator::Schema::OpenAPIv2>,
378 L<JSON::Validator::Schema::OpenAPIv3> or L<JSON::Validator::Schema>.
379  
380 The C<$url> can take many forms, but needs to point to a text file in the
381 JSON or YAML format.
382  
383 =over 4
384  
385 =item * file://...
386  
387 A file on disk. Note that it is required to use the "file" scheme if you want
388 to reference absolute paths on your file system.
389  
390 =item * http://... or https://...
391  
392 A web resource will be fetched using the L<Mojo::UserAgent>, stored in L</ua>.
393  
394 =item * data://Some::Module/spec.json
395  
396 Will load a given "spec.json" file from C<Some::Module> using
397 L<JSON::Validator::Util/data_section>.
398  
399 =item * data:///spec.json
400  
401 A "data" URL without a module name will use the current package and search up
402 the call/inheritance tree.
403  
404 =item * Any other URL
405  
406 An URL (without a recognized scheme) will be treated as a path to a file on
407 disk. If the file could not be found on disk and the path starts with "/", then
408 the will be loaded from the app defined in L</ua>. Something like this:
409  
410 $jv->ua->server->app(MyMojoApp->new);
411 $jv->ua->get('/any/other/url.json');
412  
413 =back
414  
415 =head2 validate
416  
417 Proxy method for L<JSON::Validator::Schema/validate>.
418  
419 =head1 SEE ALSO
420  
421 =over 2
422  
423 =item * L<JSON::Validator::Formats>
424  
425 L<JSON::Validator::Formats> contains utility functions for validating data
426 types. Could be useful for validating data without loading a schema.
427  
428 =item * L<JSON::Validator::Schema>
429  
430 L<JSON::Validator::Schema> is the base class for
431 L<JSON::Validator::Schema::Draft4>, L<JSON::Validator::Schema::Draft6>
432 L<JSON::Validator::Schema::Draft7>, L<JSON::Validator::Schema::Draft201909>,
433 L<JSON::Validator::Schema::OpenAPIv2> or L<JSON::Validator::Schema::OpenAPIv3>.
434  
435 =item * L<JSON::Validator::Util>
436  
437 L<JSON::Validator::Util> contains many useful function when working with
438 schemas.
439  
440 =item * L<Mojolicious::Plugin::OpenAPI>
441  
442 L<Mojolicious::Plugin::OpenAPI> is a plugin for L<Mojolicious> that utilize
443 L<JSON::Validator> and the L<OpenAPI specification|https://www.openapis.org/>
444 to build routes with input and output validation.
445  
446 =back
447  
448 =head1 COPYRIGHT AND LICENSE
449  
450 Copyright (C) 2014-2021, Jan Henning Thorsen
451  
452 This program is free software, you can redistribute it and/or modify it under
453 the terms of the Artistic License version 2.0.
454  
455 =head1 AUTHORS
456  
457 =head2 Project Founder
458  
459 Jan Henning Thorsen - C<jhthorsen@cpan.org>
460  
461 =head2 Contributors
462  
463 =over 2
464  
465 =item * Aleksandr Orlenko
466  
467 =item * Alexander Hartmaier
468  
469 =item * Alexander Karelas
470  
471 =item * Bernhard Graf
472  
473 =item * Brad Barden
474  
475 =item * Dagfinn Ilmari Mannsåker
476  
477 =item * Daniel Böhmer
478  
479 =item * David Cantrell
480  
481 =item * Ed J
482  
483 =item * Ere Maijala
484  
485 =item * Fabrizio Gennari
486  
487 =item * Ilya Rassadin
488  
489 =item * Jason Cooper
490  
491 =item * Karen Etheridge
492  
493 =item * Kenichi Ishigaki
494  
495 =item * Kevin M. Goess
496  
497 =item * Kirill Matusov
498  
499 =item * Krasimir Berov
500  
501 =item * Lari Taskula
502  
503 =item * Lee Johnson
504  
505 =item * Martin Renvoize
506  
507 =item * Mattias Päivärinta
508  
509 =item * Michael Jemmeson
510  
511 =item * Michael Schout
512  
513 =item * Mohammad S Anwar
514  
515 =item * Nick Morrott
516  
517 =item * Pierre-Aymeric Masse
518  
519 =item * Roy Storey
520  
521 =item * Russell Jenkins
522  
523 =item * Sebastian Riedel
524  
525 =item * Stephan Hradek
526  
527 =item * Tim Stallard
528  
529 =item * Zoffix Znet
530  
531 =back
532  
533 =cut
↑ Summary Validator/Schema/Draft4.pm »