| 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 |
|
| 20 |
cmp_result( |
| 21 |
$js->validate_schema({ type => 'bloop' })->TO_JSON, |
| 22 |
{ |
| 23 |
valid => false, |
| 24 |
errors => supersetof( |
| 25 |
{ |
| 26 |
instanceLocation => '/type', |
| 27 |
keywordLocation => re(qr{/enum\z}), |
| 28 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/meta/validation#/$defs/simpleTypes/enum', |
| 29 |
error => 'value does not match (from enum 0: strings not equal; from enum 1: strings not equal; from enum 2: strings not equal; from enum 3: strings not equal; from enum 4: strings not equal; from enum 5: strings not equal; from enum 6: strings not equal)', |
| 30 |
}, |
| 31 |
), |
| 32 |
}, |
| 33 |
'validate_schema on simple schema with no $schema keyword', |
| 34 |
); |
| 35 |
|
| 36 |
cmp_result( |
| 37 |
$js->validate_schema({ |
| 38 |
'$schema' => 'https://json-schema.org/draft/2019-09/schema', |
| 39 |
type => 'bloop', |
| 40 |
})->TO_JSON, |
| 41 |
{ |
| 42 |
valid => false, |
| 43 |
errors => supersetof( |
| 44 |
{ |
| 45 |
instanceLocation => '/type', |
| 46 |
keywordLocation => re(qr{/enum\z}), |
| 47 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/validation#/$defs/simpleTypes/enum', |
| 48 |
error => 'value does not match (from enum 0: strings not equal; from enum 1: strings not equal; from enum 2: strings not equal; from enum 3: strings not equal; from enum 4: strings not equal; from enum 5: strings not equal; from enum 6: strings not equal)', |
| 49 |
}, |
| 50 |
), |
| 51 |
}, |
| 52 |
'validate_schema on schema with metaschema $schema keyword', |
| 53 |
); |
| 54 |
|
| 55 |
$js->add_schema('http://example.com/myschema', { '$id' => 'http://example.com/myschema', type => 'boolean' }); |
| 56 |
|
| 57 |
is_equal( |
| 58 |
$js->validate_schema({ |
| 59 |
'$schema' => 'http://example.com/myschema', |
| 60 |
type => 'bloop', |
| 61 |
})->TO_JSON, |
| 62 |
{ |
| 63 |
valid => false, |
| 64 |
errors => [ |
| 65 |
{ |
| 66 |
instanceLocation => '', |
| 67 |
keywordLocation => '/type', |
| 68 |
absoluteKeywordLocation => 'http://example.com/myschema#/type', |
| 69 |
error => 'got object, not boolean', |
| 70 |
}, |
| 71 |
], |
| 72 |
}, |
| 73 |
'validate_schema with custom metaschema', |
| 74 |
); |
| 75 |
|
| 76 |
is_equal( |
| 77 |
$js->validate_schema({ |
| 78 |
'$id' => '#/$defs/foo', |
| 79 |
'$schema' => 'http://json-schema.org/draft-07/schema#', |
| 80 |
})->TO_JSON, |
| 81 |
{ |
| 82 |
valid => false, |
| 83 |
errors => [ |
| 84 |
{ |
| 85 |
keywordLocation => '/$id', |
| 86 |
error => '$id value "#/$defs/foo" does not match required syntax', |
| 87 |
}, |
| 88 |
], |
| 89 |
}, |
| 90 |
'validate_schema with schema that validates against the metaschema, but fails in extra traverse checks', |
| 91 |
); |
| 92 |
|
| 93 |
is_equal( |
| 94 |
$js->validate_schema({ |
| 95 |
'id' => 'foo', |
| 96 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 97 |
allOf => [ { '$ref' => '/some-location' } ], |
| 98 |
}, { strict => 1 })->TO_JSON, |
| 99 |
{ valid => true }, |
| 100 |
'draft4 schemas can be validated, even though there is no representation for $ref in the metaschema', |
| 101 |
); |
| 102 |
|
| 103 |
cmp_result( |
| 104 |
$js->validate_schema({ |
| 105 |
pattern => '^(abc]', |
| 106 |
})->TO_JSON, |
| 107 |
{ |
| 108 |
valid => false, |
| 109 |
errors => superbagof( |
| 110 |
{ |
| 111 |
instanceLocation => '/pattern', |
| 112 |
keywordLocation => re(qr{/pattern/format\z}), |
| 113 |
absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/meta/validation#/properties/pattern/format', |
| 114 |
error => 'not a valid regex string', |
| 115 |
}, |
| 116 |
), |
| 117 |
}, |
| 118 |
'validate_schema with error in format', |
| 119 |
); |
| 120 |
|
| 121 |
done_testing; |