| 1 |
use strict; |
| 2 |
use warnings; |
| 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.650'; |
| 8 |
|
| 9 |
use 5.020; |
| 10 |
use Moo; |
| 11 |
with 'JSON::Schema::Modern::ResultNode'; |
| 12 |
use strictures 2; |
| 13 |
use stable 0.031 'postderef'; |
| 14 |
use experimental 'signatures'; |
| 15 |
no autovivification warn => qw(fetch store exists delete); |
| 16 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 17 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 18 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 19 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 20 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 21 |
no feature 'switch'; |
| 22 |
use MooX::TypeTiny; |
| 23 |
use Types::Standard qw(Str Bool Enum Tuple); |
| 24 |
use Types::Common::Numeric qw(PositiveInt); |
| 25 |
use builtin::compat 'refaddr'; |
| 26 |
use Mojo::Message::Response; |
| 27 |
use Carp 'croak'; |
| 28 |
use namespace::clean; |
| 29 |
|
| 30 |
use overload |
| 31 |
'""' => sub { $_[0]->stringify }, |
| 32 |
fallback => 1; |
| 33 |
|
| 34 |
has error => ( |
| 35 |
is => 'ro', |
| 36 |
isa => Str, |
| 37 |
required => 1, |
| 38 |
); |
| 39 |
|
| 40 |
has exception => ( |
| 41 |
is => 'ro', |
| 42 |
isa => Bool, |
| 43 |
); |
| 44 |
|
| 45 |
has mode => ( |
| 46 |
is => 'ro', |
| 47 |
isa => Enum[qw(traverse evaluate)], |
| 48 |
required => 1, |
| 49 |
); |
| 50 |
|
| 51 |
has recommended_response => ( |
| 52 |
is => 'ro', |
| 53 |
isa => Tuple[PositiveInt, Str], |
| 54 |
); |
| 55 |
|
| 56 |
sub stringify ($self) { |
| 57 |
$self->mode eq 'traverse' |
| 58 |
? '\''.$self->keyword_location.'\': '.$self->error |
| 59 |
: '\''.$self->instance_location.'\': '.$self->error; |
| 60 |
} |
| 61 |
|
| 62 |
sub clone ($self, %overrides) { |
| 63 |
$self->new( |
| 64 |
$self->%{qw(instance_location keyword_location keyword depth)}, |
| 65 |
(map +(exists $self->{$_} ? $self->%{$_} : ()), qw(absolute_keyword_location _uri recommended_response)), |
| 66 |
$self->%{qw(error exception mode)}, |
| 67 |
%overrides, |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
sub __thing { 'error' } |
| 72 |
|
| 73 |
around BUILDARGS => sub ($orig, $class, @args) { |
| 74 |
my $args = $class->$orig(@args); |
| 75 |
|
| 76 |
$args->{recommended_response}[1] = |
| 77 |
Mojo::Message::Response->default_message($args->{recommended_response}[0]) // 'Unknown Error' |
| 78 |
if $args->{recommended_response} and $args->{recommended_response}->@* == 1; |
| 79 |
|
| 80 |
croak 'instance_location must be defined when mode=evaluate' |
| 81 |
if not defined $args->{instance_location} and ($args->{mode}//'') eq 'evaluate'; |
| 82 |
|
| 83 |
return $args; |
| 84 |
}; |
| 85 |
|
| 86 |
1; |
| 87 |
|
| 88 |
__END__ |
| 89 |
|
| 90 |
=pod |
| 91 |
|
| 92 |
=encoding UTF-8 |
| 93 |
|
| 94 |
=for stopwords schema fragmentless subschema |
| 95 |
|
| 96 |
=head1 NAME |
| 97 |
|
| 98 |
JSON::Schema::Modern::Error - Contains a single error from a JSON Schema evaluation |
| 99 |
|
| 100 |
=head1 VERSION |
| 101 |
|
| 102 |
version 0.650 |
| 103 |
|
| 104 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 105 |
presumed or inferred from the version being less than 1.0. |
| 106 |
|
| 107 |
=head1 SYNOPSIS |
| 108 |
|
| 109 |
use JSON::Schema::Modern; |
| 110 |
my $js = JSON::Schema::Modern->new; |
| 111 |
my $result = $js->evaluate($data, $schema); |
| 112 |
my @errors = $result->errors; |
| 113 |
|
| 114 |
my $message = $errors[0]->error; |
| 115 |
my $instance_location = $errors[0]->instance_location; |
| 116 |
|
| 117 |
my $errors_encoded = encode_json(\@errors); |
| 118 |
|
| 119 |
=head1 DESCRIPTION |
| 120 |
|
| 121 |
An instance of this class holds one error from evaluating a JSON Schema with |
| 122 |
L<JSON::Schema::Modern>. |
| 123 |
|
| 124 |
=head1 ATTRIBUTES |
| 125 |
|
| 126 |
=head2 keyword |
| 127 |
|
| 128 |
The keyword that produced the error; might be C<undef>. |
| 129 |
|
| 130 |
=head2 instance_location |
| 131 |
|
| 132 |
The path in the instance where the error occurred; encoded as per the JSON Pointer specification |
| 133 |
(L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>). |
| 134 |
|
| 135 |
=head2 keyword_location |
| 136 |
|
| 137 |
The schema path taken during evaluation to arrive at the error; encoded as per the JSON Pointer |
| 138 |
specification (L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>). |
| 139 |
|
| 140 |
=head2 absolute_keyword_location |
| 141 |
|
| 142 |
The canonical URI or URI reference of the location in the schema where the error occurred; not |
| 143 |
defined, if there is no base URI for the schema and no C<$ref> was followed. Note that this is not |
| 144 |
a fragmentless URI in most cases, as the indicated error will occur at a path |
| 145 |
below the position where the most recent identifier had been declared in the schema. Further, if the |
| 146 |
schema never declared an absolute base URI (containing a scheme), this URI won't be absolute either. |
| 147 |
|
| 148 |
=head2 error |
| 149 |
|
| 150 |
The actual error string. |
| 151 |
|
| 152 |
=head2 exception |
| 153 |
|
| 154 |
Indicates the error's severity is sufficient to stop evaluation. |
| 155 |
|
| 156 |
=head2 recommended_response |
| 157 |
|
| 158 |
=for stopwords OpenAPI |
| 159 |
|
| 160 |
A tuple, consisting of C<[ integer, string ]>, indicating the recommended HTTP response code and |
| 161 |
string to use for this error (if validating an HTTP request). This could exist for things like a |
| 162 |
failed authentication check in OpenAPI validation, in which case it would contain |
| 163 |
C<[ 401, 'Unauthorized' ]>. |
| 164 |
|
| 165 |
The string can be omitted at construction time, in which case it will be populated from the standard |
| 166 |
list of error strings corresponding to HTTP response codes (see RFC9110 ยง15). |
| 167 |
|
| 168 |
=head2 depth |
| 169 |
|
| 170 |
An integer which indicates how many subschemas deep this error was generated from. Can be used to |
| 171 |
construct a tree-like structure of errors. |
| 172 |
|
| 173 |
=head1 METHODS |
| 174 |
|
| 175 |
=for Pod::Coverage stringify mode BUILDARGS |
| 176 |
|
| 177 |
=head2 TO_JSON |
| 178 |
|
| 179 |
Returns a data structure suitable for serialization. Corresponds to one output unit as specified in |
| 180 |
L<https://json-schema.org/draft/2020-12/json-schema-core#section-12.3> and |
| 181 |
L<https://json-schema.org/draft/2020-12/output/schema>, |
| 182 |
except that C<instanceLocation> and |
| 183 |
C<keywordLocation> are JSON pointers, B<not> URI fragments, even in draft2019-09. (See the |
| 184 |
C<strict_basic> L<JSON::Schema::Modern/output_format>, only available in that version, |
| 185 |
if the distinction is important to you.) |
| 186 |
|
| 187 |
=head2 dump |
| 188 |
|
| 189 |
Returns a JSON string representing the error object, according to |
| 190 |
the L<specification|https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10>. |
| 191 |
|
| 192 |
=head2 clone |
| 193 |
|
| 194 |
my $new_error = $error->clone(instance_location => '/new/location'); |
| 195 |
|
| 196 |
Creates a clone of an existing error object, with some optional modifications to existing properties. |
| 197 |
|
| 198 |
=head1 GIVING THANKS |
| 199 |
|
| 200 |
=for stopwords MetaCPAN GitHub |
| 201 |
|
| 202 |
If you found this module to be useful, please show your appreciation by |
| 203 |
adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern> |
| 204 |
and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>. |
| 205 |
|
| 206 |
=head1 SUPPORT |
| 207 |
|
| 208 |
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
| 209 |
|
| 210 |
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
| 211 |
|
| 212 |
=for stopwords OpenAPI |
| 213 |
|
| 214 |
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
| 215 |
server|https://open-api.slack.com>, which are also great resources for finding help. |
| 216 |
|
| 217 |
=head1 AUTHOR |
| 218 |
|
| 219 |
Karen Etheridge <[email protected]> |
| 220 |
|
| 221 |
=head1 COPYRIGHT AND LICENCE |
| 222 |
|
| 223 |
This software is copyright (c) 2020 by Karen Etheridge. |
| 224 |
|
| 225 |
This is free software; you can redistribute it and/or modify it under |
| 226 |
the same terms as the Perl 5 programming language system itself. |
| 227 |
|
| 228 |
Some schema files have their own licence, in share/LICENSE. |
| 229 |
|
| 230 |
=cut |