Registry / web-framework / zod-form

zod-form

JSON →
library1.9.3jsnpmunverified

Zod Form for React (published as `zod-form` on npm, maintained at `watershed-climate/react-ts-form` on GitHub) is a robust utility library designed to streamline the creation of type-safe forms in React applications. It integrates seamlessly with Zod for schema validation and React Hook Form for efficient form state management, aiming to significantly reduce boilerplate. Currently stable at version 1.9.3, it is under active development with regular updates. Key differentiators include its strong TypeScript inference capabilities, enabling automatic generation of form components from Zod schemas, full control over component rendering via typesafe props, and a headless UI approach. The library is also notably lightweight (~3kb gzipped) and focuses on developer productivity by abstracting away common Zod and React Hook Form setup complexities. [4, 15, 16, 17, 18]

npm install zod-form
INSTALL
IMPORT
SIG · ZOD-FORM
Z
zod-form
web-frameworkjavascriptv1.9.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createFormFactory
import { createFormFactory } from 'zod-form';
import createFormFactory from 'zod-form';
The primary named export for initializing the form factory. It is not a default export.
Form
import { createFormFactory } from 'zod-form'; const { Form } = createFormFactory({ /* ... */ });
import { Form } from 'zod-form';
The `Form` component is returned by `createFormFactory`, not directly exported from the package root. It is dynamically typed based on your schema.
Field types (e.g., Form.Field, Form.Text, Form.Select)
import { createFormFactory } from 'zod-form'; const { Form } = createFormFactory({ /* ... */ }); // Then use: <Form.Field name="myField" /> or <Form.Text name="myText" />
import { Field } from 'zod-form';
Individual field components are accessed as properties of the `Form` object returned by `createFormFactory`, and are type-inferred from your Zod schema. [1, 4]

This quickstart demonstrates how to define a Zod schema, create a typed form factory, and render a complete, validated form using `zod-form`'s `Form` and `Field` components. It includes basic input fields, a number input with custom onChange handling for optional values, a checkbox, and error display.

