jhthorsen / json-validator

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

Could validate() return a list of errors when failing "allOf"? #116

Closed biafra closed 5 years ago

biafra commented 6 years ago

Usually I get a list of JSON::Validator::Error with each path and message.

Shouldn't it be the same with "allOf" ?

In the following example I get the two errors on the same message and no reference to which property name.

#!/usr/bin/env perl

use JSON::Validator;
use JSON;
use Data::Printer;

my $validator = JSON::Validator->new;

my $openapi = '{
    "Service" : {
        "type": "object",
        "properties": {
            "prop1": { "type": "string" },
            "prop2": { "type": "string" }
        }
    },
    "allOfThem" : {
        "allOf": [
            { "$ref": "#/Service" },
            {
                "properties": {
                    "prop3": { "type": "string" },
                    "prop4": { "type": "string" }
                },
                "required": [ "prop3" ]
            }
        ]
    }
}';

$validator = $validator->schema($openapi);

my $sub_schema = $validator->get('/allOfThem');

my $data_json = <<EOT
{
  "prop1": "foo",
  "prop4": 666
}
EOT
;

my $data = from_json($data_json);

my @errors = $validator->validate( $data, $sub_schema);

p @errors;
[
    [0] JSON::Validator::Error  {
        Parents       Mojo::Base
        public methods (5) : has, message, new, path, TO_JSON
        private methods (1) : __ANON__
        internals: {
            message   "allOf failed: (Missing property.. Expected string - got number.)",
            path      "/"
        }
    }
]

This happens with 2.08

Thank you!

jhthorsen commented 6 years ago

So "allOf", "anyOf" and "oneOf" has incredible bad error messages. I would love to make them better, but I simply don't know how to.

Any suggestions in the form of a PR is more than welcomed. I don't consider changing the returned errors as a breaking change, so it's fine if the return value is changed.

Another idea might be to add children or something to JSON::Validator::Error which gives more information for the sub errors.

jhthorsen commented 6 years ago

Also, if you don't make an PR, then just giving an idea about what you expect the output to be might help me in the direction of how to fix it. Just make sure the result also take into consideration if the "anyOf/allOf/oneOf" has children of "anyOf/allOf/oneOf".

biafra commented 6 years ago

Thanks for the answer Jan. I’m new to json schema and didn’t check that the ‘...Of’ could have children. I’m going to see what are my needs and if it’s an acceptable generic solution. Maybe verifying what XML/XSD validators are returning on errors :-)

Paulo

On 17 Jul 2018, at 10:45, Jan Henning Thorsen notifications@github.com wrote:

Also, if you don't make an PR, then just giving an idea about what you expect the output to be might help me in the direction of how to fix it. Just make sure the result also take into consideration if the "anyOf/allOf/oneOf" has children of "anyOf/allOf/oneOf".

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jhthorsen commented 5 years ago

Closing this in favour of #121.