CycloneDX/Schema.pm

filter: Statementstmt 77.3% 41 / 53 Branchbran 0.0% 0 / 2 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 14 / 16 Podpod 100% 4 / 4 totaltotal 76.9% 60 / 78 CC 4 SCAR 10.6
CC
4
Cov
74%
validator
12.0
BEGIN
2.0
validate
2.0
CRAP 2.9
« CycloneDX/OrganizationalEntity.pm ↑ Summary CycloneDX/List.pm »
1 package SBOM::CycloneDX::Schema;
2  
3 16 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our @EXPORT = qw(
11 schema_dir
12 schema_file
13 );
14  
15 16 ▶use SBOM::CycloneDX;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 16 ▶use Types::Standard qw(HashRef InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 ▶use constant DEBUG => $ENV{SBOM_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'SBOM_DEBUG'} || 0
l!l
016
MC/DC0%0/1
$ENV{'SBOM_DEBUG'} || 0
$ENV{'SBOM_DEBUG'}
Truth table50%1/2
A
A= $ENV{'SBOM_DEBUG'}
Aresult
11
00
26  
27 16 our @JSON_SCHEMA_REGISTRY = (
28 'bom-1.2-strict.schema.json', 'bom-1.2.schema.json', 'bom-1.3-strict.schema.json', 'bom-1.3.schema.json',
29 'bom-1.4.schema.json', 'bom-1.5.schema.json', 'bom-1.6.schema.json', 'bom-1.7.schema.json',
30 'jsf-0.82.schema.json', 'spdx.schema.json', 'cryptography-defs.schema.json'
31 );
32  
33 16 has bom => (is => 'ro', isa => InstanceOf ['SBOM::CycloneDX'] | HashRef, required => 1);
34  
35 16 ▶sub schema_dir { catfile(dirname(__FILE__), 'schema') }
Subroutine schema_dir: called CC=1 SCAR=0.0
Pod documented
36 16 ▶sub schema_file { catfile(schema_dir, shift) }
Subroutine schema_file: called CC=1 SCAR=0.0
Pod documented
37  
38 0 ▶sub validator {
Subroutine validator: not called CC=3 SCAR=24.8
Pod documented
39  
40 0 my ($self) = @_;
41  
42 0 my $jv = JSON::Validator->new;
43  
44 0 foreach my $json_schema_file (@JSON_SCHEMA_REGISTRY) {
45 0 DEBUG and say sprintf('-- Preload JSON Schema file %s', $json_schema_file);
46 0 $jv->store->load(schema_file($json_schema_file));
47 }
48  
49 0 ▶ my $spec_version = (ref $self->bom eq 'HASH') ? $self->bom->{specVersion} : $self->bom->spec_version;
Branch: 1 branch
BranchTrueFalseTotal
ref $self->bom eq 'HASH' ? : 0 0 0
50 0 my $cyclonedx_json_schema = $SBOM::CycloneDX::JSON_SCHEMA{$spec_version};
51  
52 0 DEBUG and say sprintf('-- Use %s JSON schema for validation', $cyclonedx_json_schema);
53  
54 0 $jv->schema($cyclonedx_json_schema)->schema->coerce('bool,num');
55  
56 0 return $jv;
57  
58 }
59  
60 0 ▶sub validate {
Subroutine validate: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($self) = @_;
62 0 return $self->validator->validate($self->bom);
63 }
64  
65  
66 16 1;
67  
68 =encoding utf-8
69  
70 =head1 NAME
71  
72 SBOM::CycloneDX::Schema - JSON Schema Validator
73  
74 =head1 SYNOPSIS
75  
76 use SBOM::CycloneDX::Schema;
77  
78 my $validator = SBOM::CycloneDX::Schema->new(bom => sbom);
79  
80 my @errors = $validator->validate;
81  
82 say $_ for @errors;
83  
84  
85 =head1 DESCRIPTION
86  
87 Validate CycloneDX objects using JSON Schema.
88  
89 =head2 METHODS
90  
91 =over
92  
93 =item SBOM::CycloneDX::Schema->new(object => $object)
94  
95 =item $schema->bom
96  
97 L<SBOM::CycloneDX> instance or HASH.
98  
99 =item $schema->validator
100  
101 Return L<JSON::Validator> object.
102  
103 =item $schema->validate
104  
105 Validate and return the L<JSON::Validator> errors.
106  
107 =back
108  
109 =head2 FUNCTIONS
110  
111 =over
112  
113 =item schema_dir
114  
115 Return the CycloneDX schema path.
116  
117 =item schema_file ($json_schema_file)
118  
119 Return the CycloneDX schema file path.
120  
121 schema_file('bom-1.6.schema.json'); # ../SBOM/CycloneDX/schema/bom-1.6.schema.json
122  
123 =back
124  
125 =head1 SUPPORT
126  
127 =head2 Bugs / Feature Requests
128  
129 Please report any bugs or feature requests through the issue tracker
130 at L<https://github.com/giterlizzi/perl-SBOM-CycloneDX/issues>.
131 You will be notified automatically of any progress on your issue.
132  
133 =head2 Source Code
134  
135 This is open source software. The code repository is available for
136 public review and contribution under the terms of the license.
137  
138 L<https://github.com/giterlizzi/perl-SBOM-CycloneDX>
139  
140 git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git
141  
142  
143 =head1 AUTHOR
144  
145 =over 4
146  
147 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
148  
149 =back
150  
151  
152 =head1 LICENSE AND COPYRIGHT
153  
154 This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi.
155  
156 This is free software; you can redistribute it and/or modify it under
157 the same terms as the Perl 5 programming language system itself.
158  
159 =cut
« CycloneDX/OrganizationalEntity.pm ↑ Summary CycloneDX/List.pm »