jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.98k stars 2.79k forks source link

Allowing passing a Yup async promise callback to validateYupSchema #3270

Open ziriax opened 3 years ago

ziriax commented 3 years ago

Feature request

Current Behavior

Currently validateYupSchema calls Yup's schema.validate method:

validate(
    value: any,
    options?: ValidateOptions<TContext>,
    maybeCb?: Callback,
  ) 

However, it is impossible in the current Formik implementation to pass the maybeCb callback.

This callback is needed to customize the behavior of async validation.

Desired Behavior

It should be possible to configure the Yup async validation callback.

Suggested Solution

Add an optional validationSchemaCallback property to the FormikConfig to customize this behavior.

Who does this impact? Who is this for?

This should be fully backwards compatible.

In our project we need this behavior to ignore aborted promises, or any other custom behavior where an async validation should be completely ignored.

Describe alternatives you've considered

We can return a promise that never resolves, but that can result in memory leaks.

Additional context

I can provide a PR if that helps.

Serenity09 commented 3 years ago

I think I might need this behavior for my application as well. I'm building a form which constructs validation rules from a DB call to a table representing Yup validation rules. My easiest, ideal, and most performant option would be to provide my formik context with the validation schema after the formik context has been constructed, but it would appear that my only option is to construct the formik context after waiting for my validation schema to be ready

One other alternative to solving this problem that might be worth considering is to add a method useFormik().setValidationSchema(schema) which can trigger an effect to revalidate the formik context

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days