JSON::Schema::Tiny - Validate data against a schema, minimally
version 0.021
my $data = { hello => 1 }; my $schema = { type => "object", properties => { hello => { type => "integer" } }, }; # functional interface: use JSON::Schema::Tiny qw(evaluate); my $result = evaluate($data, $schema); # { valid => true } # object-oriented interface: use JSON::Schema::Tiny; my $js = JSON::Schema::Tiny->new; my $result = $js->evaluate($data, $schema); # { valid => true }
This module aims to be a slimmed-down JSON Schema evaluator and validator, supporting the most popular keywords. (See "UNSUPPORTED JSON-SCHEMA FEATURES" below for exclusions.)
my $result = evaluate($data, $schema);
Evaluates the provided instance data against the known schema document.
The data is in the form of an unblessed nested Perl data structure representing any type that JSON allows: null, boolean, string, number, object, array. (See "TYPES" below.)
The schema must represent a valid JSON Schema in the form of a Perl data structure, such as what is returned from a JSON decode operation.
With default configuration settings, the return value is a hashref indicating the validation success or failure, plus (when validation failed), an arrayref of error strings in standard JSON Schema format. For example:
running:
$result = evaluate(1, { type => 'number' });
$result is:
$result
{ valid => true }
$result = evaluate(1, { type => 'number', multipleOf => 2 });
{ valid => false, errors => [ { instanceLocation => '', keywordLocation => '/multipleOf', error => 'value is not a multiple of 2', }, ], }
When "$BOOLEAN_RESULT" is true, the return value is a boolean (indicating evaluation success or failure).
$BOOLEAN_RESULT
All options are available as package-scoped global variables. Use local to configure them for a local scope. They may also be set via the constructor, as lower-cased values in a hash, e.g.: JSON::Schema::Tiny->new(boolean_result => 1, max_traversal_depth => 10);
JSON::Schema::Tiny->new(boolean_result => 1, max_traversal_depth => 10);
When true, "evaluate" will return a true or false result only, with no error strings. This enables short-circuit mode internally as this cannot effect results except get there faster. Defaults to false.
$SHORT_CIRCUIT
When true, "evaluate" will return from evaluating each subschema as soon as a true or false result can be determined. When $BOOLEAN_RESULT is false, an incomplete list of errors will be returned. Defaults to false.
$MAX_TRAVERSAL_DEPTH
The maximum number of levels deep a schema traversal may go, before evaluation is halted. This is to protect against accidental infinite recursion, such as from two subschemas that each reference each other, or badly-written schemas that could be optimized. Defaults to 50.
$SCALARREF_BOOLEANS
When true, any type that is expected to be a boolean in the instance data may also be expressed as the scalar references \0 or \1 (which are serialized as booleans by JSON backends). Defaults to false.
\0
\1
$SPECIFICATION_VERSION
When set, the version of the draft specification is locked to one particular value, and use of keywords inconsistent with that specification version will result in an error. Will be set internally automatically with the use of the $schema keyword. When not set, all keywords will be honoured (when otherwise supported).
$schema
Supported values for this option, and the corresponding values for the $schema keyword, are:
draft2020-12 or 2020-12, corresponding to metaschema https://json-schema.org/draft/2020-12/schema
draft2020-12
2020-12
https://json-schema.org/draft/2020-12/schema
draft2019-09 or 2019-09, corresponding to metaschema https://json-schema.org/draft/2019-09/schema
draft2019-09
2019-09
https://json-schema.org/draft/2019-09/schema
draft7 or 7, corresponding to metaschema http://json-schema.org/draft-07/schema#
draft7
7
http://json-schema.org/draft-07/schema#
Defaults to undef.
Unlike JSON::Schema::Modern, this is not a complete implementation of the JSON Schema specification. Some features and keywords are left unsupported in order to keep the code small and the execution fast. These features are not available:
any output format other than flag (when $BOOLEAN_RESULT is true) or basic (when it is false)
flag
basic
annotations in successful evaluation results
use of $ref other than to locations in the local schema in json-pointer format (e.g. #/path/to/property). This means that references to external documents, either those available locally or on the network, are not permitted.
$ref
#/path/to/property
In addition, these keywords are implemented only partially or not at all (their presence in a schema will be ignored or possibly result in an error):
$schema - only accepted if set to one of the specification metaschema URIs (see $SPECIFICATION_VERSION for supported values)
$id
$anchor
$recursiveAnchor and $recursiveRef (draft2019-09), and $dynamicAnchor and $dynamicRef (draft2020-12 and thereafter)
$recursiveAnchor
$recursiveRef
$dynamicAnchor
$dynamicRef
$vocabulary
unevaluatedItems and unevaluatedProperties (which require annotation support)
unevaluatedItems
unevaluatedProperties
format (does not cause an error when used)
format
For a more full-featured implementation of the JSON Schema specification, see JSON::Schema::Modern.
Perl is a more loosely-typed language than JSON. This module delves into a value's internal representation in an attempt to derive the true "intended" type of the value. However, if a value is used in another context (for example, a numeric value is concatenated into a string, or a numeric string is used in an arithmetic operation), additional flags can be added onto the variable causing it to resemble the other type. This should not be an issue if data validation is occurring immediately after decoding a JSON (or YAML) payload.
For more information, see "MAPPING" in Cpanel::JSON::XS.
The pattern and patternProperties keywords evaluate regular expressions from the schema. No effort is taken (at this time) to sanitize the regular expressions for embedded code or potentially pathological constructs that may pose a security risk, either via denial of service or by allowing exposure to the internals of your application. DO NOT USE SCHEMAS FROM UNTRUSTED SOURCES.
pattern
patternProperties
JSON::Schema::Modern: a more specification-compliant JSON Schema evaluator
Test::JSON::Schema::Acceptance: contains the official JSON Schema test suite
https://json-schema.org
Understanding JSON Schema: tutorial-focused documentation
Bugs may be submitted through https://github.com/karenetheridge/JSON-Schema-Tiny/issues.
I am also usually active on irc, as 'ether' at irc.perl.org and irc.libera.chat.
irc.perl.org
irc.libera.chat
You can also find me on the JSON Schema Slack server and OpenAPI Slack server, which are also great resources for finding help.
Karen Etheridge <ether@cpan.org>
Matt S Trout <mst@shadowcat.co.uk>
This software is copyright (c) 2021 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.
To install JSON::Schema::Tiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Schema::Tiny
CPAN shell
perl -MCPAN -e shell install JSON::Schema::Tiny
For more information on module installation, please visit the detailed CPAN module installation guide.