jhthorsen / json-validator

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

Incorrect behaviour when extra object properties present #240

Closed DrHyde closed 3 years ago

DrHyde commented 3 years ago

Steps to reproduce the behavior

Given a schema that specifies this object:

    ...
    "location":   {
        "type": "object",
        "properties": {
            "postcode": { "type": "string" },
            "region": { "type": "string" },
            "country": { "type": "string" }
        }
    },
    ...

ie, which validates that location is an object and that the postcode, region, and country fields are optional strings, try to validate this JSON:

    ...
    "location": {
        "postcode": "TN39 4AA",
        "region": "East Sussex",
        "country": "GB",
        "door_colour": "orange"
    },
    ...

Expected behavior

No error, because I've not said "additionalProperties": false, and the default is supposed to be true.

Actual behavior

Perl spits an error: Can't call method "req" on an undefined value ...

NB that if I explicitly set "additionalProperties": true then we get the same incorrect behaviour. If I set it to false then I get a sensible error.

jhthorsen commented 3 years ago

I don’t see how “Can't call method "req" on an undefined value” is related to this. Also the schema does not reflect the error description.

Please provide a failing test. You can use https://github.com/mojolicious/json-validator/blob/7d89c7e8ad54b5d2eb41530aaa93bfd00a7b20d0/t/jv-object.t#L29 as a template.

DrHyde commented 3 years ago

Hmmm, I may have cast blame incorrectly as I can't see how that error can possibly be generated. There's definitely something funky going on with additionalProperties, but I'll close the ticket for now and open another if I can boil it down to a small example.