The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Validator::Util - Utility functions for JSON::Validator

DESCRIPTION

JSON::Validator::Util is a package containing utility functions for JSON::Validator. Each of the "FUNCTIONS" can be imported.

FUNCTIONS

data_checksum

  $str = data_checksum $any;

Will create a checksum for any data structure stored in $any.

data_section

  $str = data_section "Some::Module", "file.json";
  $str = data_section "Some::Module", "file.json", {encode => 'UTF-8'};

Same as "data_section" in Mojo::Loader, but will also look up the file in any inherited class.

data_type

  $str = data_type $any;
  $str = data_type $any, [@schemas];
  $str = data_type $any, [{type => "integer", ...}];

Returns the JSON type for $any. $str can be array, boolean, integer, null, number object or string. Note that a list of schemas need to be provided to differentiate between "integer" and "number".

is_type

  $bool = is_type $any, $class;
  $bool = is_type $any, $type; # $type = "ARRAY", "BOOL", "HASH", "NUM" ...

Checks if $any is a, or inherits from, $class or $type. Two special types can be checked:

  • BOOL

    Checks if $any is a boolean value. $any is considered boolean if it is an object inheriting from JSON::PP::Boolean or is another object that stringifies to "1" or "0".

  • NUM

    Checks if $any is indeed a number.

json_pointer

  $str = json_pointer $path, $append;

Will concat $append on to $path, but will also escape the two special characters "~" and "/" in $append.

prefix_errors

  @errors = prefix_errors $prefix, @errors;

Consider this internal for now.

schema_extract

  $data       = schema_extract $any, $json_pointer;
  $data       = schema_extract $any, "/x/cool_beans/y";
  $collection = schema_extract $any, ["x", undef, "y"];
  schema_extract $any, $json_pointer, sub { my ($data, $json_pointer) = @_ };

The basic usage is to extract data from $any, using a $json_pointer - RFC 6901. It can however be used in a more complex way by passing in an array-ref, instead of a plain string. The array-ref can contain undef() values, will result in extracting any element on that point, regardsless of value. In that case a Mojo::Collection will be returned.

A callback can also be given. This callback will be called each time the $json_pointer matches some data, and will pass in the $json_pointer at that place.

In addition, if the $json_pointer points to a JSON::Validator::Ref at any point, the "$ref" will be followed, while if you used Mojo::JSON::Pointer, it would return either the JSON::Validator::Ref or undef().

Even though "schema_extract" has special capabilities for handling a JSON-Schema, it can be used for any data-structure, just like Mojo::JSON::Pointer.

schema_type

  $str = schema_type $hash_ref;
  $str = schema_type $hash_ref, $any;

Looks at $hash_ref and tries to figure out what kind of type the schema represents. $str can be "array", "const", "number", "object", "string", or fallback to empty string if the correct type could not be figured out.

$any can be provided to double check the type, so if $hash_ref describes an "object", but $any is an array-ref, then $str will become an empty string. Example:

  # $str = "";
  $str = schema {additionalProperties => false}, [];

  # $str = "object"
  $str = schema {additionalProperties => false};
  $str = schema {additionalProperties => false}, {};

Note that this process is relatively slow, so it will make your validation faster if you specify "type". Both of the two below is valid, but the one with "type" will be faster.

  {"type": "object", "properties": {}} # Faster
  {"properties": {}}                   # Slower

uniq

  @items = uniq @items;

See "uniq" in List::Util.

SEE ALSO

JSON::Validator.