react-hook-form / resolvers

📋 Validation resolvers: Yup, Zod, Superstruct, Joi, Vest, Class Validator, io-ts, Nope, computed-types, typanion, Ajv, TypeBox, ArkType, Valibot, effect-ts and VineJS
https://react-hook-form.com/
MIT License
1.71k stars 155 forks source link

Form not submitting when using drizzle-zod to create schemes to use in zodResolver #642

Closed jacobsamo closed 9 months ago

jacobsamo commented 10 months ago

Describe the bug When using drizzle-zod to generate schemas from database schemas and using them in the useForm resolvers the onSubmit won't fire at all for some reason,

So for example:

export const userSchema = createInsertSchema(users);

and then using it in the useForm

const { handleSubmit, register, control, getValues } = useForm<User>({
    resolver: zodResolver(userSchema),
    defaultValues: async () => {
      const res = await fetch(`/api/users/`)
      const user = (await res.json()) as User;
      return user;
    },
  });

  const onSubmit: SubmitHandler<User> = async (data) => {
    console.log(data)

    await fetch('/api/users/updateUser', {
      method: "PUT",
      body: body: JSON.stringify(data),
    })
  };

in this example when you click the Submit from the form it doesn't work at all.

however if you build your own schema using zod it works fine.

For reference this was working about a about a week ago.

No errors show in the browser console or terminal, so i don't know what is up there

To Reproduce Steps to reproduce the behavior:

  1. create a schema using zod
  2. create a zod schema using drizzle-zod and createInsertSchema
  3. import that schema into a useForm resolver
  4. try submitting the form (should not work)

    Codesandbox link (Required) very simple application that doesn't seem to submit: https://codesandbox.io/p/sandbox/react-hook-form-zod-resolver-not-working-9c7qn9

Expected behavior I expected that validation would work out of the box as usual

Desktop (please complete the following information):

Additional context

References: