Yup.when: "`NaN` (cast from the value `NaN`)"
Asked Answered
E

6

52

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 value NaN)."

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?

Esquiline answered 8/11, 2019 at 16:41 Comment(3)
I think your otherwise might be the one causing you problems, try adding .nullable() to that one.Hagi
.typeError('The field should be a number') did the job for me.Ebberta
price: Yup.number().required().typeError('add your message here') it works for me when cast from value NaN is comming.Longtin
C
119

You should use typeError

Here is an example from my code:

amount: Yup.number()
                    .typeError('Amount must be a number')
                    .required("Please provide plan cost.")
                    .min(0, "Too little")
                    .max(5000, 'Very costly!')
Coprolalia answered 6/1, 2020 at 17:37 Comment(2)
I now just set typeError(''), so no unwanted error is shown in my appDeadly
If the input comes from a text box, with "" for the empty value, the required error never triggers. Solution here: https://mcmap.net/q/353733/-value-type-error-message-displaying-instead-of-required-error-message-on-empty-input-submissionWedded
B
24

No need to use "typeError"

yup.number()
    .transform((value) => Number.isNaN(value) ? null : value )
    .nullable()
    .required('Required'),
Ballon answered 29/3, 2023 at 3:2 Comment(0)
W
11

Use this piece of code .transform((value) => (isNaN(value) ? 0 : value))

age: yup
.number()
.transform((value) => (isNaN(value) || value === null || value === undefined) ? 0 : value)
.required()
.label("Age"),
Whitening answered 12/10, 2022 at 18:27 Comment(2)
This solution can still display a message at the same time compared to just returning an empty typeError(''). You can return null of there is no value instead of 0, make it nullable, and you can display a required error message.Magdaleno
isNan check for "" empty string just change the condition for null and undefined .transform((value) => ((isNaN(value) || value === null || value === undifined)? 0 : value))Whitening
E
3

You can use:

amount: yup.number().typeError("")

This will omit the validation message, but you should be careful when doing this.

Endmost answered 11/9, 2022 at 8:43 Comment(0)
E
0

add typeError in when

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()
        .typeError('Amount must be a number')
        .required('formvalidation.required.message')
        .nullable(),
    otherwise: Yup
        .number()
        .typeError('Amount must be a number')
        .notRequired()
}),
Enemy answered 9/11, 2022 at 3:34 Comment(0)
H
-3

Since you want the field to still validate when the value is NaN and it's not a required field, changing your otherwise to this should fix it:

otherwise: Yup.number().notRequired().nullable()

Adding the .nullable() at the end is the key.

Hast answered 12/4, 2021 at 18:24 Comment(1)
The problem is with the typecasting, the solution here fix it github.com/jquense/yup/issues/500#issuecomment-818582829Appleby

© 2022 - 2024 — McMap. All rights reserved.