jhthorsen / json-validator

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

Fix oneOf validation to work properly with blessed booleans #78

Closed khvzak closed 6 years ago

khvzak commented 6 years ago

Probably should fix issue #59

Small example how to reproduce (also added to tests):

#!/usr/bin/env perl
use Mojo::Base -strict;

use JSON::Validator;
use Mojo::JSON;
use Data::Dumper;

my $validator = JSON::Validator->new;
$validator->schema("data://main/schema.yml");

say Dumper([
  $validator->validate({
    prop1 => Mojo::JSON->false,
    prop2 => Mojo::JSON->false,
  })
]);

__DATA__

@@ schema.yml
type: object
properties:
  prop1:
    $ref: "data://main/defs.yml#/definitions/item"
  prop2:
    $ref: "data://main/defs.yml#/definitions/item"

@@ defs.yml
definitions:
  item:
    oneOf:
    - type: object
    - type: boolean

It generates the following message:

          bless( {
                   'path' => '/prop1',
                   'message' => 'All of the oneOf rules match.'
                 }, 'JSON::Validator::Error' )

On unfixed version.

khvzak commented 6 years ago

Actually it's not related only to blessed booleans, if change the example above to:

my $something = {};

say Dumper([
  $validator->validate({
    prop1 => $something,
    prop2 => $something,
  })
]);

The problem will be still reproducible, but I think it's rare case. It's a general issue with _seen function.

jhthorsen commented 6 years ago

Awesome! Thank you 👍