opis / json-schema

JSON Schema validator for PHP
https://opis.io/json-schema
Apache License 2.0
568 stars 58 forks source link

Casting nonsense string to number with $pragma.cast gives valid result. #108

Open adjenks opened 2 years ago

adjenks commented 2 years ago

Based on your documentation it could just be doing exactly as described:

From To Result
string integer try to parse the integer from string, 0 if invalid number representation

However I don't see how it's useful to cast everything to 0 when casting fails.

I'd rather it something like "{type1} required, could not convert given {type2} value to {type1}."

What's the logic behind casting everything to 0 on failure?

I'm replacing the pragma cast to this for now:

{
        "field_name": {
            "title": "Field Name",
            "oneOf": [
                {
                    "type": "integer"
                },
                {
                    "type": "string",
                    "pattern": "^\\d+$"
                }
            ]
        },
}

If there is another way to do this, I'd be happy to know.

What I really want is for anything that's supposed to be a number but is a string, it should have an attempted cast, but if the cast fails, then fail to validate and provide a message as mentioned before.

Thank you for listening. Keep up the good work.