jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

can't validate an empty schema with `coerce=>'defaults'` #158

Closed karenetheridge closed 4 years ago

karenetheridge commented 5 years ago

This script:

use strict;
use warnings;
use JSON::Validator;
my $schema = {
    '$schema' => 'http://json-schema.org/draft-07/schema#',
};

my $validator = JSON::Validator->new(coerce => 'defaults');
$validator->load_and_validate_schema($schema, { schema => 'http://json-schema.org/draft-07/schema#' });

gives:

Not a HASH reference at /Users/ether/.perlbrew/libs/26.3@std/lib/perl5/JSON/Validator.pm line 929.

Inspecting the stack trace, it looks like it cannot parse the part of the schema spec that says default=>true for the object definition? $r is supposed to be a hashref on line 929 but instead it is just a JSON::PP::true.

This is with JSON::Validator 3.11 on perl 5.26.3, darwin (OSX).

jhthorsen commented 5 years ago

This is indeed a bug. And unfortunately, I don't know how to fix it. Last time I encountered this case, I added a quick fix/hack:

https://github.com/mojolicious/json-validator/blob/8c822f2fca67e5208def6b081a943ab6308d5601/lib/JSON/Validator.pm#L571

I'm not proud of that line, but that's unfortunately how it is now... I think if anyone provides a fix for this issue, then L571 and L572 can be removed as well.

jhthorsen commented 4 years ago

I'm closing this issue, since there does not seem to better fix available.

karenetheridge commented 4 years ago

Is there harm in leaving it open? I've been trying to find time to work on it, as I'll need this feature for an upcoming project. :(

jhthorsen commented 4 years ago

I'll reopen the issue if you let me know what's wrong with the fix.

karenetheridge commented 4 years ago

I don't understand -- what fix are you referring to?

karenetheridge commented 4 years ago

please reopen this -- it is still a problem.