npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@contract-kit/react-hook-form

v0.1.1

Published

React Hook Form integration for contract-kit with Standard Schema support

Readme

@contract-kit/react-hook-form

React Hook Form integration for Contract Kit

This package provides automatic form validation using your contract's body schema. Works with any Standard Schema library (Zod, Valibot, ArkType, etc.).

Installation

npm install @contract-kit/react-hook-form @contract-kit/core react-hook-form @hookform/resolvers react

TypeScript Requirements

This package requires TypeScript 5.0 or higher for proper type inference.

Usage

Basic Form

import { rhf } from "@contract-kit/react-hook-form";
import { createTodo } from "@/contracts/todos";

function CreateTodoForm() {
  const { useForm } = rhf(createTodo);
  const form = useForm({
    defaultValues: {
      title: "",
      completed: false,
    },
  });

  const onSubmit = form.handleSubmit((values) => {
    // values is typed as: { title: string; completed?: boolean }
    console.log("Creating todo:", values);
  });

  return (
    <form onSubmit={onSubmit}>
      <input
        {...form.register("title")}
        placeholder="What needs to be done?"
      />
      {form.formState.errors.title && (
        <p className="error">{form.formState.errors.title.message}</p>
      )}

      <label>
        <input type="checkbox" {...form.register("completed")} />
        Completed
      </label>

      <button type="submit" disabled={form.formState.isSubmitting}>
        Create Todo
      </button>
    </form>
  );
}

With React Query Mutation

import { rhf } from "@contract-kit/react-hook-form";
import { rq } from "@/lib/rq";
import { createTodo } from "@/contracts/todos";

function CreateTodoForm() {
  const { useForm } = rhf(createTodo);
  const form = useForm({
    defaultValues: { title: "" },
  });

  const mutation = rq(createTodo).useMutation({
    onSuccessInvalidate: true,
  });

  const onSubmit = form.handleSubmit((values) => {
    mutation.mutate({ body: values });
  });

  return (
    <form onSubmit={onSubmit}>
      <input {...form.register("title")} placeholder="Title" />
      {form.formState.errors.title && (
        <p>{form.formState.errors.title.message}</p>
      )}

      <button type="submit" disabled={mutation.isPending}>
        {mutation.isPending ? "Creating..." : "Create"}
      </button>

      {mutation.isError && (
        <p className="error">{mutation.error.message}</p>
      )}
    </form>
  );
}

Disabling Validation

If you need to disable the schema resolver (e.g., for partial form handling):

const { useForm } = rhf(createTodo);
const form = useForm({
  resolverEnabled: false, // Disable schema validation
  defaultValues: { title: "" },
});

Using Contract Config Directly

You can pass either a contract builder or its config:

import { rhf } from "@contract-kit/react-hook-form";
import { createTodo } from "@/contracts/todos";

// Using ContractBuilder directly
const { useForm } = rhf(createTodo);

// Or using the contract config
const { useForm } = rhf(createTodo.config);

API Reference

rhf(contract)

Creates a React Hook Form adapter for a contract.

const adapter = rhf(createTodo);

adapter.useForm(props?)

Returns a React Hook Form useForm result with the contract's body schema as resolver.

const form = adapter.useForm({
  defaultValues?: { ... },
  resolverEnabled?: boolean, // default: true
  // ...other React Hook Form options
});

Type Inference

Form values are automatically typed based on the contract's body schema:

// Contract definition
const createTodo = todos
  .post("/api/todos")
  .body(z.object({
    title: z.string().min(1),
    description: z.string().optional(),
    completed: z.boolean().optional(),
  }))
  .response(201, TodoSchema);

// Form values are inferred
const form = rhf(createTodo).useForm();
form.register("title");       // ✓ Valid
form.register("description"); // ✓ Valid
form.register("invalid");     // ✗ Type error

Standard Schema Support

This package uses the @hookform/resolvers/standard-schema resolver, which works with any Standard Schema compatible library:

  • Zod - z.object({ ... })
  • Valibot - v.object({ ... })
  • ArkType - type({ ... })

Validation Behavior

The resolver validates:

  1. On blur - When a field loses focus
  2. On change - After first submission attempt
  3. On submit - Before calling your submit handler

Validation errors are available via form.formState.errors:

{form.formState.errors.title && (
  <span className="error">
    {form.formState.errors.title.message}
  </span>
)}

Complete Example

import { rhf } from "@contract-kit/react-hook-form";
import { rq } from "@/lib/rq";
import { updateProfile } from "@/contracts/profile";

function ProfileForm({ profile }) {
  const { useForm } = rhf(updateProfile);
  const form = useForm({
    defaultValues: {
      name: profile.name,
      email: profile.email,
      bio: profile.bio ?? "",
    },
  });

  const mutation = rq(updateProfile).useMutation({
    onSuccess: () => {
      toast.success("Profile updated!");
    },
  });

  const onSubmit = form.handleSubmit((values) => {
    mutation.mutate({ body: values });
  });

  const { errors, isDirty, isSubmitting } = form.formState;

  return (
    <form onSubmit={onSubmit}>
      <div>
        <label htmlFor="name">Name</label>
        <input id="name" {...form.register("name")} />
        {errors.name && <span className="error">{errors.name.message}</span>}
      </div>

      <div>
        <label htmlFor="email">Email</label>
        <input id="email" type="email" {...form.register("email")} />
        {errors.email && <span className="error">{errors.email.message}</span>}
      </div>

      <div>
        <label htmlFor="bio">Bio</label>
        <textarea id="bio" {...form.register("bio")} />
        {errors.bio && <span className="error">{errors.bio.message}</span>}
      </div>

      <button type="submit" disabled={!isDirty || isSubmitting}>
        {isSubmitting ? "Saving..." : "Save Changes"}
      </button>
    </form>
  );
}

Related Packages

License

MIT