zod 3.26.0-canary.20250703T013930 → 3.26.0-canary.20250703T025502

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (269) hide show
  1. package/package.json +20 -20
  2. package/src/index.ts +4 -0
  3. package/src/v3/ZodError.ts +330 -0
  4. package/src/v3/benchmarks/datetime.ts +58 -0
  5. package/src/v3/benchmarks/discriminatedUnion.ts +80 -0
  6. package/src/v3/benchmarks/index.ts +59 -0
  7. package/src/v3/benchmarks/ipv4.ts +57 -0
  8. package/src/v3/benchmarks/object.ts +69 -0
  9. package/src/v3/benchmarks/primitives.ts +162 -0
  10. package/src/v3/benchmarks/realworld.ts +63 -0
  11. package/src/v3/benchmarks/string.ts +55 -0
  12. package/src/v3/benchmarks/union.ts +80 -0
  13. package/src/v3/errors.ts +13 -0
  14. package/src/v3/external.ts +6 -0
  15. package/src/v3/helpers/enumUtil.ts +17 -0
  16. package/src/v3/helpers/errorUtil.ts +8 -0
  17. package/src/v3/helpers/parseUtil.ts +176 -0
  18. package/src/v3/helpers/partialUtil.ts +34 -0
  19. package/src/v3/helpers/typeAliases.ts +2 -0
  20. package/src/v3/helpers/util.ts +224 -0
  21. package/src/v3/index.ts +4 -0
  22. package/src/v3/locales/en.ts +124 -0
  23. package/src/v3/standard-schema.ts +113 -0
  24. package/src/v3/tests/Mocker.ts +54 -0
  25. package/src/v3/tests/all-errors.test.ts +157 -0
  26. package/src/v3/tests/anyunknown.test.ts +28 -0
  27. package/src/v3/tests/array.test.ts +71 -0
  28. package/src/v3/tests/async-parsing.test.ts +388 -0
  29. package/src/v3/tests/async-refinements.test.ts +46 -0
  30. package/src/v3/tests/base.test.ts +29 -0
  31. package/src/v3/tests/bigint.test.ts +55 -0
  32. package/src/v3/tests/branded.test.ts +53 -0
  33. package/src/v3/tests/catch.test.ts +220 -0
  34. package/src/v3/tests/coerce.test.ts +133 -0
  35. package/src/v3/tests/complex.test.ts +56 -0
  36. package/src/v3/tests/custom.test.ts +31 -0
  37. package/src/v3/tests/date.test.ts +32 -0
  38. package/src/v3/tests/deepmasking.test.ts +186 -0
  39. package/src/v3/tests/default.test.ts +112 -0
  40. package/src/v3/tests/description.test.ts +33 -0
  41. package/src/v3/tests/discriminated-unions.test.ts +315 -0
  42. package/src/v3/tests/enum.test.ts +80 -0
  43. package/src/v3/tests/error.test.ts +551 -0
  44. package/src/v3/tests/firstparty.test.ts +87 -0
  45. package/src/v3/tests/firstpartyschematypes.test.ts +21 -0
  46. package/src/v3/tests/function.test.ts +257 -0
  47. package/src/v3/tests/generics.test.ts +48 -0
  48. package/src/v3/tests/instanceof.test.ts +37 -0
  49. package/src/v3/tests/intersection.test.ts +110 -0
  50. package/src/v3/tests/language-server.source.ts +76 -0
  51. package/src/v3/tests/language-server.test.ts +207 -0
  52. package/src/v3/tests/literal.test.ts +36 -0
  53. package/src/v3/tests/map.test.ts +110 -0
  54. package/src/v3/tests/masking.test.ts +4 -0
  55. package/src/v3/tests/mocker.test.ts +19 -0
  56. package/src/v3/tests/nan.test.ts +21 -0
  57. package/src/v3/tests/nativeEnum.test.ts +87 -0
  58. package/src/v3/tests/nullable.test.ts +42 -0
  59. package/src/v3/tests/number.test.ts +176 -0
  60. package/src/v3/tests/object-augmentation.test.ts +29 -0
  61. package/src/v3/tests/object-in-es5-env.test.ts +29 -0
  62. package/src/v3/tests/object.test.ts +434 -0
  63. package/src/v3/tests/optional.test.ts +42 -0
  64. package/src/v3/tests/parseUtil.test.ts +23 -0
  65. package/src/v3/tests/parser.test.ts +41 -0
  66. package/src/v3/tests/partials.test.ts +243 -0
  67. package/src/v3/tests/pickomit.test.ts +111 -0
  68. package/src/v3/tests/pipeline.test.ts +29 -0
  69. package/src/v3/tests/preprocess.test.ts +186 -0
  70. package/src/v3/tests/primitive.test.ts +440 -0
  71. package/src/v3/tests/promise.test.ts +90 -0
  72. package/src/v3/tests/readonly.test.ts +194 -0
  73. package/src/v3/tests/record.test.ts +171 -0
  74. package/src/v3/tests/recursive.test.ts +197 -0
  75. package/src/v3/tests/refine.test.ts +313 -0
  76. package/src/v3/tests/safeparse.test.ts +27 -0
  77. package/src/v3/tests/set.test.ts +142 -0
  78. package/src/v3/tests/standard-schema.test.ts +83 -0
  79. package/src/v3/tests/string.test.ts +916 -0
  80. package/src/v3/tests/transformer.test.ts +233 -0
  81. package/src/v3/tests/tuple.test.ts +90 -0
  82. package/src/v3/tests/unions.test.ts +57 -0
  83. package/src/v3/tests/validations.test.ts +133 -0
  84. package/src/v3/tests/void.test.ts +15 -0
  85. package/src/v3/types.ts +5136 -0
  86. package/src/v4/classic/checks.ts +30 -0
  87. package/src/v4/classic/coerce.ts +27 -0
  88. package/src/v4/classic/compat.ts +66 -0
  89. package/src/v4/classic/errors.ts +75 -0
  90. package/src/v4/classic/external.ts +50 -0
  91. package/src/v4/classic/index.ts +5 -0
  92. package/src/v4/classic/iso.ts +90 -0
  93. package/src/v4/classic/parse.ts +33 -0
  94. package/src/v4/classic/schemas.ts +2055 -0
  95. package/src/v4/classic/tests/anyunknown.test.ts +26 -0
  96. package/src/v4/classic/tests/array.test.ts +264 -0
  97. package/src/v4/classic/tests/assignability.test.ts +210 -0
  98. package/src/v4/classic/tests/async-parsing.test.ts +381 -0
  99. package/src/v4/classic/tests/async-refinements.test.ts +68 -0
  100. package/src/v4/classic/tests/base.test.ts +7 -0
  101. package/src/v4/classic/tests/bigint.test.ts +54 -0
  102. package/src/v4/classic/tests/brand.test.ts +65 -0
  103. package/src/v4/classic/tests/catch.test.ts +252 -0
  104. package/src/v4/classic/tests/coalesce.test.ts +20 -0
  105. package/src/v4/classic/tests/coerce.test.ts +160 -0
  106. package/src/v4/classic/tests/continuability.test.ts +352 -0
  107. package/src/v4/classic/tests/custom.test.ts +40 -0
  108. package/src/v4/classic/tests/date.test.ts +31 -0
  109. package/src/v4/classic/tests/datetime.test.ts +296 -0
  110. package/src/v4/classic/tests/default.test.ts +313 -0
  111. package/src/v4/classic/tests/description.test.ts +32 -0
  112. package/src/v4/classic/tests/discriminated-unions.test.ts +592 -0
  113. package/src/v4/classic/tests/enum.test.ts +285 -0
  114. package/src/v4/classic/tests/error-utils.test.ts +527 -0
  115. package/src/v4/classic/tests/error.test.ts +711 -0
  116. package/src/v4/classic/tests/file.test.ts +91 -0
  117. package/src/v4/classic/tests/firstparty.test.ts +175 -0
  118. package/src/v4/classic/tests/function.test.ts +268 -0
  119. package/src/v4/classic/tests/generics.test.ts +72 -0
  120. package/src/v4/classic/tests/index.test.ts +829 -0
  121. package/src/v4/classic/tests/instanceof.test.ts +34 -0
  122. package/src/v4/classic/tests/intersection.test.ts +171 -0
  123. package/src/v4/classic/tests/json.test.ts +108 -0
  124. package/src/v4/classic/tests/lazy.test.ts +227 -0
  125. package/src/v4/classic/tests/literal.test.ts +92 -0
  126. package/src/v4/classic/tests/map.test.ts +196 -0
  127. package/src/v4/classic/tests/nan.test.ts +21 -0
  128. package/src/v4/classic/tests/nested-refine.test.ts +168 -0
  129. package/src/v4/classic/tests/nonoptional.test.ts +86 -0
  130. package/src/v4/classic/tests/nullable.test.ts +22 -0
  131. package/src/v4/classic/tests/number.test.ts +247 -0
  132. package/src/v4/classic/tests/object.test.ts +553 -0
  133. package/src/v4/classic/tests/optional.test.ts +103 -0
  134. package/src/v4/classic/tests/partial.test.ts +147 -0
  135. package/src/v4/classic/tests/pickomit.test.ts +127 -0
  136. package/src/v4/classic/tests/pipe.test.ts +81 -0
  137. package/src/v4/classic/tests/prefault.test.ts +37 -0
  138. package/src/v4/classic/tests/preprocess.test.ts +298 -0
  139. package/src/v4/classic/tests/primitive.test.ts +175 -0
  140. package/src/v4/classic/tests/promise.test.ts +81 -0
  141. package/src/v4/classic/tests/prototypes.test.ts +23 -0
  142. package/src/v4/classic/tests/readonly.test.ts +252 -0
  143. package/src/v4/classic/tests/record.test.ts +332 -0
  144. package/src/v4/classic/tests/recursive-types.test.ts +325 -0
  145. package/src/v4/classic/tests/refine.test.ts +423 -0
  146. package/src/v4/classic/tests/registries.test.ts +195 -0
  147. package/src/v4/classic/tests/set.test.ts +179 -0
  148. package/src/v4/classic/tests/standard-schema.test.ts +57 -0
  149. package/src/v4/classic/tests/string-formats.test.ts +109 -0
  150. package/src/v4/classic/tests/string.test.ts +881 -0
  151. package/src/v4/classic/tests/stringbool.test.ts +66 -0
  152. package/src/v4/classic/tests/template-literal.test.ts +758 -0
  153. package/src/v4/classic/tests/to-json-schema.test.ts +2182 -0
  154. package/src/v4/classic/tests/transform.test.ts +250 -0
  155. package/src/v4/classic/tests/tuple.test.ts +163 -0
  156. package/src/v4/classic/tests/union.test.ts +94 -0
  157. package/src/v4/classic/tests/validations.test.ts +283 -0
  158. package/src/v4/classic/tests/void.test.ts +12 -0
  159. package/src/v4/core/api.ts +1592 -0
  160. package/src/v4/core/checks.ts +1285 -0
  161. package/src/v4/core/config.ts +15 -0
  162. package/src/v4/core/core.ts +134 -0
  163. package/src/v4/core/doc.ts +44 -0
  164. package/src/v4/core/errors.ts +420 -0
  165. package/src/v4/core/function.ts +176 -0
  166. package/src/v4/core/index.ts +15 -0
  167. package/src/v4/core/json-schema.ts +143 -0
  168. package/src/v4/core/parse.ts +94 -0
  169. package/src/v4/core/regexes.ts +135 -0
  170. package/src/v4/core/registries.ts +86 -0
  171. package/src/v4/core/schemas.ts +3781 -0
  172. package/src/v4/core/standard-schema.ts +64 -0
  173. package/src/v4/core/tests/index.test.ts +46 -0
  174. package/src/v4/core/tests/locales/be.test.ts +124 -0
  175. package/src/v4/core/tests/locales/en.test.ts +22 -0
  176. package/src/v4/core/tests/locales/ru.test.ts +128 -0
  177. package/src/v4/core/tests/locales/tr.test.ts +69 -0
  178. package/src/v4/core/to-json-schema.ts +944 -0
  179. package/src/v4/core/util.ts +775 -0
  180. package/src/v4/core/versions.ts +5 -0
  181. package/src/v4/core/zsf.ts +323 -0
  182. package/src/v4/index.ts +4 -0
  183. package/src/v4/locales/ar.ts +125 -0
  184. package/src/v4/locales/az.ts +121 -0
  185. package/src/v4/locales/be.ts +184 -0
  186. package/src/v4/locales/ca.ts +127 -0
  187. package/src/v4/locales/cs.ts +142 -0
  188. package/src/v4/locales/de.ts +124 -0
  189. package/src/v4/locales/en.ts +127 -0
  190. package/src/v4/locales/es.ts +125 -0
  191. package/src/v4/locales/fa.ts +134 -0
  192. package/src/v4/locales/fi.ts +131 -0
  193. package/src/v4/locales/fr-CA.ts +126 -0
  194. package/src/v4/locales/fr.ts +124 -0
  195. package/src/v4/locales/he.ts +125 -0
  196. package/src/v4/locales/hu.ts +126 -0
  197. package/src/v4/locales/id.ts +125 -0
  198. package/src/v4/locales/index.ts +38 -0
  199. package/src/v4/locales/it.ts +125 -0
  200. package/src/v4/locales/ja.ts +122 -0
  201. package/src/v4/locales/kh.ts +126 -0
  202. package/src/v4/locales/ko.ts +131 -0
  203. package/src/v4/locales/mk.ts +127 -0
  204. package/src/v4/locales/ms.ts +124 -0
  205. package/src/v4/locales/nl.ts +126 -0
  206. package/src/v4/locales/no.ts +124 -0
  207. package/src/v4/locales/ota.ts +125 -0
  208. package/src/v4/locales/pl.ts +126 -0
  209. package/src/v4/locales/ps.ts +133 -0
  210. package/src/v4/locales/pt.ts +123 -0
  211. package/src/v4/locales/ru.ts +184 -0
  212. package/src/v4/locales/sl.ts +126 -0
  213. package/src/v4/locales/sv.ts +127 -0
  214. package/src/v4/locales/ta.ts +125 -0
  215. package/src/v4/locales/th.ts +126 -0
  216. package/src/v4/locales/tr.ts +121 -0
  217. package/src/v4/locales/ua.ts +126 -0
  218. package/src/v4/locales/ur.ts +126 -0
  219. package/src/v4/locales/vi.ts +125 -0
  220. package/src/v4/locales/zh-CN.ts +123 -0
  221. package/src/v4/locales/zh-TW.ts +125 -0
  222. package/src/v4/mini/checks.ts +32 -0
  223. package/src/v4/mini/coerce.ts +22 -0
  224. package/src/v4/mini/external.ts +40 -0
  225. package/src/v4/mini/index.ts +3 -0
  226. package/src/v4/mini/iso.ts +62 -0
  227. package/src/v4/mini/parse.ts +1 -0
  228. package/src/v4/mini/schemas.ts +1579 -0
  229. package/src/v4/mini/tests/assignability.test.ts +129 -0
  230. package/src/v4/mini/tests/brand.test.ts +51 -0
  231. package/src/v4/mini/tests/checks.test.ts +144 -0
  232. package/src/v4/mini/tests/computed.test.ts +36 -0
  233. package/src/v4/mini/tests/error.test.ts +22 -0
  234. package/src/v4/mini/tests/functions.test.ts +43 -0
  235. package/src/v4/mini/tests/index.test.ts +871 -0
  236. package/src/v4/mini/tests/number.test.ts +95 -0
  237. package/src/v4/mini/tests/object.test.ts +185 -0
  238. package/src/v4/mini/tests/prototypes.test.ts +43 -0
  239. package/src/v4/mini/tests/recursive-types.test.ts +275 -0
  240. package/src/v4/mini/tests/string.test.ts +293 -0
  241. package/src/v4-mini/index.ts +1 -0
  242. package/v4/classic/compat.cjs +1 -7
  243. package/v4/classic/compat.d.cts +0 -2
  244. package/v4/classic/compat.d.ts +0 -2
  245. package/v4/classic/compat.js +0 -6
  246. package/v4/classic/external.cjs +2 -1
  247. package/v4/classic/external.d.cts +1 -1
  248. package/v4/classic/external.d.ts +1 -1
  249. package/v4/classic/external.js +1 -1
  250. package/v4/classic/schemas.d.cts +4 -4
  251. package/v4/classic/schemas.d.ts +4 -4
  252. package/v4/core/api.cjs +11 -10
  253. package/v4/core/api.js +11 -10
  254. package/v4/core/checks.cjs +6 -4
  255. package/v4/core/checks.js +6 -4
  256. package/v4/core/core.cjs +5 -1
  257. package/v4/core/core.d.cts +2 -0
  258. package/v4/core/core.d.ts +2 -0
  259. package/v4/core/core.js +4 -0
  260. package/v4/core/schemas.cjs +12 -13
  261. package/v4/core/schemas.js +12 -13
  262. package/v4/core/util.cjs +3 -0
  263. package/v4/core/util.d.cts +1 -1
  264. package/v4/core/util.d.ts +1 -1
  265. package/v4/core/util.js +3 -0
  266. package/v4/mini/external.cjs +2 -1
  267. package/v4/mini/external.d.cts +1 -1
  268. package/v4/mini/external.d.ts +1 -1
  269. package/v4/mini/external.js +1 -1
