Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int64 validation is incorrect #90

Open
klntsky opened this issue Feb 22, 2024 · 2 comments
Open

int64 validation is incorrect #90

klntsky opened this issue Feb 22, 2024 · 2 comments

Comments

@klntsky
Copy link

klntsky commented Feb 22, 2024

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

// 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
Copy link
Author

klntsky commented Feb 22, 2024

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

@leeleahulkcs
Copy link

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger

  • The Number.isSafeInteger() static method determines whether the provided value is a number that is a safe integer.

You could override the format, or create a new format (i.e. int64safe), and set the validation function to use Number.isSafeInteger().

Using Node.js:

> Number.isInteger(Number.MAX_SAFE_INTEGER)
true
> Number.isSafeInteger(Number.MAX_SAFE_INTEGER)
true
> Number.isInteger(Number.MAX_SAFE_INTEGER+1)
true
> Number.isSafeInteger(Number.MAX_SAFE_INTEGER+1)
false
> Number.isInteger(Number.MIN_SAFE_INTEGER)
true
> Number.isSafeInteger(Number.MIN_SAFE_INTEGER)
true
> Number.isInteger(Number.MIN_SAFE_INTEGER-1)
true
> Number.isSafeInteger(Number.MIN_SAFE_INTEGER-1)
false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants