Skip to main content
v7

Zod Form

Describe a form once, with a zod 4 schema, and render it with the library components. The type of each key picks the component. The mask, the options, the defaults and the limits come from the schema.

Package@g4rcez/componentsSectionForm Controls

Examples

Schema to form

The schema below the form is the file this form runs. Submit it empty to see the schema messages, then fill it in to see the typed result.

Create profile
Plan
Years of experience
Status
Avatar
Address
Contacts
Result

Submit the form to see what the schema returns.

The schema behind this form

tsx
import { z } from "zod";

/**
 * Every field below is rendered by <ZodForm schema={demoSchema} />.
 * The type of each key picks the component, and .meta() carries what a type cannot say:
 * a title, a mask, the option labels, or another component.
 */
export const demoSchema = z.object({
    // string -> Input. min/max become minLength/maxLength, `placeholder` comes from .meta()
    name: z.string().min(2, "Use at least 2 characters").max(40).meta({ title: "Name", placeholder: "Ada Lovelace" }),
    // z.email() -> Input type="email"
    email: z.email("Enter a valid email").meta({ title: "Email", placeholder: "ada@example.com" }),
    // a mask from .meta() goes straight to the Input. It owns the format, so the regex is not sent as `pattern`
    document: z
        .string()
        .regex(/^\d{3}\.\d{3}\.\d{3}-\d{2}$/, "Use the CPF format")
        .meta({ title: "CPF", mask: "cpf", placeholder: "000.000.000-00" }),
    phone: z.string().meta({ title: "Phone", mask: "cellphone" }),
    // a number with a money mask is typed as text and parsed back to a number on submit
    salary: z.number("Enter a salary").min(1000, "The minimum is 1.000").meta({ title: "Salary", mask: "money", currency: "BRL", locale: "pt-BR" }),
    // number -> Input type="number". int() adds step=1, min/max are copied
    age: z.number("Enter your age").int().min(18, "You must be 18 or older").max(99).meta({ title: "Age" }),
    // optional() is not required. component picks another control for the same string
    bio: z.string().max(200).optional().meta({ title: "Bio", component: "textarea", description: "Optional, up to 200 characters." }),
    // enum -> Select. The options are the values, the texts come from labels. default() is the selected value
    role: z
        .enum(["admin", "editor", "viewer"])
        .default("editor")
        .meta({ title: "Role", labels: { admin: "Administrator", editor: "Editor", viewer: "Viewer" } }),
    // the same enum as an Autocomplete
    language: z.enum(["ts", "js", "ex", "kt"], "Pick a language").meta({
        title: "Favorite language",
        component: "autocomplete",
        labels: { ts: "TypeScript", js: "JavaScript", ex: "Elixir", kt: "Kotlin" },
    }),
    // ...or as a group of Radioboxes
    plan: z
        .enum(["free", "pro", "team"])
        .default("free")
        .meta({ title: "Plan", component: "radio", labels: { free: "Free", pro: "Pro", team: "Team" } }),
    // array of enum -> MultiSelect
    skills: z
        .array(z.enum(["react", "node", "sql", "rust"]))
        .min(1, "Pick at least one skill")
        .default(["react"])
        .meta({ title: "Skills", labels: { react: "React", node: "Node.js", sql: "SQL", rust: "Rust" } }),
    // boolean -> Checkbox, or a Switch
    newsletter: z.boolean().default(true).meta({ title: "Send me the newsletter", component: "switch" }),
    // literal(true) is a required checkbox
    terms: z.literal(true, { error: "You must accept the terms" }).meta({ title: "I accept the terms" }),
    // a number as a Slider. It starts at its default
    experience: z.number().min(0).max(30).default(5).meta({ title: "Years of experience", component: "slider" }),
    // z.date() -> DatePicker storing a Date. min/max disable the days outside the range
    birthday: z
        .date("Pick your birthday")
        .min(new Date(1900, 0, 1))
        .max(new Date(2099, 11, 31))
        .meta({ title: "Birthday" }),
    // z.iso.datetime() -> DatePicker with time, storing an ISO string
    interview: z.iso.datetime("Pick a slot").meta({ title: "Interview slot" }),
    status: z
        .enum(["todo", "doing", "done"])
        .default("todo")
        .meta({ title: "Status", component: "morph", labels: { todo: "To do", doing: "Doing", done: "Done" } }),
    // z.file() -> FileUpload. mime() is the accept list, max() the size limit
    avatar: z
        .file()
        .mime(["image/png", "image/jpeg"])
        .max(2 * 1024 * 1024)
        .optional()
        .meta({ title: "Avatar" }),
    // object -> a group of fields
    address: z
        .object({
            street: z.string().min(1, "Street is required").meta({ title: "Street" }),
            city: z.string().default("Recife").meta({ title: "City" }),
        })
        .meta({ title: "Address" }),
    // array of objects -> repeatable rows. min(1) starts with one row
    contacts: z
        .array(
            z.object({
                label: z.string().min(1, "Label is required").meta({ title: "Label" }),
                phone: z.string().meta({ title: "Phone", mask: "cellphone" }),
            })
        )
        .min(1, "Add at least one contact")
        .meta({ title: "Contacts" }),
});

