ajv-formats icon indicating copy to clipboard operation
ajv-formats copied to clipboard

int64 validation is incorrect

Open klntsky opened this issue 1 year ago • 2 comments

https://github.com/ajv-validator/ajv-formats/blob/4dd65447575b35d0187c6b125383366969e6267e/src/formats.ts#L252

// JSON and javascript max Int is 2**53, so any int that passes isInteger is valid for Int64

The second part of the sentence does not follow from the first. Neither JS nor JSON do not throw on overflows.

Consider this:

> Number.isInteger(JSON.parse('' + (2 ** 65)))
true

klntsky avatar Feb 22 '24 14:02 klntsky

I'm not familiar with the overflow behavior in JS, but letting it overflow on platforms that don't support long integers and perform the exact comparison on platforms that do would seemingly be better.

This is not a proposed fix, just an illustration of the idea:

> (JSON.parse('' + (2 ** 63))) < 2 ** 64
true
> (JSON.parse('' + (2 ** 64))) < 2 ** 64
false

klntsky avatar Feb 22 '24 14:02 klntsky