AsType/Draft3/Types.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 98.0% 99 / 101 CC 7 SCAR 6.1
CC
7
Cov
97%
__ANON__
5.0
__ANON__
2.1
__ANON__
2.0
CRAP 1.8
« AsType/Draft7.pm ↑ Summary AsType/Draft7/Types.pm »
1 package JSON::Schema::AsType::Draft3::Types;
2 8 our $AUTHORITY = 'cpan:YANICK';
3 8 $JSON::Schema::AsType::Draft3::Types::VERSION = '1.0.0';
4 # ABSTRACT: JSON-schema v3 keywords as types
5  
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Types::Standard qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
12 InstanceOf
13 Str StrictNum HashRef ArrayRef
14 Int
15 Dict slurpy Optional Any
16 Tuple
17 8 /;
18  
19 use Type::Library
20 8 ▶ -base,
Subroutine BEGIN: called CC=1 SCAR=0.0
21 -declare => qw(
22 Disallow
23 Extends
24 DivisibleBy
25  
26 Properties
27  
28 Dependencies Dependency
29  
30 Schema
31 8 );
32  
33 8 ▶use List::MoreUtils qw/ all any zip none /;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use List::Util qw/ pairs pairmap reduce uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 8 ▶use JSON qw/ to_json from_json /;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 8 ▶use JSON::Schema::AsType;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 8 ▶use JSON::Schema::AsType::Draft4::Types 'Not',
Subroutine BEGIN: called CC=1 SCAR=0.0
41 'Integer', 'MultipleOf',
42 'Boolean', 'Number', 'String', 'Null', 'Object', 'Array', 'LaxNumber',
43 8 'LaxInteger', 'LaxString';
44  
45 __PACKAGE__->meta->add_type($_)
46 8 for Integer, Boolean, Number, String, Null, Object, Array, LaxNumber,
47 LaxInteger, LaxString;
48  
49 5 ▶declare Dependencies, constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 5 my %deps = @_;
51  
52 5 ▶ 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
53 8 };
54  
55 7 ▶declare Dependency, constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 7 my ( $property, $dep ) = @_;
57  
58 33 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
59 33 ▶ return 1 unless Object->check($_);
Branch: 1 branch
BranchTrueFalseTotal
unless &Object()->check($_) 3 30 33
60 30 ▶ return 1 unless exists $_->{$property};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_->{$property} 20 10 30
61  
62 10 my $obj = $_;
63  
64 10 ▶ return all { exists $obj->{$_} } @$dep if ref $dep eq 'ARRAY';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ref $dep eq "ARRAY" 4 6 10
65 6 ▶ return exists $obj->{$dep} unless ref $dep;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dep 2 4 6
66  
67 4 return $dep->check($_);
68 }
69 7 };
70  
71 34 ▶declare Properties => constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 34 my $type = Dict [ @_, slurpy Any ];
73  
74 96 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
75 96 ▶ !Object->check($_) or $type->check($_);
Branch: 1 branch
BranchTrueFalseTotal
not &Object()->check($_) or $type->check($_) 9 87 96
76 }
77 34 };
78  
79 4 ▶declare Disallow => constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 4 Not [shift];
81 8 };
82  
83 6 ▶declare Extends => constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 6 ▶ reduce { $a & $b } @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
85 8 };
86  
87 3 ▶declare DivisibleBy => constraint_generator => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 3 MultipleOf [shift];
89 8 };
90  
91 8 declare Schema, as InstanceOf ['Type::Tiny'];
92  
93 1 ▶coerce Schema, from HashRef, via {
Subroutine __ANON__: called CC=2 SCAR=7.4
94 1 my $schema = JSON::Schema::AsType->new( draft => 3, schema => $_ );
95  
96 1 ▶ if ( $schema->validate_schema ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->validate_schema) 0 1 1
97 0 die "not a valid draft3 json schema\n";
98 }
99  
100 $schema->type
101 1 };
102  
103 8 1;
104  
105 __END__
« AsType/Draft7.pm ↑ Summary AsType/Draft7/Types.pm »