How can I make a yup number accept nullable values?

You have to pass true to nullable –

nrOfApples: yup.number().min(0).max(999).nullable(true);


Working example:

Note that if you add required().nullable(true) the required overrides the nullable and null will not validate.


You can use a transform to convert the NaN value into null. I updated the runkit with this:

const contactSchema = yup.object({
  name: yup.string()
  nrOfApples: yup
    // checking self-equality works for NaN, transforming it to null
    .transform((_, val) => val === Number(val) ? val : null) 

Leave a Comment