| 1 | package JSON::Schema::AsType::Draft2020_12::Types; | ||||||||||
| 2 | 10 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | 10 | $JSON::Schema::AsType::Draft2020_12::Types::VERSION = '1.0.0'; | |||||||||
| 4 | # ABSTRACT: Type::Tiny types for draft 2020-12 schemas | ||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 10 | ▶ | use 5.42.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 10 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 10 | ▶ | use feature qw/ module_true /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 10 | ▶ | use Hash::Merge qw/ merge /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 10 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 10 | ▶ | 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 | 10 | /; | |||||||||
| 21 | |||||||||||
| 22 | use Type::Library | ||||||||||
| 23 | 10 | ▶ | -base, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | -declare => qw( | ||||||||||
| 25 | PrefixItems | ||||||||||
| 26 | Contains | ||||||||||
| 27 | MinContains MaxContains | ||||||||||
| 28 | 10 | ); | |||||||||
| 29 | |||||||||||
| 30 | 10 | ▶ | use List::MoreUtils qw/ zip none any all /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 10 | ▶ | use List::Util qw/ pairs pairmap reduce uniq /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 10 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 10 | ▶ | use JSON::Schema::AsType::Annotations; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 10 | ▶ | use JSON::Schema::AsType::Draft4::Types qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | Integer Boolean Number String Null Object Array Items | ||||||||||
| 38 | ExclusiveMinimum ExclusiveMaximum Dependencies Dependency | ||||||||||
| 39 | Not MultipleOf | ||||||||||
| 40 | 10 | /; | |||||||||
| 41 | |||||||||||
| 42 | #__PACKAGE__->meta->add_type( $_ ) for Integer, Boolean, Number, String, Null, Object, Array, Items, ExclusiveMaximum, ExclusiveMinimum; | ||||||||||
| 43 | |||||||||||
| 44 | declare PrefixItems, | ||||||||||
| 45 | 16 | ▶ | constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.5
| |||||||||||
| 46 | 16 | my $types = shift; | |||||||||
| 47 | |||||||||||
| 48 | 16 | ▶ | if ( Boolean->check($types) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | ▶ | return $types ? Any : sub { !@$_ }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | my $type = | ||||||||||
| 53 | ref $types eq 'ARRAY' | ||||||||||
| 54 | 16 | ▶ | ? Tuple [ ( map { Optional [$_] } @$types ), slurpy Any ] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | : Tuple [ slurpy ArrayRef [$types] ]; | ||||||||||
| 56 | |||||||||||
| 57 | return ~ArrayRef | ( | ||||||||||
| 58 | 59 | ▶ | $type & sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||
| 59 | 59 | ▶ | if ( ref $types eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 59 | add_annotation( 'prefixItems' => 0 .. $types->$#* ); | |||||||||
| 61 | } | ||||||||||
| 62 | else { | ||||||||||
| 63 | 0 | add_annotation( 'prefixItems' => 0 .. $_->$#* ); | |||||||||
| 64 | } | ||||||||||
| 65 | 59 | return 1; | |||||||||
| 66 | } | ||||||||||
| 67 | 16 | ); | |||||||||
| 68 | |||||||||||
| 69 | 10 | }; | |||||||||
| 70 | |||||||||||
| 71 | 15 | ▶ | declare Contains, constraint_generator => sub($type) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 129 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 129 | ▶ | return any { $type->check($_) } @$_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 74 | } | ||||||||||
| 75 | 15 | }; | |||||||||
| 76 | |||||||||||
| 77 | 4 | ▶ | declare MinContains, constraint_generator => sub($min) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 25 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 25 | annotation_for('contains')->@* >= $min; | |||||||||
| 80 | } | ||||||||||
| 81 | 4 | }; | |||||||||
| 82 | |||||||||||
| 83 | 4 | ▶ | declare MaxContains, constraint_generator => sub($max) { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 84 | 16 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 16 | annotation_for('contains')->@* <= $max; | |||||||||
| 86 | } | ||||||||||
| 87 | 4 | }; | |||||||||
| 88 | |||||||||||
| 89 | 26 | ▶ | declare Items, constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=17.2
| |||||||||||
| 90 | 26 | ▶ | if ( @_ > 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 26 | my $to_skip = shift; | |||||||||
| 92 | 26 | my $schema = shift; | |||||||||
| 93 | 242 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=17.3
| |||||||||||
| 94 | |||||||||||
| 95 | 242 | ▶ | return unless ref eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 242 | my @v = @$_; | |||||||||
| 98 | |||||||||||
| 99 | 242 | my @additional = splice @v, $to_skip; | |||||||||
| 100 | |||||||||||
| 101 | 242 | ▶ | if ( ref $schema eq 'JSON::PP::Boolean' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | my $verdict = @additional; | |||||||||
| 103 | 0 | ▶ | $verdict = !$verdict unless $schema; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | return $verdict; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 242 | ▶ | return all { $schema->check($_) } @additional; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 108 | } | ||||||||||
| 109 | 26 | } | |||||||||
| 110 | else { | ||||||||||
| 111 | 0 | my $size = shift; | |||||||||
| 112 | 0 | ▶ | if ( ref $size eq 'JSON::PP::Boolean' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 114 | 0 | ▶ | my $s = ref($_) eq 'ARRAY' ? @_ : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | $DB::single = 1; | |||||||||
| 116 | 0 | ▶ | return !!$size ? $s : !$s; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | } | ||||||||||
| 118 | 0 | } | |||||||||
| 119 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 120 | 0 | ▶ | my $s = ref($_) eq 'ARRAY' ? @_ : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | $s <= $size; | |||||||||
| 122 | 0 | }; | |||||||||
| 123 | } | ||||||||||
| 124 | 10 | }; | |||||||||
| 125 | |||||||||||
| 126 | __END__ | ||||||||||