AsType/Draft2019_09.pm

filter: Statementstmt 100% 100 / 100 Branchbran 97.2% 35 / 36 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 25 / 25 Podpod n/a totaltotal 99.3% 163 / 164 CC 22 SCAR 10.2
CC
22
Cov
99%
__ANON__
6.0
__ANON__
4.0
_has_id
4.0
CRAP 2.8
« AsType/Draft2020_12.pm ↑ Summary AsType/Draft6/Keywords.pm »
1 package JSON::Schema::AsType::Draft2019_09;
2 9 our $AUTHORITY = 'cpan:YANICK';
3 9 $JSON::Schema::AsType::Draft2019_09::VERSION = '1.0.0';
4 # ABSTRACT: A draft 2019-09 JSON Schema
5  
6  
7 9 ▶use 5.42.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use feature ':5.42', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON::Schema::AsType::Visit;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use List::Util qw/ pairmap uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use List::MoreUtils qw/ /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use List::UtilsBy qw/ nsort_by /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Moose::Util qw/ ensure_all_roles /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Module::Runtime qw/ use_module /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 9 extends qw/ JSON::Schema::AsType /;
23  
24 9 ▶use feature qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 9 my $_uri_port = 1;
27 has '+uri' => lazy => 1,
28 390 ▶ default => sub($self) {
Subroutine __ANON__: called CC=3 SCAR=11.0
29  
30 390 my $id = $self->_has_id( $self->schema );
31  
32 390 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 348 42 390
33 348 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->registered_schema($id) and do { do { $id = 'http://254.0.0.1:' . $_uri_port++ } } 0 348 348
34 348 $id = 'http://254.0.0.1:' . $_uri_port++;
35 } while $self->registered_schema($id);
36 }
37  
38 # TODO not required?
39 #$self->clear_parent_schema;
40  
41 390 return $id;
42 9 };
43  
44 9 has '+draft' => default => "2019-09";
45  
46 9 has is_own_metaschema => (
47 is => 'ro',
48 default => 0,
49 );
50  
51 has '+metaschema' => (
52 1760 ▶ default => sub($self) {
Subroutine __ANON__: called CC=6 SCAR=17.9
53 1760 ▶ return $self if $self->is_own_metaschema;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_own_metaschema 8 1752 1760
54  
55 1752 ▶ if ( ref $self->schema eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->schema eq "HASH") 1555 197 1752
56 1555 ▶ if ( my $uri = $self->schema->{'$schema'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = $self->schema->{"\$schema"}) 524 1031 1555
57 524 ▶ unless ( ref $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $uri) 516 8 524
58 516 return $self->fetch($uri);
59 }
60 }
61 }
62  
63 1236 ▶ return $self->parent_schema->metaschema if $self->parent_schema;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent_schema 1210 26 1236
64  
65 26 return _metaschema();
66 }
67 9 );
68  
69 has vocabularies => (
70 is => 'ro',
71 lazy => 1,
72 1760 ▶ default => sub($self) {
Subroutine __ANON__: called CC=2 SCAR=6.9
73  
74 1760 ▶ my $v = $self->metaschema->schema->{'$vocabulary'} or return [];
Branch: 1 branch
BranchTrueFalseTotal
my $v = $self->metaschema->schema->{"\$vocabulary"} or return [] 1749 11 1760
75  
76 1749 ▶ return [ pairmap { ($a) x !!$b } %$v ];
Subroutine __ANON__: called CC=1 SCAR=0.0
77 }
78 9 );
79  
80 %JSON::Schema::AsType::VOCABULARY = (
81 %JSON::Schema::AsType::VOCABULARY,
82 map {
83 9 m#([^/]+)$#;
84 54 $_ => __PACKAGE__ . '::Vocabulary::' . ucfirst($1) =~ s/-//r
85 } ( "https://json-schema.org/draft/2019-09/vocab/core",
86 "https://json-schema.org/draft/2019-09/vocab/applicator",
87 "https://json-schema.org/draft/2019-09/vocab/validation",
88 "https://json-schema.org/draft/2019-09/vocab/meta-data",
89 "https://json-schema.org/draft/2019-09/vocab/format",
90 "https://json-schema.org/draft/2019-09/vocab/content",
91 )
92 );
93  
94 # in D2019_09::Core ?
95 1760 ▶after _schema_trigger => sub ( $self, $schema, @ ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 JSON::Schema::AsType::Visit::visit(
97 $schema,
98 8513 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
99 8513 ▶ return unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 5819 2694 8513
100  
101 2694 ▶ my $anchor = $_->{'$anchor'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $anchor = $_->{"\$anchor"} or return 26 2668 2694
102  
103 26 my $uri = URI->new( $self->uri );
104  
105 26 ▶ if ( my $id = $self->_has_id($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_has_id($_)) 4 22 26
106 4 $uri = $self->resolve_uri($id);
107 }
108  
109 26 $uri->fragment($anchor);
110  
111 #$self->sub_schema( $_, $uri);
112 26 $self->register_schema( $uri => $_ );
113 26 return;
114 }
115 1760 );
116 9 };
117  
118 1353 ▶around sub_schema => sub ( $orig, $self, $subschema, $uri ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
119  
120 # ah AH, resolve the subschema id
121 1353 ▶ if ( my $id = $self->_has_id($subschema) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_has_id($subschema)) 187 1166 1353
122 187 ▶ $uri = $self->resolve_uri($id) unless $subschema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless $subschema->{"\$ref"} 156 31 187
123 187 $subschema->{'$id'} = "" . $uri; # TODO sane?
124 }
125 1353 $orig->( $self, $subschema, $uri );
126 9 };
127  
128 9 my %keyword_index = reverse indexed '$id', '$ref', '$recursiveRef',
129 qw/ properties items patternItems prefixItems patternProperties additionalProperties additionalItems
130 allOf anyOf oneOf if dependentSchemas unevaluatedProperties
131 unevaluatedItems /;
132  
133 1334 ▶override all_keywords => sub($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
134  
135 51938 ▶ return nsort_by {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 51938 ▶ $keyword_index{$_} // 999
Condition100%2/2
$keyword_index{$_} // 999
l!l
1998631952
MC/DC100%1/1
$keyword_index{$_} // 999
$keyword_index{$_}
Truth table100%2/2
A
A= $keyword_index{$_}
Aresult
11
00
137 }
138 1334 map { /^_keyword_(.*)/ } $self->meta->get_method_list;
139 9 };
140  
141 1760 ▶sub _schema_trigger( $self, $schema, @ ) {
Subroutine _schema_trigger: called CC=1 SCAR=0.0
142 JSON::Schema::AsType::Visit::visit(
143 $schema,
144 8513 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
145 8513 ▶ return unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 5819 2694 8513
146  
147 2694 ▶ my $id = $self->_has_id($_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->_has_id($_) or return 50 2644 2694
148  
149 50 $self->sub_schema( $_, $id );
150 50 return;
151 }
152 1760 );
153 }
154  
155 4673 ▶sub _has_id ( $self, $schema = {} ) {
Subroutine _has_id: called CC=4 SCAR=13.9
156 4673 ▶ return unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 199 4474 4673
157 4474 my $id = $schema->{'$id'};
158 4474 ▶ return if ref $id; # not a real $id
Branch: 1 branch
BranchTrueFalseTotal
if ref $id 30 4444 4474
159 4444 return $id;
160 }
161  
162 33 ▶sub _metaschema {
Subroutine _metaschema: called CC=3 SCAR=11.0
163 33 state @docs = ( from_json join '', <DATA> )->@*;
164  
165 33 state $METASCHEMA = __PACKAGE__->new(
166 uri => "https://json-schema.org/draft/2019-09/schema",
167 schema => shift @docs,
168 is_own_metaschema => 1,
169 );
170  
171 33 state $done = 0;
172  
173 33 ▶ if ( not $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 8 25 33
174 8 $METASCHEMA->register_schema(
175 "https://json-schema.org/v1" => $METASCHEMA, );
176 8 $METASCHEMA->register_schema( $_->{'$id'} => $_ ) for @docs;
177 8 $done++;
178 }
179  
180 33 return $METASCHEMA;
181 }
182  
183 =pod
184  
185 =encoding UTF-8
186  
187 =head1 NAME
188  
189 JSON::Schema::AsType::Draft2019_09 - A draft 2019-09 JSON Schema
190  
191 =head1 VERSION
192  
193 version 1.0.0
194  
195 =head1 DESCRIPTION
196  
197 Internal module for L<JSON::Schema:::AsType>.
198  
199 =head1 AUTHOR
200  
201 Yanick Champoux <yanick@babyl.dyndns.org>
202  
203 =head1 COPYRIGHT AND LICENSE
204  
205 This software is copyright (c) 2026 by Yanick Champoux.
206  
207 This is free software; you can redistribute it and/or modify it under
208 the same terms as the Perl 5 programming language system itself.
209  
210 =cut
211  
212 __DATA__
« AsType/Draft2020_12.pm ↑ Summary AsType/Draft6/Keywords.pm »