CLI Tool
The zod-rs-ts CLI tool generates TypeScript Zod schemas from Rust source files in batch.
Installation
Section titled “Installation”cargo install zod-rs-ts --features cliGenerate from a directory
Section titled “Generate from a directory”zod-rs-ts generate --input src/ --output schemas/This scans all Rust files in src/ for types that derive ZodTs and generates corresponding TypeScript files in the schemas/ directory.
Single-file output
Section titled “Single-file output”zod-rs-ts generate --input src/ --output schemas/index.ts --single-fileCombines all generated schemas into a single TypeScript file.
Workflow
Section titled “Workflow”A typical workflow for shared validation:
- Define your types in Rust with
#[derive(ZodTs)] - Run the CLI to generate TypeScript schemas
- Import the generated schemas in your TypeScript frontend
- Both sides validate with the same rules
use zod_rs_ts::ZodTs;
#[derive(ZodTs)]struct CreateUserRequest { #[zod(min_length(2), max_length(50))] name: String,
#[zod(email)] email: String,}zod-rs-ts generate --input src/models/ --output frontend/src/schemas/import { CreateUserRequestSchema } from './schemas';
const result = CreateUserRequestSchema.safeParse(formData);