What the schema became

zodToFields reads the schema and returns one descriptor per field. This is the data the form renders from.

json
[
  {
    "name": "name",
    "kind": "string",
    "options": [],
    "required": true,
    "constraints": {
      "minLength": 2,
      "maxLength": 40
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Name",
      "placeholder": "Ada Lovelace"
    },
    "title": "Name",
    "placeholder": "Ada Lovelace",
    "component": "input"
  },
  {
    "name": "email",
    "kind": "string",
    "options": [],
    "required": true,
    "constraints": {
      "type": "email"
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Email",
      "placeholder": "ada@example.com"
    },
    "title": "Email",
    "placeholder": "ada@example.com",
    "component": "input"
  },
  {
    "name": "document",
    "kind": "string",
    "options": [],
    "required": true,
    "constraints": {},
    "mask": "cpf",
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "CPF",
      "mask": "cpf",
      "placeholder": "000.000.000-00"
    },
    "title": "CPF",
    "placeholder": "000.000.000-00",
    "component": "input"
  },
  {
    "name": "phone",
    "kind": "string",
    "options": [],
    "required": false,
    "constraints": {},
    "mask": "cellphone",
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Phone",
      "mask": "cellphone"
    },
    "title": "Phone",
    "component": "input"
  },
  {
    "name": "salary",
    "kind": "number",
    "options": [],
    "required": true,
    "constraints": {
      "min": 1000,
      "type": "text"
    },
    "mask": "money",
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Salary",
      "mask": "money",
      "currency": "BRL",
      "locale": "pt-BR"
    },
    "title": "Salary",
    "currency": "BRL",
    "locale": "pt-BR",
    "component": "input"
  },
  {
    "name": "age",
    "kind": "number",
    "options": [],
    "required": true,
    "constraints": {
      "min": 18,
      "max": 99,
      "step": 1,
      "type": "number"
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Age"
    },
    "title": "Age",
    "component": "input"
  },
  {
    "name": "bio",
    "kind": "string",
    "options": [],
    "required": false,
    "constraints": {
      "maxLength": 200
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Bio",
      "component": "textarea",
      "description": "Optional, up to 200 characters."
    },
    "info": "Optional, up to 200 characters.",
    "title": "Bio",
    "component": "textarea"
  },
  {
    "name": "role",
    "kind": "enum",
    "options": [
      {
        "value": "admin",
        "label": "Administrator"
      },
      {
        "value": "editor",
        "label": "Editor"
      },
      {
        "value": "viewer",
        "label": "Viewer"
      }
    ],
    "required": false,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": "editor",
    "meta": {
      "title": "Role",
      "labels": {
        "admin": "Administrator",
        "editor": "Editor",
        "viewer": "Viewer"
      }
    },
    "title": "Role",
    "component": "select"
  },
  {
    "name": "language",
    "kind": "enum",
    "options": [
      {
        "value": "ts",
        "label": "TypeScript"
      },
      {
        "value": "js",
        "label": "JavaScript"
      },
      {
        "value": "ex",
        "label": "Elixir"
      },
      {
        "value": "kt",
        "label": "Kotlin"
      }
    ],
    "required": true,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Favorite language",
      "component": "autocomplete",
      "labels": {
        "ts": "TypeScript",
        "js": "JavaScript",
        "ex": "Elixir",
        "kt": "Kotlin"
      }
    },
    "title": "Favorite language",
    "component": "autocomplete"
  },
  {
    "name": "plan",
    "kind": "enum",
    "options": [
      {
        "value": "free",
        "label": "Free"
      },
      {
        "value": "pro",
        "label": "Pro"
      },
      {
        "value": "team",
        "label": "Team"
      }
    ],
    "required": false,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": "free",
    "meta": {
      "title": "Plan",
      "component": "radio",
      "labels": {
        "free": "Free",
        "pro": "Pro",
        "team": "Team"
      }
    },
    "title": "Plan",
    "component": "radio"
  },
  {
    "name": "skills",
    "kind": "multi",
    "options": [
      {
        "value": "react",
        "label": "React"
      },
      {
        "value": "node",
        "label": "Node.js"
      },
      {
        "value": "sql",
        "label": "SQL"
      },
      {
        "value": "rust",
        "label": "Rust"
      }
    ],
    "required": false,
    "constraints": {
      "minItems": 1
    },
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": [
      "react"
    ],
    "meta": {
      "title": "Skills",
      "labels": {
        "react": "React",
        "node": "Node.js",
        "sql": "SQL",
        "rust": "Rust"
      }
    },
    "title": "Skills",
    "component": "multiselect"
  },
  {
    "name": "newsletter",
    "kind": "boolean",
    "options": [],
    "required": false,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": true,
    "meta": {
      "title": "Send me the newsletter",
      "component": "switch"
    },
    "title": "Send me the newsletter",
    "component": "switch"
  },
  {
    "name": "terms",
    "kind": "boolean",
    "options": [],
    "required": true,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "I accept the terms"
    },
    "title": "I accept the terms",
    "component": "checkbox"
  },
  {
    "name": "experience",
    "kind": "number",
    "options": [],
    "required": false,
    "constraints": {
      "min": 0,
      "max": 30,
      "type": "number"
    },
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": 5,
    "meta": {
      "title": "Years of experience",
      "component": "slider"
    },
    "title": "Years of experience",
    "component": "slider"
  },
  {
    "name": "birthday",
    "kind": "date",
    "options": [],
    "required": true,
    "constraints": {
      "minDate": "1900-01-01",
      "maxDate": "2099-12-31"
    },
    "children": [],
    "storage": "date",
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Birthday"
    },
    "title": "Birthday",
    "component": "datepicker"
  },
  {
    "name": "interview",
    "kind": "datetime",
    "options": [],
    "required": true,
    "constraints": {},
    "children": [],
    "storage": "iso-datetime",
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Interview slot"
    },
    "title": "Interview slot",
    "component": "datepicker"
  },
  {
    "name": "status",
    "kind": "enum",
    "options": [
      {
        "value": "todo",
        "label": "To do"
      },
      {
        "value": "doing",
        "label": "Doing"
      },
      {
        "value": "done",
        "label": "Done"
      }
    ],
    "required": false,
    "constraints": {},
    "children": [],
    "nullable": false,
    "hasDefault": true,
    "defaultValue": "todo",
    "meta": {
      "title": "Status",
      "component": "morph",
      "labels": {
        "todo": "To do",
        "doing": "Doing",
        "done": "Done"
      }
    },
    "title": "Status",
    "component": "morph"
  },
  {
    "name": "avatar",
    "kind": "file",
    "options": [],
    "required": false,
    "constraints": {
      "accept": "image/png,image/jpeg",
      "maxSize": 2097152,
      "multiple": false
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "meta": {
      "title": "Avatar"
    },
    "title": "Avatar",
    "component": "file"
  },
  {
    "name": "address",
    "kind": "object",
    "options": [],
    "required": true,
    "constraints": {},
    "children": [
      {
        "name": "address.street",
        "kind": "string",
        "options": [],
        "required": true,
        "constraints": {
          "minLength": 1
        },
        "children": [],
        "nullable": false,
        "hasDefault": false,
        "meta": {
          "title": "Street"
        },
        "title": "Street",
        "component": "input"
      },
      {
        "name": "address.city",
        "kind": "string",
        "options": [],
        "required": false,
        "constraints": {},
        "children": [],
        "nullable": false,
        "hasDefault": true,
        "defaultValue": "Recife",
        "meta": {
          "title": "City"
        },
        "title": "City",
        "component": "input"
      }
    ],
    "nullable": false,
    "hasDefault": false,
    "defaultValue": {
      "city": "Recife"
    },
    "meta": {
      "title": "Address"
    },
    "title": "Address",
    "component": "group"
  },
  {
    "name": "contacts",
    "kind": "array",
    "options": [],
    "required": true,
    "constraints": {
      "minItems": 1
    },
    "children": [],
    "nullable": false,
    "hasDefault": false,
    "defaultValue": [
      {}
    ],
    "meta": {
      "title": "Contacts"
    },
    "title": "Contacts",
    "component": "list",
    "element": {
      "name": "contacts[]",
      "kind": "object",
      "options": [],
      "required": true,
      "constraints": {},
      "children": [
        {
          "name": "contacts[].label",
          "kind": "string",
          "options": [],
          "required": true,
          "constraints": {
            "minLength": 1
          },
          "children": [],
          "nullable": false,
          "hasDefault": false,
          "meta": {
            "title": "Label"
          },
          "title": "Label",
          "component": "input"
        },
        {
          "name": "contacts[].phone",
          "kind": "string",
          "options": [],
          "required": false,
          "constraints": {},
          "mask": "cellphone",
          "children": [],
          "nullable": false,
          "hasDefault": false,
          "meta": {
            "title": "Phone",
            "mask": "cellphone"
          },
          "title": "Phone",
          "component": "input"
        }
      ],
      "nullable": false,
      "hasDefault": false,
      "meta": {},
      "title": "Contacts",
      "component": "group"
    }
  }
]

Compose the layout yourself

useForm reads the same schema. Each helper returns the props of one component, so you place the fields and ZodField renders a whole group.

Home
{
  "accountRole": "member",
  "interests": [
    "code"
  ],
  "home": {
    "city": "Recife"
  }
}

Overrides and custom render

Change the control in the schema with .meta({ component }), merge props by field path with `fields`, or replace one field with `render`.

Level

Custom render for "consent": read the terms, then tick the box.

When to use Zod Form

Use when

Use Zod Form when a zod schema already describes the data. It removes the second copy of every limit, option list and default that otherwise lives in the markup.

Avoid when

Avoid it for a form that is mostly bespoke markup. Use the useForm helpers on single fields instead.

Accessibility

Keep the Zod Form labeled and use its documented keyboard interaction. The component preserves visible focus and communicates state without relying on color alone.