NAME

JSON::Schema::Modern::Utilities - Internal utilities for JSON::Schema::Modern

VERSION

version 0.630

SYNOPSIS

use JSON::Schema::Modern::Utilities qw(func1 func2..);

DESCRIPTION

This class contains internal utilities to be used by JSON::Schema::Modern, and other useful helpers.

FUNCTIONS

is_type

if (is_type('string', $value)) { ... }

Returns a boolean indicating whether the provided value is of the specified core type (null, boolean, string, number, object, array) or integer. Also optionally takes a hashref { legacy_ints = 1 }> indicating that draft4 number semantics should apply (where unlike later drafts, 2.0 is not an integer).

get_type

my $type = get_type($value);

Returns one of the core types (null, boolean, string, number, object, array) or integer. Also optionally takes a hashref { legacy_ints = 1 }> indicating that draft4 number semantics should apply. Behaviour is consistent with "is_type".

is_bool

if (is_bool($value)) { ... }

Equivalent to is_type('boolean', $value). Accepts JSON booleans and builtin booleans, but not dualvars (because JSON encoders do not recognize these as booleans).

is_schema

if (is_schema($value)) { ... }

Equivalent to is_type('object') || is_type('boolean').

is_equal

if (not is_equal($x, $y, my $state = {})) {
  say "values differ starting at $state->{path}: $state->{error}";
}

Compares two arbitrary data payloads for equality, as per Instance Equality in the JSON Schema draft2020-12 specification.

The optional third argument hashref supports the following fields:

  • scalarref_booleans (provided by caller input): as in "scalarref_booleans" in JSON::Schema::Modern

  • stringy_numbers (provided by caller input): when set, strings will also be compared numerically, as in "stringy_numbers" in JSON::Schema::Modern

  • path (populated by function): if result is false, the json pointer location of the first difference

  • error (populated by function): if result is false, an error description of the first difference

is_elements_unique

if (not is_elements_unique($arrayref, my $state = {}) {
  say "lists differ starting at $state->{path}: $state->{error}";
}

Compares all elements of an arrayref for uniqueness.

The optional second argument hashref supports the same options as "is_equal", plus:

  • equal_indices (populated by function): if result is false, the list of indices of the (first set of) equal items found.

jsonp

# '/paths/~1foo~1{foo_id}/get/responses'
my $jsonp = jsonp(qw(/paths /foo/{foo_id} get responses));

Constructs a json pointer string from a list of path components, with correct escaping; the first argument must be '' or an already-escaped json pointer, to which the rest of the path components are appended.

unjsonp

# ('', 'paths', '/foo/{foo_id}', 'get', 'responses')
my @components = unjsonp('/paths/~1foo~1{foo_id}/get/responses');

Splits a json pointer string into its path components, with correct unescaping.

jsonp_set

my $data = { a => 1, b => { c => 3, d => 4 } };
my $defaults = {
  '/b/d' => 5,
  '/b/e' => 6,
  '/f' => 7,
  '/g/h/i/1' => [ 10 ],
};
jsonp_set($data, $_, $defaults->{$_}) foreach keys %$defaults;

# data is now:
# { a => 1, b => { c => 3, d => 5, e => 6 }, f => 7, g => { h => { i => [ undef, [ 10 ] ] } } }

Given an arbitrary data structure, a json pointer string, and an arbitrary value, assigns that value to the given position in the data structure. This is a destructive operation, overwriting whatever data was there before if needed (even if an incompatible type: e.g. a hash key will overwrite an existing arrayref). Intermediary keys or indexes will spring into existence as needed.

json_pointer_type

A Type::Tiny type representing a json pointer string.

canonical_uri_type

A Type::Tiny type representing a canonical URI: a Mojo::URL with either no fragment, or with a json pointer fragment.

load_cached_document

my $evaluator = JSON::Schema::Modern->new;
my $uri = 'https://json-schema.org/draft-07/schema#';
my $document = load_cached_document($evaluator, $uri);

my $result = $evaluator->evaluate($data, $uri);

Loads a document object from global cache, loading data from disk if needed. This should only be used for officially-published schemas and metaschemas that are bundled with this distribution or another related one.

GIVING THANKS

If you found this module to be useful, please show your appreciation by adding a +1 in MetaCPAN and a star in GitHub.

SUPPORT

Bugs may be submitted through https://github.com/karenetheridge/JSON-Schema-Modern/issues.

I am also usually active on irc, as 'ether' at irc.perl.org and 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.

AUTHOR

Karen Etheridge <ether@cpan.org>

COPYRIGHT AND LICENCE

This software is copyright (c) 2020 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.

Some schema files have their own licence, in share/LICENSE.