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   1104 use strict;
  38         79  
  38         1411  
2 38     38   146 use warnings;
  38         83  
  38         2871  
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.635';
8              
9 38     38   600 use 5.020;
  38         181  
10 38     38   175 use Moo;
  38         70  
  38         202  
11 38     38   11211 use strictures 2;
  38         233  
  38         1328  
12 38     38   12943 use stable 0.031 'postderef';
  38         517  
  38         225  
13 38     38   5651 use experimental 'signatures';
  38         69  
  38         122  
14 38     38   1827 no autovivification warn => qw(fetch store exists delete);
  38         88  
  38         206  
15 38     38   2352 use if "$]" >= 5.022, experimental => 're_strict';
  38         98  
  38         751  
16 38     38   2695 no if "$]" >= 5.031009, feature => 'indirect';
  38         71  
  38         2233  
17 38     38   169 no if "$]" >= 5.033001, feature => 'multidimensional';
  38         60  
  38         1751  
18 38     38   182 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  38         64  
  38         1729  
19 38     38   159 no if "$]" >= 5.041009, feature => 'smartmatch';
  38         106  
  38         1280  
20 38     38   177 no feature 'switch';
  38         222  
  38         973  
21 38     38   159 use Storable 'dclone';
  38         74  
  38         2148  
22 38     38   222 use Feature::Compat::Try;
  38         106  
  38         340  
23 38     38   2069 use JSON::Schema::Modern::Utilities qw(is_type A assert_keyword_type E abort jsonp_set);
  38         53  
  38         2427  
24 38     38   195 use namespace::clean;
  38         55  
  38         346  
25              
26             with 'JSON::Schema::Modern::Vocabulary';
27              
28 22     22 0 36 sub vocabulary ($class) {
  22         27  
  22         31  
29 22         64 '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         202  
  126         214  
  126         216  
36             return (
37 126 100       2368 $spec_version !~ /^draft[46]\z/ ? qw(contentEncoding contentMediaType) : (),
    100          
38             $spec_version !~ /^draft[467]\z/ ? 'contentSchema' : (),
39             );
40             }
41              
42 174     174   276 sub _traverse_keyword_contentEncoding ($class, $schema, $state) {
  174         240  
  174         247  
  174         579  
  174         244  
43 174         435 return assert_keyword_type($state, $schema, 'string');
44             }
45              
46 86     86   147 sub _eval_keyword_contentEncoding ($class, $data, $schema, $state) {
  86         140  
  86         148  
  86         118  
  86         114  
  86         135  
47 86 100       257 return 1 if not is_type('string', $data);
48              
49 70 100       227 if ($state->{validate_content_schemas}) {
50 20         92 my $decoder = $state->{evaluator}->get_encoding($schema->{contentEncoding});
51             abort($state, 'cannot find decoder for contentEncoding "%s"', $schema->{contentEncoding})
52 20 100       205 if not $decoder;
53              
54             # decode the data now, so we can report errors for the right keyword
55 19         32 try {
56 19         63 $state->{_content_ref} = $decoder->(\$data);
57 13         57 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
58             }
59             catch ($e) {
60 6         15 chomp $e;
61 6         22 return E($state, 'could not decode %s string: %s', $schema->{contentEncoding}, $e);
62             }
63             }
64              
65 63         242 return A($state, $schema->{$state->{keyword}});
66             }
67              
68             *_traverse_keyword_contentMediaType = \&_traverse_keyword_contentEncoding;
69              
70 86     86   132 sub _eval_keyword_contentMediaType ($class, $data, $schema, $state) {
  86         135  
  86         123  
  86         139  
  86         147  
  86         118  
71 86 100       189 return 1 if not is_type('string', $data);
72              
73 70 100       204 if ($state->{validate_content_schemas}) {
74 20         99 my $decoder = $state->{evaluator}->get_media_type($schema->{contentMediaType});
75             abort($state, 'cannot find decoder for contentMediaType "%s"', $schema->{contentMediaType})
76 20 100       50 if not $decoder;
77              
78             # contentEncoding failed to decode the content
79 19 100 100     81 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         32 try {
83 16   100     87 $state->{_content_ref} = $decoder->($state->{_content_ref} // \$data);
84 10         37 $state->{data} = jsonp_set($state->{data}, $state->{data_path}, $state->{_content_ref}->$*);
85             }
86             catch ($e) {
87 6         19 chomp $e;
88 6         15 delete $state->{_content_ref};
89 6         29 return E($state, 'could not decode %s string: %s', $schema->{contentMediaType}, $e);
90             }
91             }
92              
93 60         192 return A($state, $schema->{$state->{keyword}});
94             }
95              
96 43     43   63 sub _traverse_keyword_contentSchema ($class, $schema, $state) {
  43         72  
  43         59  
  43         56  
  43         52  
97 43         196 $class->traverse_subschema($schema, $state);
98             }
99              
100 41     41   58 sub _eval_keyword_contentSchema ($class, $data, $schema, $state) {
  41         68  
  41         59  
  41         61  
  41         57  
  41         51  
101 41 50       152 return 1 if not exists $schema->{contentMediaType};
102 41 100       92 return 1 if not is_type('string', $data);
103              
104 37 100       99 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       108 { %$state, keyword_path => $state->{keyword_path}.'/contentSchema' });
109             }
110              
111 33 50       3057 return A($state, ref $schema->{contentSchema} ? dclone($schema->{contentSchema}) : $schema->{contentSchema});
112             }
113              
114             1;
115              
116             __END__