| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use stable 0.031 'postderef'; |
| 4 |
use experimental 'signatures'; |
| 5 |
no autovivification warn => qw(fetch store exists delete); |
| 6 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 7 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 8 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 9 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 10 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 11 |
no feature 'switch'; |
| 12 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 13 |
|
| 14 |
use lib 't/lib'; |
| 15 |
use Helper; |
| 16 |
|
| 17 |
my $js = JSON::Schema::Modern->new; |
| 18 |
|
| 19 |
subtest 'evaluation callbacks' => sub { |
| 20 |
my @used_ref_at; |
| 21 |
my $result = $js->evaluate( |
| 22 |
[ { a => { b => { c => { d => 'e' } } } } ], |
| 23 |
my $schema = { |
| 24 |
'$defs' => { |
| 25 |
object_or_string => { |
| 26 |
anyOf => [ |
| 27 |
{ |
| 28 |
type => 'object', |
| 29 |
additionalProperties => { '$ref' => '#/$defs/object_or_string' }, |
| 30 |
}, |
| 31 |
{ |
| 32 |
type => 'string' |
| 33 |
}, |
| 34 |
], |
| 35 |
}, |
| 36 |
}, |
| 37 |
contains => { '$ref' => '#/$defs/object_or_string' }, |
| 38 |
}, |
| 39 |
my $config = { |
| 40 |
callbacks => { |
| 41 |
'$ref' => sub ($data, $schema, $state) { |
| 42 |
push @used_ref_at, $state->{data_path}; |
| 43 |
}, |
| 44 |
}, |
| 45 |
}, |
| 46 |
); |
| 47 |
ok($result->valid, 'evaluation was successful'); |
| 48 |
cmp_result( |
| 49 |
\@used_ref_at, |
| 50 |
bag( |
| 51 |
'/0', |
| 52 |
'/0/a', |
| 53 |
'/0/a/b', |
| 54 |
'/0/a/b/c', |
| 55 |
'/0/a/b/c/d', |
| 56 |
), |
| 57 |
'identified all data paths where a $ref was used', |
| 58 |
); |
| 59 |
|
| 60 |
|
| 61 |
undef @used_ref_at; |
| 62 |
$result = $js->evaluate( |
| 63 |
[ { a => { b => 2 } } ], |
| 64 |
$schema, |
| 65 |
$config, |
| 66 |
); |
| 67 |
ok(!$result->valid, 'evaluation was not successful'); |
| 68 |
is_equal( |
| 69 |
\@used_ref_at, |
| 70 |
[], |
| 71 |
'no callbacks on failure: innermost $ref failed, so all other $refs failed too', |
| 72 |
); |
| 73 |
|
| 74 |
|
| 75 |
undef @used_ref_at; |
| 76 |
$result = $js->evaluate( |
| 77 |
[ |
| 78 |
{ a => { b => 'c' } }, |
| 79 |
{ x => { y => 1 } }, |
| 80 |
], |
| 81 |
{ |
| 82 |
'$defs' => { |
| 83 |
object_or_string => { |
| 84 |
anyOf => [ |
| 85 |
{ |
| 86 |
type => 'object', |
| 87 |
additionalProperties => { '$ref' => '#/$defs/object_or_string' }, |
| 88 |
}, |
| 89 |
{ |
| 90 |
type => 'string' |
| 91 |
}, |
| 92 |
], |
| 93 |
}, |
| 94 |
}, |
| 95 |
contains => { '$ref' => '#/$defs/object_or_string' }, |
| 96 |
}, |
| 97 |
$config, |
| 98 |
); |
| 99 |
ok($result->valid, 'evaluation was successful'); |
| 100 |
|
| 101 |
cmp_result( |
| 102 |
\@used_ref_at, |
| 103 |
bag( |
| 104 |
'/0', |
| 105 |
'/0/a', |
| 106 |
'/0/a/b', |
| 107 |
), |
| 108 |
'successful subschemas have callbacks called, but not failed subschemas', |
| 109 |
); |
| 110 |
}; |
| 111 |
|
| 112 |
subtest 'callbacks for keywords without eval subs' => sub { |
| 113 |
my %keywords; |
| 114 |
my $result = $js->evaluate( |
| 115 |
'hello', |
| 116 |
{ |
| 117 |
'$id' => 'my_weird_schema', |
| 118 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 119 |
'$vocabulary' => { 'https://json-schema.org/draft/2020-12/vocab/core' => true }, |
| 120 |
'$anchor' => 'my_anchor', |
| 121 |
'$comment' => 'my comment', |
| 122 |
'$defs' => { foo => true }, |
| 123 |
'$dynamicAnchor' => 'dynamicanchor', |
| 124 |
if => true, then => true, else => true, |
| 125 |
}, |
| 126 |
{ |
| 127 |
callbacks => { |
| 128 |
map +($_ => sub ($data, $schema, $state) { |
| 129 |
++$keywords{$state->{keyword}} |
| 130 |
}), qw($anchor $comment $defs $dynamicAnchor if then else $schema $vocabulary), |
| 131 |
}, |
| 132 |
}, |
| 133 |
); |
| 134 |
ok($result->valid, 'evaluation was successful'); |
| 135 |
|
| 136 |
is_equal( |
| 137 |
\%keywords, |
| 138 |
{ map +($_ => 1), qw($anchor $comment $defs $dynamicAnchor if then else $schema $vocabulary) }, |
| 139 |
'callbacks are triggered for keywords even when they lack evaluation subs', |
| 140 |
); |
| 141 |
}; |
| 142 |
|
| 143 |
subtest 'callbacks that produce errors' => sub { |
| 144 |
my $result = $js->evaluate( |
| 145 |
my $data = { |
| 146 |
alpha => 1, |
| 147 |
beta => 'foo', |
| 148 |
}, |
| 149 |
my $schema = { |
| 150 |
properties => { alpha => { type => 'number' } }, |
| 151 |
additionalProperties => { type => 'number' }, |
| 152 |
}, |
| 153 |
my $configs = { |
| 154 |
callbacks => { |
| 155 |
type => sub ($data, $schema, $state) { |
| 156 |
JSON::Schema::Modern::Utilities::E($state, 'this is a callback error'); |
| 157 |
}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
); |
| 161 |
is_equal( |
| 162 |
$result->TO_JSON, |
| 163 |
{ |
| 164 |
valid => false, |
| 165 |
errors => [ |
| 166 |
{ |
| 167 |
instanceLocation => '/alpha', |
| 168 |
keywordLocation => '/properties/alpha/type', |
| 169 |
error => 'this is a callback error', |
| 170 |
}, |
| 171 |
{ |
| 172 |
instanceLocation => '', |
| 173 |
keywordLocation => '/properties', |
| 174 |
error => 'not all properties are valid', |
| 175 |
}, |
| 176 |
{ |
| 177 |
instanceLocation => '/beta', |
| 178 |
keywordLocation => '/additionalProperties/type', |
| 179 |
error => 'got string, not number', |
| 180 |
}, |
| 181 |
{ |
| 182 |
instanceLocation => '', |
| 183 |
keywordLocation => '/additionalProperties', |
| 184 |
error => 'not all additional properties are valid', |
| 185 |
}, |
| 186 |
], |
| 187 |
}, |
| 188 |
'result object contains the callback error, and the other errors', |
| 189 |
); |
| 190 |
|
| 191 |
$result = $js->evaluate($data, $schema, { %$configs, short_circuit => 1 }); |
| 192 |
is_equal( |
| 193 |
$result->TO_JSON, |
| 194 |
{ |
| 195 |
valid => false, |
| 196 |
errors => [ |
| 197 |
{ |
| 198 |
instanceLocation => '/alpha', |
| 199 |
keywordLocation => '/properties/alpha/type', |
| 200 |
error => 'this is a callback error', |
| 201 |
}, |
| 202 |
{ |
| 203 |
instanceLocation => '', |
| 204 |
keywordLocation => '/properties', |
| 205 |
error => 'not all properties are valid', |
| 206 |
}, |
| 207 |
], |
| 208 |
}, |
| 209 |
'result object contains the callback error, and short-circuits execution', |
| 210 |
); |
| 211 |
}; |
| 212 |
|
| 213 |
done_testing; |