| 1 | package JSON::Schema::AsType::Draft6::Types; | ||||||||||
| 2 | 4 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | # ABSTRACT: JSON-schema v6 keywords as types | ||||||||||
| 4 | 4 | $JSON::Schema::AsType::Draft6::Types::VERSION = '0.4.4'; | |||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 4 | ▶ | use Type::Utils -all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | use Types::Standard qw/ | |||||||||
| 11 | Str StrictNum HashRef ArrayRef | ||||||||||
| 12 | Int | ||||||||||
| 13 | Dict slurpy Optional Any | ||||||||||
| 14 | Tuple | ||||||||||
| 15 | InstanceOf | ||||||||||
| 16 | 4 | ▶ | /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | use Type::Library | ||||||||||
| 19 | 4 | -base, | |||||||||
| 20 | -declare => qw( | ||||||||||
| 21 | PropertyNames | ||||||||||
| 22 | Contains | ||||||||||
| 23 | Schema | ||||||||||
| 24 | 4 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | 4 | ▶ | use List::MoreUtils qw/ all any zip none /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 4 | ▶ | use List::Util qw/ pairs pairmap reduce uniq /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 4 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 4 | use JSON::Schema::AsType::Draft4::Types qw/ | |||||||||
| 32 | Integer Boolean Number String Null Object Array Items | ||||||||||
| 33 | ExclusiveMinimum ExclusiveMaximum Dependencies Dependency | ||||||||||
| 34 | Not MultipleOf | ||||||||||
| 35 | 4 | ▶ | /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 4 | __PACKAGE__->meta->add_type( $_ ) for Integer, Boolean, Number, String, Null, Object, Array, Items, ExclusiveMaximum, ExclusiveMinimum; | |||||||||
| 38 | |||||||||||
| 39 | declare Contains, | ||||||||||
| 40 | constraint_generator => sub{ | ||||||||||
| 41 | 4 | ▶ | my $type = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 42 | |||||||||||
| 43 | return sub { | ||||||||||
| 44 | 19 | ▶ | return 1 unless Array->check($_); | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||
| 45 | |||||||||||
| 46 | 18 | ▶ | if( Boolean->check($type) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | return any { | ||||||||||
| 50 | 32 | ▶ | $type->check($_); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 18 | } @$_; | |||||||||
| 52 | } | ||||||||||
| 53 | 4 | }; | |||||||||
| 54 | |||||||||||
| 55 | declare PropertyNames, | ||||||||||
| 56 | constraint_generator => sub { | ||||||||||
| 57 | 5 | ▶ | my $type = shift; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 58 | return sub { | ||||||||||
| 59 | 10 | ▶ | return 1 unless Object->check($_); | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 9 | ▶ | return 1 if $type eq Any; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 7 | ▶ | return !keys %$_ if $type eq ~Any; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 4 | ▶ | return all { $type->check($_) } keys %$_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 65 | 5 | }; | |||||||||
| 66 | 4 | }; | |||||||||
| 67 | |||||||||||
| 68 | declare Dependencies, | ||||||||||
| 69 | constraint_generator => sub { | ||||||||||
| 70 | 5 | ▶ | my %deps = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 71 | |||||||||||
| 72 | 5 | ▶ | return reduce { $a & $b } pairmap { Dependency[$a => $b] } %deps; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 4 | }; | |||||||||
| 74 | |||||||||||
| 75 | declare Dependency, | ||||||||||
| 76 | constraint_generator => sub { | ||||||||||
| 77 | 6 | ▶ | my( $property, $dep) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | |||||||||||
| 79 | sub { | ||||||||||
| 80 | 47 | ▶ | return 1 unless Object->check($_); | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||
| 81 | 46 | ▶ | return 1 unless exists $_->{$property}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | |||||||||||
| 83 | 31 | my $obj = $_; | |||||||||
| 84 | |||||||||||
| 85 | 31 | ▶ | return all { exists $obj->{$_} } @$dep if ref $dep eq 'ARRAY'; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 86 | 20 | ▶ | return exists $obj->{$dep} unless ref $dep; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 20 | return $dep->check($_); | |||||||||
| 89 | } | ||||||||||
| 90 | 6 | }; | |||||||||
| 91 | |||||||||||
| 92 | declare Properties => | ||||||||||
| 93 | constraint_generator => sub { | ||||||||||
| 94 | 0 | ▶ | my $type = Dict[@_, slurpy Any]; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.6
| |||||||||||
| 95 | |||||||||||
| 96 | sub { | ||||||||||
| 97 | 0 | ▶ | ! Object->check($_) or $type->check($_) | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 98 | } | ||||||||||
| 99 | 0 | }; | |||||||||
| 100 | |||||||||||
| 101 | declare Disallow => | ||||||||||
| 102 | constraint_generator => sub { | ||||||||||
| 103 | 0 | ▶ | Not[ shift ]; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 104 | 4 | }; | |||||||||
| 105 | |||||||||||
| 106 | declare Extends => | ||||||||||
| 107 | constraint_generator => sub { | ||||||||||
| 108 | 0 | ▶ | reduce { $a & $b } @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 109 | 4 | }; | |||||||||
| 110 | |||||||||||
| 111 | declare DivisibleBy => | ||||||||||
| 112 | constraint_generator => sub { | ||||||||||
| 113 | 0 | ▶ | MultipleOf[shift]; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 114 | 4 | }; | |||||||||
| 115 | |||||||||||
| 116 | 4 | declare Schema, as InstanceOf['Type::Tiny']; | |||||||||
| 117 | |||||||||||
| 118 | coerce Schema, | ||||||||||
| 119 | from HashRef, | ||||||||||
| 120 | via { | ||||||||||
| 121 | 1 | ▶ | my $schema = JSON::Schema::AsType->new( draft_version => 6, schema => $_ ); | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.4
| |||||||||||
| 122 | |||||||||||
| 123 | 1 | ▶ | if ( $schema->validate_schema ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 0 | die "not a valid draft6 json schema\n"; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | $schema->type | ||||||||||
| 128 | 1 | }; | |||||||||
| 129 | |||||||||||
| 130 | 4 | 1; | |||||||||
| 131 | |||||||||||
| 132 | __END__ | ||||||||||
| 133 | |||||||||||
| 134 | =pod | ||||||||||
| 135 | |||||||||||
| 136 | =encoding UTF-8 | ||||||||||
| 137 | |||||||||||
| 138 | =head1 NAME | ||||||||||
| 139 | |||||||||||
| 140 | JSON::Schema::AsType::Draft6::Types - JSON-schema v6 keywords as types | ||||||||||
| 141 | |||||||||||
| 142 | =head1 VERSION | ||||||||||
| 143 | |||||||||||
| 144 | version 0.4.4 | ||||||||||
| 145 | |||||||||||
| 146 | =head1 SYNOPSIS | ||||||||||
| 147 | |||||||||||
| 148 | use JSON::Schema::AsType::Draft6::Types '-all'; | ||||||||||
| 149 | |||||||||||
| 150 | my $type = Object & | ||||||||||
| 151 | Properties[ | ||||||||||
| 152 | foo => Minimum[3] | ||||||||||
| 153 | ]; | ||||||||||
| 154 | |||||||||||
| 155 | $type->check({ foo => 5 }); # => 1 | ||||||||||
| 156 | $type->check({ foo => 1 }); # => 0 | ||||||||||
| 157 | |||||||||||
| 158 | =head1 EXPORTED TYPES | ||||||||||
| 159 | |||||||||||
| 160 | Null Boolean Array Object String Integer Pattern Number Enum | ||||||||||
| 161 | |||||||||||
| 162 | OneOf AllOf AnyOf | ||||||||||
| 163 | |||||||||||
| 164 | Not | ||||||||||
| 165 | |||||||||||
| 166 | Minimum ExclusiveMinimum Maximum ExclusiveMaximum MultipleOf | ||||||||||
| 167 | |||||||||||
| 168 | MaxLength MinLength | ||||||||||
| 169 | |||||||||||
| 170 | Items AdditionalItems MaxItems MinItems UniqueItems | ||||||||||
| 171 | |||||||||||
| 172 | PatternProperties AdditionalProperties MaxProperties MinProperties | ||||||||||
| 173 | |||||||||||
| 174 | Dependencies Dependency | ||||||||||
| 175 | |||||||||||
| 176 | =head2 Schema | ||||||||||
| 177 | |||||||||||
| 178 | Only verifies that the variable is a L<Type::Tiny>. | ||||||||||
| 179 | |||||||||||
| 180 | Can coerce the value from a hashref defining the schema. | ||||||||||
| 181 | |||||||||||
| 182 | my $schema = Schema->coerce( \%schema ); | ||||||||||
| 183 | |||||||||||
| 184 | # equivalent to | ||||||||||
| 185 | |||||||||||
| 186 | $schema = JSON::Schema::AsType::Draft4->new( | ||||||||||
| 187 | draft_version => 6, | ||||||||||
| 188 | schema => \%schema; | ||||||||||
| 189 | )->type; | ||||||||||
| 190 | |||||||||||
| 191 | =head1 AUTHOR | ||||||||||
| 192 | |||||||||||
| 193 | Yanick Champoux <yanick@babyl.dyndns.org> | ||||||||||
| 194 | |||||||||||
| 195 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 196 | |||||||||||
| 197 | This software is copyright (c) 2024 by Yanick Champoux. | ||||||||||
| 198 | |||||||||||
| 199 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 200 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 201 | |||||||||||
| 202 | =cut | ||||||||||