| 1 | package JSON::Schema::AsType::Draft6::Types; | ||||||||||
| 2 | 25 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | 25 | $JSON::Schema::AsType::Draft6::Types::VERSION = '1.0.0'; | |||||||||
| 4 | # ABSTRACT: JSON-schema v6 keywords as types | ||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 25 | ▶ | use 5.42.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 25 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 25 | ▶ | use experimental 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 25 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 25 | ▶ | use Types::Standard qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | Str StrictNum HashRef ArrayRef | ||||||||||
| 15 | Int | ||||||||||
| 16 | Dict slurpy Optional Any | ||||||||||
| 17 | Tuple | ||||||||||
| 18 | InstanceOf | ||||||||||
| 19 | 25 | /; | |||||||||
| 20 | |||||||||||
| 21 | use Type::Library | ||||||||||
| 22 | 25 | ▶ | -base, | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | -declare => qw( | ||||||||||
| 24 | PropertyNames | ||||||||||
| 25 | Contains | ||||||||||
| 26 | Schema | ||||||||||
| 27 | 25 | ); | |||||||||
| 28 | |||||||||||
| 29 | 25 | ▶ | use List::MoreUtils qw/ all any zip none /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 25 | ▶ | use List::Util qw/ pairs pairmap reduce uniq /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | |||||||||||
| 32 | 25 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | 25 | ▶ | use JSON::Schema::AsType::Draft4::Types qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | Integer Boolean Number String Null Object Array Items | ||||||||||
| 36 | ExclusiveMinimum ExclusiveMaximum Dependencies Dependency | ||||||||||
| 37 | Not MultipleOf | ||||||||||
| 38 | 25 | /; | |||||||||
| 39 | |||||||||||
| 40 | __PACKAGE__->meta->add_type($_) | ||||||||||
| 41 | 25 | for Integer, | |||||||||
| 42 | Boolean, Number, String, Null, Object, Array, Items, ExclusiveMaximum, | ||||||||||
| 43 | ExclusiveMinimum; | ||||||||||
| 44 | |||||||||||
| 45 | 27 | declare Contains, | |||||||||
| 46 | 27 | ▶ | constraint_generator => sub( $type, $min = 1, $max = 9E99 ) { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 47 | |||||||||||
| 48 | 101 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 49 | 101 | ▶ | return 1 unless Array->check($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 95 | my @contains = map { $type->check($_) } @$_; | |||||||||
| 52 | |||||||||||
| 53 | 95 | $JSON::Schema::AsType::CONTEXT{contains} = \@contains; | |||||||||
| 54 | |||||||||||
| 55 | 95 | return $min <= @contains <= $max; | |||||||||
| 56 | } | ||||||||||
| 57 | 27 | }; | |||||||||
| 58 | |||||||||||
| 59 | 41 | ▶ | declare PropertyNames, constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 60 | 41 | my $type = shift; | |||||||||
| 61 | 88 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 62 | 88 | ▶ | return 1 unless Object->check($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 76 | ▶ | return 1 if $type eq Any; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 68 | ▶ | return !keys %$_ if $type eq ~Any; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 60 | ▶ | return all { $type->check($_) } keys %$_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 41 | }; | |||||||||
| 69 | 25 | }; | |||||||||
| 70 | |||||||||||
| 71 | 11 | ▶ | declare Dependencies, constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 11 | my %deps = @_; | |||||||||
| 73 | |||||||||||
| 74 | 11 | ▶ | return reduce { $a & $b } pairmap { Dependency [ $a => $b ] } %deps; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 25 | }; | |||||||||
| 76 | |||||||||||
| 77 | 17 | ▶ | declare Dependency, constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 17 | my ( $property, $dep ) = @_; | |||||||||
| 79 | |||||||||||
| 80 | 130 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 81 | 130 | ▶ | return 1 unless Object->check($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 122 | ▶ | return 1 unless exists $_->{$property}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | 53 | my $obj = $_; | |||||||||
| 85 | |||||||||||
| 86 | 53 | ▶ | return all { exists $obj->{$_} } @$dep if ref $dep eq 'ARRAY'; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 87 | 30 | ▶ | return exists $obj->{$dep} unless ref $dep; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | |||||||||||
| 89 | 30 | return $dep->check($_); | |||||||||
| 90 | } | ||||||||||
| 91 | 17 | }; | |||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | declare Properties => constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.6
| |||||||||||
| 94 | 0 | my $type = Dict [ @_, slurpy Any ]; | |||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 97 | 0 | ▶ | !Object->check($_) or $type->check($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | } | ||||||||||
| 99 | 0 | }; | |||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | declare Disallow => constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 102 | 0 | Not [shift]; | |||||||||
| 103 | 25 | }; | |||||||||
| 104 | |||||||||||
| 105 | 0 | ▶ | declare Extends => constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | 0 | ▶ | reduce { $a & $b } @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 107 | 25 | }; | |||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | declare DivisibleBy => constraint_generator => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 110 | 0 | MultipleOf [shift]; | |||||||||
| 111 | 25 | }; | |||||||||
| 112 | |||||||||||
| 113 | 25 | declare Schema, as InstanceOf ['Type::Tiny']; | |||||||||
| 114 | |||||||||||
| 115 | 1 | ▶ | coerce Schema, from HashRef, via { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||
| 116 | 1 | my $schema = JSON::Schema::AsType->new( draft => 6, schema => $_ ); | |||||||||
| 117 | |||||||||||
| 118 | 1 | ▶ | if ( $schema->validate_schema ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | die "not a valid draft6 json schema\n"; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | $schema->type | ||||||||||
| 123 | 1 | }; | |||||||||
| 124 | |||||||||||
| 125 | 25 | 1; | |||||||||
| 126 | |||||||||||
| 127 | __END__ | ||||||||||