core/src
Schema-driven form generation for Zod v4.
Two paths to forms:
- CLI codegen (recommended): Write a
z2f.config.ts, runnpx zod-to-form generate, get static.tsxcomponents. Zero runtime overhead, hand-readable output. - Runtime: Import
walkSchema()and render dynamically withuseZodForm()in React.
Both paths share the same core: a recursive schema walker that produces FormField[]
from Zod v4's native introspection API.
Remarks
Requires Zod v4 — uses _zod.def, _zod.bag, and z.registry() APIs.
Does NOT work with Zod v3 (which uses _def internals).
Key concepts:
- Processors: Per-type handlers that extract structure from Zod schemas
- Registry:
z.registry<FormMeta>()stores per-schema field config - Optimization: L1 decomposes validation per-field, L2 extracts native HTML rules
- Config presets:
shadcnpreset maps to controlled components with field expressions
Use When
- You have Zod v4 schemas and need forms generated from them
- You want type-safe form generation that stays in sync with your schema
- You need both runtime rendering and static codegen from the same schema
Avoid When
- You're on Zod v3 — this library requires Zod v4's native API
- You need forms for non-Zod schemas (use a form builder instead)
- Your forms have no relationship to your validation schema
Pitfalls
- NEVER use with Zod v3 — accessing
_zod.bagon v3 schemas throws at runtime with cryptic errors - NEVER forget to call
normalizeFormValues()beforeschema.safeParse()— HTML inputs produce empty strings for unset optional fields, which Zod rejects as invalid - NEVER pass a non-object schema as the root to
walkSchema()— it must bez.object({...})at the top level - NEVER mutate
builtinOptimizersdirectly — it's a module-level singleton. UsecreateOptimizers(custom)to extend - NEVER mix
registerDeep()andregisterFlat()on the same schema — registry entries conflict silently
Configuration
Normalization
Optimization
Other
- processors
- FormFieldConstraints
- FormFieldOption
- FormOptimizerContext
- FormProcessorContext
- NativeRules
- ProcessParams
- SchemaLiteCollector
- ValidationStrategy
- WalkResult
- CodegenConfig
- ComponentOverride
- ComponentPreset
- ConfigDefaults
- FieldExpression
- FormOptimizer
- OptimizationConfig
- SchemaLiteInfo
- StripIndexSignature
- TypedFieldConfig
- ZodFormRegistry
- ZodTypeConfig
- builtinOptimizers
- builtinProcessors
- DEFAULT_OVERRIDES
- SHADCN_OVERRIDES
- canonicalizeConfig
- collectFieldSections
- createBaseField
- createProcessors
- createSchemaLiteCollector
- getEmptyDefault
- inferLabel
- joinPath
- normalizeConfig
- normalizeFieldKey
- resolveFieldConfig