|
| 1 | +import type { JSONSchema } from "@trigger.dev/core/v3"; |
| 2 | +import * as z4 from "zod/v4/core"; |
| 3 | +import type { ConversionOptions } from "./index.js"; |
| 4 | + |
| 5 | +type Zod4Converter = { |
| 6 | + version: { major: number; minor: number }; |
| 7 | + toJSONSchema: (schema: any, options: any) => unknown; |
| 8 | +}; |
| 9 | + |
| 10 | +export function convertZod4Schema( |
| 11 | + schema: any, |
| 12 | + options?: ConversionOptions, |
| 13 | + converter: Zod4Converter = z4 |
| 14 | +): JSONSchema | undefined { |
| 15 | + const useReferences = options?.useReferences ?? false; |
| 16 | + const supportsUnrepresentableHandler = |
| 17 | + converter.version.major > 4 || (converter.version.major === 4 && converter.version.minor >= 5); |
| 18 | + |
| 19 | + return converter.toJSONSchema(schema, { |
| 20 | + target: "draft-7", |
| 21 | + io: "output", |
| 22 | + reused: useReferences ? "ref" : "inline", |
| 23 | + unrepresentable: supportsUnrepresentableHandler |
| 24 | + ? ({ zodSchema }: { zodSchema: z4.$ZodTypes }) => |
| 25 | + zodSchema._zod.def.type === "date" ? { type: "string", format: "date-time" } : "throw" |
| 26 | + : "any", |
| 27 | + override: ({ zodSchema, jsonSchema }: { zodSchema: z4.$ZodTypes; jsonSchema: any }) => { |
| 28 | + const def = zodSchema._zod.def; |
| 29 | + |
| 30 | + if (!supportsUnrepresentableHandler) { |
| 31 | + enforceLegacyUnrepresentableTypes(def, jsonSchema); |
| 32 | + } |
| 33 | + |
| 34 | + if (def.type === "date") { |
| 35 | + jsonSchema.type = "string"; |
| 36 | + jsonSchema.format = "date-time"; |
| 37 | + } |
| 38 | + |
| 39 | + if (def.type === "undefined") { |
| 40 | + throw new Error("Undefined cannot be represented in JSON Schema"); |
| 41 | + } |
| 42 | + |
| 43 | + if (def.type === "object" && jsonSchema.required) { |
| 44 | + const required = jsonSchema.required.filter((key: string) => { |
| 45 | + const field = def.shape[key]; |
| 46 | + return !field || field._zod.optout === "optional" || !hasOptionalOutput(field); |
| 47 | + }); |
| 48 | + if (required.length > 0) { |
| 49 | + jsonSchema.required = required; |
| 50 | + } else { |
| 51 | + delete jsonSchema.required; |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + }) as JSONSchema; |
| 56 | +} |
| 57 | + |
| 58 | +function enforceLegacyUnrepresentableTypes(def: any, jsonSchema: Record<string, unknown>) { |
| 59 | + switch (def.type) { |
| 60 | + case "bigint": |
| 61 | + throw new Error("BigInt cannot be represented in JSON Schema"); |
| 62 | + case "symbol": |
| 63 | + throw new Error("Symbols cannot be represented in JSON Schema"); |
| 64 | + case "void": |
| 65 | + throw new Error("Void cannot be represented in JSON Schema"); |
| 66 | + case "map": |
| 67 | + throw new Error("Map cannot be represented in JSON Schema"); |
| 68 | + case "set": |
| 69 | + throw new Error("Set cannot be represented in JSON Schema"); |
| 70 | + case "transform": |
| 71 | + throw new Error("Transforms cannot be represented in JSON Schema"); |
| 72 | + case "nan": |
| 73 | + throw new Error("NaN cannot be represented in JSON Schema"); |
| 74 | + case "custom": |
| 75 | + throw new Error("Custom types cannot be represented in JSON Schema"); |
| 76 | + case "function": |
| 77 | + throw new Error("Function types cannot be represented in JSON Schema"); |
| 78 | + case "literal": { |
| 79 | + if (def.values.some((value: unknown) => value === undefined)) { |
| 80 | + throw new Error("Literal `undefined` cannot be represented in JSON Schema"); |
| 81 | + } |
| 82 | + if (def.values.some((value: unknown) => typeof value === "bigint")) { |
| 83 | + throw new Error("BigInt literals cannot be represented in JSON Schema"); |
| 84 | + } |
| 85 | + return; |
| 86 | + } |
| 87 | + case "catch": { |
| 88 | + if ("default" in jsonSchema) return; |
| 89 | + try { |
| 90 | + def.catchValue(undefined); |
| 91 | + } catch { |
| 92 | + throw new Error("Dynamic catch values are not supported in JSON Schema"); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function hasOptionalOutput(schema: z4.$ZodType, seen = new Set<z4.$ZodType>()): boolean { |
| 99 | + if (schema._zod.optout === "optional") return true; |
| 100 | + if (seen.has(schema)) return false; |
| 101 | + seen.add(schema); |
| 102 | + |
| 103 | + const def = (schema as z4.$ZodTypes)._zod.def; |
| 104 | + switch (def.type) { |
| 105 | + case "union": |
| 106 | + return def.options.some((option) => hasOptionalOutput(option, seen)); |
| 107 | + case "nullable": |
| 108 | + case "readonly": |
| 109 | + case "catch": |
| 110 | + return hasOptionalOutput(def.innerType, seen); |
| 111 | + case "lazy": |
| 112 | + return hasOptionalOutput(def.getter(), seen); |
| 113 | + case "pipe": |
| 114 | + return hasOptionalOutput(def.out, seen); |
| 115 | + default: |
| 116 | + return false; |
| 117 | + } |
| 118 | +} |
0 commit comments