File Coverage

blib/lib/STIX/Schema.pm
Criterion Covered Total %
statement 38 38 100.0
branch n/a
condition 1 2 50.0
subroutine 13 13 100.0
pod 2 3 66.6
total 54 56 96.4


line stmt bran cond sub pod time code
1             package STIX::Schema;
2              
3 26     26   482 use 5.010001;
  26         102  
4 26     26   141 use strict;
  26         54  
  26         786  
5 26     26   158 use warnings;
  26         67  
  26         1531  
6 26     26   211 use utf8;
  26         74  
  26         218  
7              
8 26     26   2053 use Exporter 'import';
  26         152  
  26         1529  
9              
10 26     26   400 use File::Basename qw(dirname);
  26         251  
  26         2824  
11 26     26   12938 use File::Spec::Functions qw(catfile);
  26         28887  
  26         2196  
12 26     26   14199 use JSON::Validator;
  26         16945406  
  26         270  
13              
14 26     26   1515 use Moo;
  26         1292  
  26         306  
15              
16 26   50 26   13716 use constant DEBUG => $ENV{STIX_DEBUG} || 0;
  26         82  
  26         9134  
17              
18             has object => (is => 'ro');
19              
20 155     155 0 13484 sub schema_cache_path { catfile(dirname(__FILE__), 'cache') }
21              
22             sub validator {
23              
24 155     155 1 416 my ($self) = @_;
25              
26 155         2078 my $jv = JSON::Validator->new;
27              
28 155         3227 DEBUG and say sprintf('-- Load validator and use %s schema', $self->object->SCHEMA);
29              
30 155         595 $jv->cache_paths([schema_cache_path]);
31 155         9147 $jv->schema($self->object->SCHEMA)->schema->coerce('bool,num');
32              
33 155         23743867 return $jv;
34              
35             }
36              
37             sub validate {
38 155     155 1 37113 my ($self) = @_;
39 155         817 return $self->validator->validate($self->object);
40             }
41              
42             1;
43              
44             =encoding utf-8
45              
46             =head1 NAME
47              
48             STIX::Schema - JSON Schema Validator
49              
50             =head1 SYNOPSIS
51              
52             use STIX::Schema;
53              
54             my $validator = STIX::Schema->new(object => $indicator)->validator;
55              
56             my @errors = $validator->validate;
57              
58             say $_ for @errors;
59              
60              
61             =head1 DESCRIPTION
62              
63             Validate STIX objects using JSON Schema.
64              
65             =head2 METHODS
66              
67             =over
68              
69             =item STIX::Schema->new(object => $object)
70              
71             =item $schema->validator
72              
73             Return L object.
74              
75             =item $schema->validate
76              
77             Validate and return the L errors.
78              
79             =back
80              
81              
82             =head1 SUPPORT
83              
84             =head2 Bugs / Feature Requests
85              
86             Please report any bugs or feature requests through the issue tracker
87             at L.
88             You will be notified automatically of any progress on your issue.
89              
90             =head2 Source Code
91              
92             This is open source software. The code repository is available for
93             public review and contribution under the terms of the license.
94              
95             L
96              
97             git clone https://github.com/giterlizzi/perl-STIX.git
98              
99              
100             =head1 AUTHOR
101              
102             =over 4
103              
104             =item * Giuseppe Di Terlizzi
105              
106             =back
107              
108              
109             =head1 LICENSE AND COPYRIGHT
110              
111             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             =cut