Question/Suggestion
Async validation captures thrown exceptions.
I believe that we should not drive the application flow using exceptions, but instead, we could use Promise.reject to return the errors. Moreover, it is harder to reason about a function when one could return promises or throw exceptions (i.e. it is not clear that we should throw an exception inside the validation function). Some more regarding this discussion can be seen here.
I know that this is a breaking change but since Formik is getting a major version soon, this could be the right time to do it.
Add a catch handler to the validation function.
As I stumbled upon this as well I want to add my 2 cents:
Exceptions should be reserved for unexpected errors that break the control flow.
Validation errors on the other hand are something and is to be expected.
Also the sync API and the async API should behave the same (apart from being async)
The synch API expects a error object to be returned and not to be thrown, so the async API should expect the object to be returned via Promise.resolve as well, not being throw or via Promise.reject
Rejecting would signal, that the validation could not be completed, which is different from not yielding any errors.
Therefore validation errors should be resolved while errors during validation should be rejected
Hi Lukas,
Thanks for your input.
I agree with your point of view: we should resolve with the errors since that is the expected behavior. Furthermore, that鈥檚 how the sync API works. I was just trying to follow the asynchronous API鈥檚 existing pattern.
Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.
ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.
FWIW, I just spent 30 mins trying to debug a missing form validation ("why, nay, /how/ is this code not running?!?") before I realized the validator itself was crashing, but Formik was trapping the exception, presumably because it thought the validator was trying to return a form error asynchronously.
Most helpful comment
As I stumbled upon this as well I want to add my 2 cents:
Exceptions should be reserved for unexpected errors that break the control flow.
Validation errors on the other hand are something and is to be expected.
Also the sync API and the async API should behave the same (apart from being async)
The synch API expects a error object to be returned and not to be thrown, so the async API should expect the object to be returned via
Promise.resolveas well, not being throw or viaPromise.rejectRejecting would signal, that the validation could not be completed, which is different from not yielding any errors.
Therefore validation errors should be resolved while errors during validation should be rejected