AsType/Draft4.pm

filter: Statementstmt 98.6% 150 / 152 Branchbran 90.6% 29 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 45 / 45 Podpod n/a totaltotal 97.8% 224 / 229 CC 19 SCAR 6.4
CC
19
Cov
97%
_keyword_items
4.0
_keyword_type
4.0
_keyword_additionalItems
4.0
CRAP 1.9
« AsType/Draft6.pm ↑ Summary AsType/Draft3/Types.pm »
1 package JSON::Schema::AsType::Draft4;
2 12 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: Role processing draft4 JSON Schema
4 12 $JSON::Schema::AsType::Draft4::VERSION = '0.4.4';
5  
6 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Scalar::Util qw/ looks_like_number /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use List::Util qw/ reduce pairmap pairs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use List::MoreUtils qw/ any all none uniq zip /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Types::Standard qw/InstanceOf HashRef StrictNum Any Str ArrayRef Int slurpy Dict Optional slurpy /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use JSON::Schema::AsType;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 ▶use JSON::Schema::AsType::Draft4::Types '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 override all_keywords => sub {
24 11350 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
25
26 # $ref trumps all
27 11350 ▶ return '$ref' if $self->schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->schema->{"\$ref"} 987 10363 11350
28  
29 10363 return uniq 'id', super();
30 12 };
31  
32 __PACKAGE__->meta->add_method( '_keyword_$ref' => sub {
33 284 ▶ my( $self, $ref ) = @_;
Subroutine _keyword_$ref: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 return Type::Tiny->new(
36 name => 'Ref',
37 display_name => "Ref($ref)",
38 constraint => sub {
39
40 1542 ▶ my $r = $self->resolve_reference($ref);
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 1542 $r->check($_);
43 },
44 message => sub {
45 12 ▶ my $schema = $self->resolve_reference($ref);
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47 12 join "\n", "ref schema is " . to_json($schema->schema, { allow_nonref => 1 }), @{$schema->validate_explain($_)}
48 }
49 284 );
50 12 } );
51  
52 sub _keyword_id {
53 434 ▶ my( $self, $id ) = @_;
Subroutine _keyword_id: called CC=2 SCAR=6.9
54  
55 434 ▶ unless( $self->uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->uri) 396 38 434
56 396 my $id = $self->absolute_id($id);
57 396 $self->uri($id);
58 }
59  
60 434 return;
61 }
62  
63 sub _keyword_definitions {
64 87 ▶ my( $self, $defs ) = @_;
Subroutine _keyword_definitions: called CC=2 SCAR=6.9
65  
66 87 $self->sub_schema( $_ ) for values %$defs;
67  
68 87 return;
69 };
70  
71 sub _keyword_pattern {
72 38 ▶ my( $self, $pattern ) = @_;
Subroutine _keyword_pattern: called CC=1 SCAR=0.0
73  
74 38 Pattern[$pattern];
75 }
76  
77 sub _keyword_enum {
78 286 ▶ my( $self, $enum ) = @_;
Subroutine _keyword_enum: called CC=1 SCAR=0.0
79
80 286 Enum[@$enum];
81 }
82  
83 sub _keyword_uniqueItems {
84 219 ▶ my( $self, $unique ) = @_;
Subroutine _keyword_uniqueItems: called CC=2 SCAR=7.1
85  
86 219 ▶ return unless $unique; # unique false? all is good
Branch: 1 branch
BranchTrueFalseTotal
unless $unique 0 219 219
87  
88 219 return UniqueItems;
89 }
90  
91 sub _keyword_dependencies {
92 27 ▶ my( $self, $dependencies ) = @_;
Subroutine _keyword_dependencies: called CC=1 SCAR=0.0
93  
94 return Dependencies[
95 27 ▶ pairmap { $a => ref $b eq 'HASH' ? $self->sub_schema($b) : $b } %$dependencies
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'HASH' ? : 1 50 51
96 ];
97  
98 }
99  
100 sub _keyword_additionalProperties {
101 153 ▶ my( $self, $addi ) = @_;
Subroutine _keyword_additionalProperties: called CC=3 SCAR=11.0
102  
103 153 my $add_schema;
104 153 ▶ $add_schema = $self->sub_schema($addi) if ref $addi eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $addi eq "HASH" 147 6 153
105  
106 my @known_keys = (
107 153 eval { keys %{ $self->schema->{properties} } },
108 153 map { qr/$_/ } eval { keys %{ $self->schema->{patternProperties} } } );
109  
110 153 ▶ return AdditionalProperties[ \@known_keys, $add_schema ? $add_schema->type : $addi ];
Branch: 1 branch
BranchTrueFalseTotal
$add_schema ? : 147 6 153
111 }
112  
113 sub _keyword_patternProperties {
114 163 ▶ my( $self, $properties ) = @_;
Subroutine _keyword_patternProperties: called CC=1 SCAR=0.0
115  
116 my %prop_schemas = pairmap {
117 23 ▶ $a => $self->sub_schema($b)->type
Subroutine __ANON__: called CC=1 SCAR=0.0
118 163 } %$properties;
119  
120 163 return PatternProperties[ %prop_schemas ];
121 }
122  
123 sub _keyword_properties {
124 632 ▶ my( $self, $properties ) = @_;
Subroutine _keyword_properties: called CC=1 SCAR=0.0
125  
126 Properties[
127 pairmap {
128 1819 ▶ my $schema = $self->sub_schema($b);
Subroutine __ANON__: called CC=1 SCAR=0.0
129 1819 $a => $schema->type;
130 632 } %$properties
131 ];
132  
133 }
134  
135 sub _keyword_maxProperties {
136 2 ▶ my( $self, $max ) = @_;
Subroutine _keyword_maxProperties: called CC=1 SCAR=0.0
137  
138 2 MaxProperties[ $max ];
139 }
140  
141 sub _keyword_minProperties {
142 2 ▶ my( $self, $min ) = @_;
Subroutine _keyword_minProperties: called CC=1 SCAR=0.0
143  
144 2 MinProperties[ $min ];
145 }
146  
147 sub _keyword_required {
148 420 ▶ my( $self, $required ) = @_;
Subroutine _keyword_required: called CC=1 SCAR=0.0
149  
150 420 Required[@$required];
151 }
152  
153 sub _keyword_not {
154 10 ▶ my( $self, $schema ) = @_;
Subroutine _keyword_not: called CC=1 SCAR=0.0
155 10 Not[ $self->sub_schema($schema) ];
156 }
157  
158 sub _keyword_oneOf {
159 9 ▶ my( $self, $options ) = @_;
Subroutine _keyword_oneOf: called CC=1 SCAR=0.0
160  
161 9 OneOf[ map { $self->sub_schema( $_ ) } @$options ];
162 }
163  
164  
165 sub _keyword_anyOf {
166 151 ▶ my( $self, $options ) = @_;
Subroutine _keyword_anyOf: called CC=1 SCAR=0.0
167  
168 151 AnyOf[ map { $self->sub_schema($_)->type } @$options ];
169 }
170  
171 sub _keyword_allOf {
172 47 ▶ my( $self, $options ) = @_;
Subroutine _keyword_allOf: called CC=1 SCAR=0.0
173  
174 47 AllOf[ map { $self->sub_schema($_)->type } @$options ];
175 }
176  
177 sub _keyword_type {
178 2006 ▶ my( $self, $struct_type ) = @_;
Subroutine _keyword_type: called CC=4 SCAR=13.9
179  
180 my %keyword_map = map {
181 2006 lc $_->name => $_
182 } Integer, Number, String, Object, Array, Boolean, Null;
183  
184 2006 ▶ unless( $self->strict_string ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->strict_string) 9 1997 2006
185 9 $keyword_map{number} = LaxNumber;
186 9 $keyword_map{integer} = LaxInteger;
187 9 $keyword_map{string} = LaxString;
188 }
189  
190  
191 return $keyword_map{$struct_type}
192 2006 ▶ if $keyword_map{$struct_type};
Branch: 1 branch
BranchTrueFalseTotal
if $keyword_map{$struct_type} 1998 8 2006
193  
194 8 ▶ if( ref $struct_type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $struct_type eq "ARRAY") 8 0 8
195 8 return AnyOf[map { $self->_keyword_type($_) } @$struct_type];
196 }
197  
198 0 return;
199 }
200  
201 sub _keyword_multipleOf {
202 6 ▶ my( $self, $num ) = @_;
Subroutine _keyword_multipleOf: called CC=1 SCAR=0.0
203  
204 6 MultipleOf[$num];
205 };
206  
207 sub _keyword_maxItems {
208 7 ▶ my( $self, $max ) = @_;
Subroutine _keyword_maxItems: called CC=1 SCAR=0.0
209  
210 7 MaxItems[$max];
211 }
212  
213 sub _keyword_minItems {
214 232 ▶ my( $self, $min ) = @_;
Subroutine _keyword_minItems: called CC=1 SCAR=0.0
215  
216 232 MinItems[$min];
217 }
218  
219 sub _keyword_maxLength {
220 8 ▶ my( $self, $max ) = @_;
Subroutine _keyword_maxLength: called CC=1 SCAR=0.0
221  
222 8 MaxLength[$max];
223 }
224  
225 sub _keyword_minLength {
226 10 ▶ my( $self, $min ) = @_;
Subroutine _keyword_minLength: called CC=1 SCAR=0.0
227  
228 10 return MinLength[$min];
229 }
230  
231 sub _keyword_maximum {
232 13 ▶ my( $self, $maximum ) = @_;
Subroutine _keyword_maximum: called CC=2 SCAR=6.9
233  
234 return $self->schema->{exclusiveMaximum}
235 13 ▶ ? ExclusiveMaximum[$maximum]
Branch: 1 branch
BranchTrueFalseTotal
$self->schema->{'exclusiveMaximum'} ? : 2 11 13
236 : Maximum[$maximum];
237  
238 }
239  
240 sub _keyword_minimum {
241 123 ▶ my( $self, $minimum ) = @_;
Subroutine _keyword_minimum: called CC=2 SCAR=6.9
242  
243 123 ▶ if ( $self->schema->{exclusiveMinimum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->schema->{"exclusiveMinimum"}) 36 87 123
244 36 return ExclusiveMinimum[$minimum];
245 }
246  
247 87 return Minimum[$minimum];
248 }
249  
250 sub _keyword_additionalItems {
251 12 ▶ my( $self, $s ) = @_;
Subroutine _keyword_additionalItems: called CC=4 SCAR=13.9
252  
253 12 ▶ unless($s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s) 9 3 12
254 9 ▶ my $items = $self->schema->{items} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $items = $self->schema->{"items"} or return 3 6 9
255 6 ▶ return if ref $items eq 'HASH'; # it's a schema, nevermind
Branch: 1 branch
BranchTrueFalseTotal
if ref $items eq "HASH" 3 3 6
256 3 my $size = @$items;
257  
258 3 return AdditionalItems[$size];
259 }
260  
261 3 my $schema = $self->sub_schema($s);
262  
263 3 my $to_skip = @{ $self->schema->{items} };
264  
265 3 return AdditionalItems[$to_skip,$schema];
266  
267 }
268  
269 sub _keyword_items {
270 289 ▶ my( $self, $items ) = @_;
Subroutine _keyword_items: called CC=4 SCAR=14.0
271  
272 289 ▶ if ( Boolean->check($items) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Boolean()->check($items)) 0 289 289
273 0 return Items[$items];
274 }
275  
276 289 ▶ if( ref $items eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $items eq "HASH") 279 10 289
277 279 my $type = $self->sub_schema($items)->type;
278  
279 279 return Items[$type];
280 }
281  
282 # TODO forward declaration not workie
283 10 my @types;
284 10 for ( @$items ) {
285 16 push @types, $self->sub_schema($_)->type;
286 }
287  
288 10 return Items[\@types];
289 }
290  
291 JSON::Schema::AsType->new(
292 specification => 'draft4',
293 uri => "http${_}://json-schema.org/draft-04/schema",
294 12 schema => from_json <<'END_JSON' )->type for '', 's';
295 {
296 "id": "http://json-schema.org/draft-04/schema#",
297 "$schema": "http://json-schema.org/draft-04/schema#",
298 "description": "Core schema meta-schema",
299 "definitions": {
300 "schemaArray": {
301 "type": "array",
302 "minItems": 1,
303 "items": { "$ref": "#" }
304 },
305 "positiveInteger": {
306 "type": "integer",
307 "minimum": 0
308 },
309 "positiveIntegerDefault0": {
310 "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
311 },
312 "simpleTypes": {
313 "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
314 },
315 "stringArray": {
316 "type": "array",
317 "items": { "type": "string" },
318 "minItems": 1,
319 "uniqueItems": true
320 }
321 },
322 "type": "object",
323 "properties": {
324 "id": {
325 "type": "string",
326 "format": "uri"
327 },
328 "$schema": {
329 "type": "string",
330 "format": "uri"
331 },
332 "title": {
333 "type": "string"
334 },
335 "description": {
336 "type": "string"
337 },
338 "default": {},
339 "multipleOf": {
340 "type": "number",
341 "minimum": 0,
342 "exclusiveMinimum": true
343 },
344 "maximum": {
345 "type": "number"
346 },
347 "exclusiveMaximum": {
348 "type": "boolean",
349 "default": false
350 },
351 "minimum": {
352 "type": "number"
353 },
354 "exclusiveMinimum": {
355 "type": "boolean",
356 "default": false
357 },
358 "maxLength": { "$ref": "#/definitions/positiveInteger" },
359 "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
360 "pattern": {
361 "type": "string",
362 "format": "regex"
363 },
364 "additionalItems": {
365 "anyOf": [
366 { "type": "boolean" },
367 { "$ref": "#" }
368 ],
369 "default": {}
370 },
371 "items": {
372 "anyOf": [
373 { "$ref": "#" },
374 { "$ref": "#/definitions/schemaArray" }
375 ],
376 "default": {}
377 },
378 "maxItems": { "$ref": "#/definitions/positiveInteger" },
379 "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
380 "uniqueItems": {
381 "type": "boolean",
382 "default": false
383 },
384 "maxProperties": { "$ref": "#/definitions/positiveInteger" },
385 "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
386 "required": { "$ref": "#/definitions/stringArray" },
387 "additionalProperties": {
388 "anyOf": [
389 { "type": "boolean" },
390 { "$ref": "#" }
391 ],
392 "default": {}
393 },
394 "definitions": {
395 "type": "object",
396 "additionalProperties": { "$ref": "#" },
397 "default": {}
398 },
399 "properties": {
400 "type": "object",
401 "additionalProperties": { "$ref": "#" },
402 "default": {}
403 },
404 "patternProperties": {
405 "type": "object",
406 "additionalProperties": { "$ref": "#" },
407 "default": {}
408 },
409 "dependencies": {
410 "type": "object",
411 "additionalProperties": {
412 "anyOf": [
413 { "$ref": "#" },
414 { "$ref": "#/definitions/stringArray" }
415 ]
416 }
417 },
418 "enum": {
419 "type": "array",
420 "minItems": 1,
421 "uniqueItems": true
422 },
423 "type": {
424 "anyOf": [
425 { "$ref": "#/definitions/simpleTypes" },
426 {
427 "type": "array",
428 "items": { "$ref": "#/definitions/simpleTypes" },
429 "minItems": 1,
430 "uniqueItems": true
431 }
432 ]
433 },
434 "allOf": { "$ref": "#/definitions/schemaArray" },
435 "anyOf": { "$ref": "#/definitions/schemaArray" },
436 "oneOf": { "$ref": "#/definitions/schemaArray" },
437 "not": { "$ref": "#" }
438 },
439 "dependencies": {
440 "exclusiveMaximum": [ "maximum" ],
441 "exclusiveMinimum": [ "minimum" ]
442 },
443 "default": {}
444 }
445 END_JSON
446  
447 12 1;
448  
449 __END__
450  
451 =pod
452  
453 =encoding UTF-8
454  
455 =head1 NAME
456  
457 JSON::Schema::AsType::Draft4 - Role processing draft4 JSON Schema
458  
459 =head1 VERSION
460  
461 version 0.4.4
462  
463 =head1 DESCRIPTION
464  
465 This role is not intended to be used directly. It is used internally
466 by L<JSON::Schema::AsType> objects.
467  
468 Importing this module auto-populate the Draft4 schema in the
469 L<JSON::Schema::AsType> schema cache.
470  
471 =head1 AUTHOR
472  
473 Yanick Champoux <yanick@babyl.dyndns.org>
474  
475 =head1 COPYRIGHT AND LICENSE
476  
477 This software is copyright (c) 2024 by Yanick Champoux.
478  
479 This is free software; you can redistribute it and/or modify it under
480 the same terms as the Perl 5 programming language system itself.
481  
482 =cut
« AsType/Draft6.pm ↑ Summary AsType/Draft3/Types.pm »