I'm trying to implement a quite basic validation for a form field/select. Validation schema:
vehicleProvider: Yup.object() // This is an object which is null by default
.required('formvalidation.required.message')
.nullable(),
reserveVehicle: Yup.number().when('vehicleProvider', { // This is a number which is null by default
is: provider => provider?.hasReserve,
then: Yup.number()
.required('formvalidation.required.message')
.nullable(),
otherwise: Yup.number().notRequired()
}),
What I want to do: Only require/validate reserveVehicle
if provider.hasReserve
is true
. Otherwise, don't require the number.
I get this error:
"reserveVehicle must be a
number
type, but the final value was:NaN
(cast from the valueNaN
)."
This makes sense (kind of) because, well null
is Not a number. But as I'm trying to tell it that it shouldn't be required, in my opinion it shouldn't try to evaluate it.
Did I miss any key concepts of Yup
?
otherwise
might be the one causing you problems, try adding.nullable()
to that one. – Hagiprice: Yup.number().required().typeError('add your message here')
it works for me when cast from value NaN is comming. – Longtin