Skip to content

Commit f39a98c

Browse files
downgrade zod to v3 for astro compatibility
1 parent 7228a22 commit f39a98c

File tree

6 files changed

+26
-208
lines changed

6 files changed

+26
-208
lines changed

package-lock.json

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"vite-plugin-dts": "^4.5.4",
7171
"vite-tsconfig-paths": "^5.1.4",
7272
"vitest": "^3.2.4",
73-
"zod": "^4.3.5"
73+
"zod": "^3.23.8"
7474
},
7575
"peerDependencies": {
7676
"zod": "^3.0.0 || ^4.0.0"

tsdown.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ export default defineConfig([
88
index: resolve(import.meta.dirname, "src/index.ts"),
99
urls: resolve(import.meta.dirname, "src/urls.ts"),
1010
"zod-schemas": resolve(import.meta.dirname, "types/zod/generated/schemas.ts"),
11-
"zod-schemas-v4": resolve(import.meta.dirname, "types/zod/generated/schemas-v4.ts"),
1211
},
1312
dts: true,
1413
clean: true,
1514
unbundle: true,
16-
external: ["zod", "zod/v3"],
15+
external: ["zod"],
1716
},
1817
]);

types/zod/fix-schemas.mjs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
/**
2-
* Allows ts-to-zod to generate schemas for both Zod 3 and Zod 4 by:
3-
*
4-
* 1. Fixing enum handling for Zod 3: ts-to-zod generates z.enum(EnumName) but
5-
* Zod 3 requires z.nativeEnum(EnumName) for TypeScript enums.
6-
* 2. Adjusting the import path:
7-
* - schemas.ts imports from "zod/v3" (Zod 3 API)
8-
* - schemas-v4.ts imports from "zod" (Zod 4 API)
2+
* Post-processes schemas generated by ts-to-zod to fix enum handling: ts-to-zod
3+
* generates z.enum(EnumName) but Zod v3 requires z.nativeEnum(EnumName) for
4+
* TypeScript enums.
95
*/
106
import { readFileSync, writeFileSync } from "fs";
117
import { resolve } from "path";
128

13-
const schemasPath = resolve(import.meta.dirname, "generated/schemas.ts");
14-
const schemasV4Path = resolve(import.meta.dirname, "generated/schemas-v4.ts");
15-
16-
const content = readFileSync(schemasPath, "utf-8");
17-
189
const ENUM_PATTERN = /z\.enum\(([A-Z][a-zA-Z0-9]*)\)/g;
1910

20-
const zodV3Content = content
21-
.replace(ENUM_PATTERN, "z.nativeEnum($1)")
22-
.replace(/from ["']zod["']/g, 'from "zod/v3"');
11+
const schemasPath = resolve(import.meta.dirname, "generated/schemas.ts");
12+
const content = readFileSync(schemasPath, "utf-8");
13+
const fixedContent = content.replace(ENUM_PATTERN, "z.nativeEnum($1)");
2314

2415
const matches = content.match(ENUM_PATTERN);
2516
if (matches) {
26-
console.log(`✔ Fixed ${matches.length} enum schemas to use z.nativeEnum() for Zod 3`);
17+
console.log(`✔ Fixed ${matches.length} enum schemas to use z.nativeEnum()`);
2718
}
2819

29-
writeFileSync(schemasPath, zodV3Content);
30-
console.log("✔ Generated types/zod/schemas.ts (Zod 3)");
31-
32-
// Fixes for Zod 4
33-
const zodV4Content = content;
34-
writeFileSync(schemasV4Path, zodV4Content);
35-
console.log("✔ Generated types/zod/schemas-v4.ts (Zod 4)");
20+
writeFileSync(schemasPath, fixedContent);
21+
console.log("✔ Generated types/zod/schemas.ts");

types/zod/generated/schemas-v4.ts

Lines changed: 0 additions & 177 deletions
This file was deleted.

types/zod/generated/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Generated by ts-to-zod
2-
import { z } from "zod/v3";
2+
import { z } from "zod";
33
import { WorkRatings, WorkCategory, WorkWarningStatus, WorkWarnings } from "./../../entities";
44

55
export const archiveIdSchema = z.number();

0 commit comments

Comments
 (0)