| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use 5.020; |
| 4 |
use stable 0.031 'postderef'; |
| 5 |
use experimental 'signatures'; |
| 6 |
no autovivification warn => qw(fetch store exists delete); |
| 7 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 8 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 9 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 10 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 11 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 12 |
no feature 'switch'; |
| 13 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 14 |
|
| 15 |
use lib 't/lib'; |
| 16 |
use Helper; |
| 17 |
|
| 18 |
my $js = JSON::Schema::Modern->new; |
| 19 |
ok(!$js->strict, 'strict defaults to false'); |
| 20 |
|
| 21 |
my $schema = { |
| 22 |
'$id' => 'my_loose_schema', |
| 23 |
type => 'object', |
| 24 |
properties => { |
| 25 |
alpha => { |
| 26 |
title => 'bloop', # produces an annotation for 'title' with value 'bloop' |
| 27 |
bloop => 'hi', # unknown keyword |
| 28 |
barf => 'no', # unknown keyword |
| 29 |
'x-alpha' => 1, # ok |
| 30 |
}, |
| 31 |
beta => { |
| 32 |
zip => 1, # unknown keyword |
| 33 |
dah => 2, # unknown keyword |
| 34 |
'x-beta' => 3, # ok |
| 35 |
}, |
| 36 |
}, |
| 37 |
}; |
| 38 |
|
| 39 |
my $document = $js->add_schema($schema); |
| 40 |
|
| 41 |
is_equal( |
| 42 |
$js->evaluate({ alpha => 1, beta => 2 }, 'my_loose_schema')->TO_JSON, |
| 43 |
{ valid => true }, |
| 44 |
'by default, unknown keywords are allowed in evaluate()', |
| 45 |
); |
| 46 |
|
| 47 |
is_equal( |
| 48 |
$js->evaluate({ alpha => 1, beta => 2 }, 'my_loose_schema', { strict => 1 })->TO_JSON, |
| 49 |
{ |
| 50 |
valid => false, |
| 51 |
errors => [ |
| 52 |
{ |
| 53 |
instanceLocation => '/alpha', |
| 54 |
keywordLocation => '/properties/alpha', |
| 55 |
absoluteKeywordLocation => 'my_loose_schema#/properties/alpha', |
| 56 |
error => 'unknown keywords seen in schema: barf, bloop', |
| 57 |
}, |
| 58 |
# beta not examined -- we immediately abort |
| 59 |
], |
| 60 |
}, |
| 61 |
'strict mode aborts immediately on unknown keywords during evaluation via a config override', |
| 62 |
); |
| 63 |
|
| 64 |
is_equal( |
| 65 |
$js->validate_schema($schema)->TO_JSON, |
| 66 |
{ valid => true }, |
| 67 |
'by default, unknown keywords are allowed in validate_schema()', |
| 68 |
); |
| 69 |
|
| 70 |
is_equal( |
| 71 |
$js->validate_schema($schema, { strict => 1 })->TO_JSON, |
| 72 |
my $schema_result = { |
| 73 |
valid => false, |
| 74 |
errors => [ |
| 75 |
{ |
| 76 |
instanceLocation => '/properties/alpha', |
| 77 |
keywordLocation => '', |
| 78 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/schema', |
| 79 |
error => 'unknown keywords seen in schema: barf, bloop', |
| 80 |
}, |
| 81 |
{ |
| 82 |
instanceLocation => '/properties/beta', |
| 83 |
keywordLocation => '', |
| 84 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/schema', |
| 85 |
error => 'unknown keywords seen in schema: dah, zip', |
| 86 |
}, |
| 87 |
], |
| 88 |
}, |
| 89 |
'strict mode finds all unknown keywords in validate_schema() via a config override', |
| 90 |
); |
| 91 |
|
| 92 |
|
| 93 |
$js = JSON::Schema::Modern->new(strict => 1); |
| 94 |
|
| 95 |
is_equal( |
| 96 |
$js->validate_schema($schema)->TO_JSON, |
| 97 |
$schema_result, |
| 98 |
'strict mode disallows unknown keywords in the schema data passed to validate_schema()', |
| 99 |
); |
| 100 |
|
| 101 |
delete $schema->{'$id'}; |
| 102 |
is_equal( |
| 103 |
$js->evaluate({ alpha => 1 }, $schema)->TO_JSON, |
| 104 |
{ |
| 105 |
valid => false, |
| 106 |
errors => [ |
| 107 |
{ |
| 108 |
# note no instance location - indicating evaluation has not started |
| 109 |
keywordLocation => '/properties/alpha', |
| 110 |
error => 'unknown keywords seen in schema: barf, bloop', |
| 111 |
}, |
| 112 |
{ |
| 113 |
keywordLocation => '/properties/beta', |
| 114 |
error => 'unknown keywords seen in schema: dah, zip', |
| 115 |
}, |
| 116 |
], |
| 117 |
}, |
| 118 |
'strict mode finds all unknown keywords during traverse', |
| 119 |
); |
| 120 |
|
| 121 |
my $lax_metaschema = { |
| 122 |
'$id' => 'https://example.com/my_lax_metaschema', |
| 123 |
'$schema' => 'https://json-schema.org/draft/2020-12/schema', |
| 124 |
'$dynamicAnchor' => 'meta', |
| 125 |
'$ref' => 'https://json-schema.org/draft/2020-12/schema', |
| 126 |
properties => { |
| 127 |
bloop => true, # bloop is now a recognized property |
| 128 |
}, |
| 129 |
}; |
| 130 |
|
| 131 |
$js->add_schema($lax_metaschema); |
| 132 |
$schema->{'$schema'} = 'https://example.com/my_lax_metaschema'; |
| 133 |
|
| 134 |
is_equal( |
| 135 |
$js->validate_schema($schema)->TO_JSON, |
| 136 |
{ |
| 137 |
valid => false, |
| 138 |
errors => [ |
| 139 |
{ |
| 140 |
instanceLocation => '/properties/alpha', |
| 141 |
keywordLocation => '', |
| 142 |
absoluteKeywordLocation => 'https://example.com/my_lax_metaschema', |
| 143 |
error => 'unknown keyword seen in schema: barf', |
| 144 |
}, |
| 145 |
{ |
| 146 |
instanceLocation => '/properties/beta', |
| 147 |
keywordLocation => '', |
| 148 |
absoluteKeywordLocation => 'https://example.com/my_lax_metaschema', |
| 149 |
error => 'unknown keywords seen in schema: dah, zip', |
| 150 |
}, |
| 151 |
], |
| 152 |
}, |
| 153 |
'strict mode only detected one property in alpha this time - bloop is evaluated', |
| 154 |
); |
| 155 |
|
| 156 |
|
| 157 |
$schema->{'$schema'} = 'http://json-schema.org/draft-07/schema#'; |
| 158 |
|
| 159 |
is_equal( |
| 160 |
$js->validate_schema($schema)->TO_JSON, |
| 161 |
{ |
| 162 |
valid => false, |
| 163 |
errors => [ |
| 164 |
{ |
| 165 |
instanceLocation => '/properties/alpha', |
| 166 |
keywordLocation => '', |
| 167 |
absoluteKeywordLocation => 'http://json-schema.org/draft-07/schema', |
| 168 |
error => 'unknown keywords seen in schema: barf, bloop', |
| 169 |
}, |
| 170 |
{ |
| 171 |
instanceLocation => '/properties/beta', |
| 172 |
keywordLocation => '', |
| 173 |
absoluteKeywordLocation => 'http://json-schema.org/draft-07/schema', |
| 174 |
error => 'unknown keywords seen in schema: dah, zip', |
| 175 |
}, |
| 176 |
], |
| 177 |
}, |
| 178 |
'strict mode detects unknown keywords using draft7', |
| 179 |
); |
| 180 |
|
| 181 |
subtest 'strict and if/then/else' => sub { |
| 182 |
is_equal( |
| 183 |
JSON::Schema::Modern->new(strict => 1)->evaluate( |
| 184 |
$_, |
| 185 |
{ |
| 186 |
if => { const => 0 }, |
| 187 |
then => true, |
| 188 |
else => true, |
| 189 |
}, |
| 190 |
{ strict => 1 }, |
| 191 |
)->TO_JSON, |
| 192 |
|
| 193 |
{ valid => true }, |
| 194 |
'no unknown keywords are identified, even though "' . ($_?'else':'then').'" is never evaluated', |
| 195 |
) foreach (0..1); |
| 196 |
}; |
| 197 |
|
| 198 |
subtest 'strict and short-circuit' => sub { |
| 199 |
is_equal( |
| 200 |
JSON::Schema::Modern->new->evaluate( |
| 201 |
{ foo => 1, bar => 2 }, |
| 202 |
{ |
| 203 |
if => { |
| 204 |
required => ['bar'], |
| 205 |
properties => { |
| 206 |
bar => { const => 1 }, |
| 207 |
}, |
| 208 |
additionalProperties => false, |
| 209 |
bloop => 1, |
| 210 |
}, |
| 211 |
then => false, |
| 212 |
else => true, |
| 213 |
}, |
| 214 |
{ strict => 1 }, |
| 215 |
)->TO_JSON, |
| 216 |
{ |
| 217 |
valid => false, |
| 218 |
errors => [ |
| 219 |
{ |
| 220 |
instanceLocation => '', |
| 221 |
keywordLocation => '/if', |
| 222 |
error => 'unknown keyword seen in schema: bloop', |
| 223 |
}, |
| 224 |
# no errors from additionalProperties, because we abort from within the 'if' subschema, |
| 225 |
# and 'if' doesn't preserve its errors |
| 226 |
], |
| 227 |
}, |
| 228 |
'strict mode will work properly even when some keywords short-circuit', |
| 229 |
); |
| 230 |
|
| 231 |
is_equal( |
| 232 |
JSON::Schema::Modern->new->evaluate( |
| 233 |
{ alpha => { beta => 2, gamma => 3 } }, |
| 234 |
# this is not a valid test, because we never preserve errors from 'if'. |
| 235 |
# so we need something with a subschema that preserves errors, like properties. |
| 236 |
my $schema = { |
| 237 |
properties => { |
| 238 |
alpha => { |
| 239 |
required => ['beta'], |
| 240 |
properties => { |
| 241 |
beta => { const => 2 }, |
| 242 |
}, |
| 243 |
additionalProperties => false, |
| 244 |
bloop => 1, |
| 245 |
}, |
| 246 |
}, |
| 247 |
}, |
| 248 |
{ strict => 1, short_circuit => 1 }, |
| 249 |
)->TO_JSON, |
| 250 |
{ |
| 251 |
valid => false, |
| 252 |
errors => [ |
| 253 |
{ |
| 254 |
instanceLocation => '/alpha/gamma', |
| 255 |
keywordLocation => '/properties/alpha/additionalProperties', |
| 256 |
error => 'additional property not permitted', |
| 257 |
}, |
| 258 |
{ |
| 259 |
instanceLocation => '/alpha', |
| 260 |
keywordLocation => '/properties/alpha/additionalProperties', |
| 261 |
error => 'not all additional properties are valid', |
| 262 |
}, |
| 263 |
{ |
| 264 |
instanceLocation => '/alpha', |
| 265 |
keywordLocation => '/properties/alpha', |
| 266 |
error => 'unknown keyword seen in schema: bloop', |
| 267 |
}, |
| 268 |
], |
| 269 |
}, |
| 270 |
'strict mode will still work even when enabled together with short_circuit', |
| 271 |
) |
| 272 |
}; |
| 273 |
|
| 274 |
done_testing; |