json-schema icon indicating copy to clipboard operation
json-schema copied to clipboard

bug: type order in 'allOf' has an impact

Open kroc702 opened this issue 1 year ago • 3 comments

Hello, When validate with multiple schema using 'allOf', definition order has a behavior impact. cf attached code to test testSchemaValidator2.zip

Correct:

input value: "true"
schema: {
    "allOf": [
        {
            "type": "string"
        },
        {
            "type": "boolean"
        }
    ]
}
value is valid

reverse order lead to incorrect validation:

input value: "true"
schema: {
    "allOf": [
        {
            "type": "boolean"
        },
        {
            "type": "string"
        }
    ]
}
Boolean value found, but a string is required
Failed to match all schemas

kroc702 avatar Feb 13 '24 13:02 kroc702

I forget to mention version:

            "name": "justinrainbow/json-schema",
            "version": "v5.2.13",
            "source": {
                "type": "git",
                "url": "https://github.com/justinrainbow/json-schema.git",
                "reference": "fbbe7e5d79f618997bc3332a6f49246036c45793"
            },

kroc702 avatar Feb 13 '24 14:02 kroc702

@kroc702 thanks for the report. Based on your schema provided it seems no input data will be valid ever as the a single value cannot be boolean and string at the same time. Using https://jsonschemalint.com/ you can see that both versions of you schema should raise errors whereas you mention that in both cases you expect them to be valid.

DannyvdSluijs avatar Feb 14 '24 11:02 DannyvdSluijs

Well it's a choice, your choice ;) As php is loosely typed, it doesn't hurt me to validate the strings "true" or "1" as boolean or number. Changing this behavior may be a braking change for the lib.

We just need a consistent behavior with all types and order shouldn't impact behavior of any/all/oneOf.

kroc702 avatar Feb 14 '24 13:02 kroc702