@loydjs/zod-compat
v1.0.1
Published
Loyd zod-compat — Zod migration tools
Maintainers
Readme
Overview
@loydjs/zod-compat provides bidirectional conversion between Zod and Loyd schemas, plus an automated codemod that migrates your entire codebase in one command. If you have an existing Zod project and want Loyd's performance, this is your migration path.
Installation
npm install @loydjs/zod-compatRequires
@loydjs/core·@loydjs/schema·zod· Node.js ≥ 20 · TypeScript ≥ 5.4
API
fromZod(zodSchema)
Converts a Zod schema to a Loyd schema. Supports all common Zod types.
import { fromZod } from "@loydjs/zod-compat";
import { z } from "zod";
const ZodUser = z.object({
name: z.string().min(2).max(100),
email: z.string().email(),
age: z.number().int().min(0).max(120),
role: z.enum(["admin", "user", "guest"]),
tags: z.array(z.string()),
address: z.object({
street: z.string(),
city: z.string(),
}).optional(),
});
const LoydUser = fromZod(ZodUser);
// Use as a normal Loyd schema
const result = safeParse(LoydUser, req.body);toZod(loydSchema)
Converts a Loyd schema back to a Zod schema. Useful when you need to use a Zod-specific API (e.g. tRPC, OpenAPI generators that only support Zod).
import { toZod } from "@loydjs/zod-compat";
const ZodUser = toZod(LoydUser);
// Use with tRPC, Zod-specific libraries, etc.
const router = t.router({
createUser: t.procedure
.input(ZodUser)
.mutation(({ input }) => db.users.create(input)),
});runCodemod(path, options)
Automated migration — scans your codebase and rewrites Zod imports and schema definitions to Loyd equivalents.
import { runCodemod } from "@loydjs/zod-compat";
await runCodemod("./src", {
write: true, // write changes to disk (default: false = dry run)
verbose: true, // log each transformed file
});
// Output:
// src/schemas/user.ts (3 schemas migrated)
// src/schemas/post.ts (1 schema migrated)
// src/api/validation.ts (5 schemas migrated)
// Migrated 47 files, 312 schemasOr use the CLI directly:
npx loyd-codemod ./src --write
npx loyd-codemod ./src --write --verbose
npx loyd-codemod ./src --dry-run # preview without writingSchema conversion table
| Zod | Loyd |
|:---|:---|
| z.string() | string() |
| z.string().min(n) | string().minLength(n) |
| z.string().max(n) | string().maxLength(n) |
| z.string().email() | string().email() |
| z.string().uuid() | string().uuid() |
| z.string().url() | string().url() |
| z.number() | number() |
| z.number().min(n) | number().min(n) |
| z.number().max(n) | number().max(n) |
| z.number().int() | number().int() |
| z.boolean() | boolean() |
| z.literal(v) | literal(v) |
| z.object({}) | object({}) |
| z.array(s) | array(s) |
| z.union([...]) | union([...]) |
| z.discriminatedUnion(k, [...]) | discriminatedUnion(k, [...]) |
| z.optional(s) | optional(s) |
| z.nullable(s) | nullable(s) |
| z.enum([...]) | union([literal("a"), literal("b"), ...]) |
| z.tuple([...]) | tuple([...]) |
| z.record(s) | record(s) |
| z.map(k, v) | map(k, v) |
| z.set(s) | set(s) |
Dependencies
| Package | Role |
|:---|:---|
| @loydjs/core | LoydSchema base types |
| @loydjs/schema | Target schema constructors |
Peer dependencies
| Package | Version |
|:---|:---|
| zod | ≥ 3.0.0 |
Documentation
License
MIT © b3nito404
