Registry / testing / zod-empty

zod-empty

JSON →
library2.1.0jsnpmunverified

Generates minimum default data from Zod schemas, designed for forms and react-hook-form integration. Current stable version is 2.1.0, which requires Zod 4.x and TypeScript 5.x. It provides two functions: `init()` creates empty values (empty string, 0, empty array) and `empty()` creates null defaults. Unlike random data generators, it produces deterministic, minimal data suitable for form initialization. Zod 3 users should use version 1.x. Actively maintained with regular releases.

npm install zod-empty
INSTALL
IMPORT
SIG · ZOD-EMPTY
Z
zod-empty
testingjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

init
import { init } from 'zod-empty'
const init = require('zod-empty').init
ESM-only; no CommonJS support.
empty
import { empty } from 'zod-empty'
import empty from 'zod-empty'
empty is a named export, not default.
z
import { z } from 'zod'
import { z } from 'zod-empty'
z is from 'zod' package, not 'zod-empty'.

Demonstrates using init and empty to generate default values from a Zod schema, and integrating with react-hook-form.

import { z } from 'zod'; import { init, empty } from 'zod-empty'; const schema = z.object({ name: z.string(), age: z.number().min(10), hobbies: z.array(z.string()), }); const defaultValues = init(schema); console.log(defaultValues); // { name: '', age: 0, hobbies: [] } const nullValues = empty(schema); console.log(nullValues); // { name: null, age: null, hobbies: null } // With react-hook-form import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; const App = () => { const { register, handleSubmit } = useForm({ defaultValues: init(schema), resolver: zodResolver(schema), }); return ( <form onSubmit={handleSubmit((d) => console.log(d))}> <input {...register('name')} /> <input type="number" {...register('age', { valueAsNumber: true })} /> <input type="submit" /> </form> ); };
Debug
Known issues
breakingVersion 2.x is not compatible with Zod 3.x. Use zod-empty@1 for Zod 3.
fix
Run `npm install zod-empty@1` for Zod 3.x projects, or upgrade to Zod 4.x.
affects: >=2.0.0
breakingNode.js version requirement is >=20.10.0 for version 2.x.
fix
Upgrade Node.js to >=20.10.0.
affects: >=2.0.0
deprecatedThe default export from 'zod-empty' has been removed; use named exports 'init' and 'empty'.
fix
Replace `import ZodEmpty from 'zod-empty'` with `import { init, empty } from 'zod-empty'`.
affects: >=2.0.0
gotcha`init` outputs 0 for numbers and empty string for strings, which may not validate against min constraints (e.g., `z.number().min(10)` produces 0). This is intended for form defaults, not valid values.
fix
Set your own default values if the generated ones don't meet validation requirements.
affects: >=1.0.0
gotchaThe library does not support all Zod types (e.g., unions, discriminated unions); may throw or produce unexpected results.
fix
Check the supported types in the source or tests before using complex schemas.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'zod-empty' or its corresponding type declarations.
Missing installation or incompatible version (v2 requires Zod 4).
fix
Ensure you have installed zod-empty@2 and zod@4: `npm install zod@4 zod-empty`
Uncaught TypeError: init is not a function
Using default import instead of named import (requires named import).
fix
Change `import init from 'zod-empty'` to `import { init } from 'zod-empty'`
TypeError: Cannot read properties of undefined (reading 'shape')
Passing a non-object Zod schema (e.g., z.string()) directly to init/empty.
fix
Ensure you pass a Zod object schema: `init(z.object({...}))`
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
zodrequiredpeer dependency: zod 4.x is required
typescriptoptionalpeer dependency: typescript 5.x required for type generation
Agent activity
64 hits · last 30 days
node
54
Resources
zod-empty — npm install zod-empty · libregistry