| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
package JSON::Schema::Modern::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.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 'Bool'; |
| 24 |
use Carp 'croak'; |
| 25 |
use namespace::clean; |
| 26 |
|
| 27 |
has '+instance_location' => ( |
| 28 |
required => 1, |
| 29 |
); |
| 30 |
|
| 31 |
# https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.7.7.1 |
| 32 |
has annotation => ( |
| 33 |
is => 'ro', |
| 34 |
required => 1, |
| 35 |
); |
| 36 |
|
| 37 |
has unknown => ( |
| 38 |
is => 'ro', |
| 39 |
isa => Bool, |
| 40 |
default => 0, |
| 41 |
); |
| 42 |
|
| 43 |
around BUILDARGS => sub ($orig, $class, @args) { |
| 44 |
my $args = $class->$orig(@args); |
| 45 |
|
| 46 |
# undef is not okay for annotations |
| 47 |
croak 'keyword must be defined' if exists $args->{keyword} and not defined $args->{keyword}; |
| 48 |
return $args; |
| 49 |
}; |
| 50 |
|
| 51 |
sub __thing { 'annotation' } |
| 52 |
|
| 53 |
1; |
| 54 |
|
| 55 |
__END__ |
| 56 |
|
| 57 |
=pod |
| 58 |
|
| 59 |
=encoding UTF-8 |
| 60 |
|
| 61 |
=for stopwords schema fragmentless subschema |
| 62 |
|
| 63 |
=head1 NAME |
| 64 |
|
| 65 |
JSON::Schema::Modern::Annotation - Contains a single annotation from a JSON Schema evaluation |
| 66 |
|
| 67 |
=head1 VERSION |
| 68 |
|
| 69 |
version 0.650 |
| 70 |
|
| 71 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 72 |
presumed or inferred from the version being less than 1.0. |
| 73 |
|
| 74 |
=head1 SYNOPSIS |
| 75 |
|
| 76 |
use JSON::Schema::Modern; |
| 77 |
my $js = JSON::Schema::Modern->new; |
| 78 |
my $result = $js->evaluate($data, $schema); |
| 79 |
my @annotations = $result->annotations; |
| 80 |
|
| 81 |
my $value = $annotations[0]->annotation; |
| 82 |
my $instance_location = $annotations[0]->instance_location; |
| 83 |
|
| 84 |
my $annotations_encoded = encode_json(\@annotations); |
| 85 |
|
| 86 |
=head1 DESCRIPTION |
| 87 |
|
| 88 |
An instance of this class holds one annotation from evaluating a JSON Schema with |
| 89 |
L<JSON::Schema::Modern>. |
| 90 |
|
| 91 |
=head1 ATTRIBUTES |
| 92 |
|
| 93 |
=head2 keyword |
| 94 |
|
| 95 |
The keyword that produced the annotation. |
| 96 |
|
| 97 |
=head2 instance_location |
| 98 |
|
| 99 |
The path in the instance where the annotation was produced; encoded as per the JSON Pointer |
| 100 |
specification (L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>). |
| 101 |
|
| 102 |
=head2 keyword_location |
| 103 |
|
| 104 |
The schema path taken during evaluation to arrive at the annotation; encoded as per the JSON Pointer |
| 105 |
specification (L<RFC 6901|https://datatracker.ietf.org/doc/html/rfc6901>). |
| 106 |
|
| 107 |
=head2 absolute_keyword_location |
| 108 |
|
| 109 |
The canonical URI or URI reference of the location in the schema where the error occurred; not |
| 110 |
defined, if there is no base URI for the schema and no C<$ref> was followed. Note that this is not |
| 111 |
actually fragmentless URI in most cases, as the indicated error will occur at a path |
| 112 |
below the position where the most recent identifier had been declared in the schema. Further, if the |
| 113 |
schema never declared an absolute base URI (containing a scheme), this URI won't be absolute either. |
| 114 |
|
| 115 |
=head2 unknown |
| 116 |
|
| 117 |
A boolean flag, indicating whether the keyword is a known vocabulary keyword or unknown. |
| 118 |
|
| 119 |
=head2 annotation |
| 120 |
|
| 121 |
The actual annotation value (which may or may not be a string). |
| 122 |
|
| 123 |
=head2 depth |
| 124 |
|
| 125 |
An integer which indicates how many subschemas deep this annotation was generated from. Can be used |
| 126 |
to construct a tree-like structure of annotations. |
| 127 |
|
| 128 |
=head1 METHODS |
| 129 |
|
| 130 |
=for Pod::Coverage BUILDARGS |
| 131 |
|
| 132 |
=head2 TO_JSON |
| 133 |
|
| 134 |
Returns a data structure suitable for serialization. Corresponds to one output unit as specified in |
| 135 |
L<https://json-schema.org/draft/2020-12/json-schema-core#section-12.3> and |
| 136 |
L<https://json-schema.org/draft/2020-12/output/schema>, |
| 137 |
except that C<instanceLocation> and |
| 138 |
C<keywordLocation> are JSON pointers, B<not> URI fragments, even in draft2019-09. (See the |
| 139 |
C<strict_basic> L<JSON::Schema::Modern/output_format>, only available in that version, |
| 140 |
if the distinction is important to you.) |
| 141 |
|
| 142 |
=head2 dump |
| 143 |
|
| 144 |
Returns a JSON string representing the error object, according to |
| 145 |
the L<specification|https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.section.10>. |
| 146 |
|
| 147 |
=head1 GIVING THANKS |
| 148 |
|
| 149 |
=for stopwords MetaCPAN GitHub |
| 150 |
|
| 151 |
If you found this module to be useful, please show your appreciation by |
| 152 |
adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern> |
| 153 |
and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>. |
| 154 |
|
| 155 |
=head1 SUPPORT |
| 156 |
|
| 157 |
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
| 158 |
|
| 159 |
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
| 160 |
|
| 161 |
=for stopwords OpenAPI |
| 162 |
|
| 163 |
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
| 164 |
server|https://open-api.slack.com>, which are also great resources for finding help. |
| 165 |
|
| 166 |
=head1 AUTHOR |
| 167 |
|
| 168 |
Karen Etheridge <[email protected]> |
| 169 |
|
| 170 |
=head1 COPYRIGHT AND LICENCE |
| 171 |
|
| 172 |
This software is copyright (c) 2020 by Karen Etheridge. |
| 173 |
|
| 174 |
This is free software; you can redistribute it and/or modify it under |
| 175 |
the same terms as the Perl 5 programming language system itself. |
| 176 |
|
| 177 |
Some schema files have their own licence, in share/LICENSE. |
| 178 |
|
| 179 |
=cut |