File Coverage

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


line stmt bran cond sub pod time code
1 38     38   1124 use strict;
  38         76  
  38         1342  
2 38     38   154 use warnings;
  38         69  
  38         2904  
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.637';
8              
9 38     38   566 use 5.020;
  38         125  
10 38     38   149 use Moo;
  38         62  
  38         209  
11 38     38   11747 use strictures 2;
  38         305  
  38         1292  
12 38     38   13279 use stable 0.031 'postderef';
  38         543  
  38         247  
13 38     38   5868 use experimental 'signatures';
  38         84  
  38         153  
14 38     38   1800 no autovivification warn => qw(fetch store exists delete);
  38         104  
  38         280  
15 38     38   2489 use if "$]" >= 5.022, experimental => 're_strict';
  38         70  
  38         805  
16 38     38   2670 no if "$]" >= 5.031009, feature => 'indirect';
  38         77  
  38         2093  
17 38     38   183 no if "$]" >= 5.033001, feature => 'multidimensional';
  38         79  
  38         1825  
18 38     38   162 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  38         91  
  38         1711  
19 38     38   207 no if "$]" >= 5.041009, feature => 'smartmatch';
  38         74  
  38         1349  
20 38     38   178 no feature 'switch';
  38         335  
  38         1084  
21 38     38   168 use Storable 'dclone';
  38         71  
  38         2379  
22 38     38   189 use Feature::Compat::Try;
  38         60  
  38         404  
23 38     38   2155 use JSON::Schema::Modern::Utilities qw(is_type A assert_keyword_type E abort jsonp_set);
  38         73  
  38         2479  
24 38     38   187 use namespace::clean;
  38         60  
  38         284  
25              
26             with 'JSON::Schema::Modern::Vocabulary';
27              
28 22     22 0 32 sub vocabulary ($class) {
  22         40  
  22         34  
29 22         84 '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 734 sub keywords ($class, $spec_version) {
  126         220  
  126         201  
  126         195  
36             return (
37 126 100       2502 $spec_version !~ /^draft[46]\z/ ? qw(contentEncoding contentMediaType) : (),
    100          
38             $spec_version !~ /^draft[467]\z/ ? 'contentSchema' : (),
39             );
40             }
41              
42 174     174   257 sub _traverse_keyword_contentEncoding ($class, $schema, $state) {
  174         234  
  174         221  
  174         756  
  174         206  
43 174         502 return assert_keyword_type($state, $schema, 'string');
44             }
45              
46 86     86   123 sub _eval_keyword_contentEncoding ($class, $data, $schema, $state) {
  86         161  
  86         135  
  86         145  
  86         126  
  86         108  
47 86 100       303 return 1 if not is_type('string', $data);
48              
49 70 100       229 if ($state->{validate_content_schemas}) {
50 20         93 my $decoder = $state->{evaluator}->get_encoding($schema->{contentEncoding});
51             abort($state, 'cannot find decoder for contentEncoding "%s"', $schema->{contentEncoding})
52 20 100       203 if not $decoder;
53              
54             # decode the data now, so we can report errors for the right keyword
55 19         26 try {
56 19         53 $state->{_content_ref} = $decoder->(\$data);
57 13         76 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
58             }
59             catch ($e) {
60 6         16 chomp $e;
61 6         22 return E($state, 'could not decode %s string: %s', $schema->{contentEncoding}, $e);
62             }
63             }
64              
65 63         280 return A($state, $schema->{$state->{keyword}});
66             }
67              
68             *_traverse_keyword_contentMediaType = \&_traverse_keyword_contentEncoding;
69              
70 86     86   146 sub _eval_keyword_contentMediaType ($class, $data, $schema, $state) {
  86         135  
  86         127  
  86         135  
  86         108  
  86         117  
71 86 100       220 return 1 if not is_type('string', $data);
72              
73 70 100       219 if ($state->{validate_content_schemas}) {
74 20         104 my $decoder = $state->{evaluator}->get_media_type($schema->{contentMediaType});
75             abort($state, 'cannot find decoder for contentMediaType "%s"', $schema->{contentMediaType})
76 20 100       47 if not $decoder;
77              
78             # contentEncoding failed to decode the content
79 19 100 100     85 return 1 if exists $schema->{contentEncoding} and not exists $state->{_content_ref};
80              
81             # decode the data now, so we can report errors for the right keyword
82 16         27 try {
83 16   100     147 $state->{_content_ref} = $decoder->($state->{_content_ref} // \$data);
84 10         48 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
85             }
86             catch ($e) {
87 6         15 chomp $e;
88 6         16 delete $state->{_content_ref};
89 6         23 return E($state, 'could not decode %s string: %s', $schema->{contentMediaType}, $e);
90             }
91             }
92              
93 60         213 return A($state, $schema->{$state->{keyword}});
94             }
95              
96 43     43   83 sub _traverse_keyword_contentSchema ($class, $schema, $state) {
  43         77  
  43         64  
  43         56  
  43         64  
97 43         201 $class->traverse_subschema($schema, $state);
98             }
99              
100 41     41   63 sub _eval_keyword_contentSchema ($class, $data, $schema, $state) {
  41         75  
  41         67  
  41         66  
  41         59  
  41         61  
101 41 50       143 return 1 if not exists $schema->{contentMediaType};
102 41 100       105 return 1 if not is_type('string', $data);
103              
104 37 100       112 if ($state->{validate_content_schemas}) {
105 7 100       22 return 1 if not exists $state->{_content_ref}; # contentMediaType failed to decode the content
106             return E($state, 'subschema is not valid')
107             if not $class->eval($state->{_content_ref}->$*, $schema->{contentSchema},
108 5 100       91 { %$state, keyword_path => $state->{keyword_path}.'/contentSchema' });
109             }
110              
111 33 50       3362 return A($state, ref $schema->{contentSchema} ? dclone($schema->{contentSchema}) : $schema->{contentSchema});
112             }
113              
114             1;
115              
116             __END__