lib/JSON/Structure/Types.pm

filter: Statementstmt 91.9% 126 / 137 Branchbran 70.0% 7 / 10 Conditioncond 83.3% 20 / 24 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 91.6% 44 / 48 Podpod 57.1% 4 / 7 totaltotal 87.4% 209 / 239 CC 17 SCAR 8.1
CC
17
Cov
89%
new
6.4
merge
6.0
to_string
4.0
CRAP 2.2
« bin/pjstruct ↑ Summary blib/lib/JSON/Structure.pm »
1 package JSON::Structure::Types;
2  
3 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶ use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 our $VERSION = '0.8.2';
8  
9 18 ▶ use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 our @EXPORT_OK = qw(
12 make_validation_result
13 make_validation_error
14 make_json_location
15 SEVERITY_ERROR
16 SEVERITY_WARNING
17 PRIMITIVE_TYPES
18 COMPOUND_TYPES
19 ALL_TYPES
20 NUMERIC_TYPES
21 is_valid_type
22 is_numeric_type
23 is_primitive_type
24 is_compound_type
25 );
26  
27 18 our %EXPORT_TAGS = ( all => \@EXPORT_OK, );
28  
29 =head1 NAME
30  
31 JSON::Structure::Types - Type definitions for JSON Structure validation
32  
33 =head1 DESCRIPTION
34  
35 This module provides type definitions and constants used throughout
36 the JSON Structure validation library.
37  
38 =cut
39  
40 # Primitive types from JSON Structure Core specification
41 18 ▶ use constant PRIMITIVE_TYPES => [
Subroutine BEGIN: called CC=1 SCAR=0.0
42 qw(
43 string boolean null
44 int8 uint8 int16 uint16 int32 uint32 int64 uint64 int128 uint128
45 float float8 double decimal
46 number integer
47 date datetime time duration
48 uuid uri binary jsonpointer
49 )
50 18 ];
51  
52 # Compound types from JSON Structure Core specification
53 18 ▶ use constant COMPOUND_TYPES => [
Subroutine BEGIN: called CC=1 SCAR=0.0
54 qw(
55 object array set map tuple choice any
56 )
57 18 ];
58  
59 # All valid types
60 18 ▶ use constant ALL_TYPES => [ @{ PRIMITIVE_TYPES() }, @{ COMPOUND_TYPES() } ];
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 # Numeric types
63 18 ▶ use constant NUMERIC_TYPES => [
Subroutine BEGIN: called CC=1 SCAR=0.0
64 qw(
65 number integer float double decimal float8
66 int8 uint8 int16 uint16 int32 uint32 int64 uint64 int128 uint128
67 )
68 18 ];
69  
70 # Create lookup hashes for O(1) type checking
71 18 my %_primitive_types = map { $_ => 1 } @{ PRIMITIVE_TYPES() };
72 18 my %_compound_types = map { $_ => 1 } @{ COMPOUND_TYPES() };
73 18 my %_all_types = map { $_ => 1 } @{ ALL_TYPES() };
74 18 my %_numeric_types = map { $_ => 1 } @{ NUMERIC_TYPES() };
75  
76 =head1 FUNCTIONS
77  
78 =head2 is_valid_type($type)
79  
80 Returns true if the given type name is a valid JSON Structure type.
81  
82 =cut
83  
84 4 ▶ sub is_valid_type {
Subroutine is_valid_type: called CC=1 SCAR=0.0
Pod documented
85 4 my ($type) = @_;
86 4 return exists $_all_types{$type};
87 }
88  
89 =head2 is_primitive_type($type)
90  
91 Returns true if the given type is a primitive type.
92  
93 =cut
94  
95 3 ▶ sub is_primitive_type {
Subroutine is_primitive_type: called CC=1 SCAR=0.0
Pod documented
96 3 my ($type) = @_;
97 3 return exists $_primitive_types{$type};
98 }
99  
100 =head2 is_compound_type($type)
101  
102 Returns true if the given type is a compound type.
103  
104 =cut
105  
106 3 ▶ sub is_compound_type {
Subroutine is_compound_type: called CC=1 SCAR=0.0
Pod documented
107 3 my ($type) = @_;
108 3 return exists $_compound_types{$type};
109 }
110  
111 =head2 is_numeric_type($type)
112  
113 Returns true if the given type is a numeric type.
114  
115 =cut
116  
117 3 ▶ sub is_numeric_type {
Subroutine is_numeric_type: called CC=1 SCAR=0.0
Pod documented
118 3 my ($type) = @_;
119 3 return exists $_numeric_types{$type};
120 }
121  
122 #############################################################################
123 # JsonLocation - Represents a location in a JSON document
124 #############################################################################
125  
126 package JSON::Structure::Types::JsonLocation;
127  
128 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
130  
131 =head1 JSON::Structure::Types::JsonLocation
132  
133 Represents a location in a JSON document with line and column information.
134  
135 =cut
136  
137 292 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
138 292 my ( $class, %args ) = @_;
139 my $self = bless {
140 line => $args{line} // 0,
141 292 ▶ column => $args{column} // 0,
Condition100%4/4
$args{"line"} // 0
l!l
2911
$args{"column"} // 0
l!l
2911
MC/DC100%1/1
$args{"line"} // 0
$args{"line"}
MC/DC100%1/1
$args{"column"} // 0
$args{"column"}
Truth table100%2/2
A
A= $args{"line"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"column"}
Aresult
11
00
142 }, $class;
143 292 return $self;
144 }
145  
146 283 ▶ sub unknown {
Subroutine unknown: called CC=1 SCAR=0.0
147 283 my ($class) = @_;
148 283 return $class->new( line => 0, column => 0 );
149 }
150  
151 5 ▶ sub line { $_[0]->{line} }
Subroutine line: called CC=1 SCAR=0.0
152 4 ▶ sub column { $_[0]->{column} }
Subroutine column: called CC=1 SCAR=0.0
153  
154 20 ▶ sub is_known {
Subroutine is_known: called CC=2 SCAR=7.1
155 20 my ($self) = @_;
156 20 ▶ return $self->{line} > 0 && $self->{column} > 0;
Condition66%2/3
$self->{'line'} > 0 && $self->{'column'} > 0
!ll&&!rl&&r
10010
MC/DC50%1/2
$self->{'line'} > 0 && $self->{'column'} > 0
$self->{'line'} > 0 $self->{'column'} > 0
Truth table66%2/3
A && B
A= $self->{'line'} > 0B= $self->{'column'} > 0
ABresult
0X0
100
111
157 }
158  
159 3 ▶ sub to_string {
Subroutine to_string: called CC=2 SCAR=6.9
160 3 my ($self) = @_;
161 3 ▶ return $self->is_known ? "($self->{line}:$self->{column})" : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->is_known ? : 2 1 3
162 }
163  
164 #############################################################################
165 # ValidationSeverity - Enum for validation severity levels
166 #############################################################################
167  
168 package JSON::Structure::Types::ValidationSeverity;
169  
170 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 18 ▶ use constant ERROR => 'error';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 18 ▶ use constant WARNING => 'warning';
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 #############################################################################
177 # ValidationError - Represents a single validation error
178 #############################################################################
179  
180 package JSON::Structure::Types::ValidationError;
181  
182 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 =head1 JSON::Structure::Types::ValidationError
186  
187 Represents a validation error with code, message, and location information.
188  
189 =cut
190  
191 285 ▶ sub new {
Subroutine new: called CC=6 SCAR=18.5
192 285 my ( $class, %args ) = @_;
193 my $self = bless {
194 code => $args{code} // '',
195 message => $args{message} // '',
196 path => $args{path} // '',
197 severity => $args{severity}
198 // JSON::Structure::Types::ValidationSeverity::ERROR,
199 location => $args{location}
200 // JSON::Structure::Types::JsonLocation->unknown(),
201 schema_path => $args{schema_path},
202 285 ▶ }, $class;
Condition72%8/11
$args{"code"} // ""
l!l
2850
$args{"message"} // ""
l!l
2850
$args{"path"} // ""
l!l
2841
$args{"severity"} // "error"
l!l
2805
$args{"location"} // "JSON::Structure::Types::JsonLocation"->unknown
l!l&&r!l&&!r
27960
MC/DC0%0/1
$args{"code"} // ""
$args{"code"}
MC/DC0%0/1
$args{"message"} // ""
$args{"message"}
MC/DC100%1/1
$args{"path"} // ""
$args{"path"}
MC/DC100%1/1
$args{"severity"} // "error"
$args{"severity"}
MC/DC0%0/2
$args{"location"} // "JSON::Structure::Types::JsonLocation"->unknown
$args{"location"} "JSON::Structure::Types::JsonLocation"->unknown
Truth table50%1/2
A
A= $args{"code"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"message"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"path"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"severity"}
Aresult
11
00
Truth table66%2/3
A // B
A= $args{"location"}B= "JSON::Structure::Types::JsonLocation"->unknown
ABresult
1X1
011
000
203 285 return $self;
204 }
205  
206 39 ▶ sub code { $_[0]->{code} }
Subroutine code: called CC=1 SCAR=0.0
207 8 ▶ sub message { $_[0]->{message} }
Subroutine message: called CC=1 SCAR=0.0
208 10 ▶ sub path { $_[0]->{path} }
Subroutine path: called CC=1 SCAR=0.0
209 3 ▶ sub severity { $_[0]->{severity} }
Subroutine severity: called CC=1 SCAR=0.0
210 9 ▶ sub location { $_[0]->{location} }
Subroutine location: called CC=1 SCAR=0.0
211 7 ▶ sub schema_path { $_[0]->{schema_path} }
Subroutine schema_path: called CC=1 SCAR=0.0
212  
213 3 ▶ sub to_string {
Subroutine to_string: called CC=4 SCAR=13.9
214 3 my ($self) = @_;
215 3 my @parts;
216  
217 3 ▶ push @parts, $self->{path} if $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path"} 3 0 3
218 push @parts, $self->{location}->to_string
219 3 ▶ if $self->{location}->is_known;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"location"}->is_known 1 2 3
220 3 push @parts, "[$self->{code}]";
221 3 push @parts, $self->{message};
222 push @parts, "(schema: $self->{schema_path})"
223 3 ▶ if defined $self->{schema_path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"schema_path"} 1 2 3
224  
225 3 return join( ' ', @parts );
226 }
227  
228 #############################################################################
229 # ValidationResult - Represents the result of a validation operation
230 #############################################################################
231  
232 package JSON::Structure::Types::ValidationResult;
233  
234 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
236  
237 =head1 JSON::Structure::Types::ValidationResult
238  
239 Represents the result of a validation operation.
240  
241 =cut
242  
243 571 ▶ sub new {
Subroutine new: called CC=4 SCAR=13.9
244 571 my ( $class, %args ) = @_;
245 my $self = bless {
246 is_valid => $args{is_valid} // 1,
247 errors => $args{errors} // [],
248 571 ▶ warnings => $args{warnings} // [],
Condition100%6/6
$args{"is_valid"} // 1
l!l
5701
$args{"errors"} // []
l!l
5674
$args{"warnings"} // []
l!l
5665
MC/DC100%1/1
$args{"is_valid"} // 1
$args{"is_valid"}
MC/DC100%1/1
$args{"errors"} // []
$args{"errors"}
MC/DC100%1/1
$args{"warnings"} // []
$args{"warnings"}
Truth table100%2/2
A
A= $args{"is_valid"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"errors"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"warnings"}
Aresult
11
00
249 }, $class;
250 571 return $self;
251 }
252  
253 573 ▶ sub is_valid { $_[0]->{is_valid} }
Subroutine is_valid: called CC=1 SCAR=0.0
254 50 ▶ sub errors { $_[0]->{errors} }
Subroutine errors: called CC=1 SCAR=0.0
255 2 ▶ sub warnings { $_[0]->{warnings} }
Subroutine warnings: called CC=1 SCAR=0.0
256  
257 2 ▶ sub add_error {
Subroutine add_error: called CC=1 SCAR=0.0
258 2 my ( $self, $error ) = @_;
259 2 push @{ $self->{errors} }, $error;
260 2 $self->{is_valid} = 0;
261 }
262  
263 1 ▶ sub add_warning {
Subroutine add_warning: called CC=1 SCAR=0.0
264 1 my ( $self, $warning ) = @_;
265 1 push @{ $self->{warnings} }, $warning;
266 }
267  
268 0 ▶ sub merge {
Subroutine merge: not called CC=2 SCAR=17.9
269 0 my ( $self, $other ) = @_;
270 0 push @{ $self->{errors} }, @{ $other->errors };
271 0 push @{ $self->{warnings} }, @{ $other->warnings };
272 0 ▶ $self->{is_valid} = 0 if !$other->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if not $other->is_valid 0 0 0
273 }
274  
275 # Export convenience constructors and constants
276 package JSON::Structure::Types;
277  
278 0 ▶ sub make_validation_result {
Subroutine make_validation_result: not called CC=1 SCAR=6.9
Pod undocumented
279 0 JSON::Structure::Types::ValidationResult->new(@_);
280 }
281  
282 0 ▶ sub make_validation_error {
Subroutine make_validation_error: not called CC=1 SCAR=6.9
Pod undocumented
283 0 JSON::Structure::Types::ValidationError->new(@_);
284 }
285 0 ▶ sub make_json_location { JSON::Structure::Types::JsonLocation->new(@_) }
Subroutine make_json_location: not called CC=1 SCAR=6.9
Pod undocumented
286  
287 # Severity constants
288 18 ▶ use constant SEVERITY_ERROR =>
Subroutine BEGIN: called CC=1 SCAR=0.0
289 18 JSON::Structure::Types::ValidationSeverity::ERROR;
290 18 ▶ use constant SEVERITY_WARNING =>
Subroutine BEGIN: called CC=1 SCAR=0.0
291 18 JSON::Structure::Types::ValidationSeverity::WARNING;
292  
293 18 1;
294  
295 __END__
« bin/pjstruct ↑ Summary blib/lib/JSON/Structure.pm »