Skip to content

Instantly share code, notes, and snippets.

@danieljpgo
Last active October 16, 2024 15:15
Show Gist options
  • Save danieljpgo/61e5973910c9e2df11a76f52b4869d10 to your computer and use it in GitHub Desktop.
Save danieljpgo/61e5973910c9e2df11a76f52b4869d10 to your computer and use it in GitHub Desktop.

Revisions

  1. danieljpgo revised this gist Oct 16, 2024. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions useForm.ts
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,12 @@ import {
    import { zodResolver } from "@hookform/resolvers/zod";
    import { TypeOf, ZodSchema } from "zod";

    interface UseFormProps<Z extends ZodSchema>
    extends Exclude<useHookFormProps<TypeOf<Z>>, "resolver"> {
    type UseFormProps<Z extends ZodSchema> = Omit<
    useHookFormProps<TypeOf<Z>>,
    'resolver'
    > & {
    schema: Z;
    }
    };

    export function useForm<Z extends ZodSchema>({
    schema,
  2. danieljpgo created this gist Jan 7, 2023.
    18 changes: 18 additions & 0 deletions useForm.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import {
    useForm as useHookForm,
    UseFormProps as useHookFormProps,
    } from "react-hook-form";
    import { zodResolver } from "@hookform/resolvers/zod";
    import { TypeOf, ZodSchema } from "zod";

    interface UseFormProps<Z extends ZodSchema>
    extends Exclude<useHookFormProps<TypeOf<Z>>, "resolver"> {
    schema: Z;
    }

    export function useForm<Z extends ZodSchema>({
    schema,
    ...props
    }: UseFormProps<Z>) {
    return useHookForm({ ...props, resolver: zodResolver(schema) });
    }