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 🙏

© 2025 – Pkg Stats / Ryan Hefner

zod-auto-form

v0.0.32

Published

Auto-generate typed React forms from Zod schemas

Readme

zod-auto-form

A React library for automatic form generation using Zod schemas.

Features

  • 🚀 Automatically generate forms from Zod schemas
  • 🧩 Customizable components for each field type
  • 🔍 Input validation using Zod
  • 🎨 Flexible styling options
  • 📱 Responsive form layouts
  • 🔄 Support for complex nested objects and arrays
  • 👁️ Field visibility rules for conditional rendering
  • 🛠️ Field type overrides for custom input components

Installation

npm install zod-auto-form zod
# or
yarn add zod-auto-form zod
# or
pnpm add zod-auto-form zod

Default Components

You can quickly get started with the default shadcn/ui components:

# Using npm
npx shadcn@latest add https://raw.githubusercontent.com/atul7017128880/component-registry/refs/heads/master/public/registry/auto-form-component.json

# Using pnpm
pnpm dlx shadcn@latest add https://raw.githubusercontent.com/atul7017128880/component-registry/refs/heads/master/public/registry/auto-form-component.json

# Using Bun
bunx --bun shadcn@latest add https://raw.githubusercontent.com/atul7017128880/component-registry/refs/heads/master/public/registry/auto-form-component.json

Quick Start

import { z } from 'zod';
import { AutoForm, AutoFormComponentsProvider } from 'zod-auto-form';
import YourCustomComponents from './your-components';
import { ComponentRegistry } from '../shadcn-ui-components';
// 1. Define your schema with Zod
const userFormSchema = z.object({
  name: z.string().min(2, "Name must be at least 2 characters"),
  email: z.string().email("Invalid email address"),
  age: z.number().min(18, "Must be at least 18 years old"),
  isSubscribed: z.boolean().default(false),
});

// 2. Create your form component
function UserForm() {
  return (
    <AutoFormComponentsProvider components={ComponentRegistry  or YourCustomComponents}>
      <AutoForm
        formSchema={userFormSchema}
        onSubmit={(values) => {
          console.log(values);
          // Handle form submission
        }}
        onSubmitError={(error, errorMessages, values) => {
          console.error('Form validation failed:', errorMessages);
        }}
      />
    </AutoFormComponentsProvider>
  );
}

Component Registration

You need to provide your UI components through the AutoFormComponentsProvider:

import { AutoFormComponentsProvider,ComponentRegistry} from 'zod-auto-form';

// Your custom form components that match the expected interfaces fully typed
const myComponents = ComponentRegistry({
  Input: MyTextInput,
  Select: MySelectComponent,
  NumberInput: MyNumberInput,
  // ... other component types
});

function App() {
  return (
    <AutoFormComponentsProvider components={myComponents}>
      {/* Your app/forms here */}
    </AutoFormComponentsProvider>
  );
}

Supported Field Types

  • string - Text inputs
  • number - Numeric inputs
  • boolean - Checkboxes and switches
  • array - Array fields with add/remove functionality
  • object - Nested object fields
  • select - Dropdown select fields
  • multi-select - Multiple selection fields
  • date - Date/time pickers
  • textarea - Multi-line text inputs
  • switch - Toggle switches
  • radio - Radio button groups

Advanced Usage

Field Type Overrides

Override the automatically determined field types:

<AutoForm
  formSchema={schema}
  fieldTypeOverrides={{
    description: {
      typeOfField: "textarea",
    },
  }}
  // ...
/>

Visibility Rules

Control which fields are displayed based on other field values:

<AutoForm
  formSchema={schema}
	visibilityRules={[
					{
						sourceFields: ["age"],
						targetField: "address",
						when: (values) => values.age > 18,
						type: "show",
					},
					{
						sourceFields: ["name"],
						targetField: "address.city",
						when: (values) => values.name === "John",
						type: "show",
					},
				]}
  // ...
/>

Grouped Fields

Organize fields into logical groups:

// The library will automatically detect and group fields defined in nested objects
<AutoForm
  formSchema={schema}
		groupByZodKeys={{
					Users: ["age", "name"],
					Address: ["address"],
				}}
  // ...
/>

Change Position of Fields

Change the position of fields:

<AutoForm
  formSchema={schema}
		changePositionOfFields={{
					nested: {
						age: {
							position: 0,
						},
						name: {
							position: 1,
						},
						address: {
							position: 2,
							nested: {
								city: {
									position: 0,
								},
								street: {
									position: 1,
								},
							},
						},
					},
				}}
  // ...
/>

API Reference

<AutoForm> Props

| Prop | Type | Description | |------|------|-------------| | formSchema | z.ZodSchema | The Zod schema used to generate the form | | fieldTypeOverrides | object | Override the automatically determined field types | | changePositionOfFields | object | Change the position of fields | | groupByZodKeys | object | Group fields by Zod keys | | visibilityRules | object | Rules for conditionally showing/hiding fields | | defaultValues | object | Initial values for the form fields | | formId | string | Optional ID for the form element | | onSubmit | function | Callback when form is successfully submitted | | onSubmitError | function | Callback when validation fails on submission |

<AutoFormComponentsProvider> Props

| Prop | Type | Description | |------|------|-------------| | components | object | Custom UI components for rendering different field types | | children | ReactNode | Child components that will use the provided form components |

<ComponentRegistry> Props

| Prop | Type | Description | |------|------|-------------| | components | object | Custom UI components for rendering different field types |

My Info

License

MIT