@zod-codepen/zod-v4
v1.0.1
Published
Zod v4 adapter for zod-to-code
Maintainers
Readme
[!TIP] @zod-codepen/zod-v4 converts live Zod v4 schema objects into equivalent TypeScript/JavaScript code strings. Supports all Zod v4 variants:
zod,zod/mini,zod/v4,zod/v4/mini,zod/v4/core. Visit our documentation site for complete guides and API reference.
Installation
npm install @zod-codepen/zod-v4Quick Start
import { serialize, generateModule } from '@zod-codepen/zod-v4';
import { z } from 'zod';
// Serialize a single schema
serialize(z.string().email());
// → 'z.string().email()'
// Serialize an object schema
const UserSchema = z.object({
id: z.string().uuid(),
email: z.string().email(),
age: z.number().int().min(0).optional(),
});
serialize(UserSchema);
// → z.object({
// id: z.string().uuid(),
// email: z.string().email(),
// age: z.number().int().nonnegative().optional()
// })
// Generate a complete module
generateModule({
User: UserSchema,
Status: z.enum(['active', 'inactive']),
});
// → import { z } from 'zod';
//
// export const User = z.object({...});
// export const Status = z.enum([...]);API
serialize(schema, options?)
Serialize a single Zod schema to a code string.
serialize(z.string().email());
// → 'z.string().email()'generateModule(schemas, options?)
Generate a complete TypeScript module with named exports.
generateModule({
User: z.object({ name: z.string() }),
});registerHandler(type, handler)
Register a custom handler for a schema type.
registerHandler('string', (schema, ctx) => {
return '/* custom */ z.string()';
});Options
interface SerializeOptions {
indent?: string; // Default: ' '
indentLevel?: number; // Default: 0
format?: boolean; // Default: true
}v4 Variant Support
This package supports all Zod v4 entry points:
| Variant | Import | Description |
|---------|--------|-------------|
| Default | zod | Full API |
| Mini | zod/mini | Lightweight, tree-shakeable |
| v4 Classic | zod/v4 | Full v4 API |
| v4 Mini | zod/v4/mini | v4 lightweight |
| v4 Core | zod/v4/core | Core functionality |
Supported Types
- Primitives: string, number, boolean, bigint, date, undefined, null, void, any, unknown, never, nan, symbol
- Literals & Enums: literal, enum, nativeEnum
- Wrappers: optional, nullable, default, catch, readonly
- Collections: array, object, record, map, set, tuple
- Unions: union, discriminatedUnion, intersection
- Advanced: lazy, promise, function, effects, pipe
v4 Limitations
Some features work differently in v4:
- String transforms (trim, toLowerCase, toUpperCase) may not serialize correctly
refine()detection is limitedbrand()detection is limitedfunction().args()API changed
Requirements
- Node.js ≥ 20
- Zod ^4.0.0
License
MPL-2.0
