jsonschema
jsonschema copied to clipboard
Instance validates if `multipleOf` is an integer, but fails if it's a float
from jsonschema import validate
instance = 9007199254740995
validate(instance, schema={"type": "integer", "multipleOf": 11}) # passes
validate(instance, schema={"type": "integer", "multipleOf": 11.0}) # fails
I'm pretty sure that this is a bug, on the basis that the spec says all integer-valued numbers should be treated as integers.
Found via https://github.com/python-jsonschema/hypothesis-jsonschema/pull/103.
Nice :)
Needs thinking though / reading the spec language. It's true the spec says to treat floats with no fractional part as integers, but it also says (or really leaves the JSON spec to say) that floats are an acceptable implementation of number -- and I'm not sure where if anywhere covers what the requirements are on coercion to a common type.
Specifically here, 9007199254740995 is not representable as a float, the closest float is 9007199254740996.0 which is indeed not divisible by 11, so doing the division somehow will require coercing to float.
But it's possible you're both correct on whether this is covered by the spec and also possible this is fixable, not sure, needs thinking.
Both passed on older versions of jsonschema, so for whatever it's worth this probably changed in version 4.18. I've just skipped the affected test though, and happy to go with your opinion 👍
Ah that indeed is interesting! Will have to remind myself.
On Sun, Sep 10, 2023, 19:27 Zac Hatfield-Dodds @.***> wrote:
Both passed on older versions of jsonschema, so for whatever it's worth this probably changed in version 4.18. I've just skipped the affected test though, and happy to go with your opinion 👍
— Reply to this email directly, view it on GitHub https://github.com/python-jsonschema/jsonschema/issues/1159#issuecomment-1712970145, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACQQXWKRVOUYR6GRW3MERDXZZEHZANCNFSM6AAAAAA4SOQ2YY . You are receiving this because you commented.Message ID: @.***>