Throws a type error, saying it recieved '' instead of a number. How do you make a number field non-required?

Tried: setting strict to false, setting nullable to true
From the readme: "Failed casts return NaN.", apparently not...
Ok... Seem to arrived at the desired behavior:
.test('is-int', 'Please provide a valid Facebook Id', (value) => {
return Number.isInteger(+value)
})
not 'dead simple', but it works.
looks like your form is returning an empty string, which in non strict mode is cast to NaN. you can do what you have their and write your own less strict test, or make sure you only try and validate a number, something that can be directly parsed as a number or null if it's nullable
I don't think my form returns an empty string... If I add a .transform() to my schema object and log it, I can see the value is null, but it's getting cast to an empty string somewhere later in the validation.
When I submit the form I see the null value as well... So I'm not convinced this string casting is happening on the form side and not the yup side.
it's almost definitely not happening on the yup side, if you are using the number type. number casting is really simple and could only produce a number or NaN, you can take a peek at the code if you are inclined.
generally my thought is that the form is the issue because native inputs return empty strings not null for empty values.
I'm happy to take a look tho if you want to post your schema
I've the same problem, and I use react-formal for inputs fields.
@jquense Maybe we should discuss this issue in react-formal context ?
I use number type for input, but of course it return empty string not null.
I think it's an interaction problem between react-formal and yup.
http://jquense.github.io/react-formal/#/getting-started?_k=vcpbfo
First form with Date of Birth. This isn't required field. But when we input something and than clear field, we'll get an error about Invalid date and can't submit the form.
number().transform((cv, ov) => {
return ov === '' ? undefined : cv;
})
This transform will help for forms, which returns empty string instead of undefined
I found that I had to check for NaN rather than empty string, whether with ov or cv:
minimumTenure: yup.number().transform( cv => isNaN(cv) ? undefined : cv).positive().integer()
As @TheCrow1213 said, doesn't seem 'dead simple' to require a transform for the common case of an empty non-required field, but it works.
Most helpful comment
This transform will help for forms, which returns empty string instead of
undefined