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

  1. DESCRIPTION
  2. FUNCTIONS
    1. data_checksum
    2. data_section
    3. data_type
    4. is_bool
    5. is_num
    6. is_type
    7. negotiate_content_type
    8. prefix_errors
    9. schema_type
    10. str2data
  3. SEE ALSO

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_bool

$bool = is_bool $any;

Checks if $any looks like a boolean.

is_num

$bool = is_num $any;

Checks if $any looks like a number.

is_type

$bool = is_type $any, $class;
$bool = is_type $any, $type;

Checks if $any is a, or inherits from, $class or $type.

negotiate_content_type

$content_type = negotiate_content_type($header, \@content_types);

This method can take a "Content-Type" or "Accept" header and find the closest matching content type in @content_types. @content_types can contain wildcards, meaning "*/*" will match anything.

prefix_errors

@errors = prefix_errors $prefix, @errors;

Consider this internal for now.

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

str2data

$any = str2data $str;

Will try to parse $str as JSON or YAML, and return a data structure.

SEE ALSO

JSON::Validator.