NAME
Test::JSON::Schema - Test your data against a JSON Schema
VERSION
version 0.001
SYNOPSIS
use Test2::V0l
use Test::JSON::Schema;
# something in your application that generates a data structure
# { foo => 1, bar => 2 }
my $data = MyApp->execute(...);
is(
$data,
json_schema({
properties => {
foo => { type => 'string' },
bar => { type => 'number' },
},
}),
'data matches the expected schema',
);
# file contains: { "type": "number" }
load_json_schema('t/data/common-app-data-format.json');
is(
$data,
json_schema({
properties => {
foo => { type => 'string' },
bar => { '$ref' => 'file://t/data/common-app-data-format.json' },
},
}),
'data matches the expected schema, with some of it loaded from an external file',
);
is(
'hello',
json_schema('t/data/common-app-data-format.json'),
'data matches the expected schema, with the entirety of it loaded from an external file',
prints:
not ok 1 data matches the expected schema
# [
# {
# "instanceLocation": "/foo",
# "keywordLocation": "/properties/foo/type",
# "error": "got number, not string",
# },
# {
# "instanceLocation": "",
# "keywordLocation": "/properties",
# "error": "not all properties are valid",
# }
not ok 2 data matches the expected schema, with some of it loaded from an external file
# {
# "instanceLocation": "/bar",
# "keywordLocation": "/properties/bar/$ref/type",
# "absoluteKeywordLocation": "file://t/data/common-app-data-format.json#/type",
# "error": "got string, not number",
# },
# {
# "instanceLocation": "",
# "keywordLocation": "/properties",
# "error": "not all properties are valid",
# }
not ok 3 data matches the expected schema, with the entirety of it loaded from an external file
# {
# "instanceLocation": "hello",
# "keywordLocation": "type",
# "absoluteKeywordLocation": "file://t/data/common-app-data-format.json#/type",
# "error": "got string, not number",
# },
DESCRIPTION
NOTE: this distribution is currently vapourware and is not yet implemented! If you have some opinions about the interface, please come talk to me!
Use a JSON Schema to describe your expected data structure, and embed that in a Test2 function call.
FUNCTIONS/METHODS
json_schema
Expresses expected data in the form of a JSON Schema.
load_json_schema
Loads a JSON Schema into the evaluator so it can be used in subsequent json_schema
calls.
If it is a hashref, it is treated as an inline JSON Schema; you must include an $id
keyword so it can be later used via $ref
keywords. If it is a string, it is treated as a filename: the file is loaded from disk; it must have a .json
or .yaml
extension so that its format can be determined. The filename itself is used as the identifier, if an $id
keyword is not used in the schema.
SEE ALSO
SUPPORT
Bugs may be submitted through https://github.com/karenetheridge/Test-JSON-Schema/issues.
There is also a mailing list available for users of this distribution, at http://lists.perl.org/list/perl-qa.html.
There is also an irc channel available for users of this distribution, at #perl
on irc.perl.org
.
I am also usually active on irc, as 'ether' at irc.perl.org
and irc.libera.chat
.
AUTHOR
Karen Etheridge <ether@cpan.org>
COPYRIGHT AND LICENCE
This software is copyright (c) 2025 by Karen Etheridge.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.