File Coverage

blib/lib/JSON/Schema/Modern/Vocabulary/Content.pm
Criterion Covered Total %
statement 118 122 96.7
branch 28 30 93.3
condition 5 5 100.0
subroutine 25 26 96.1
pod 0 3 0.0
total 176 186 94.6


line stmt bran cond sub pod time code
1 38     38   1802 use strict;
  38         98  
  38         1772  
2 38     38   250 use warnings;
  38         96  
  38         3996  
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.632';
8              
9 38     38   853 use 5.020;
  38         163  
10 38     38   233 use Moo;
  38         84  
  38         275  
11 38     38   17188 use strictures 2;
  38         351  
  38         1827  
12 38     38   19767 use stable 0.031 'postderef';
  38         725  
  38         321  
13 38     38   8993 use experimental 'signatures';
  38         100  
  38         242  
14 38     38   2722 no autovivification warn => qw(fetch store exists delete);
  38         92  
  38         418  
15 38     38   3617 use if "$]" >= 5.022, experimental => 're_strict';
  38         91  
  38         1152  
16 38     38   3883 no if "$]" >= 5.031009, feature => 'indirect';
  38         110  
  38         3188  
17 38     38   266 no if "$]" >= 5.033001, feature => 'multidimensional';
  38         114  
  38         2823  
18 38     38   263 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  38         95  
  38         2791  
19 38     38   242 no if "$]" >= 5.041009, feature => 'smartmatch';
  38         118  
  38         1968  
20 38     38   239 no feature 'switch';
  38         114  
  38         1745  
21 38     38   232 use Storable 'dclone';
  38         88  
  38         3136  
22 38     38   316 use Feature::Compat::Try;
  38         106  
  38         513  
23 38     38   3036 use JSON::Schema::Modern::Utilities qw(is_type A assert_keyword_type E abort);
  38         95  
  38         3475  
24 38     38   252 use namespace::clean;
  38         103  
  38         469  
25              
26             with 'JSON::Schema::Modern::Vocabulary';
27              
28 22     22 0 60 sub vocabulary ($class) {
  22         48  
  22         44  
29 22         140 '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 0     0 0 0 sub evaluation_order ($class) { 4 }
  0         0  
  0         0  
  0         0  
34              
35 126     126 0 3750 sub keywords ($class, $spec_version) {
  126         311  
  126         281  
  126         254  
36             return (
37 126 100       3462 $spec_version !~ /^draft[46]\z/ ? qw(contentEncoding contentMediaType) : (),
    100          
38             $spec_version !~ /^draft[467]\z/ ? 'contentSchema' : (),
39             );
40             }
41              
42 172     172   432 sub _traverse_keyword_contentEncoding ($class, $schema, $state) {
  172         385  
  172         389  
  172         372  
  172         340  
43 172         884 return assert_keyword_type($state, $schema, 'string');
44             }
45              
46 85     85   267 sub _eval_keyword_contentEncoding ($class, $data, $schema, $state) {
  85         328  
  85         286  
  85         198  
  85         217  
  85         197  
47 85 100       481 return 1 if not is_type('string', $data);
48              
49 69 100       463 if ($state->{validate_content_schemas}) {
50 19         202 my $decoder = $state->{evaluator}->get_encoding($schema->{contentEncoding});
51             abort($state, 'cannot find decoder for contentEncoding "%s"', $schema->{contentEncoding})
52 19 100       335 if not $decoder;
53              
54             # decode the data now, so we can report errors for the right keyword
55 18         59 try {
56 18         103 $state->{_content_ref} = $decoder->(\$data);
57             }
58             catch ($e) {
59 6         22 chomp $e;
60 6         49 return E($state, 'could not decode %s string: %s', $schema->{contentEncoding}, $e);
61             }
62             }
63              
64 62         510 return A($state, $schema->{$state->{keyword}});
65             }
66              
67             *_traverse_keyword_contentMediaType = \&_traverse_keyword_contentEncoding;
68              
69 85     85   218 sub _eval_keyword_contentMediaType ($class, $data, $schema, $state) {
  85         257  
  85         240  
  85         200  
  85         204  
  85         165  
70 85 100       365 return 1 if not is_type('string', $data);
71              
72 69 100       369 if ($state->{validate_content_schemas}) {
73 19         177 my $decoder = $state->{evaluator}->get_media_type($schema->{contentMediaType});
74             abort($state, 'cannot find decoder for contentMediaType "%s"', $schema->{contentMediaType})
75 19 100       84 if not $decoder;
76              
77             # contentEncoding failed to decode the content
78 18 100 100     164 return 1 if exists $schema->{contentEncoding} and not exists $state->{_content_ref};
79              
80             # decode the data now, so we can report errors for the right keyword
81 15         36 try {
82 15   100     121 $state->{_content_ref} = $decoder->($state->{_content_ref} // \$data);
83             }
84             catch ($e) {
85 6         25 chomp $e;
86 6         40 delete $state->{_content_ref};
87 6         54 return E($state, 'could not decode %s string: %s', $schema->{contentMediaType}, $e);
88             }
89             }
90              
91 59         370 return A($state, $schema->{$state->{keyword}});
92             }
93              
94 42     42   114 sub _traverse_keyword_contentSchema ($class, $schema, $state) {
  42         113  
  42         95  
  42         126  
  42         91  
95 42         367 $class->traverse_subschema($schema, $state);
96             }
97              
98 40     40   117 sub _eval_keyword_contentSchema ($class, $data, $schema, $state) {
  40         111  
  40         150  
  40         85  
  40         87  
  40         95  
99 40 50       200 return 1 if not exists $schema->{contentMediaType};
100 40 100       157 return 1 if not is_type('string', $data);
101              
102 36 100       185 if ($state->{validate_content_schemas}) {
103 6 100       39 return 1 if not exists $state->{_content_ref}; # contentMediaType failed to decode the content
104             return E($state, 'subschema is not valid')
105             if not $class->eval($state->{_content_ref}->$*, $schema->{contentSchema},
106 4 100       111 { %$state, keyword_path => $state->{keyword_path}.'/contentSchema' });
107             }
108              
109 32 50       4351 return A($state, ref $schema->{contentSchema} ? dclone($schema->{contentSchema}) : $schema->{contentSchema});
110             }
111              
112             1;
113              
114             __END__