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

Joi.number allows Infinity #494

Closed
dbrockman opened this issue Nov 24, 2014 · 7 comments
Closed

Joi.number allows Infinity #494

dbrockman opened this issue Nov 24, 2014 · 7 comments
Assignees
Labels
breaking changes Change that can breaking existing code feature New functionality or improvement
Milestone

Comments

@dbrockman
Copy link

Feature request for a breaking change. Joi version 4.9.0

I'm not sure if this is the desired behaviour or not. It seems to me like Infinity should be rejected just like NaN.

This seems a bit awkward in order to validate that the value is a finite number:

joi.number().invalid(Infinity, -Infinity)
@kanongil
Copy link
Contributor

This seems like a non-issue. Infinity is a perfectly valid number, where typeof Infinity === 'number'. As such I would expect Infinity to validate.

@Marsup Marsup added the request label Nov 24, 2014
@Marsup
Copy link
Collaborator

Marsup commented Nov 24, 2014

All mathematical operations I tried on Infinity generate perfectly valid results, so I'd tend to agree with @kanongil. What bothers you ? That it's not serializable ?

@Marsup Marsup self-assigned this Nov 24, 2014
@dbrockman
Copy link
Author

I almost didn't post the issue in the first place because I agree that Infinity is a valid number but it's far more likely the result of a bug than a valid value.

0 / 0  // NaN
1 / 0  // Infinity
-1 / 0 // -Infinity

When I validate numbers I use isFinite() instead of ! isNaN() to guard against infinity.

But maybe this is outside of the scope of Joi.

@Marsup Marsup added this to the 5.0.0 milestone Nov 25, 2014
@Marsup
Copy link
Collaborator

Marsup commented Nov 25, 2014

I'll probably make that change, serialization poses a serious problem since it produces unexpected results.
Let's say you're validating Joi.object({ val: Joi.number().required() }) before stringifying.
For { val: Infinity } you'll get { val: null } which is not what I would expect.

@Marsup Marsup added the breaking changes Change that can breaking existing code label Nov 25, 2014
@kanongil
Copy link
Contributor

Looking at it again, Joi explicitly fails on string conversions for "Infinity". This leads me to think that when Infinity validates it is a straight up bug. In any case it's a weird inconsistency.

@Marsup
Copy link
Collaborator

Marsup commented Nov 25, 2014

Like I said Infinity is not serializable, that can't be worked around without a special case, but anyway I'd rather not support an edge case that breaks people's expectations.

@Marsup Marsup closed this as completed in a9d0d53 Nov 25, 2014
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants