AsType/Draft2020_12.pm

filter: Statementstmt 100% 96 / 96 Branchbran 97.0% 33 / 34 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 99.3% 155 / 156 CC 21 SCAR 10.5
CC
21
Cov
99%
__ANON__
6.0
__ANON__
4.0
_has_id
4.0
CRAP 2.9
« AsType/Draft2019_09/Types.pm ↑ Summary AsType/Draft2019_09.pm »
1 package JSON::Schema::AsType::Draft2020_12;
2 8 our $AUTHORITY = 'cpan:YANICK';
3 8 $JSON::Schema::AsType::Draft2020_12::VERSION = '1.0.0';
4 # ABSTRACT: A draft 2020-12 JSON Schema
5  
6  
7 8 ▶use 5.42.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use feature ':5.42', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use JSON::Schema::AsType::Visit;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use List::Util qw/ pairmap uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use List::MoreUtils qw/ /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use List::UtilsBy qw/ nsort_by /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Moose::Util qw/ ensure_all_roles /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Module::Runtime qw/ use_module /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 extends qw/ JSON::Schema::AsType /;
23  
24 8 ▶use feature qw/ signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 8 my $_uri_port = 1;
27 has '+uri' => lazy => 1,
28 259 ▶ default => sub($self) {
Subroutine __ANON__: called CC=3 SCAR=11.0
29  
30 259 my $id = $self->_has_id( $self->schema );
31  
32 259 ▶ unless ($id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 250 9 259
33 250 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->registered_schema($id) and do { do { $id = 'http://254.0.0.1:' . $_uri_port++ } } 0 250 250
34 250 $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 259 return $id;
42 8 };
43  
44 8 has '+draft' => default => "2020-12";
45  
46 8 has is_own_metaschema => (
47 is => 'ro',
48 default => 0,
49 );
50  
51 has '+metaschema' => (
52 828 ▶ default => sub($self) {
Subroutine __ANON__: called CC=6 SCAR=17.9
53 828 ▶ return $self if $self->is_own_metaschema;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_own_metaschema 7 821 828
54  
55 821 ▶ if ( ref $self->schema eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->schema eq "HASH") 754 67 821
56 754 ▶ if ( my $uri = $self->schema->{'$schema'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = $self->schema->{"\$schema"}) 352 402 754
57 352 ▶ unless ( ref $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $uri) 345 7 352
58 345 return $self->fetch($uri);
59 }
60 }
61 }
62  
63 476 ▶ return $self->parent_schema->metaschema if $self->parent_schema;
Branch: 1 branch
BranchTrueFalseTotal
if $self->parent_schema 455 21 476
64  
65 21 return _metaschema();
66 }
67 8 );
68  
69 %JSON::Schema::AsType::VOCABULARY = (
70 %JSON::Schema::AsType::VOCABULARY,
71 map {
72 8 m#([^/]+)$#;
73 56 $_ => __PACKAGE__ . '::Vocabulary::' . ucfirst($1) =~ s/-//r
74 } ( "https://json-schema.org/draft/2020-12/vocab/core",
75 "https://json-schema.org/draft/2020-12/vocab/applicator",
76 "https://json-schema.org/draft/2020-12/vocab/validation",
77 "https://json-schema.org/draft/2020-12/vocab/unevaluated",
78 "https://json-schema.org/draft/2020-12/vocab/meta-data",
79 "https://json-schema.org/draft/2020-12/vocab/format-annotation",
80 "https://json-schema.org/draft/2020-12/vocab/content",
81 )
82 );
83  
84 # in D2019_09::Core ?
85 827 ▶after _schema_trigger => sub ( $self, $schema, @ ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 JSON::Schema::AsType::Visit::visit(
87 $schema,
88 4591 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
89 4591 ▶ return unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 3145 1446 4591
90  
91 1446 ▶ my $anchor = $_->{'$anchor'} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $anchor = $_->{"\$anchor"} or return 20 1426 1446
92  
93 20 my $uri = URI->new( $self->uri );
94  
95 20 ▶ if ( my $id = $self->_has_id($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_has_id($_)) 3 17 20
96 3 $uri = $self->resolve_uri($id);
97 }
98  
99 20 $uri->fragment($anchor);
100  
101 #$self->sub_schema( $_, $uri);
102 20 $self->register_schema( $uri => $_ );
103 20 return;
104 }
105 827 );
106 8 };
107  
108 552 ▶around sub_schema => sub ( $orig, $self, $subschema, $uri ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
109  
110 # ah AH, resolve the subschema id
111 552 ▶ if ( my $id = $self->_has_id($subschema) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_has_id($subschema)) 96 456 552
112 96 ▶ $uri = $self->resolve_uri($id) unless $subschema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless $subschema->{"\$ref"} 87 9 96
113 96 $subschema->{'$id'} = "" . $uri; # TODO sane?
114 }
115 552 $orig->( $self, $subschema, $uri );
116 8 };
117  
118 8 my %keyword_index = reverse indexed '$id', '$ref', '$recursiveRef', qw/
119 properties
120 items
121 contains
122 minContains
123 maxContains
124 prefixItems
125 patternItems
126 prefixItems
127 patternProperties
128 additionalProperties
129 additionalItems
130 allOf anyOf oneOf if dependentSchemas unevaluatedProperties
131 unevaluatedItems /;
132  
133 599 ▶override all_keywords => sub($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
134  
135 25068 ▶ return nsort_by {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 25068 ▶ $keyword_index{$_} // 999
Condition100%2/2
$keyword_index{$_} // 999
l!l
1134313725
MC/DC100%1/1
$keyword_index{$_} // 999
$keyword_index{$_}
Truth table100%2/2
A
A= $keyword_index{$_}
Aresult
11
00
137 }
138 599 map { /^_keyword_(.*)/ } $self->meta->get_method_list;
139 8 };
140  
141 827 ▶sub _schema_trigger( $self, $schema, @ ) {
Subroutine _schema_trigger: called CC=1 SCAR=0.0
142 JSON::Schema::AsType::Visit::visit(
143 $schema,
144 4591 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
145 4591 ▶ return unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 3145 1446 4591
146  
147 1446 ▶ my $id = $self->_has_id($_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->_has_id($_) or return 12 1434 1446
148  
149 12 $self->sub_schema( $_, $id );
150 12 return;
151 }
152 827 );
153 }
154  
155 2324 ▶sub _has_id ( $self, $schema = {} ) {
Subroutine _has_id: called CC=4 SCAR=13.9
156 2324 ▶ return unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 67 2257 2324
157 2257 my $id = $schema->{'$id'};
158 2257 ▶ return if ref $id; # not a real $id
Branch: 1 branch
BranchTrueFalseTotal
if ref $id 23 2234 2257
159 2234 return $id;
160 }
161  
162 22 ▶sub _metaschema {
Subroutine _metaschema: called CC=3 SCAR=11.0
163 22 state @docs = ( from_json join '', <DATA> )->@*;
164  
165 22 state $METASCHEMA = __PACKAGE__->new(
166 uri => "https://json-schema.org/draft/2020-12/schema",
167 schema => shift @docs,
168 is_own_metaschema => 1,
169 );
170  
171 22 state $done = 0;
172  
173 22 ▶ if ( not $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 7 15 22
174 7 $METASCHEMA->register_schema(
175 "https://json-schema.org/v1" => $METASCHEMA, );
176 7 $METASCHEMA->register_schema( $_->{'$id'} => $_ ) for @docs;
177 7 $done++;
178 }
179  
180 22 return $METASCHEMA;
181 }
182  
183 =pod
184  
185 =encoding UTF-8
186  
187 =head1 NAME
188  
189 JSON::Schema::AsType::Draft2020_12 - A draft 2020-12 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/Draft2019_09/Types.pm ↑ Summary AsType/Draft2019_09.pm »