.notRequired on yup string not working if string is empty and still throws an error

Viewed 6098

I am trying to validate a string that if empty should not be validated but if there is a character then validation should start. I tried this but to no avail

iban: yup
      .string()
      .notRequired()
      .min(5, 'Minimum of 5')
      .max(34, 'Maximum of 34'),

If empty string as in '' it still throws a minimum of 5 error How can I fix this?

2 Answers

According to the docs on github, it says passing in undefined is acceptable, however the empty string does not equal undefined.

mixed.notRequired(): Schema
  Mark the schema as not required. Passing undefined as value will not fail validation.
Related