resolvers icon indicating copy to clipboard operation
resolvers copied to clipboard

`trigger` function to return a boolean on validation schema "sync" mode

Open esivan-cloudinary opened this issue 1 year ago • 0 comments

Is your feature request related to a problem? Please describe.

I'm managing a global state that keeps the form values of multiple forms (each with React Hook Form), To validate all forms at once, I have to subscribe each form's 'trigger' function to an array, and execute all on click "Save"/"Update". Since I don't have a reason to use asynchronous validation I use yupResolver(filtersSchema, undefined, { mode: 'sync' }) for synchronous validation. But the trigger function always returns a Promise. This makes it a bit weird in the code (using Tanstack Query):

 const { mutate: createFlow, isLoading: isLoadingCreate } = useCreateFlow();

 const handleSave = async () => {
    const isFormValid = await validateForm();

    if (!isFormValid) {
      return;
    }

    createFlow({
      name: flowInitialName,
      graphInfo: { nodes: graphInfo.nodes.slice(0, -1), edges: graphInfo.edges.slice(0, -1) },
      values: formValues,
    });
  };

It's weird since I need to await for validateForm function but this shouldn't really be async.

Describe the solution you'd like

When { mode: 'sync' } is specified, trigger function should return a boolean (whether form is valid).

Describe alternatives you've considered

This solution suggested by @bluebill1049, but I haven't tried if it works sychronously. https://github.com/react-hook-form/resolvers/issues/88#issuecomment-741094198

esivan-cloudinary avatar Sep 18 '24 11:09 esivan-cloudinary