# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : use strictures 2; use 5.020; use stable 0.031 'postderef'; use experimental 'signatures'; no autovivification warn => qw(fetch store exists delete); use if "$]" >= 5.022, experimental => 're_strict'; no if "$]" >= 5.031009, feature => 'indirect'; no if "$]" >= 5.033001, feature => 'multidimensional'; no if "$]" >= 5.033006, feature => 'bareword_filehandles'; no if "$]" >= 5.041009, feature => 'smartmatch'; no feature 'switch'; use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 use lib 't/lib'; use Helper; my $js = JSON::Schema::Modern->new; cmp_result( $js->validate_schema({ type => 'bloop' })->TO_JSON, { valid => false, errors => supersetof( { instanceLocation => '/type', keywordLocation => re(qr{/enum\z}), absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/meta/validation#/$defs/simpleTypes/enum', 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)', }, ), }, 'validate_schema on simple schema with no $schema keyword', ); cmp_result( $js->validate_schema({ '$schema' => 'https://json-schema.org/draft/2019-09/schema', type => 'bloop', })->TO_JSON, { valid => false, errors => supersetof( { instanceLocation => '/type', keywordLocation => re(qr{/enum\z}), absoluteKeywordLocation => 'https://json-schema.org/draft/2019-09/meta/validation#/$defs/simpleTypes/enum', 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)', }, ), }, 'validate_schema on schema with metaschema $schema keyword', ); $js->add_schema('http://example.com/myschema', { '$id' => 'http://example.com/myschema', type => 'boolean' }); is_equal( $js->validate_schema({ '$schema' => 'http://example.com/myschema', type => 'bloop', })->TO_JSON, { valid => false, errors => [ { instanceLocation => '', keywordLocation => '/type', absoluteKeywordLocation => 'http://example.com/myschema#/type', error => 'got object, not boolean', }, ], }, 'validate_schema with custom metaschema', ); is_equal( $js->validate_schema({ '$id' => '#/$defs/foo', '$schema' => 'http://json-schema.org/draft-07/schema#', })->TO_JSON, { valid => false, errors => [ { keywordLocation => '/$id', error => '$id value "#/$defs/foo" does not match required syntax', }, ], }, 'validate_schema with schema that validates against the metaschema, but fails in extra traverse checks', ); is_equal( $js->validate_schema({ 'id' => 'foo', '$schema' => 'http://json-schema.org/draft-04/schema#', allOf => [ { '$ref' => '/some-location' } ], }, { strict => 1 })->TO_JSON, { valid => true }, 'draft4 schemas can be validated, even though there is no representation for $ref in the metaschema', ); cmp_result( $js->validate_schema({ pattern => '^(abc]', })->TO_JSON, { valid => false, errors => superbagof( { instanceLocation => '/pattern', keywordLocation => re(qr{/pattern/format\z}), absoluteKeywordLocation => 'https://json-schema.org/draft/2020-12/meta/validation#/properties/pattern/format', error => 'not a valid regex string', }, ), }, 'validate_schema with error in format', ); done_testing;