NAME

JSON::Validator::Store - Load and caching JSON schemas

SYNOPSIS

use JSON::Validator;
my $jv = JSON::Validator->new;
$jv->store->add("urn:uuid:ee564b8a-7a87-4125-8c96-e9f123d6766f" => {...});
$jv->store->load("http://api.example.com/my/schema.json");

DESCRIPTION

JSON::Validator::Store is a class for loading and caching JSON-Schemas.

ATTRIBUTES

cache_paths

my $store     = $store->cache_paths(\@paths);
my $array_ref = $store->cache_paths;

A list of directories to where cached specifications are stored. Defaults to JSON_VALIDATOR_CACHE_PATH environment variable and the specs that is bundled with this distribution.

JSON_VALIDATOR_CACHE_PATH can be a list of directories, each separated by ":".

See "Bundled specifications" in JSON::Validator for more details.

schemas

my $hash_ref = $store->schemas;
my $store = $store->schemas({});

Hold the schemas as data structures. The keys are schema "id".

ua

my $ua    = $store->ua;
my $store = $store->ua(Mojo::UserAgent->new);

Holds a Mojo::UserAgent object, used by "schema" to load a JSON schema from remote location.

The default Mojo::UserAgent will detect proxy settings and have "max_redirects" in Mojo::UserAgent set to 3.

METHODS

add

my $normalized_id = $store->add($id => \%schema);

Used to add a schema data structure. Note that $id might not be the same as $normalized_id.

exists

my $normalized_id = $store->exists($id);

Returns a $normalized_id if it is present in the "schemas".

get

my $schema = $store->get($normalized_id);

Used to retrieve a $schema added by "add" or "load".

load

my $normalized_id = $store->load('https://...');
my $normalized_id = $store->load('data://main/foo.json');
my $normalized_id = $store->load('---\nid: yaml');
my $normalized_id = $store->load('{"id":"yaml"}');
my $normalized_id = $store->load(\$text);
my $normalized_id = $store->load('/path/to/foo.json');
my $normalized_id = $store->load('file:///path/to/foo.json');
my $normalized_id = $store->load('/load/from/ua-server-app');

Can load a $schema from many different sources. The input can be a string or a string-like object, and the "load" method will try to resolve it in the order listed in above.

Loading schemas from $text will generate an $normalized_id in "schemas" looking like "urn:text:$text_checksum". This might change in the future!

Loading files from disk will result in a $normalized_id that always start with "file://".

Loading can also be done with relative path, which will then load from:

$store->ua->server->app;

This method is EXPERIMENTAL, but unlikely to change significantly.

resolve

$hash_ref = $store->resolve($url, \%defaults);

Takes a $url (can also be a file, urn, ...) with or without a fragment and returns this structure about the schema:

{
  base_url => $str,  # the part before the fragment in the $url
  fragment => $str,  # fragment part of the $url
  id       => $str,  # store ID
  root     => ...,   # the root schema
  schema   => ...,   # the schema inside "root" if fragment is present
}

This method is EXPERIMENTAL and can change without warning.

SEE ALSO

JSON::Validator.