File Coverage

blib/lib/JSON/Schema/Draft201909/Annotation.pm
Criterion Covered Total %
statement 34 34 100.0
branch 1 2 50.0
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 48 49 97.9


line stmt bran cond sub pod time code
1 22     22   165 use strict;
  22         54  
  22         727  
2 22     22   135 use warnings;
  22         58  
  22         1219  
3             package JSON::Schema::Draft201909::Annotation;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Contains a single annotation from a JSON Schema evaluation
6              
7             our $VERSION = '0.028';
8              
9 22     22   465 use 5.016;
  22         90  
10 22     22   150 no if "$]" >= 5.031009, feature => 'indirect';
  22         60  
  22         240  
11 22     22   1196 no if "$]" >= 5.033001, feature => 'multidimensional';
  22         63  
  22         201  
12 22     22   1062 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  22         78  
  22         159  
13 22     22   1134 use strictures 2;
  22         174  
  22         964  
14 22     22   4193 use Moo;
  22         58  
  22         204  
15 22     22   8963 use MooX::TypeTiny;
  22         52  
  22         152  
16 22     22   18039 use Types::Standard 'Str';
  22         55  
  22         186  
17 22     22   12332 use namespace::clean;
  22         54  
  22         188  
18              
19             has [qw(
20             keyword
21             instance_location
22             keyword_location
23             )] => (
24             is => 'ro',
25             isa => Str,
26             required => 1,
27             );
28              
29             has absolute_keyword_location => (
30             is => 'ro',
31             isa => Str, # always a uri (absolute uri or uri reference)
32             coerce => sub { "$_[0]" },
33             );
34              
35             # https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.7.7.1
36             has annotation => (
37             is => 'ro',
38             required => 1,
39             );
40              
41             sub TO_JSON {
42 20     20 1 5959 my $self = shift;
43             return +{
44             # note that locations are json pointers, not uri fragments!
45 20 50       279 instanceLocation => $self->instance_location,
46             keywordLocation => $self->keyword_location,
47             !defined($self->absolute_keyword_location) ? ()
48             : ( absoluteKeywordLocation => $self->absolute_keyword_location ),
49             annotation => $self->annotation,
50             };
51             }
52              
53             1;
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =for stopwords schema fragmentless
62              
63             =head1 NAME
64              
65             JSON::Schema::Draft201909::Annotation - Contains a single annotation from a JSON Schema evaluation
66              
67             =head1 VERSION
68              
69             version 0.028
70              
71             =head1 SYNOPSIS
72              
73             use JSON::Schema::Draft201909;
74             my $js = JSON::Schema::Draft201909->new;
75             my $result = $js->evaluate($data, $schema);
76             my @annotations = $result->annotations;
77              
78             my $value = $annotations[0]->annotation;
79             my $instance_location = $annotations[0]->instance_location;
80              
81             my $annotations_encoded = encode_json(\@annotations);
82              
83             =head1 DESCRIPTION
84              
85             An instance of this class holds one annotation from evaluating a JSON Schema with
86             L<JSON::Schema::Draft201909>.
87              
88             =head1 ATTRIBUTES
89              
90             =head2 keyword
91              
92             The keyword that produced the annotation.
93              
94             =head2 instance_location
95              
96             The path in the instance where the annotation was produced; encoded as per the JSON Pointer
97             specification (L<RFC 6901|https://tools.ietf.org/html/rfc6901>).
98              
99             =head2 keyword_location
100              
101             The schema path taken during evaluation to arrive at the annotation; encoded as per the JSON Pointer
102             specification (L<RFC 6901|https://tools.ietf.org/html/rfc6901>).
103              
104             =head2 absolute_keyword_location
105              
106             The canonical URI or URI reference of the location in the schema where the error occurred; not
107             defined, if there is no base URI for the schema and no C<$ref> was followed. Note that this is not
108             actually fragmentless URI in most cases, as the indicated error will occur at a path
109             below the position where the most recent identifier had been declared in the schema. Further, if the
110             schema never declared an absolute base URI (containing a scheme), this URI won't be absolute either.
111              
112             =head2 annotation
113              
114             The actual annotation value (which may or may not be a string).
115              
116             =head1 METHODS
117              
118             =head2 TO_JSON
119              
120             Returns a data structure suitable for serialization. Corresponds to one output unit as specified in
121             L<https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10.4.2> and
122             L<https://json-schema.org/draft/2019-09/output/schema>, except that C<instanceLocation> and
123             C<keywordLocation> are json pointers, B<not> URI fragments. (See the
124             C<strict_basic> L<JSON::Schema::Draft201909/output_format>
125             if the distinction is important to you.)
126              
127             =head1 SUPPORT
128              
129             Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Draft201909/issues>.
130              
131             I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
132              
133             =head1 AUTHOR
134              
135             Karen Etheridge <ether@cpan.org>
136              
137             =head1 COPYRIGHT AND LICENCE
138              
139             This software is copyright (c) 2020 by Karen Etheridge.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut