| 1 | package JSON::Schema::AsType::Draft2019_09::Types; | ||||||||||
| 2 | 16 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | 16 | $JSON::Schema::AsType::Draft2019_09::Types::VERSION = '1.0.0'; | |||||||||
| 4 | # ABSTRACT: Type::Tiny types for draft 2019-09 schemas | ||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 16 | ▶ | use 5.42.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 16 | ▶ | use feature qw/ module_true /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 16 | ▶ | use Hash::Merge qw/ merge /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 16 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 16 | ▶ | use Types::Standard qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | Str StrictNum HashRef ArrayRef | ||||||||||
| 16 | Int | ||||||||||
| 17 | Dict slurpy Optional Any | ||||||||||
| 18 | Tuple | ||||||||||
| 19 | InstanceOf | ||||||||||
| 20 | 16 | /; | |||||||||
| 21 | |||||||||||
| 22 | use Type::Library | ||||||||||
| 23 | 16 | ▶ | -base, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | -declare => qw( | ||||||||||
| 25 | DependentRequired | ||||||||||
| 26 | DependentSchemas | ||||||||||
| 27 | UnevaluatedProperties | ||||||||||
| 28 | UnevaluatedItems | ||||||||||
| 29 | 16 | ); | |||||||||
| 30 | |||||||||||
| 31 | 16 | ▶ | use List::MoreUtils qw/ zip none any all /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 16 | ▶ | use List::Util qw/ pairs pairmap reduce uniq /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 16 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | |||||||||||
| 36 | 16 | ▶ | use JSON::Schema::AsType::Annotations; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 16 | ▶ | use JSON::Schema::AsType::Draft4::Types qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | Integer Boolean Number String Null Object Array Items | ||||||||||
| 40 | ExclusiveMinimum ExclusiveMaximum Dependencies Dependency | ||||||||||
| 41 | Not MultipleOf | ||||||||||
| 42 | 16 | /; | |||||||||
| 43 | |||||||||||
| 44 | #__PACKAGE__->meta->add_type( $_ ) for Integer, Boolean, Number, String, Null, Object, Array, Items, ExclusiveMaximum, ExclusiveMinimum; | ||||||||||
| 45 | |||||||||||
| 46 | 4 | ▶ | declare DependentRequired => constraint_generator => sub($depends) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 40 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 48 | |||||||||||
| 49 | # only for objects | ||||||||||
| 50 | 40 | ▶ | return 1 unless ref eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 32 | for my ( $prop, $deps ) (%$depends) { | |||||||||
| 53 | 38 | ▶ | next unless exists $_->{$prop}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 22 | for my $d (@$deps) { | |||||||||
| 55 | 24 | ▶ | return 0 unless exists $_->{$d}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | 20 | return 1; | |||||||||
| 59 | } | ||||||||||
| 60 | 4 | }; | |||||||||
| 61 | |||||||||||
| 62 | 11 | ▶ | declare DependentSchemas => constraint_generator => sub($depends) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | |||||||||||
| 64 | 45 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 65 | |||||||||||
| 66 | # only for objects | ||||||||||
| 67 | 45 | ▶ | return 1 unless ref eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | |||||||||||
| 69 | 39 | for my ( $prop, $dep ) (%$depends) { | |||||||||
| 70 | 51 | ▶ | next unless exists $_->{$prop}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 29 | ▶ | return 0 unless $dep->check($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 19 | return 1; | |||||||||
| 75 | } | ||||||||||
| 76 | 11 | }; | |||||||||
| 77 | |||||||||||
| 78 | 9 | ▶ | declare UnevaluatedProperties => constraint_generator => sub($type) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | |||||||||||
| 80 | 325 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 81 | |||||||||||
| 82 | # only for objects | ||||||||||
| 83 | 325 | ▶ | return 1 unless ref eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 319 | my $target = $_; | |||||||||
| 86 | |||||||||||
| 87 | 319 | my %keys = map { $_ => 1 } annotation_properties(); | |||||||||
| 88 | |||||||||||
| 89 | 319 | my @keys = grep { !$keys{$_} } keys %$target; | |||||||||
| 90 | |||||||||||
| 91 | 319 | add_annotation( 'unevaluatedProperties', @keys ); | |||||||||
| 92 | |||||||||||
| 93 | 319 | ▶ | return all { $type->check($_) } map { $target->{$_} } @keys; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 94 | } | ||||||||||
| 95 | 9 | }; | |||||||||
| 96 | |||||||||||
| 97 | 8 | ▶ | declare UnevaluatedItems => constraint_generator => sub($type) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 98 | |||||||||||
| 99 | 219 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 100 | |||||||||||
| 101 | # only for arrays | ||||||||||
| 102 | 219 | ▶ | return 1 unless ref eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 213 | my $target = $_; | |||||||||
| 105 | |||||||||||
| 106 | 213 | my %indexes; | |||||||||
| 107 | |||||||||||
| 108 | 213 | $indexes{$_}++ for annotation_items(); | |||||||||
| 109 | |||||||||||
| 110 | 213 | for my $i ( grep { !$indexes{$_} } 0 .. $target->$#* ) { | |||||||||
| 111 | 45 | ▶ | return 0 unless $type->check( $target->[$i] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 21 | add_annotation( 'unevaluatedItems', $i ); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 189 | return 1; | |||||||||
| 116 | } | ||||||||||
| 117 | 8 | }; | |||||||||
| 118 | |||||||||||
| 119 | __END__ | ||||||||||