NAME
Data::Sah::Filter::perl::JSON::check_decode_hash - Check that value can be decoded as JSON and it is a hash
VERSION
This document describes version 0.007 of Data::Sah::Filter::perl::JSON::check_decode_hash (from Perl distribution Sah-Schemas-JSON), released on 2022-11-15.
SYNOPSIS
Using in Sah schema's prefilters
(or postfilters
) clause
[
"str"
,
"prefilters"
,[[
"JSON::check_decode_hash"
]]]
Using with Data::Sah:
my
$schema
= [
"str"
,
"prefilters"
,[[
"JSON::check_decode_hash"
]]];
my
$validator
= gen_validator(
$schema
);
if
(
$validator
->(
$some_data
)) {
'Valid!'
}
Using with Data::Sah:Filter directly:
my
$filter
= gen_filter([[
"JSON::check_decode_hash"
]]);
# $errmsg will be empty/undef when filtering succeeds
my
(
$errmsg
,
$filtered_value
) =
$filter
->(
$some_data
);
Sample data and filtering results
""
# INVALID (String is not a valid JSON: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "(end of string)") at (eval 2357) line 8. ), unchanged (Empty string is not valid JSON)
"null"
# INVALID (String is a valid JSON but not an encoded hash), unchanged (Valid JSON but not a hash)
"[1,2,3]"
# INVALID (String is a valid JSON but not an encoded hash), unchanged (Valid JSON but not a hash)
"{\"a\":1}"
# valid, unchanged
"\"foo\""
# INVALID (String is a valid JSON but not an encoded hash), unchanged (Valid JSON but not a hash)
"\"foo"
# INVALID (String is not a valid JSON: unexpected end of string while parsing JSON string, at character offset 4 (before "(end of string)") at (eval 2362) line 8. ), unchanged (Invalid JSON, missing closing quote)
{}
# INVALID (String is not a valid JSON: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "HASH(0x559dffd6ec58)") at (eval 2363) line 8. ), unchanged (Will become something like "HASH(0x560142d4e5e8)" and fail because it is not valid JSON)
DESCRIPTION
This is like the JSON::check_decode filter with the additional check that the decoded value is a hash.
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Sah-Schemas-JSON.
SOURCE
Source repository is at https://github.com/perlancar/perl-Sah-Schemas-JSON.
AUTHOR
perlancar <perlancar@cpan.org>
CONTRIBUTING
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
COPYRIGHT AND LICENSE
This software is copyright (c) 2022, 2021 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-JSON
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.