jhthorsen / json-validator

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

Validator reports incomplete message when validating null in a set of types. #217

Closed traveltekmichael closed 4 years ago

traveltekmichael commented 4 years ago

Steps to reproduce the behavior

When a schema allows a set of types, and 'null' is one of the possible types, if an data type is provided that is not in the set of types permitted by the schema, the error message returned is simply 'Not Null.' whereas in previous versions this would report which types are allowed.

See the script below for a simple example.

#!/usr/bin/perl

use strict;
use warnings;

use JSON::Validator();
my $jsv = JSON::Validator->new();

$jsv->schema({
    type => 'object',
    properties => {
        price => {
            type => [
                'number',
                'null'
            ]
        }
    }
});

my @errors = $jsv->validate({ price => '10.11' });

foreach my $error (@errors) {
    print $error->message;
    print "\n";
}

Expected behavior

Should get message: 'Expected number/null - got string.'

Actual behavior

Get message: 'Not null.'

jhthorsen commented 4 years ago

This was a special case for the "null" type: It was not reported as a "type" error, but rather a "null" error.

Thank you for the report! This is now fixed in master.