mantine icon indicating copy to clipboard operation
mantine copied to clipboard

[@mantine/form] zod-resolver doesn't handle asynchronous validation

Open Punie opened this issue 3 years ago • 2 comments

What package has an issue

@mantine/form

Describe the bug

First of all, thank you for the amazing work done on Mantine 4.0 🙏

I my current project, I am making heavy use of form validation through zod and in some cases I have a need for asynchronous validation, eg:

const signupSchema = z
  .object({
    username: z
      .string()
      .refine(
        value =>
          axios
            .post('/api/check_unique_username', { username: value })
            .then(() => true)
            .catch(() => false),
        {
          message: 'Username already exists',
        },
      ),
  });

However, this doesn't work with the current implementation of zodResolver because it uses the safeParse method (see here).

In which browser did the problem occur

All of them

If possible, please include a link to a codesandbox with the reproduced problem

No response

Do you know how to fix the issue

Yes

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

Replace the call to safeParse by a call to safeParseAsync in zodResolver

Punie avatar Mar 25 '22 22:03 Punie

You can send a PR to the master branch

rtivital avatar Mar 26 '22 06:03 rtivital

Is anyone working on this? i see https://github.com/mantinedev/mantine/pull/1255 is closed..

maticrivo avatar Jul 09 '22 07:07 maticrivo