-
Notifications
You must be signed in to change notification settings - Fork 934
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
Allowing yup.number() to accept NaN #1330
Comments
for me i use this solution and it's work yup.number().transform((value) => (isNaN(value) ? undefined : value)).nullable() |
@X-SLAYER thats more a hack for me. Its a basic feature and needs to be pushed |
@jquense why was this closed? What is the resolution/solution for this problem? (i.e. it's still a problem for me.) |
Also finding issues with this at 1.0.2 when at 0.32 I wasn't. I'm trying to migrate and the following rule:
was working fine and printed the expected message when the input is null, but now I end up seeing the |
Same issue. I have use UI NumberInput component that supports "" (undefined) value as default value, and if input is required I get this error |
Thanks this worked. I tried setting the default values which didnt worked . It felt like the required() was't being triggered. |
Just to add a note of closure here. Yup intentionally doesn't allow NaN in same way that it doesn't allow |
@jquense the thing is a field with "" is converted to NaN. If this field isn't required, then it fails with NaN, even tho it shouldnt |
An empty string isn't a number which is why it (correctly) fails. Any failed coercion to a number results in NaN not just empty strings. Allowing NaN isn't the right way to deal with that case, transform the value to undefined or null if you want to treat empty strings as empty number values. There are past discussions you can look up for more solutions. |
But then the Required flag is totally useless. There should be a elegant way to allow empty fields. Transform is bad imo |
Also undefined isnt a number, nan is technically? Why not like the OP proposed like nanable |
I can accept the definition of NaN stablished in the library, but that still is a technical detail. The main point I see is that the library client wants to achieve two things:
In case NaN values deserve a separate treatment, the |
@LuisGilGB you can handle these cases with
What aspect do you see as inconsistent, the other yup schema act similarly, treating parsing failures as invalid types, e.g failing to parse a date produces a similar InvalidDate instance of Date, that is in fact a valid Date. @TheUnlimited64 solving for "I have an empty form field that is a number type" is valid thing but doing it via allowing NaN is not the correct solution, it is too broad of a fix that introduces a bunch of other inconsistencies and isn't going to be added. I'd recommend looking for the issue specifically dealing with empty strings and numbers and adding to the conversation there, or adding a custom method to your number schema to do what you want. Yup isn't trying to solve every case out of the box, but is intentionally flexible and extensible to allow users to change it to suit their needs. |
I mean that making
|
@LuisGilGB if your original issue is that |
Sorry for replying this late. I got the chance to remove the |
The Problem
I can't see a way for a
yup.number()
schema to accept aNaN
value as valid.Should NaN be allowed as a number?
When working with common math libraries (e.g. numpy) it is common for
NaN
to still be considered a number, it allows further calculations to be made on it without having to check if numbers are there:Typescript feels the same:
In particular I am working on a front-end that performs calculations on numbers that are outputted from numpy. I believe it would be clean to just pass the NaNs through the calculations and would like a way to allow them into my application without needing to write a custom yup test.
Proposed Solution
I propose adding
.nanable()
equivalent to.nullable()
for numbers:If you agree on this plan I could try a PR for it.
The text was updated successfully, but these errors were encountered: