-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
notRequired() with empty string causes validation to fail #313
Comments
@dan003400 Could you please look into the lib code and file a PR for this if possible? Thank You 😃 |
@dan003400 I think this works as a workaround:
Using .when() referencing a sibling boolean key/value pair let's you check if the phone string is blank, and only use yup-phone if it isn't. On the off chance you're using Formik doing this quick check whenever validations happen seems to be working alright:
|
Or actually a better workaround without creating additional values like 'isPhoneBlank' is to add new method to yup where we can conditionally validate different schemas if value is blank:
Or simply use .test method in schema without adding new method:
|
Add this to the repo please
|
@nightness Would you like to file a PR with the given code? |
@abhisekp Just did the PR. |
@abhisekp corrected my PR errors. |
Thanks for your PR @nightness |
Hi folks, I really need this change for a project, |
@JulienRst Kindly, review #461 (comment) |
Kindly, reach out to me in any of the platforms given below to volunteer for maintaining this repository. |
To add here from @KmaCoder
|
Clone #718 |
To avoid empty field validation try the below solution.
The reason for duplicating the dependencies is to avoid cyclic dependency errors. Check this StackOverflow discussion for more details. |
This was how I fixed empty field validation.
According to yup string.ensure() documentation
I hope this works for someone. All thanks to @abhisekp. Happy coding. |
There is no way to make a field optional but validate when not an empty string.
Yup.string().phone(true, false, 'Phone not valid').notRequired()
The text was updated successfully, but these errors were encountered: