File Coverage

blib/lib/JSON/Schema/Modern/Error.pm
Criterion Covered Total %
statement 64 65 98.4
branch 5 6 83.3
condition 2 2 100.0
subroutine 20 21 95.2
pod 2 3 66.6
total 93 97 95.8


line stmt bran cond sub pod time code
1 36     36   739 use strict;
  36         97  
  36         1318  
2 36     36   225 use warnings;
  36         118  
  36         1804  
3             package JSON::Schema::Modern::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.571';
8              
9 36     36   732 use 5.020;
  36         140  
10 36     36   1414 use Moo;
  36         14575  
  36         413  
11 36     36   20345 use strictures 2;
  36         506  
  36         1873  
12 36     36   8821 use stable 0.031 'postderef';
  36         740  
  36         381  
13 36     36   7065 use experimental 'signatures';
  36         104  
  36         211  
14 36     36   3417 use if "$]" >= 5.022, experimental => 're_strict';
  36         147  
  36         519  
15 36     36   3950 no if "$]" >= 5.031009, feature => 'indirect';
  36         104  
  36         360  
16 36     36   1873 no if "$]" >= 5.033001, feature => 'multidimensional';
  36         140  
  36         294  
17 36     36   1832 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  36         96  
  36         271  
18 36     36   2692 use Safe::Isa;
  36         1082  
  36         5896  
19 36     36   28694 use JSON::PP ();
  36         545920  
  36         1221  
20 36     36   1306 use MooX::TypeTiny;
  36         706  
  36         446  
21 36     36   54281 use Types::Standard qw(Str Undef InstanceOf Enum);
  36         243496  
  36         330  
22 36     36   88094 use namespace::clean;
  36         342738  
  36         534  
23              
24             use overload
25 36     36   15066 '""' => sub { $_[0]->stringify };
  36     0   116  
  36         318  
  0         0  
26              
27             has [qw(
28             instance_location
29             keyword_location
30             error
31             )] => (
32             is => 'ro',
33             isa => Str,
34             required => 1,
35             );
36              
37             has absolute_keyword_location => (
38             is => 'ro',
39             isa => InstanceOf['Mojo::URL'],
40             coerce => sub { $_[0]->$_isa('Mojo::URL') ? $_[0] : Mojo::URL->new($_[0]) },
41             );
42              
43             has keyword => (
44             is => 'ro',
45             isa => Str|Undef,
46             required => 1,
47             );
48              
49             has exception => (
50             is => 'ro',
51             isa => InstanceOf['JSON::PP::Boolean'],
52             coerce => sub { $_[0] ? JSON::PP::true : JSON::PP::false },
53             );
54              
55             has mode => (
56             is => 'rw',
57             isa => Enum[qw(traverse evaluate)],
58             );
59              
60 10015     10015 1 807913 sub TO_JSON ($self) {
  10015         17817  
  10015         14629  
61             return +{
62             # note that locations are JSON pointers, not uri fragments!
63 10015 100       169148 instanceLocation => $self->instance_location,
64             keywordLocation => $self->keyword_location,
65             !defined($self->absolute_keyword_location) ? ()
66             : ( absoluteKeywordLocation => $self->absolute_keyword_location->to_string ),
67             error => $self->error, # TODO: allow localization
68             };
69             }
70              
71 6     6 0 155 sub stringify ($self) {
  6         7  
  6         11  
72 6 100 100     91 ($self->mode//'evaluate') eq 'traverse'
73             ? '\''.$self->keyword_location.'\': '.$self->error
74             : '\''.$self->instance_location.'\': '.$self->error;
75             }
76              
77 30     30 1 2626 sub dump ($self) {
  30         59  
  30         43  
78 30         120 my $encoder = JSON::MaybeXS->new(utf8 => 0, convert_blessed => 1, canonical => 1, indent => 1, space_after => 1);
79 30 50       660 $encoder->indent_length(2) if $encoder->can('indent_length');
80 30         130 $encoder->encode($self);
81             }
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =for stopwords schema fragmentless
92              
93             =head1 NAME
94              
95             JSON::Schema::Modern::Error - Contains a single error from a JSON Schema evaluation
96              
97             =head1 VERSION
98              
99             version 0.571
100              
101             =head1 SYNOPSIS
102              
103             use JSON::Schema::Modern;
104             my $js = JSON::Schema::Modern->new;
105             my $result = $js->evaluate($data, $schema);
106             my @errors = $result->errors;
107              
108             my $message = $errors[0]->error;
109             my $instance_location = $errors[0]->instance_location;
110              
111             my $errors_encoded = encode_json(\@errors);
112              
113             =head1 DESCRIPTION
114              
115             An instance of this class holds one error from evaluating a JSON Schema with
116             L<JSON::Schema::Modern>.
117              
118             =head1 ATTRIBUTES
119              
120             =head2 keyword
121              
122             The keyword that produced the error; might be C<undef>.
123              
124             =head2 instance_location
125              
126             The path in the instance where the error occurred; encoded as per the JSON Pointer specification
127             (L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>).
128              
129             =head2 keyword_location
130              
131             The schema path taken during evaluation to arrive at the error; encoded as per the JSON Pointer
132             specification (L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>).
133              
134             =head2 absolute_keyword_location
135              
136             The canonical URI or URI reference of the location in the schema where the error occurred; not
137             defined, if there is no base URI for the schema and no C<$ref> was followed. Note that this is not
138             a fragmentless URI in most cases, as the indicated error will occur at a path
139             below the position where the most recent identifier had been declared in the schema. Further, if the
140             schema never declared an absolute base URI (containing a scheme), this URI won't be absolute either.
141              
142             =head2 error
143              
144             The actual error string.
145              
146             =head2 exception
147              
148             Indicates the error's severity is sufficient to stop evaluation.
149              
150             =head1 METHODS
151              
152             =for Pod::Coverage stringify mode
153              
154             =head2 TO_JSON
155              
156             Returns a data structure suitable for serialization. Corresponds to one output unit as specified in
157             L<https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10.4.2> and
158             L<https://json-schema.org/draft/2019-09/output/schema>, except that C<instanceLocation> and
159             C<keywordLocation> are JSON pointers, B<not> URI fragments. (See the
160             C<strict_basic> L<JSON::Schema::Modern/output_format>
161             if the distinction is important to you.)
162              
163             =head2 dump
164              
165             Returns a JSON string representing the error object, according to
166             the L<specification|https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10>.
167              
168             =for stopwords OpenAPI
169              
170             =head1 SUPPORT
171              
172             Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>.
173              
174             I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
175              
176             You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack
177             server|https://open-api.slack.com>, which are also great resources for finding help.
178              
179             =head1 AUTHOR
180              
181             Karen Etheridge <ether@cpan.org>
182              
183             =head1 COPYRIGHT AND LICENCE
184              
185             This software is copyright (c) 2020 by Karen Etheridge.
186              
187             This is free software; you can redistribute it and/or modify it under
188             the same terms as the Perl 5 programming language system itself.
189              
190             =cut