If the validate property is an async function it expects the errors to be thrown instead of returned. See source file Formik.tsx: https://github.com/jaredpalmer/formik/blob/0df51c2cdda3d01c65fabdbc8f3b6f443a7b0f2e/src/Formik.tsx#L215-L233
Synchronous validation functions need to return the error object instead. IMHO this is inconsistent and should be either throw or return for both cases.
Write a simple validation function and make it async. Errors will magically disappear until you throw them instead of returning.
Will not validate
<Formik
validate={() => ({ fooField: 'Foo is required!' })}
>
{/* MyForm */}
</Formik>
Will validate
<Formik
validate={async () => ({ fooField: 'Foo is required!' })}
>
{/* MyForm */}
</Formik>
Errors should either be returned or thrown in both cases.
I'll open a PR where its returned in both cases if everyone agrees on my opinion. But let's discuss it.
The documentation describes this behavior - so probably not an issue for new devs. Still - what do you guys & girls think?
@jaredpalmer any thoughts or should I just close this?
I agree, it seems to me that it isn't very intuitive to throw in async validate rather then simply return them like in regular validate.
PR is open. I guess this could be something for the next major release since it is a breaking change.
+1
I just ran into this and spent half an hour wondering why validation wasn't working as soon as I made it asynchronous. Would be great to land this soon!
Im also running into problems with this design. Its difficult to differentiate between real unexpected runtime errors and expected validation errors.
this will fixed in v2
Most helpful comment
PR is open. I guess this could be something for the next major release since it is a breaking change.