NAME
Test::JSON::Schema::Acceptance - Acceptance testing for JSON-Schema based validators like JSON::Schema
VERSION
version 0.993
SYNOPSIS
This module allows the JSON Schema Test Suite tests to be used in perl to test a module that implements the JSON Schema specification ("json-schema"). These are the same tests that many modules (libraries, plugins, packages, etc.) use to confirm support of json-schema. Using this module to confirm support gives assurance of interoperability with other modules that run the same tests in different languages.
In the JSON::Schema module, a test could look like the following:
use Test::More;
use JSON::Schema;
use Test::JSON::Schema::Acceptance;
my $accepter = Test::JSON::Schema::Acceptance->new(specification => 'draft3');
$accepter->acceptance(
validate_data => sub {
my ($schema, $input_data) = @_;
return JSON::Schema->new($schema)->validate($input_data);
},
todo_tests => [ { file => 'dependencies.json' } ],
);
done_testing();
This would determine if JSON::Schema's validate
method returns the right result for all of the cases in the JSON Schema Test Suite, except for those listed in $skip_tests
.
DESCRIPTION
JSON Schema is an IETF draft (at time of writing) which allows you to define the structure of JSON.
From the overview of the draft 2019-09 version of the specification:
This document proposes a new media type "application/schema+json" to identify a JSON Schema for describing JSON data. It also proposes a further optional media type, "application/schema-instance+json", to provide additional integration features. JSON Schemas are themselves JSON documents. This, and related specifications, define keywords allowing authors to describe JSON data in several ways.
JSON Schema uses keywords to assert constraints on JSON instances or annotate those instances with additional information. Additional keywords are used to apply assertions and annotations to more complex JSON data structures, or based on some sort of condition.
This module allows other perl modules (for example JSON::Schema) to test that they are JSON Schema-compliant, by running the tests from the official test suite, without having to manually convert them to perl tests.
You are unlikely to want this module, unless you are attempting to write a module which implements JSON Schema the specification, and want to test your compliance.
CONSTRUCTOR
Test::JSON::Schema::Acceptance->new(specification => $specification_version)
Create a new instance of Test::JSON::Schema::Acceptance.
Available options are:
specification
This determines the draft version of the schema to confirm compliance to. Possible values are:
draft3
draft4
draft6
draft7
draft2019-09
latest
(alias fordraft2019-09
)
The default is latest
, but in the synopsis example, JSON::Schema is testing draft 3 compliance.
(For backwards compatibility, new
can be called with a single numeric argument of 3 to 7, which maps to draft3
through draft7
.)
test_dir
Instead of specifying a draft specification to test against, which will select the most appropriate tests, you can pass in the name of a directory of tests to run directly. Files in this directory should be .json files following the format described in https://github.com/json-schema-org/JSON-Schema-Test-Suite/blob/master/README.md.
SUBROUTINES/METHODS
acceptance
Accepts a hash of options as its arguments.
(Backwards-compatibility mode: accepts a subroutine which is used as validate_json_string
, and a hashref of arguments.)
Available options are:
validate_data
A subroutine reference, which is passed two arguments: the JSON Schema, and the inflated data structure to be validated.
The subroutine should return truthy or falsey depending on if the schema was valid for the input or not.
Either validate_data
or validate_json_string
is required.
validate_json_string
A subroutine reference, which is passed two arguments: the JSON Schema, and the JSON string containing the data to be validated.
The subroutine should return truthy or falsey depending on if the schema was valid for the input or not.
Either validate_data
or validate_json_string
is required.
verbose
Optional. When true, prints version information and test result table such that it is visible during make test
or prove
.
tests
Optional. Restricts tests to just those mentioned (the conditions are ANDed together, not ORed). The syntax can take one of many forms:
# run tests in this file
tests => { file => 'dependencies.json' }
# run tests in these files
tests => { file => [ 'dependencies.json', 'refRemote.json' ] }
# run tests in this file with this group description
tests => {
file => 'refRemote.json',
group_description => 'remote ref',
}
# run tests in this file with these group descriptions
tests => {
file => 'const.json',
group_description => [ 'const validation', 'const with object' ],
}
# run tests in this file with this group description and test description
tests => {
file => 'const.json',
group_description => 'const validation',
test_description => 'another type is invalid',
}
# run tests in this file with this group description and these test descriptions
tests => {
file => 'const.json',
group_description => 'const validation',
test_description => [ 'same value is valid', 'another type is invalid' ],
}
todo_tests
Optional. Mentioned tests will run as "TODO". Uses arrayrefs of the same hashref structure as "tests" above, which are ORed together.
todo_tests => [
# all tests in this file are TODO
{ file => 'dependencies.json' },
# just some tests in this file are TODO
{ file => 'boolean_schema.json', test_description => 'array is invalid' },
# .. etc
]
ACKNOWLEDGEMENTS
Daniel Perrett <perrettdl@cpan.org> for the concept and help in design.
Ricardo Signes <rjbs@cpan.org> for direction to and creation of Test::Fatal.
Various others in #perl-help.
SUPPORT
Bugs may be submitted through https://github.com/karenetheridge/Test-JSON-Schema-Acceptance/issues.
AUTHOR
Ben Hutton (@relequestual) <relequest@cpan.org>
CONTRIBUTORS
Karen Etheridge <ether@cpan.org>
Ben Hutton <relequestual@cpan.org>
Daniel Perrett <dp13@sanger.ac.uk>
COPYRIGHT AND LICENCE
This software is Copyright (c) 2015 by Ben Hutton.
This is free software, licensed under:
The MIT (X11) License