| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
package JSON::Schema::Modern::Vocabulary::FormatAnnotation; |
| 4 |
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
| 5 |
# ABSTRACT: Implementation of the JSON Schema Format-Annotation 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 Feature::Compat::Try; |
| 22 |
use JSON::Schema::Modern::Utilities qw(A E assert_keyword_type get_type); |
| 23 |
use JSON::Schema::Modern::Vocabulary::FormatAssertion; |
| 24 |
use if "$]" < 5.041010, 'List::Util' => 'any'; |
| 25 |
use if "$]" >= 5.041010, experimental => 'keyword_any'; |
| 26 |
use Scalar::Util 'looks_like_number'; |
| 27 |
use namespace::clean; |
| 28 |
|
| 29 |
with 'JSON::Schema::Modern::Vocabulary'; |
| 30 |
|
| 31 |
sub vocabulary ($class) { |
| 32 |
'https://json-schema.org/draft/2019-09/vocab/format' => 'draft2019-09', |
| 33 |
'https://json-schema.org/draft/2020-12/vocab/format-annotation' => 'draft2020-12'; |
| 34 |
} |
| 35 |
|
| 36 |
sub evaluation_order ($class) { 2 } |
| 37 |
|
| 38 |
sub keywords ($class, $spec_version) { |
| 39 |
qw(format); |
| 40 |
} |
| 41 |
|
| 42 |
sub _traverse_keyword_format ($class, $schema, $state) { |
| 43 |
return if not assert_keyword_type($state, $schema, 'string'); |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
sub _eval_keyword_format ($class, $data, $schema, $state) { |
| 48 |
A($state, $schema->{format}); |
| 49 |
return 1 if not $state->{validate_formats}; |
| 50 |
|
| 51 |
# { type => .., sub => .. } |
| 52 |
my $spec = $state->{evaluator}->_get_format_validation($schema->{format}) |
| 53 |
// JSON::Schema::Modern::Vocabulary::FormatAssertion->_get_default_format_validation($state, $schema->{format}); |
| 54 |
|
| 55 |
# §7.2.1 (draft2020-12) "Specifying the Format-Annotation vocabulary and enabling validation in an |
| 56 |
# implementation should not be viewed as being equivalent to specifying the Format-Assertion |
| 57 |
# vocabulary since implementations are not required to provide full validation support when the |
| 58 |
# Format-Assertion vocabulary is not specified." |
| 59 |
# §7.2.3 (draft2019-09) "An implementation MUST NOT fail validation or cease processing due to an |
| 60 |
# unknown format attribute." |
| 61 |
return 1 if not $spec; |
| 62 |
|
| 63 |
my $type = get_type($data); |
| 64 |
$type = 'number' if $type eq 'integer'; |
| 65 |
|
| 66 |
return 1 if |
| 67 |
not ref $spec->{type} eq 'ARRAY' ? any { $type eq $_ } $spec->{type}->@* : $type eq $spec->{type} |
| 68 |
and not ($state->{stringy_numbers} and $type eq 'string' |
| 69 |
and ref $spec->{type} eq 'ARRAY' ? any { $_ eq 'number' } $spec->{type}->@* : $spec->{type} eq 'number' |
| 70 |
and looks_like_number($data)); |
| 71 |
|
| 72 |
my $valid; |
| 73 |
try { |
| 74 |
$valid = $spec->{sub}->($data); |
| 75 |
} |
| 76 |
catch ($e) { |
| 77 |
# we treat a missing optional prereq as if the format is not implemented, therefore it is valid |
| 78 |
$valid = 1; |
| 79 |
} |
| 80 |
|
| 81 |
return E($state, 'not a valid %s %s', $schema->{format}, |
| 82 |
ref $spec->{type} eq 'ARRAY' ? join(', ', $spec->{type}->@*) : $spec->{type}) |
| 83 |
if not $valid; |
| 84 |
return 1; |
| 85 |
} |
| 86 |
|
| 87 |
1; |
| 88 |
|
| 89 |
__END__ |
| 90 |
|
| 91 |
=pod |
| 92 |
|
| 93 |
=encoding UTF-8 |
| 94 |
|
| 95 |
=head1 NAME |
| 96 |
|
| 97 |
JSON::Schema::Modern::Vocabulary::FormatAnnotation - Implementation of the JSON Schema Format-Annotation vocabulary |
| 98 |
|
| 99 |
=head1 VERSION |
| 100 |
|
| 101 |
version 0.650 |
| 102 |
|
| 103 |
I use a linearly-increasing version numbering scheme. No meaning should be |
| 104 |
presumed or inferred from the version being less than 1.0. |
| 105 |
|
| 106 |
=head1 DESCRIPTION |
| 107 |
|
| 108 |
=for Pod::Coverage vocabulary evaluation_order keywords |
| 109 |
|
| 110 |
=for stopwords metaschema |
| 111 |
|
| 112 |
Implementation of the JSON Schema Draft 2020-12 "Format-Annotation" vocabulary, indicated in metaschemas |
| 113 |
with the URI C<https://json-schema.org/draft/2020-12/vocab/format-annotation> and formally specified in |
| 114 |
L<https://json-schema.org/draft/2020-12/json-schema-validation.html#section-7>. |
| 115 |
|
| 116 |
Support is also provided for |
| 117 |
|
| 118 |
=over 4 |
| 119 |
|
| 120 |
=item * |
| 121 |
|
| 122 |
the equivalent Draft 2019-09 keyword, indicated in metaschemas with the URI C<https://json-schema.org/draft/2019-09/vocab/format> and formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-02#section-7>. |
| 123 |
|
| 124 |
=item * |
| 125 |
|
| 126 |
the equivalent Draft 7 keyword, as formally specified in L<https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-7>. |
| 127 |
|
| 128 |
=item * |
| 129 |
|
| 130 |
the equivalent Draft 6 keyword, as formally specified in L<https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.8>. |
| 131 |
|
| 132 |
=item * |
| 133 |
|
| 134 |
the equivalent Draft 4 keyword, as formally specified in L<https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.7>. |
| 135 |
|
| 136 |
=back |
| 137 |
|
| 138 |
It also implements format assertion behaviour in a relaxed mode, meaning the |
| 139 |
L<JSON::Schema::Modern/validate_formats> option has been enabled, and unknown formats will not |
| 140 |
generate errors; this differs from the more strict behaviour in |
| 141 |
LJSON::Schema::Modern::Vocabulary::FormatAssertion> which requires all formats used in the schema to |
| 142 |
be supported and defined. |
| 143 |
|
| 144 |
When this vocabulary (the Format-Annotation vocabulary) is specified (which is the default for the |
| 145 |
draft2020-12 metaschema) and combined with the C<validate_formats> option set to true, unimplemented |
| 146 |
formats will silently validate, but implemented formats will validate completely. Note that some |
| 147 |
formats require optional module dependencies, and the lack of these modules will generate an error. |
| 148 |
|
| 149 |
When the Format-Assertion vocabulary is specified, unimplemented formats will generate an error on use. |
| 150 |
|
| 151 |
=head1 SEE ALSO |
| 152 |
|
| 153 |
=over 4 |
| 154 |
|
| 155 |
=item * |
| 156 |
|
| 157 |
L<JSON::Schema::Modern/Format Validation> |
| 158 |
|
| 159 |
=item * |
| 160 |
|
| 161 |
L<JSON::Schema::Modern::Vocabulary::FormatAssertion> |
| 162 |
|
| 163 |
=back |
| 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 |