| 1 | package JSON::Schema::AsType::Draft3::Keywords; | ||||||||||
| 2 | 6 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||
| 3 | 6 | $JSON::Schema::AsType::Draft3::Keywords::VERSION = '1.0.0'; | |||||||||
| 4 | # ABSTRACT: Role processing draft3 JSON Schema | ||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 6 | ▶ | use Type::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use Scalar::Util qw/ looks_like_number /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use List::Util qw/ reduce pairmap pairs /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | ▶ | use List::MoreUtils qw/ any all none uniq zip /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 6 | ▶ | use JSON::Schema::AsType; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 6 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 6 | ▶ | use JSON::Schema::AsType::Draft3::Types '-all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 6 | ▶ | use Types::Standard 'Optional'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 6 | with 'JSON::Schema::AsType::Draft4::Keywords' => | |||||||||
| 25 | { -excludes => [qw/ _keyword_properties _keyword_required _keyword_type /] | ||||||||||
| 26 | }; | ||||||||||
| 27 | |||||||||||
| 28 | 36 | ▶ | sub _keyword_properties { | ||||||||
|
Subroutine
_keyword_properties: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 36 | my ( $self, $properties ) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 128 | ▶ | my @props = pairmap { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 32 | { | ||||||||||
| 33 | 128 | my $schema = $self->sub_schema( $b, "#./properties/$a" ); | |||||||||
| 34 | 128 | my $p = $schema->type; | |||||||||
| 35 | 128 | ▶ | $p = Optional [$p] unless $b->{required}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 128 | $a => $p | |||||||||
| 37 | } | ||||||||||
| 38 | } | ||||||||||
| 39 | 36 | %$properties; | |||||||||
| 40 | |||||||||||
| 41 | 36 | return Properties [@props]; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 3 | ▶ | sub _keyword_disallow { | ||||||||
|
Subroutine
_keyword_disallow: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 3 | Disallow [ $_[0]->_keyword_type( $_[1] ) ]; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 6 | ▶ | sub _keyword_extends { | ||||||||
|
Subroutine
_keyword_extends: called CC=2 SCAR=6.9
| |||||||||||
| 49 | 6 | my ( $self, $extends ) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 6 | my $i = 0; | |||||||||
| 52 | my @extends = | ||||||||||
| 53 | ref $extends eq 'ARRAY' | ||||||||||
| 54 | 6 | ▶ | ? ( map { $self->sub_schema( $_, "#./extends/" . $i++ )->type } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | @$extends ) | ||||||||||
| 56 | : ( $self->sub_schema( $extends, "#./extends" )->type ); | ||||||||||
| 57 | |||||||||||
| 58 | 6 | return Extends [@extends]; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 246 | ▶ | sub _keyword_type { | ||||||||
|
Subroutine
_keyword_type: called CC=6 SCAR=17.9
| |||||||||||
| 62 | 246 | my ( $self, $struct_type ) = @_; | |||||||||
| 63 | |||||||||||
| 64 | 246 | my %type_map = map { lc $_->name => $_ } Integer, Boolean, Number, | |||||||||
| 65 | String, Null, Object, Array; | ||||||||||
| 66 | |||||||||||
| 67 | 246 | ▶ | unless ( $self->strict_string ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 9 | $type_map{number} = LaxNumber; | |||||||||
| 69 | 9 | $type_map{integer} = LaxInteger; | |||||||||
| 70 | 9 | $type_map{string} = LaxString; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 246 | ▶ | return if $struct_type eq 'any'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | |||||||||||
| 75 | 242 | ▶ | return $type_map{$struct_type} if $type_map{$struct_type}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 35 | ▶ | if ( my @types = eval { @$struct_type } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 35 | my $i = 0; | |||||||||
| 79 | 37 | ▶ | return reduce { $a | $b } map { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 35 | ▶ | ref $_ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | ? $self->sub_schema( $_, "#./type/" . $i++ )->type | ||||||||||
| 82 | : $self->_keyword_type($_) | ||||||||||
| 83 | } @types; | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | die "unknown type '$struct_type'"; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 3 | ▶ | sub _keyword_divisibleBy { | ||||||||
|
Subroutine
_keyword_divisibleBy: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 3 | my ( $self, $divisibleBy ) = @_; | |||||||||
| 91 | |||||||||||
| 92 | 3 | DivisibleBy [$divisibleBy]; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 6 | ▶ | sub _keyword_dependencies { | ||||||||
|
Subroutine
_keyword_dependencies: called CC=1 SCAR=0.0
| |||||||||||
| 96 | 6 | my ( $self, $dependencies ) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 6 | my $i = 0; | |||||||||
| 99 | return Dependencies [ | ||||||||||
| 100 | 9 | ▶ | pairmap { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 101 | 9 | ▶ | $a => ref $b eq 'HASH' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | ? $self->sub_schema( $b, '#./dependencies/' . $i++ )->type | ||||||||||
| 103 | : $b | ||||||||||
| 104 | 6 | } %$dependencies | |||||||||
| 105 | ]; | ||||||||||
| 106 | |||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 6 | 1; | |||||||||
| 110 | |||||||||||
| 111 | __END__ | ||||||||||