JSON::Validator(3pm) | User Contributed Perl Documentation | JSON::Validator(3pm) |
JSON::Validator - Validate data against a JSON schema
JSON::Validator::Schema or any of the sub classes can be used instead of JSON::Validator. The only reason to use JSON::Validator directly is if you don't know the schema version up front.
use JSON::Validator; my $jv = JSON::Validator->new; # Define a schema - http://json-schema.org/learn/miscellaneous-examples.html # You can also load schema from disk or web $jv->schema({ type => "object", required => ["firstName", "lastName"], properties => { firstName => {type => "string"}, lastName => {type => "string"}, age => {type => "integer", minimum => 0, description => "Age in years"} } }); # Validate your data my @errors = $jv->validate({firstName => "Jan Henning", lastName => "Thorsen", age => -42}); # Do something if any errors was found die "@errors" if @errors;
# Use joi() to build the schema use JSON::Validator::Joi 'joi'; $jv->schema(joi->object->props({ firstName => joi->string->required, lastName => joi->string->required, age => joi->integer->min(0), })); # joi() can also validate directly my @errors = joi( {firstName => "Jan Henning", lastName => "Thorsen", age => -42}, joi->object->props({ firstName => joi->string->required, lastName => joi->string->required, age => joi->integer->min(0), }), );
JSON::Validator is a data structure validation library based around JSON Schema <https://json-schema.org/>. This module can be used directly with a JSON schema or you can use the elegant DSL schema-builder JSON::Validator::Joi to define the schema programmatically.
JSON::Validator can load JSON schemas in multiple formats: Plain perl data structured (as shown in "SYNOPSIS"), JSON or YAML. The JSON parsing is done with Mojo::JSON, while YAML files requires YAML::PP or YAML::XS.
Here are some resources that are related to JSON schemas and validation:
This module comes with some JSON specifications bundled, so your application don't have to fetch those from the web. These specifications should be up to date, but please submit an issue if they are not.
Files referenced to an URL will automatically be cached if the first element in "cache_paths" is a writable directory. Note that the cache headers for the remote assets are not honored, so you will manually need to remove any cached file, should you need to refresh them.
To download and cache an online asset, do this:
JSON_VALIDATOR_CACHE_PATH=/some/writable/directory perl myapp.pl
Here is the list of the bundled specifications:
Web page: <http://json-schema.org>
$ref: <http://json-schema.org/draft-04/schema#>, <http://json-schema.org/draft-06/schema#>, <http://json-schema.org/draft-07/schema#>.
Web page: <http://jsonpatch.com>
$ref: <http://json.schemastore.org/json-patch#>
Web page: <https://openapis.org>
$ref: <http://swagger.io/v2/schema.json#>
Web page: <https://openapis.org>
$ref: https://spec.openapis.org/oas/3.0/schema/2019-04-02 <https://github.com/OAI/OpenAPI-Specification/blob/master/schemas/v3.0/schema.json>
This specification is still EXPERIMENTAL.
This is used for unit tests, and should not be relied on by external users.
Installing Sereal::Encoder v4.00 (or later) will make "data_checksum" in JSON::Validator::Util significantly faster. This function is used both when parsing schemas and validating data.
See the documentation in JSON::Validator::Formats for other optional modules to do validation of specific "format", such as "hostname", "ipv4" and others.
Proxy attribute for "cache_paths" in JSON::Validator::Store.
This attribute will be used as default value for "formats" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:
$jv->formats(...); # Legacy $jv->schema->formats(...); # Recommended way
This attribute will be used as default value for "recursive_data_protection" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:
$jv->recursive_data_protection(...); # Legacy $jv->schema->recursive_data_protection(...); # Recommended way
$store = $jv->store;
Holds a JSON::Validator::Store object that caches the retrieved schemas. This object will be shared amongst different "schema" objects to prevent a schema from having to be downloaded again.
Proxy attribute for "ua" in JSON::Validator::Store.
This method can be used to get a bundled version of "schema". It will however return a data-structure instead of a new object. See "bundle" in JSON::Validator::Schema for an alternative.
# These two lines does the same $data = $jv->bundle; $data = $jv->schema->bundle->data; # Recommended way $schema = $jv->schema->bundle;
This attribute will be used as default value for "coerce" in JSON::Validator::Schema. It is highly recommended to change this directly on the "schema" instead:
$jv->coerce(...); # Legacy $jv->schema->coerce(...); # Recommended way
Proxy method for "get" in JSON::Validator::Schema.
$jv = JSON::Validator->new(%attributes); $jv = JSON::Validator->new(\%attributes);
Creates a new JSON::Validate object.
This method will be deprecated in the future. See "errors" in JSON::Validator::Schema and "is_invalid" in JSON::Validator::Schema instead.
$jv = $jv->schema($json_or_yaml_string); $jv = $jv->schema($url); $jv = $jv->schema(\%schema); $jv = $jv->schema(JSON::Validator::Joi->new); $jv = $jv->schema(JSON::Validator::Schema->new); $schema = $jv->schema;
Used to set a schema from either a data structure or a URL.
$schema will be an instance of JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6 JSON::Validator::Schema::Draft7, JSON::Validator::Schema::Draft201909, JSON::Validator::Schema::OpenAPIv2, JSON::Validator::Schema::OpenAPIv3 or JSON::Validator::Schema.
The $url can take many forms, but needs to point to a text file in the JSON or YAML format.
A file on disk. Note that it is required to use the "file" scheme if you want to reference absolute paths on your file system.
A web resource will be fetched using the Mojo::UserAgent, stored in "ua".
Will load a given "spec.json" file from "Some::Module" using "data_section" in JSON::Validator::Util.
A "data" URL without a module name will use the current package and search up the call/inheritance tree.
An URL (without a recognized scheme) will be treated as a path to a file on disk. If the file could not be found on disk and the path starts with "/", then the will be loaded from the app defined in "ua". Something like this:
$jv->ua->server->app(MyMojoApp->new); $jv->ua->get('/any/other/url.json');
Proxy method for "validate" in JSON::Validator::Schema.
JSON::Validator::Formats contains utility functions for validating data types. Could be useful for validating data without loading a schema.
JSON::Validator::Schema is the base class for JSON::Validator::Schema::Draft4, JSON::Validator::Schema::Draft6 JSON::Validator::Schema::Draft7, JSON::Validator::Schema::Draft201909, JSON::Validator::Schema::OpenAPIv2 or JSON::Validator::Schema::OpenAPIv3.
JSON::Validator::Util contains many useful function when working with schemas.
Mojolicious::Plugin::OpenAPI is a plugin for Mojolicious that utilize JSON::Validator and the OpenAPI specification <https://www.openapis.org/> to build routes with input and output validation.
Copyright (C) 2014-2021, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Jan Henning Thorsen - "jhthorsen@cpan.org"
2023-03-06 | perl v5.36.0 |