import { createFormFactory } from 'zod-form'; import { z } from 'zod'; import React from 'react'; const userSchema = z.object({ name: z.string().min(1, 'Name is required').max(50), email: z.string().email('Invalid email address'), age: z.number().min(18, 'Must be at least 18').max(120, 'Must be at most 120').optional(), acceptTerms: z.boolean().refine(val => val === true, 'You must accept the terms'), }); interface UserFormData extends z.infer<typeof userSchema> {} const { Form, Field } = createFormFactory<UserFormData>()({ schema: userSchema, defaultValues: { name: '', email: '', age: undefined, acceptTerms: false, }, }); function UserProfileForm() { const onSubmit = (data: UserFormData) => { console.log('Form submitted:', data); alert(JSON.stringify(data, null, 2)); }; return ( <Form onSubmit={onSubmit} className="space-y-4 p-4 border rounded shadow-sm max-w-md mx-auto"> <div> <label htmlFor="name" className="block text-sm font-medium text-gray-700">Name</label> <Field name="name" render={({ field }) => ( <input {...field} id="name" type="text" placeholder="Your name" className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" /> )} /> <Form.Error name="name" className="text-red-500 text-sm" /> </div> <div> <label htmlFor="email" className="block text-sm font-medium text-gray-700">Email</label> <Field name="email" render={({ field }) => ( <input {...field} id="email" type="email" placeholder="your@example.com" className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" /> )} /> <Form.Error name="email" className="text-red-500 text-sm" /> </div> <div> <label htmlFor="age" className="block text-sm font-medium text-gray-700">Age (Optional)</label> <Field name="age" render={({ field }) => ( <input {...field} id="age" type="number" placeholder="Age" className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm p-2" onChange={e => field.onChange(e.target.value === '' ? undefined : Number(e.target.value))} /> )} /> <Form.Error name="age" className="text-red-500 text-sm" /> </div> <div className="flex items-center"> <Field name="acceptTerms" render={({ field }) => ( <input {...field} id="acceptTerms" type="checkbox" checked={field.value} className="h-4 w-4 text-indigo-600 border-gray-300 rounded" /> )} /> <label htmlFor="acceptTerms" className="ml-2 block text-sm text-gray-900">I accept the terms and conditions</label> <Form.Error name="acceptTerms" className="text-red-500 text-sm ml-2" /> </div> <Form.SubmitButton className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"> Submit </Form.SubmitButton> </Form> ); } export default UserProfileForm;
Debug
Known issues
breakingStrict mode (`"strict": true`) in `tsconfig.json` is a requirement for `zod-form` to ensure proper type inference and prevent common TypeScript mistakes. Failing to enable it can lead to unexpected type errors or incorrect type inference, undermining the library's core benefits. [4, 16]
fix
Ensure `compilerOptions.strict` is set to `true` in your `tsconfig.json` file.
affects: >=1.0.0
gotchaMismatched peer dependency versions for `react`, `zod`, `react-hook-form`, or `@hookform/resolvers` can cause runtime errors or unexpected behavior. This is particularly relevant with `zod` v4, where `@hookform/resolvers` introduced specific support. [8, 10]
fix
Carefully check the `peerDependencies` listed in `zod-form`'s `package.json` and ensure your installed versions of `zod`, `react-hook-form`, and `@hookform/resolvers` are compatible. Upgrade or downgrade as necessary, e.g., `npm install zod@^3.19.0 react-hook-form@^7.39.0 @hookform/resolvers@^3.0.0` (adjusting to the exact compatible range).
affects: >=1.0.0
gotcha`zod-form` (from `react-ts-form`) does not currently support class components in React. It is designed exclusively for use with functional components and React Hooks. [4]
fix
Ensure all components interacting with `zod-form` are functional components.
affects: >=1.0.0
gotchaThe library does not yet support 'dependent field props', meaning you cannot dynamically change one field component's props or behavior based on the real-time value of another field within the form using built-in mechanisms. [4]
fix
For dependent field logic, you might need to manage some state outside the form component or use `react-hook-form`'s `watch` function to observe field changes and conditionally render/pass props manually.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'schema')
The `schema` property was not correctly provided to `createFormFactory` or is `undefined` at runtime.
fix
Ensure your Zod schema is properly imported and passed as the `schema` option to `createFormFactory`.
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
This typically indicates a mismatch in React versions or a violation of React Hooks rules (e.g., calling hooks outside a functional component or conditionally). This library relies on `react-hook-form`, which uses hooks extensively. [11]
fix
Verify that you have only one instance of React installed (check `npm ls react`). Ensure that `zod-form` components and hooks are only used within functional React components. Also check `react-hook-form` and `react` peer dependency versions.
TS2345: Argument of type '...' is not assignable to parameter of type 'ZodSchema<any>'.
The TypeScript type of the schema passed to `createFormFactory` (or related utilities) does not conform to `ZodSchema`, or there's a version mismatch between Zod and `@hookform/resolvers`. [12]
fix
Check your Zod schema definition for correctness. Ensure `zod` is imported as `z` and that you are using Zod methods correctly (e.g., `z.object`, `z.string`). Verify compatible versions of `zod` and `@hookform/resolvers`.
Property 'XXX' does not exist on type 'IntrinsicAttributes & ...'
You are attempting to pass an HTML attribute or prop to a `zod-form` component (or its underlying HTML element) that is not recognized by its TypeScript definition. This often happens with typos or incorrect prop usage. [12]
fix
Review the specific component's expected props or the standard HTML attributes for the element being rendered. Correct any typos. If you're using `render` props, ensure the props passed to the native HTML element are valid.
Upgrade
Version history
1.9.3latest on npm
Audit
Dependencies
zodrequiredPeer dependency for defining form validation schemas.
reactrequiredPeer dependency for React component rendering.
react-hook-formrequiredPeer dependency for underlying form state management and hooks.
@hookform/resolversrequiredPeer dependency to bridge Zod schemas with React Hook Form validation.
Agent activity
45 hits · last 30 days
node
38
OpenAI (training)
1
Resources