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;
Using joi
# 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),
}),
);
DESCRIPTION
JSON::Validator is a data structure validation library based around
JSON Schema. 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.
Supported schema formats
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.
Resources
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.
See the documentation in JSON::Validator::Formats for other optional modules
to do validation of specific “format”, such as “hostname”, “ipv4” and others.
$jv->recursive_data_protection(...); # Legacy
$jv->schema->recursive_data_protection(...); # Recommended way
store
$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.
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;
A “data” URL without a module name will use the current package and search up
the call/inheritance tree.
Any other URL
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:
NAME
JSON::Validator - Validate data against a JSON schema
SYNOPSIS
Using a schema object
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.
Basics
Using joi
DESCRIPTION
JSON::Validator is a data structure validation library based around JSON Schema. 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.
Supported schema formats
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.
Resources
Here are some resources that are related to JSON schemas and validation:
Bundled specifications
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:
Here is the list of the bundled specifications:
JSON schema, draft 4, 6, 7, 2019-09.
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#.JSON schema for JSONPatch files
Web page: http://jsonpatch.com
$ref: http://json.schemastore.org/json-patch#Swagger / OpenAPI specification, version 2
Web page: https://openapis.org
$ref: http://swagger.io/v2/schema.json#OpenAPI specification, version 3
Web page: https://openapis.org
$ref: https://spec.openapis.org/oas/3.0/schema/2019-04-02This specification is still EXPERIMENTAL.
Swagger Petstore
This is used for unit tests, and should not be relied on by external users.
Optional modules
Sereal::Encoder
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.
Format validators
See the documentation in JSON::Validator::Formats for other optional modules to do validation of specific “format”, such as “hostname”, “ipv4” and others.
ATTRIBUTES
cache_paths
Proxy attribute for “cache_paths” in JSON::Validator::Store.
formats
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:
recursive_data_protection
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:
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.
ua
Proxy attribute for “ua” in JSON::Validator::Store.
METHODS
bundle
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.
coerce
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:
get
Proxy method for “get” in JSON::Validator::Schema.
new
Creates a new JSON::Validate object.
load_and_validate_schema
This method will be deprecated in the future. See “errors” in JSON::Validator::Schema and “is_invalid” in JSON::Validator::Schema instead.
schema
Used to set a schema from either a data structure or a URL.
$schemawill 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
$urlcan take many forms, but needs to point to a text file in the JSON or YAML format.file://…
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.
http://… or https://…
A web resource will be fetched using the Mojo::UserAgent, stored in “ua”.
data://Some::Module/spec.json
Will load a given “spec.json” file from
Some::Moduleusing “data_section” in JSON::Validator::Util.data:///spec.json
A “data” URL without a module name will use the current package and search up the call/inheritance tree.
Any other URL
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:
validate
Proxy method for “validate” in JSON::Validator::Schema.
SEE ALSO
JSON::Validator::Formats
JSON::Validator::Formats contains utility functions for validating data types. Could be useful for validating data without loading a schema.
JSON::Validator::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
JSON::Validator::Util contains many useful function when working with schemas.
Mojolicious::Plugin::OpenAPI
Mojolicious::Plugin::OpenAPI is a plugin for Mojolicious that utilize JSON::Validator and the OpenAPI specification to build routes with input and output validation.
COPYRIGHT AND LICENSE
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.
AUTHORS
Project Founder
Jan Henning Thorsen -
jhthorsen@cpan.orgContributors