Formik: Validation behavior inconsistent

Created on 5 Oct 2018  路  7Comments  路  Source: formium/formik

Current Behavior


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.

Steps to Reproduce


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>

Expected behavior


Errors should either be returned or thrown in both cases.

Suggested solution(s)


I'll open a PR where its returned in both cases if everyone agrees on my opinion. But let's discuss it.

Additional Info

The documentation describes this behavior - so probably not an issue for new devs. Still - what do you guys & girls think?

stale

Most helpful comment

PR is open. I guess this could be something for the next major release since it is a breaking change.

All 7 comments

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

emartini picture emartini  路  3Comments

jeffbski picture jeffbski  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

outaTiME picture outaTiME  路  3Comments