File Coverage

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


line stmt bran cond sub pod time code
1 22     22   172 use strict;
  22         57  
  22         805  
2 22     22   132 use warnings;
  22         44  
  22         1188  
3             package JSON::Schema::Draft201909::Error;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Contains a single error from a JSON Schema evaluation
6              
7             our $VERSION = '0.028';
8              
9 22     22   389 use 5.016;
  22         75  
10 22     22   134 no if "$]" >= 5.031009, feature => 'indirect';
  22         58  
  22         290  
11 22     22   1238 no if "$]" >= 5.033001, feature => 'multidimensional';
  22         59  
  22         145  
12 22     22   1175 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  22         61  
  22         143  
13 22     22   842 use strictures 2;
  22         248  
  22         959  
14 22     22   6628 use Moo;
  22         15236  
  22         198  
15 22     22   12639 use MooX::TypeTiny;
  22         695  
  22         195  
16 22     22   43021 use Types::Standard qw(Str Undef);
  22         211282  
  22         230  
17 22     22   26734 use namespace::clean;
  22         243954  
  22         173  
18              
19             has [qw(
20             instance_location
21             keyword_location
22             error
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             has keyword => (
36             is => 'ro',
37             isa => Str|Undef,
38             required => 1,
39             );
40              
41             sub TO_JSON {
42 5096     5096 1 276469 my $self = shift;
43             return +{
44             # note that locations are json pointers, not uri fragments!
45 5096 100       67397 instanceLocation => $self->instance_location,
46             keywordLocation => $self->keyword_location,
47             !defined($self->absolute_keyword_location) ? ()
48             : ( absoluteKeywordLocation => $self->absolute_keyword_location ),
49             error => $self->error, # TODO: allow localization
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::Error - Contains a single error 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 @errors = $result->errors;
77              
78             my $message = $errors[0]->error;
79             my $instance_location = $errors[0]->instance_location;
80              
81             my $errors_encoded = encode_json(\@errors);
82              
83             =head1 DESCRIPTION
84              
85             An instance of this class holds one error 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 error; might be C<undef>.
93              
94             =head2 instance_location
95              
96             The path in the instance where the error occurred; encoded as per the JSON Pointer specification
97             (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 error; 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             a 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 error
113              
114             The actual error 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