File Coverage

blib/lib/JSON/Schema/Modern/Vocabulary/Content.pm
Criterion Covered Total %
statement 121 125 96.8
branch 28 30 93.3
condition 5 5 100.0
subroutine 25 26 96.1
pod 0 3 0.0
total 179 189 94.7


line stmt bran cond sub pod time code
1 38     38   1147 use strict;
  38         76  
  38         1279  
2 38     38   163 use warnings;
  38         66  
  38         2763  
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.641';
8              
9 38     38   610 use 5.020;
  38         123  
10 38     38   138 use Moo;
  38         71  
  38         216  
11 38     38   10891 use strictures 2;
  38         261  
  38         1165  
12 38     38   12790 use stable 0.031 'postderef';
  38         543  
  38         235  
13 38     38   5488 use experimental 'signatures';
  38         72  
  38         126  
14 38     38   1739 no autovivification warn => qw(fetch store exists delete);
  38         78  
  38         244  
15 38     38   2459 use if "$]" >= 5.022, experimental => 're_strict';
  38         103  
  38         700  
16 38     38   2587 no if "$]" >= 5.031009, feature => 'indirect';
  38         73  
  38         2087  
17 38     38   180 no if "$]" >= 5.033001, feature => 'multidimensional';
  38         88  
  38         1751  
18 38     38   331 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  38         65  
  38         1633  
19 38     38   171 no if "$]" >= 5.041009, feature => 'smartmatch';
  38         89  
  38         1216  
20 38     38   143 no feature 'switch';
  38         63  
  38         939  
21 38     38   362 use Clone 'clone';
  38         75  
  38         2123  
22 38     38   174 use Feature::Compat::Try;
  38         82  
  38         402  
23 38     38   2109 use JSON::Schema::Modern::Utilities qw(is_type A assert_keyword_type E abort jsonp_set decode_media_type);
  38         66  
  38         2689  
24 38     38   354 use namespace::clean;
  38         63  
  38         264  
25              
26             with 'JSON::Schema::Modern::Vocabulary';
27              
28 22     22 0 36 sub vocabulary ($class) {
  22         48  
  22         65  
29 22         92 '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 752 sub keywords ($class, $spec_version) {
  126         220  
  126         209  
  126         185  
36             return (
37 126 100       2262 $spec_version !~ /^draft[46]\z/ ? qw(contentEncoding contentMediaType) : (),
    100          
38             $spec_version !~ /^draft[467]\z/ ? 'contentSchema' : (),
39             );
40             }
41              
42 174     174   259 sub _traverse_keyword_contentEncoding ($class, $schema, $state) {
  174         244  
  174         229  
  174         241  
  174         236  
43 174         491 return assert_keyword_type($state, $schema, 'string');
44             }
45              
46 86     86   137 sub _eval_keyword_contentEncoding ($class, $data, $schema, $state) {
  86         129  
  86         158  
  86         130  
  86         136  
  86         142  
47 86 100       309 return 1 if not is_type('string', $data);
48              
49 70 100       258 if ($state->{validate_content_schemas}) {
50 20         95 my $decoder = $state->{evaluator}->get_encoding($schema->{contentEncoding});
51             abort($state, 'cannot find decoder for contentEncoding "%s"', $schema->{contentEncoding})
52 20 100       228 if not $decoder;
53              
54             # decode the data now, so we can report errors for the right keyword
55 19         36 try {
56 19         62 $state->{_content_ref} = $decoder->(\$data);
57 13         63 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
58             }
59             catch ($e) {
60 6         13 chomp $e;
61 6         29 return E($state, 'could not decode %s string: %s', $schema->{contentEncoding}, $e);
62             }
63             }
64              
65 63         323 return A($state, $schema->{$state->{keyword}});
66             }
67              
68             *_traverse_keyword_contentMediaType = \&_traverse_keyword_contentEncoding;
69              
70 86     86   138 sub _eval_keyword_contentMediaType ($class, $data, $schema, $state) {
  86         150  
  86         138  
  86         144  
  86         142  
  86         145  
71 86 100       207 return 1 if not is_type('string', $data);
72              
73 70 100       230 if ($state->{validate_content_schemas}) {
74             # contentEncoding failed to decode the content
75 20 100 100     107 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 17         24 my $contentref;
79 17         29 try {
80 17   100     95 $contentref = decode_media_type($schema->{contentMediaType}, $state->{_content_ref} // \$data);
81             }
82             catch ($e) {
83 6         18 chomp $e;
84 6         17 delete $state->{_content_ref};
85 6         22 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 11 100       34 if not $contentref;
90              
91 10         25 $state->{_content_ref} = $contentref;
92 10         45 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
93             }
94              
95 60         201 return A($state, $schema->{$state->{keyword}});
96             }
97              
98 43     43   60 sub _traverse_keyword_contentSchema ($class, $schema, $state) {
  43         79  
  43         75  
  43         65  
  43         60  
99 43         197 $class->traverse_subschema($schema, $state);
100             }
101              
102 41     41   67 sub _eval_keyword_contentSchema ($class, $data, $schema, $state) {
  41         79  
  41         69  
  41         64  
  41         71  
  41         56  
103 41 50       147 return 1 if not exists $schema->{contentMediaType};
104 41 100       97 return 1 if not is_type('string', $data);
105              
106 37 100       111 if ($state->{validate_content_schemas}) {
107 7 100       21 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 5 100       90 { %$state, keyword_path => $state->{keyword_path}.'/contentSchema' });
111             }
112              
113 33 50       689 return A($state, ref $schema->{contentSchema} ? clone($schema->{contentSchema}) : $schema->{contentSchema});
114             }
115              
116             1;
117              
118             __END__