@@ -0,0 +1,944 @@
1
+ import type * as checks from "./checks.js";
2
+ import type * as JSONSchema from "./json-schema.js";
3
+ import { $ZodRegistry, globalRegistry } from "./registries.js";
4
+ import type * as schemas from "./schemas.js";
5
+ import { getEnumValues } from "./util.js";
6
+
7
+ interface JSONSchemaGeneratorParams {
8
+ /** A registry used to look up metadata for each schema. Any schema with an `id` property will be extracted as a $def.
9
+ * @default globalRegistry */
10
+ metadata?: $ZodRegistry<Record<string, any>>;
11
+ /** The JSON Schema version to target.
12
+ * - `"draft-2020-12"` — Default. JSON Schema Draft 2020-12
13
+ * - `"draft-7"` — JSON Schema Draft 7 */
14
+ target?: "draft-7" | "draft-2020-12";
15
+ /** How to handle unrepresentable types.
16
+ * - `"throw"` — Default. Unrepresentable types throw an error
17
+ * - `"any"` — Unrepresentable types become `{}` */
18
+ unrepresentable?: "throw" | "any";
19
+ /** Arbitrary custom logic that can be used to modify the generated JSON Schema. */
20
+ override?: (ctx: { zodSchema: schemas.$ZodTypes; jsonSchema: JSONSchema.BaseSchema }) => void;
21
+ /** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, Error converting schema to JSONz, defaults, coerced primitives, etc.
22
+ * - `"output" — Default. Convert the output schema.
23
+ * - `"input"` — Convert the input schema. */
24
+ io?: "input" | "output";
25
+ }
26
+
27
+ interface ProcessParams {
28
+ schemaPath: schemas.$ZodType[];
29
+ path: (string | number)[];
30
+ }
31
+
32
+ interface EmitParams {
33
+ /** How to handle cycles.
34
+ * - `"ref"` — Default. Cycles will be broken using $defs
35
+ * - `"throw"` — Cycles will throw an error if encountered */
36
+ cycles?: "ref" | "throw";
37
+ /* How to handle reused schemas.
38
+ * - `"inline"` — Default. Reused schemas will be inlined
39
+ * - `"ref"` — Reused schemas will be extracted as $defs */
40
+ reused?: "ref" | "inline";
41
+
42
+ external?:
43
+ | {
44
+ /** */
45
+ registry: $ZodRegistry<{ id?: string | undefined }>;
46
+ uri: (id: string) => string;
47
+ defs: Record<string, JSONSchema.BaseSchema>;
48
+ }
49
+ | undefined;
50
+ }
51
+
52
+ interface Seen {
53
+ /** JSON Schema result for this Zod schema */
54
+ schema: JSONSchema.BaseSchema;
55
+ /** A cached version of the schema that doesn't get overwritten during ref resolution */
56
+ def?: JSONSchema.BaseSchema;
57
+ defId?: string | undefined;
58
+ /** Number of times this schema was encountered during traversal */
59
+ count: number;
60
+ /** Cycle path */
61
+ cycle?: (string | number)[] | undefined;
62
+ isParent?: boolean | undefined;
63
+ ref?: schemas.$ZodType | undefined | null;
64
+ }
65
+
66
+ export class JSONSchemaGenerator {
67
+ metadataRegistry: $ZodRegistry<Record<string, any>>;
68
+ target: "draft-7" | "draft-2020-12";
69
+ unrepresentable: "throw" | "any";
70
+ override: (ctx: { zodSchema: schemas.$ZodTypes; jsonSchema: JSONSchema.BaseSchema }) => void;
71
+ io: "input" | "output";
72
+
73
+ counter = 0;
74
+ seen: Map<schemas.$ZodType, Seen>;
75
+
76
+ constructor(params?: JSONSchemaGeneratorParams) {
77
+ this.metadataRegistry = params?.metadata ?? globalRegistry;
78
+ this.target = params?.target ?? "draft-2020-12";
79
+ this.unrepresentable = params?.unrepresentable ?? "throw";
80
+ this.override = params?.override ?? (() => {});
81
+ this.io = params?.io ?? "output";
82
+
83
+ this.seen = new Map();
84
+ }
85
+
86
+ process(schema: schemas.$ZodType, _params: ProcessParams = { path: [], schemaPath: [] }): JSONSchema.BaseSchema {
87
+ const def = (schema as schemas.$ZodTypes)._zod.def;
88
+
89
+ const formatMap: Partial<Record<checks.$ZodStringFormats, string | undefined>> = {
90
+ guid: "uuid",
91
+ url: "uri",
92
+ datetime: "date-time",
93
+ json_string: "json-string",
94
+ regex: "", // do not set
95
+ };
96
+
97
+ // check for schema in seens
98
+ const seen = this.seen.get(schema);
99
+
100
+ if (seen) {
101
+ seen.count++;
102
+
103
+ // check if cycle
104
+ const isCycle = _params.schemaPath.includes(schema);
105
+ if (isCycle) {
106
+ seen.cycle = _params.path;
107
+ }
108
+
109
+ return seen.schema;
110
+ }
111
+
112
+ // initialize
113
+ const result: Seen = { schema: {}, count: 1, cycle: undefined };
114
+ this.seen.set(schema, result);
115
+
116
+ // custom method overrides default behavior
117
+ const overrideSchema = schema._zod.toJSONSchema?.();
118
+ if (overrideSchema) {
119
+ result.schema = overrideSchema as any;
120
+ } else {
121
+ const params = {
122
+ ..._params,
123
+ schemaPath: [..._params.schemaPath, schema],
124
+ path: _params.path,
125
+ };
126
+
127
+ const parent = schema._zod.parent;
128
+
129
+ if (parent) {
130
+ // schema was cloned from another schema
131
+ result.ref = parent;
132
+ this.process(parent, params);
133
+ this.seen.get(parent)!.isParent = true;
134
+ } else {
135
+ const _json = result.schema;
136
+ switch (def.type) {
137
+ case "string": {
138
+ const json: JSONSchema.StringSchema = _json as any;
139
+ json.type = "string";
140
+ const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
141
+ .bag as schemas.$ZodStringInternals<unknown>["bag"];
142
+ if (typeof minimum === "number") json.minLength = minimum;
143
+ if (typeof maximum === "number") json.maxLength = maximum;
144
+ // custom pattern overrides format
145
+ if (format) {
146
+ json.format = formatMap[format as checks.$ZodStringFormats] ?? format;
147
+ if (json.format === "") delete json.format; // empty format is not valid
148
+ }
149
+ if (contentEncoding) json.contentEncoding = contentEncoding;
150
+ if (patterns && patterns.size > 0) {
151
+ const regexes = [...patterns];
152
+ if (regexes.length === 1) json.pattern = regexes[0]!.source;
153
+ else if (regexes.length > 1) {
154
+ result.schema.allOf = [
155
+ ...regexes.map((regex) => ({
156
+ ...(this.target === "draft-7" ? ({ type: "string" } as const) : {}),
157
+ pattern: regex.source,
158
+ })),
159
+ ];
160
+ }
161
+ }
162
+
163
+ break;
164
+ }
165
+ case "number": {
166
+ const json: JSONSchema.NumberSchema | JSONSchema.IntegerSchema = _json as any;
167
+ const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
168
+ if (typeof format === "string" && format.includes("int")) json.type = "integer";
169
+ else json.type = "number";
170
+
171
+ if (typeof exclusiveMinimum === "number") json.exclusiveMinimum = exclusiveMinimum;
172
+ if (typeof minimum === "number") {
173
+ json.minimum = minimum;
174
+ if (typeof exclusiveMinimum === "number") {
175
+ if (exclusiveMinimum >= minimum) delete json.minimum;
176
+ else delete json.exclusiveMinimum;
177
+ }
178
+ }
179
+
180
+ if (typeof exclusiveMaximum === "number") json.exclusiveMaximum = exclusiveMaximum;
181
+ if (typeof maximum === "number") {
182
+ json.maximum = maximum;
183
+ if (typeof exclusiveMaximum === "number") {
184
+ if (exclusiveMaximum <= maximum) delete json.maximum;
185
+ else delete json.exclusiveMaximum;
186
+ }
187
+ }
188
+
189
+ if (typeof multipleOf === "number") json.multipleOf = multipleOf;
190
+
191
+ break;
192
+ }
193
+ case "boolean": {
194
+ const json = _json as JSONSchema.BooleanSchema;
195
+ json.type = "boolean";
196
+ break;
197
+ }
198
+ case "bigint": {
199
+ if (this.unrepresentable === "throw") {
200
+ throw new Error("BigInt cannot be represented in JSON Schema");
201
+ }
202
+ break;
203
+ }
204
+ case "symbol": {
205
+ if (this.unrepresentable === "throw") {
206
+ throw new Error("Symbols cannot be represented in JSON Schema");
207
+ }
208
+ break;
209
+ }
210
+ case "undefined": {
211
+ const json = _json as JSONSchema.NullSchema;
212
+ json.type = "null";
213
+ break;
214
+ }
215
+ case "null": {
216
+ _json.type = "null";
217
+ break;
218
+ }
219
+ case "any": {
220
+ break;
221
+ }
222
+ case "unknown": {
223
+ break;
224
+ }
225
+ case "never": {
226
+ _json.not = {};
227
+ break;
228
+ }
229
+ case "void": {
230
+ if (this.unrepresentable === "throw") {
231
+ throw new Error("Void cannot be represented in JSON Schema");
232
+ }
233
+ break;
234
+ }
235
+ case "date": {
236
+ if (this.unrepresentable === "throw") {
237
+ throw new Error("Date cannot be represented in JSON Schema");
238
+ }
239
+ break;
240
+ }
241
+ case "array": {
242
+ const json: JSONSchema.ArraySchema = _json as any;
243
+ const { minimum, maximum } = schema._zod.bag;
244
+ if (typeof minimum === "number") json.minItems = minimum;
245
+ if (typeof maximum === "number") json.maxItems = maximum;
246
+
247
+ json.type = "array";
248
+ json.items = this.process(def.element, { ...params, path: [...params.path, "items"] });
249
+ break;
250
+ }
251
+ case "object": {
252
+ const json: JSONSchema.ObjectSchema = _json as any;
253
+ json.type = "object";
254
+ json.properties = {};
255
+ const shape = def.shape; // params.shapeCache.get(schema)!;
256
+
257
+ for (const key in shape) {
258
+ json.properties[key] = this.process(shape[key]!, {
259
+ ...params,
260
+ path: [...params.path, "properties", key],
261
+ });
262
+ }
263
+
264
+ // required keys
265
+ const allKeys = new Set(Object.keys(shape));
266
+ // const optionalKeys = new Set(def.optional);
267
+ const requiredKeys = new Set(
268
+ [...allKeys].filter((key) => {
269
+ const v = def.shape[key]!._zod;
270
+ if (this.io === "input") {
271
+ return v.optin === undefined;
272
+ } else {
273
+ return v.optout === undefined;
274
+ }
275
+ })
276
+ );
277
+
278
+ if (requiredKeys.size > 0) {
279
+ json.required = Array.from(requiredKeys);
280
+ }
281
+
282
+ // catchall
283
+ if (def.catchall?._zod.def.type === "never") {
284
+ // strict
285
+ json.additionalProperties = false;
286
+ } else if (!def.catchall) {
287
+ // regular
288
+ if (this.io === "output") json.additionalProperties = false;
289
+ } else if (def.catchall) {
290
+ json.additionalProperties = this.process(def.catchall, {
291
+ ...params,
292
+ path: [...params.path, "additionalProperties"],
293
+ });
294
+ }
295
+
296
+ break;
297
+ }
298
+ case "union": {
299
+ const json: JSONSchema.BaseSchema = _json as any;
300
+ json.anyOf = def.options.map((x, i) =>
301
+ this.process(x, {
302
+ ...params,
303
+ path: [...params.path, "anyOf", i],
304
+ })
305
+ );
306
+ break;
307
+ }
308
+ case "intersection": {
309
+ const json: JSONSchema.BaseSchema = _json as any;
310
+ const a = this.process(def.left, {
311
+ ...params,
312
+ path: [...params.path, "allOf", 0],
313
+ });
314
+ const b = this.process(def.right, {
315
+ ...params,
316
+ path: [...params.path, "allOf", 1],
317
+ });
318
+
319
+ const isSimpleIntersection = (val: any) => "allOf" in val && Object.keys(val).length === 1;
320
+ const allOf = [
321
+ ...(isSimpleIntersection(a) ? (a.allOf as any[]) : [a]),
322
+ ...(isSimpleIntersection(b) ? (b.allOf as any[]) : [b]),
323
+ ];
324
+ json.allOf = allOf;
325
+ break;
326
+ }
327
+ case "tuple": {
328
+ const json: JSONSchema.ArraySchema = _json as any;
329
+ json.type = "array";
330
+ const prefixItems = def.items.map((x, i) =>
331
+ this.process(x, { ...params, path: [...params.path, "prefixItems", i] })
332
+ );
333
+ if (this.target === "draft-2020-12") {
334
+ json.prefixItems = prefixItems;
335
+ } else {
336
+ json.items = prefixItems;
337
+ }
338
+
339
+ if (def.rest) {
340
+ const rest = this.process(def.rest, {
341
+ ...params,
342
+ path: [...params.path, "items"],
343
+ });
344
+ if (this.target === "draft-2020-12") {
345
+ json.items = rest;
346
+ } else {
347
+ json.additionalItems = rest;
348
+ }
349
+ }
350
+
351
+ // additionalItems
352
+ if (def.rest) {
353
+ json.items = this.process(def.rest, {
354
+ ...params,
355
+ path: [...params.path, "items"],
356
+ });
357
+ }
358
+
359
+ // length
360
+ const { minimum, maximum } = schema._zod.bag as {
361
+ minimum?: number;
362
+ maximum?: number;
363
+ };
364
+ if (typeof minimum === "number") json.minItems = minimum;
365
+ if (typeof maximum === "number") json.maxItems = maximum;
366
+ break;
367
+ }
368
+ case "record": {
369
+ const json: JSONSchema.ObjectSchema = _json as any;
370
+ json.type = "object";
371
+ json.propertyNames = this.process(def.keyType, { ...params, path: [...params.path, "propertyNames"] });
372
+ json.additionalProperties = this.process(def.valueType, {
373
+ ...params,
374
+ path: [...params.path, "additionalProperties"],
375
+ });
376
+ break;
377
+ }
378
+ case "map": {
379
+ if (this.unrepresentable === "throw") {
380
+ throw new Error("Map cannot be represented in JSON Schema");
381
+ }
382
+ break;
383
+ }
384
+ case "set": {
385
+ if (this.unrepresentable === "throw") {
386
+ throw new Error("Set cannot be represented in JSON Schema");
387
+ }
388
+ break;
389
+ }
390
+ case "enum": {
391
+ const json: JSONSchema.BaseSchema = _json as any;
392
+ const values = getEnumValues(def.entries);
393
+ // Number enums can have both string and number values
394
+ if (values.every((v) => typeof v === "number")) json.type = "number";
395
+ if (values.every((v) => typeof v === "string")) json.type = "string";
396
+ json.enum = values;
397
+ break;
398
+ }
399
+ case "literal": {
400
+ const json: JSONSchema.BaseSchema = _json as any;
401
+ const vals: (string | number | boolean | null)[] = [];
402
+ for (const val of def.values) {
403
+ if (val === undefined) {
404
+ if (this.unrepresentable === "throw") {
405
+ throw new Error("Literal `undefined` cannot be represented in JSON Schema");
406
+ } else {
407
+ // do not add to vals
408
+ }
409
+ } else if (typeof val === "bigint") {
410
+ if (this.unrepresentable === "throw") {
411
+ throw new Error("BigInt literals cannot be represented in JSON Schema");
412
+ } else {
413
+ vals.push(Number(val));
414
+ }
415
+ } else {
416
+ vals.push(val);
417
+ }
418
+ }
419
+ if (vals.length === 0) {
420
+ // do nothing (an undefined literal was stripped)
421
+ } else if (vals.length === 1) {
422
+ const val = vals[0]!;
423
+ json.type = val === null ? ("null" as const) : (typeof val as any);
424
+ json.const = val;
425
+ } else {
426
+ if (vals.every((v) => typeof v === "number")) json.type = "number";
427
+ if (vals.every((v) => typeof v === "string")) json.type = "string";
428
+ if (vals.every((v) => typeof v === "boolean")) json.type = "string";
429
+ if (vals.every((v) => v === null)) json.type = "null";
430
+ json.enum = vals;
431
+ }
432
+ break;
433
+ }
434
+
435
+ case "file": {
436
+ const json: JSONSchema.StringSchema = _json as any;
437
+ const file: JSONSchema.StringSchema = {
438
+ type: "string",
439
+ format: "binary",
440
+ contentEncoding: "binary",
441
+ };
442
+
443
+ const { minimum, maximum, mime } = schema._zod.bag as schemas.$ZodFileInternals["bag"];
444
+ if (minimum !== undefined) file.minLength = minimum;
445
+ if (maximum !== undefined) file.maxLength = maximum;
446
+ if (mime) {
447
+ if (mime.length === 1) {
448
+ file.contentMediaType = mime[0]!;
449
+ Object.assign(json, file);
450
+ } else {
451
+ json.anyOf = mime.map((m) => {
452
+ const mFile: JSONSchema.StringSchema = { ...file, contentMediaType: m };
453
+ return mFile;
454
+ });
455
+ }
456
+ } else {
457
+ Object.assign(json, file);
458
+ }
459
+
460
+ // if (this.unrepresentable === "throw") {
461
+ // throw new Error("File cannot be represented in JSON Schema");
462
+ // }
463
+ break;
464
+ }
465
+ case "transform": {
466
+ if (this.unrepresentable === "throw") {
467
+ throw new Error("Transforms cannot be represented in JSON Schema");
468
+ }
469
+ break;
470
+ }
471
+
472
+ case "nullable": {
473
+ const inner = this.process(def.innerType, params);
474
+ _json.anyOf = [inner, { type: "null" }];
475
+ break;
476
+ }
477
+ case "nonoptional": {
478
+ this.process(def.innerType, params);
479
+ result.ref = def.innerType;
480
+ break;
481
+ }
482
+ case "success": {
483
+ const json = _json as JSONSchema.BooleanSchema;
484
+ json.type = "boolean";
485
+ break;
486
+ }
487
+ case "default": {
488
+ this.process(def.innerType, params);
489
+ result.ref = def.innerType;
490
+ _json.default = JSON.parse(JSON.stringify(def.defaultValue));
491
+ break;
492
+ }
493
+ case "prefault": {
494
+ this.process(def.innerType, params);
495
+ result.ref = def.innerType;
496
+ if (this.io === "input") _json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
497
+
498
+ break;
499
+ }
500
+ case "catch": {
501
+ // use conditionals
502
+ this.process(def.innerType, params);
503
+ result.ref = def.innerType;
504
+ let catchValue: any;
505
+ try {
506
+ catchValue = def.catchValue(undefined as any);
507
+ } catch {
508
+ throw new Error("Dynamic catch values are not supported in JSON Schema");
509
+ }
510
+ _json.default = catchValue;
511
+ break;
512
+ }
513
+ case "nan": {
514
+ if (this.unrepresentable === "throw") {
515
+ throw new Error("NaN cannot be represented in JSON Schema");
516
+ }
517
+ break;
518
+ }
519
+ case "template_literal": {
520
+ const json = _json as JSONSchema.StringSchema;
521
+ const pattern = schema._zod.pattern;
522
+ if (!pattern) throw new Error("Pattern not found in template literal");
523
+ json.type = "string";
524
+ json.pattern = pattern.source;
525
+ break;
526
+ }
527
+ case "pipe": {
528
+ const innerType = this.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
529
+ this.process(innerType, params);
530
+ result.ref = innerType;
531
+ break;
532
+ }
533
+ case "readonly": {
534
+ this.process(def.innerType, params);
535
+ result.ref = def.innerType;
536
+ _json.readOnly = true;
537
+ break;
538
+ }
539
+ // passthrough types
540
+ case "promise": {
541
+ this.process(def.innerType, params);
542
+ result.ref = def.innerType;
543
+ break;
544
+ }
545
+ case "optional": {
546
+ this.process(def.innerType, params);
547
+ result.ref = def.innerType;
548
+ break;
549
+ }
550
+ case "lazy": {
551
+ const innerType = (schema as schemas.$ZodLazy)._zod.innerType;
552
+ this.process(innerType, params);
553
+ result.ref = innerType;
554
+ break;
555
+ }
556
+ case "custom": {
557
+ if (this.unrepresentable === "throw") {
558
+ throw new Error("Custom types cannot be represented in JSON Schema");
559
+ }
560
+ break;
561
+ }
562
+ default: {
563
+ def satisfies never;
564
+ }
565
+ }
566
+ }
567
+ }
568
+
569
+ // metadata
570
+ const meta = this.metadataRegistry.get(schema);
571
+ if (meta) Object.assign(result.schema, meta);
572
+
573
+ if (this.io === "input" && isTransforming(schema)) {
574
+ // examples/defaults only apply to output type of pipe
575
+ delete result.schema.examples;
576
+ delete result.schema.default;
577
+ }
578
+
579
+ // set prefault as default
580
+ if (this.io === "input" && result.schema._prefault) result.schema.default ??= result.schema._prefault;
581
+ delete result.schema._prefault;
582
+
583
+ // pulling fresh from this.seen in case it was overwritten
584
+ const _result = this.seen.get(schema)!;
585
+
586
+ return _result.schema;
587
+ }
588
+
589
+ emit(schema: schemas.$ZodType, _params?: EmitParams): JSONSchema.BaseSchema {
590
+ const params = {
591
+ cycles: _params?.cycles ?? "ref",
592
+ reused: _params?.reused ?? "inline",
593
+ // unrepresentable: _params?.unrepresentable ?? "throw",
594
+ // uri: _params?.uri ?? ((id) => `${id}`),
595
+ external: _params?.external ?? undefined,
596
+ } satisfies EmitParams;
597
+
598
+ // iterate over seen map;
599
+ const root = this.seen.get(schema);
600
+
601
+ if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
602
+
603
+ // initialize result with root schema fields
604
+ // Object.assign(result, seen.cached);
605
+
606
+ const makeURI = (entry: [schemas.$ZodType<unknown, unknown>, Seen]): { ref: string; defId?: string } => {
607
+ // comparing the seen objects because sometimes
608
+ // multiple schemas map to the same seen object.
609
+ // e.g. lazy
610
+
611
+ // external is configured
612
+ const defsSegment = this.target === "draft-2020-12" ? "$defs" : "definitions";
613
+ if (params.external) {
614
+ const externalId = params.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${this.counter++}`;
615
+
616
+ // check if schema is in the external registry
617
+ if (externalId) return { ref: params.external.uri(externalId) };
618
+
619
+ // otherwise, add to __shared
620
+ const id: string = entry[1].defId ?? (entry[1].schema.id as string) ?? `schema${this.counter++}`;
621
+ entry[1].defId = id;
622
+ return { defId: id, ref: `${params.external.uri("__shared")}#/${defsSegment}/${id}` };
623
+ }
624
+
625
+ if (entry[1] === root) {
626
+ return { ref: "#" };
627
+ }
628
+
629
+ // self-contained schema
630
+ const uriPrefix = `#`;
631
+ const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
632
+ const defId = entry[1].schema.id ?? `__schema${this.counter++}`;
633
+ return { defId, ref: defUriPrefix + defId };
634
+ };
635
+
636
+ // stored cached version in `def` property
637
+ // remove all properties, set $ref
638
+ const extractToDef = (entry: [schemas.$ZodType<unknown, unknown>, Seen]): void => {
639
+ if (entry[1].schema.$ref) {
640
+ return;
641
+ }
642
+ const seen = entry[1];
643
+ const { ref, defId } = makeURI(entry);
644
+
645
+ seen.def = { ...seen.schema };
646
+ // defId won't be set if the schema is a reference to an external schema
647
+ if (defId) seen.defId = defId;
648
+ // wipe away all properties except $ref
649
+ const schema = seen.schema;
650
+ for (const key in schema) {
651
+ delete schema[key];
652
+ }
653
+ schema.$ref = ref;
654
+ };
655
+
656
+ // extract schemas into $defs
657
+ for (const entry of this.seen.entries()) {
658
+ const seen = entry[1];
659
+
660
+ // convert root schema to # $ref
661
+ // also prevents root schema from being extracted
662
+ if (schema === entry[0]) {
663
+ // do not copy to defs...this is the root schema
664
+ extractToDef(entry);
665
+ continue;
666
+ }
667
+
668
+ // extract schemas that are in the external registry
669
+ if (params.external) {
670
+ const ext = params.external.registry.get(entry[0])?.id;
671
+ if (schema !== entry[0] && ext) {
672
+ extractToDef(entry);
673
+ continue;
674
+ }
675
+ }
676
+
677
+ // extract schemas with `id` meta
678
+ const id = this.metadataRegistry.get(entry[0])?.id;
679
+ if (id) {
680
+ extractToDef(entry);
681
+
682
+ continue;
683
+ }
684
+
685
+ // break cycles
686
+ if (seen.cycle) {
687
+ if (params.cycles === "throw") {
688
+ throw new Error(
689
+ "Cycle detected: " +
690
+ `#/${seen.cycle?.join("/")}/<root>` +
691
+ '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.'
692
+ );
693
+ } else if (params.cycles === "ref") {
694
+ extractToDef(entry);
695
+ }
696
+ continue;
697
+ }
698
+
699
+ // extract reused schemas
700
+ if (seen.count > 1) {
701
+ if (params.reused === "ref") {
702
+ extractToDef(entry);
703
+ // biome-ignore lint:
704
+ continue;
705
+ }
706
+ }
707
+ }
708
+
709
+ // flatten _refs
710
+ const flattenRef = (zodSchema: schemas.$ZodType, params: Pick<ToJSONSchemaParams, "target">) => {
711
+ const seen = this.seen.get(zodSchema)!;
712
+ const schema = seen.def ?? seen.schema;
713
+
714
+ const _cached = { ...schema };
715
+
716
+ // already seen
717
+ if (seen.ref === null) {
718
+ return;
719
+ }
720
+
721
+ // flatten ref if defined
722
+ const ref = seen.ref;
723
+ seen.ref = null; // prevent recursion
724
+ if (ref) {
725
+ flattenRef(ref, params);
726
+
727
+ // merge referenced schema into current
728
+ const refSchema = this.seen.get(ref)!.schema;
729
+ if (refSchema.$ref && params.target === "draft-7") {
730
+ schema.allOf = schema.allOf ?? [];
731
+ schema.allOf.push(refSchema);
732
+ } else {
733
+ Object.assign(schema, refSchema);
734
+ Object.assign(schema, _cached); // prevent overwriting any fields in the original schema
735
+ }
736
+ }
737
+
738
+ // execute overrides
739
+ if (!seen.isParent)
740
+ this.override({
741
+ zodSchema: zodSchema as schemas.$ZodTypes,
742
+ jsonSchema: schema,
743
+ });
744
+ };
745
+
746
+ for (const entry of [...this.seen.entries()].reverse()) {
747
+ flattenRef(entry[0], { target: this.target });
748
+ }
749
+
750
+ const result: JSONSchema.BaseSchema = {};
751
+ if (this.target === "draft-2020-12") {
752
+ result.$schema = "https://json-schema.org/draft/2020-12/schema";
753
+ } else if (this.target === "draft-7") {
754
+ result.$schema = "http://json-schema.org/draft-07/schema#";
755
+ } else {
756
+ console.warn(`Invalid target: ${this.target}`);
757
+ }
758
+
759
+ Object.assign(result, root.def);
760
+
761
+ // build defs object
762
+ const defs: JSONSchema.BaseSchema["$defs"] = params.external?.defs ?? {};
763
+ for (const entry of this.seen.entries()) {
764
+ const seen = entry[1];
765
+ if (seen.def && seen.defId) {
766
+ defs[seen.defId] = seen.def;
767
+ }
768
+ }
769
+
770
+ // set definitions in result
771
+ if (!params.external && Object.keys(defs).length > 0) {
772
+ if (this.target === "draft-2020-12") {
773
+ result.$defs = defs;
774
+ } else {
775
+ result.definitions = defs;
776
+ }
777
+ }
778
+
779
+ try {
780
+ // this "finalizes" this schema and ensures all cycles are removed
781
+ // each call to .emit() is functionally independent
782
+ // though the seen map is shared
783
+ return JSON.parse(JSON.stringify(result));
784
+ } catch (_err) {
785
+ throw new Error("Error converting schema to JSON.");
786
+ }
787
+ }
788
+ }
789
+
790
+ interface ToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {}
791
+ interface RegistryToJSONSchemaParams extends Omit<JSONSchemaGeneratorParams & EmitParams, "external"> {
792
+ uri?: (id: string) => string;
793
+ }
794
+
795
+ export function toJSONSchema(schema: schemas.$ZodType, _params?: ToJSONSchemaParams): JSONSchema.BaseSchema;
796
+ export function toJSONSchema(
797
+ registry: $ZodRegistry<{ id?: string | undefined }>,
798
+ _params?: RegistryToJSONSchemaParams
799
+ ): { schemas: Record<string, JSONSchema.BaseSchema> };
800
+ export function toJSONSchema(
801
+ input: schemas.$ZodType | $ZodRegistry<{ id?: string | undefined }>,
802
+ _params?: ToJSONSchemaParams
803
+ ): any {
804
+ if (input instanceof $ZodRegistry) {
805
+ const gen = new JSONSchemaGenerator(_params);
806
+ const defs: any = {};
807
+ for (const entry of input._idmap.entries()) {
808
+ const [_, schema] = entry;
809
+ gen.process(schema);
810
+ }
811
+
812
+ const schemas: Record<string, JSONSchema.BaseSchema> = {};
813
+ const external = {
814
+ registry: input,
815
+ uri: (_params as RegistryToJSONSchemaParams)?.uri || ((id) => id),
816
+ defs,
817
+ };
818
+ for (const entry of input._idmap.entries()) {
819
+ const [key, schema] = entry;
820
+ schemas[key] = gen.emit(schema, {
821
+ ..._params,
822
+ external,
823
+ });
824
+ }
825
+
826
+ if (Object.keys(defs).length > 0) {
827
+ const defsSegment = gen.target === "draft-2020-12" ? "$defs" : "definitions";
828
+ schemas.__shared = {
829
+ [defsSegment]: defs,
830
+ };
831
+ }
832
+
833
+ return { schemas };
834
+ }
835
+
836
+ const gen = new JSONSchemaGenerator(_params);
837
+ gen.process(input);
838
+
839
+ return gen.emit(input, _params);
840
+ }
841
+
842
+ function isTransforming(
843
+ _schema: schemas.$ZodType,
844
+ _ctx?: {
845
+ seen: Set<schemas.$ZodType>;
846
+ }
847
+ ): boolean {
848
+ const ctx = _ctx ?? { seen: new Set() };
849
+
850
+ if (ctx.seen.has(_schema)) return false;
851
+ ctx.seen.add(_schema);
852
+
853
+ const schema = _schema as schemas.$ZodTypes;
854
+ const def = schema._zod.def;
855
+ switch (def.type) {
856
+ case "string":
857
+ case "number":
858
+ case "bigint":
859
+ case "boolean":
860
+ case "date":
861
+ case "symbol":
862
+ case "undefined":
863
+ case "null":
864
+ case "any":
865
+ case "unknown":
866
+ case "never":
867
+ case "void":
868
+ case "literal":
869
+ case "enum":
870
+ case "nan":
871
+ case "file":
872
+ case "template_literal":
873
+ return false;
874
+ case "array": {
875
+ return isTransforming(def.element, ctx);
876
+ }
877
+ case "object": {
878
+ for (const key in def.shape) {
879
+ if (isTransforming(def.shape[key]!, ctx)) return true;
880
+ }
881
+ return false;
882
+ }
883
+ case "union": {
884
+ for (const option of def.options) {
885
+ if (isTransforming(option, ctx)) return true;
886
+ }
887
+ return false;
888
+ }
889
+ case "intersection": {
890
+ return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
891
+ }
892
+ case "tuple": {
893
+ for (const item of def.items) {
894
+ if (isTransforming(item, ctx)) return true;
895
+ }
896
+ if (def.rest && isTransforming(def.rest, ctx)) return true;
897
+ return false;
898
+ }
899
+ case "record": {
900
+ return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
901
+ }
902
+ case "map": {
903
+ return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
904
+ }
905
+ case "set": {
906
+ return isTransforming(def.valueType, ctx);
907
+ }
908
+
909
+ // inner types
910
+ case "promise":
911
+ case "optional":
912
+ case "nonoptional":
913
+ case "nullable":
914
+ case "readonly":
915
+ return isTransforming(def.innerType, ctx);
916
+ case "lazy":
917
+ return isTransforming(def.getter(), ctx);
918
+ case "default": {
919
+ return isTransforming(def.innerType, ctx);
920
+ }
921
+ case "prefault": {
922
+ return isTransforming(def.innerType, ctx);
923
+ }
924
+ case "custom": {
925
+ return false;
926
+ }
927
+ case "transform": {
928
+ return true;
929
+ }
930
+ case "pipe": {
931
+ return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
932
+ }
933
+ case "success": {
934
+ return false;
935
+ }
936
+ case "catch": {
937
+ return false;
938
+ }
939
+
940
+ default:
941
+ def satisfies never;
942
+ }
943
+ throw new Error(`Unknown schema type: ${(def as any).type}`);
944
+ }