| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
package JSON::Schema::Modern::Vocabulary::Content; |
| 4 |
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
| 5 |
# ABSTRACT: Implementation of the JSON Schema Content vocabulary |
| 6 |
|
| 7 |
our $VERSION = '0.650'; |
| 8 |
|
| 9 |
use 5.020; |
| 10 |
use Moo; |
| 11 |
use strictures 2; |
| 12 |
use stable 0.031 'postderef'; |
| 13 |
use experimental 'signatures'; |
| 14 |
no autovivification warn => qw(fetch store exists delete); |
| 15 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 16 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 17 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 18 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 19 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 20 |
no feature 'switch'; |
| 21 |
use Clone 'clone'; |
| 22 |
use Feature::Compat::Try; |
| 23 |
use JSON::Schema::Modern::Utilities qw(is_type A assert_keyword_type E abort jsonp_set decode_media_type); |
| 24 |
use namespace::clean; |
| 25 |
|
| 26 |
with 'JSON::Schema::Modern::Vocabulary'; |
| 27 |
|
| 28 |
sub vocabulary ($class) { |
| 29 |
'https://json-schema.org/draft/2019-09/vocab/content' => 'draft2019-09', |
| 30 |
'https://json-schema.org/draft/2020-12/vocab/content' => 'draft2020-12'; |
| 31 |
} |
| 32 |
|
| 33 |
sub evaluation_order ($class) { 4 } |
| 34 |
|
| 35 |
sub keywords ($class, $spec_version) { |
| 36 |
return ( |
| 37 |
$spec_version !~ /^draft[46]\z/ ? qw(contentEncoding contentMediaType) : (), |
| 38 |
$spec_version !~ /^draft[467]\z/ ? 'contentSchema' : (), |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub _traverse_keyword_contentEncoding ($class, $schema, $state) { |
| 43 |
return assert_keyword_type($state, $schema, 'string'); |
| 44 |
} |
| 45 |
|
| 46 |
sub _eval_keyword_contentEncoding ($class, $data, $schema, $state) { |
| 47 |
return 1 if not is_type('string', $data); |
| 48 |
|
| 49 |
if ($state->{validate_content_schemas}) { |
| 50 |
my $decoder = $state->{evaluator}->get_encoding($schema->{contentEncoding}); |
| 51 |
abort($state, 'cannot find decoder for contentEncoding "%s"', $schema->{contentEncoding}) |
| 52 |
if not $decoder; |
| 53 |
|
| 54 |
# decode the data now, so we can report errors for the right keyword |
| 55 |
try { |
| 56 |
$state->{_content_ref} = $decoder->(\$data); |
| 57 |
$state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*); |
| 58 |
} |
| 59 |
catch ($e) { |
| 60 |
chomp $e; |
| 61 |
return E($state, 'could not decode %s string: %s', $schema->{contentEncoding}, $e); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return A($state, $schema->{$state->{keyword}}); |
| 66 |
} |
| 67 |
|
| 68 |
*_traverse_keyword_contentMediaType = \&_traverse_keyword_contentEncoding; |
| 69 |
|
| 70 |
sub _eval_keyword_contentMediaType ($class, $data, $schema, $state) { |
| 71 |
return 1 if not is_type('string', $data); |
| 72 |
|
| 73 |
if ($state->{validate_content_schemas}) { |
| 74 |
# contentEncoding failed to decode the content |
| 75 |
return 1 if exists $schema->{contentEncoding} and not exists $state->{_content_ref}; |
| 76 |
|
| 77 |
# decode the data now, so we can report errors for the right keyword |
| 78 |
my $contentref; |
| 79 |
try { |
| 80 |
$contentref = decode_media_type($schema->{contentMediaType}, $state->{_content_ref} // \$data); |
| 81 |
} |
| 82 |
catch ($e) { |
| 83 |
chomp $e; |
| 84 |
delete $state->{_content_ref}; |
| 85 |
return E($state, 'could not decode %s string: %s', $schema->{contentMediaType}, $e); |
| 86 |
} |
| 87 |
|
| 88 |
abort($state, 'cannot find decoder for contentMediaType "%s"', $schema->{contentMediaType}) |
| 89 |
if not $contentref; |
| 90 |
|
| 91 |
$state->{_content_ref} = $contentref; |
| 92 |
$state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*); |
| 93 |
} |
| 94 |
|
| 95 |
return A($state, $schema->{$state->{keyword}}); |
| 96 |
} |
| 97 |
|
| 98 |
sub _traverse_keyword_contentSchema ($class, $schema, $state) { |
| 99 |
$class->traverse_subschema($schema, $state); |
| 100 |
} |
| 101 |
|
| 102 |
sub _eval_keyword_contentSchema ($class, $data, $schema, $state) { |
| 103 |
return 1 if not exists $schema->{contentMediaType}; |
| 104 |
return 1 if not is_type('string', $data); |
| 105 |
|
| 106 |
if ($state->{validate_content_schemas}) { |
| 107 |
return 1 if not exists $state->{_content_ref}; # contentMediaType failed to decode the content |
| 108 |
return E($state, 'subschema is not valid') |
| 109 |
if not $class->eval($state->{_content_ref}->$*, $schema->{contentSchema}, |
| 110 |
{ %$state, keyword_path => $state->{keyword_path}.'/contentSchema' }); |
| 111 |
} |
| 112 |
|
| 113 |
return A($state, ref $schema->{contentSchema} ? clone($schema->{contentSchema}) : $schema->{contentSchema}); |
| 114 |
} |
| 115 |
|
| 116 |
1; |
| 117 |
|
| 118 |
__END__ |
| 119 |
|
| 120 |
=pod |
| 121 |
|
| 122 |
=encoding UTF-8 |
| 123 |
|
| 124 |
=head1 NAME |
| 125 |
|
| 126 |
JSON::Schema::Modern::Vocabulary::Content - Implementation of the JSON Schema Content vocabulary |
| 127 |
|
| 128 |
=head1 VERSION |
| 129 |
|
| 130 |
version 0.650 |
| 131 |
|
| 132 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 133 |
presumed or inferred from the version being less than 1.0. |
| 134 |
|
| 135 |
=head1 DESCRIPTION |
| 136 |
|
| 137 |
=for Pod::Coverage vocabulary evaluation_order keywords |
| 138 |
|
| 139 |
=for stopwords metaschema |
| 140 |
|
| 141 |
Implementation of the JSON Schema Draft 2020-12 "Content" vocabulary, indicated in metaschemas |
| 142 |
with the URI C<https://json-schema.org/draft/2020-12/vocab/content> and formally specified in |
| 143 |
L<https://json-schema.org/draft/2020-12/json-schema-validation.html#section-8>. |
| 144 |
|
| 145 |
Support is also provided for |
| 146 |
|
| 147 |
=over 4 |
| 148 |
|
| 149 |
=item * |
| 150 |
|
| 151 |
the equivalent Draft 2019-09 keywords, indicated in metaschemas with the URI C<https://json-schema.org/draft/2019-09/vocab/content> and formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-02#section-8>. |
| 152 |
|
| 153 |
=item * |
| 154 |
|
| 155 |
the equivalent Draft 7 keywords that correspond to this vocabulary and are formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-8>. |
| 156 |
|
| 157 |
=back |
| 158 |
|
| 159 |
Assertion behaviour can be enabled by toggling the L<JSON::Schema::Modern/validate_content_schemas> |
| 160 |
option. |
| 161 |
|
| 162 |
New handlers for C<contentEncoding> and C<contentMediaType> can be done through |
| 163 |
L<JSON::Schema::Modern/add_encoding> and L<JSON::Schema::Modern/add_media_type>. |
| 164 |
|
| 165 |
=head1 GIVING THANKS |
| 166 |
|
| 167 |
=for stopwords MetaCPAN GitHub |
| 168 |
|
| 169 |
If you found this module to be useful, please show your appreciation by |
| 170 |
adding a +1 in L<MetaCPAN|https://metacpan.org/dist/JSON-Schema-Modern> |
| 171 |
and a star in L<GitHub|https://github.com/karenetheridge/JSON-Schema-Modern>. |
| 172 |
|
| 173 |
=head1 SUPPORT |
| 174 |
|
| 175 |
Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern/issues>. |
| 176 |
|
| 177 |
I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>. |
| 178 |
|
| 179 |
=for stopwords OpenAPI |
| 180 |
|
| 181 |
You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack |
| 182 |
server|https://open-api.slack.com>, which are also great resources for finding help. |
| 183 |
|
| 184 |
=head1 AUTHOR |
| 185 |
|
| 186 |
Karen Etheridge <[email protected]> |
| 187 |
|
| 188 |
=head1 COPYRIGHT AND LICENCE |
| 189 |
|
| 190 |
This software is copyright (c) 2020 by Karen Etheridge. |
| 191 |
|
| 192 |
This is free software; you can redistribute it and/or modify it under |
| 193 |
the same terms as the Perl 5 programming language system itself. |
| 194 |
|
| 195 |
Some schema files have their own licence, in share/LICENSE. |
| 196 |
|
| 197 |
=cut |