@anthonyalayo/zod 0.0.0-stage → 4.7.0

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 (842) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +217 -2
  3. package/compile.cjs +96 -0
  4. package/compile.d.cts +1 -0
  5. package/compile.d.ts +1 -0
  6. package/compile.js +52 -0
  7. package/index.cjs +52 -0
  8. package/index.d.cts +3 -0
  9. package/index.d.ts +3 -0
  10. package/index.js +7 -0
  11. package/locales/index.cjs +36 -0
  12. package/locales/index.d.cts +1 -0
  13. package/locales/index.d.ts +1 -0
  14. package/locales/index.js +1 -0
  15. package/locales/package.json +7 -0
  16. package/mini/index.cjs +51 -0
  17. package/mini/index.d.cts +3 -0
  18. package/mini/index.d.ts +3 -0
  19. package/mini/index.js +3 -0
  20. package/mini/package.json +7 -0
  21. package/package.json +144 -4
  22. package/src/compile.ts +56 -0
  23. package/src/index.ts +7 -0
  24. package/src/locales/index.ts +1 -0
  25. package/src/mini/index.ts +3 -0
  26. package/src/v3/ZodError.ts +340 -0
  27. package/src/v3/benchmarks/datetime.ts +58 -0
  28. package/src/v3/benchmarks/discriminatedUnion.ts +80 -0
  29. package/src/v3/benchmarks/index.ts +59 -0
  30. package/src/v3/benchmarks/ipv4.ts +57 -0
  31. package/src/v3/benchmarks/object.ts +69 -0
  32. package/src/v3/benchmarks/primitives.ts +162 -0
  33. package/src/v3/benchmarks/realworld.ts +63 -0
  34. package/src/v3/benchmarks/string.ts +55 -0
  35. package/src/v3/benchmarks/union.ts +80 -0
  36. package/src/v3/errors.ts +13 -0
  37. package/src/v3/external.ts +6 -0
  38. package/src/v3/helpers/enumUtil.ts +17 -0
  39. package/src/v3/helpers/errorUtil.ts +8 -0
  40. package/src/v3/helpers/parseUtil.ts +176 -0
  41. package/src/v3/helpers/partialUtil.ts +34 -0
  42. package/src/v3/helpers/typeAliases.ts +2 -0
  43. package/src/v3/helpers/util.ts +224 -0
  44. package/src/v3/index.ts +4 -0
  45. package/src/v3/locales/en.ts +124 -0
  46. package/src/v3/standard-schema.ts +113 -0
  47. package/src/v3/tests/Mocker.ts +54 -0
  48. package/src/v3/tests/all-errors.test.ts +157 -0
  49. package/src/v3/tests/anyunknown.test.ts +28 -0
  50. package/src/v3/tests/array.test.ts +92 -0
  51. package/src/v3/tests/async-parsing.test.ts +388 -0
  52. package/src/v3/tests/async-refinements.test.ts +46 -0
  53. package/src/v3/tests/base.test.ts +29 -0
  54. package/src/v3/tests/bigint.test.ts +55 -0
  55. package/src/v3/tests/branded.test.ts +53 -0
  56. package/src/v3/tests/catch.test.ts +220 -0
  57. package/src/v3/tests/coerce.test.ts +133 -0
  58. package/src/v3/tests/complex.test.ts +70 -0
  59. package/src/v3/tests/custom.test.ts +31 -0
  60. package/src/v3/tests/date.test.ts +32 -0
  61. package/src/v3/tests/deepmasking.test.ts +186 -0
  62. package/src/v3/tests/default.test.ts +112 -0
  63. package/src/v3/tests/description.test.ts +33 -0
  64. package/src/v3/tests/discriminated-unions.test.ts +315 -0
  65. package/src/v3/tests/enum.test.ts +80 -0
  66. package/src/v3/tests/error.test.ts +586 -0
  67. package/src/v3/tests/firstparty.test.ts +87 -0
  68. package/src/v3/tests/firstpartyschematypes.test.ts +21 -0
  69. package/src/v3/tests/function.test.ts +261 -0
  70. package/src/v3/tests/generics.test.ts +48 -0
  71. package/src/v3/tests/instanceof.test.ts +37 -0
  72. package/src/v3/tests/intersection.test.ts +136 -0
  73. package/src/v3/tests/language-server.source.ts +76 -0
  74. package/src/v3/tests/language-server.test.ts +206 -0
  75. package/src/v3/tests/literal.test.ts +36 -0
  76. package/src/v3/tests/map.test.ts +110 -0
  77. package/src/v3/tests/masking.test.ts +4 -0
  78. package/src/v3/tests/mocker.test.ts +19 -0
  79. package/src/v3/tests/nan.test.ts +24 -0
  80. package/src/v3/tests/nativeEnum.test.ts +87 -0
  81. package/src/v3/tests/nullable.test.ts +42 -0
  82. package/src/v3/tests/number.test.ts +176 -0
  83. package/src/v3/tests/object-augmentation.test.ts +29 -0
  84. package/src/v3/tests/object-in-es5-env.test.ts +29 -0
  85. package/src/v3/tests/object.test.ts +431 -0
  86. package/src/v3/tests/optional.test.ts +42 -0
  87. package/src/v3/tests/parseUtil.test.ts +23 -0
  88. package/src/v3/tests/parser.test.ts +41 -0
  89. package/src/v3/tests/partials.test.ts +243 -0
  90. package/src/v3/tests/pickomit.test.ts +111 -0
  91. package/src/v3/tests/pipeline.test.ts +29 -0
  92. package/src/v3/tests/preprocess.test.ts +186 -0
  93. package/src/v3/tests/primitive.test.ts +440 -0
  94. package/src/v3/tests/promise.test.ts +90 -0
  95. package/src/v3/tests/readonly.test.ts +114 -0
  96. package/src/v3/tests/record.test.ts +171 -0
  97. package/src/v3/tests/recursive.test.ts +197 -0
  98. package/src/v3/tests/refine.test.ts +313 -0
  99. package/src/v3/tests/safeparse.test.ts +27 -0
  100. package/src/v3/tests/set.test.ts +148 -0
  101. package/src/v3/tests/standard-schema.test.ts +83 -0
  102. package/src/v3/tests/string.test.ts +922 -0
  103. package/src/v3/tests/transformer.test.ts +233 -0
  104. package/src/v3/tests/tuple.test.ts +90 -0
  105. package/src/v3/tests/unions.test.ts +57 -0
  106. package/src/v3/tests/validations.test.ts +133 -0
  107. package/src/v3/tests/void.test.ts +15 -0
  108. package/src/v3/types.ts +5149 -0
  109. package/src/v4/classic/checks.ts +33 -0
  110. package/src/v4/classic/coerce.ts +27 -0
  111. package/src/v4/classic/compat.ts +78 -0
  112. package/src/v4/classic/deep-partial.ts +76 -0
  113. package/src/v4/classic/errors.ts +97 -0
  114. package/src/v4/classic/external.ts +57 -0
  115. package/src/v4/classic/from-json-schema.ts +944 -0
  116. package/src/v4/classic/in-out.ts +45 -0
  117. package/src/v4/classic/index.ts +5 -0
  118. package/src/v4/classic/iso.ts +20 -0
  119. package/src/v4/classic/parse.ts +88 -0
  120. package/src/v4/classic/schemas.ts +3009 -0
  121. package/src/v4/classic/tests/anyunknown.test.ts +26 -0
  122. package/src/v4/classic/tests/apply.test.ts +86 -0
  123. package/src/v4/classic/tests/array.test.ts +339 -0
  124. package/src/v4/classic/tests/assignability.test.ts +402 -0
  125. package/src/v4/classic/tests/async-parsing.test.ts +381 -0
  126. package/src/v4/classic/tests/async-refinements.test.ts +68 -0
  127. package/src/v4/classic/tests/base.test.ts +7 -0
  128. package/src/v4/classic/tests/bigint.test.ts +89 -0
  129. package/src/v4/classic/tests/brand.test.ts +106 -0
  130. package/src/v4/classic/tests/catch.test.ts +604 -0
  131. package/src/v4/classic/tests/coalesce.test.ts +20 -0
  132. package/src/v4/classic/tests/codec-examples.test.ts +572 -0
  133. package/src/v4/classic/tests/codec.test.ts +704 -0
  134. package/src/v4/classic/tests/coerce.test.ts +160 -0
  135. package/src/v4/classic/tests/continuability.test.ts +374 -0
  136. package/src/v4/classic/tests/credit-card.test.ts +76 -0
  137. package/src/v4/classic/tests/custom.test.ts +40 -0
  138. package/src/v4/classic/tests/cyclic-data.test.ts +1248 -0
  139. package/src/v4/classic/tests/date.test.ts +92 -0
  140. package/src/v4/classic/tests/datetime.test.ts +316 -0
  141. package/src/v4/classic/tests/deep-partial.test.ts +380 -0
  142. package/src/v4/classic/tests/default-locale.test.ts +32 -0
  143. package/src/v4/classic/tests/default.test.ts +455 -0
  144. package/src/v4/classic/tests/describe-meta-checks.test.ts +27 -0
  145. package/src/v4/classic/tests/description.test.ts +32 -0
  146. package/src/v4/classic/tests/detached-methods.test.ts +221 -0
  147. package/src/v4/classic/tests/discriminated-unions.test.ts +942 -0
  148. package/src/v4/classic/tests/enum.test.ts +305 -0
  149. package/src/v4/classic/tests/error-utils.test.ts +917 -0
  150. package/src/v4/classic/tests/error.test.ts +1108 -0
  151. package/src/v4/classic/tests/file.test.ts +96 -0
  152. package/src/v4/classic/tests/firstparty.test.ts +179 -0
  153. package/src/v4/classic/tests/fix-json-issue.test.ts +26 -0
  154. package/src/v4/classic/tests/from-json-schema.test.ts +1467 -0
  155. package/src/v4/classic/tests/function.test.ts +391 -0
  156. package/src/v4/classic/tests/generics.test.ts +72 -0
  157. package/src/v4/classic/tests/global-config.test.ts +32 -0
  158. package/src/v4/classic/tests/hash.test.ts +67 -0
  159. package/src/v4/classic/tests/iban.test.ts +81 -0
  160. package/src/v4/classic/tests/in-out.test.ts +159 -0
  161. package/src/v4/classic/tests/index.test.ts +958 -0
  162. package/src/v4/classic/tests/instance-footprint.test.ts +319 -0
  163. package/src/v4/classic/tests/instanceof.test.ts +127 -0
  164. package/src/v4/classic/tests/intersection.test.ts +368 -0
  165. package/src/v4/classic/tests/issue-schema.test.ts +215 -0
  166. package/src/v4/classic/tests/jitless-allows-eval.test.ts +39 -0
  167. package/src/v4/classic/tests/json.test.ts +109 -0
  168. package/src/v4/classic/tests/lazy.test.ts +276 -0
  169. package/src/v4/classic/tests/literal.test.ts +128 -0
  170. package/src/v4/classic/tests/locales_ka.test.ts +29 -0
  171. package/src/v4/classic/tests/locales_ro.test.ts +24 -0
  172. package/src/v4/classic/tests/map.test.ts +349 -0
  173. package/src/v4/classic/tests/nan.test.ts +21 -0
  174. package/src/v4/classic/tests/nested-refine.test.ts +168 -0
  175. package/src/v4/classic/tests/nonoptional.test.ts +101 -0
  176. package/src/v4/classic/tests/nullable.test.ts +22 -0
  177. package/src/v4/classic/tests/number.test.ts +401 -0
  178. package/src/v4/classic/tests/object.test.ts +1015 -0
  179. package/src/v4/classic/tests/optin-ladder.test.ts +235 -0
  180. package/src/v4/classic/tests/optional.test.ts +366 -0
  181. package/src/v4/classic/tests/partial.test.ts +497 -0
  182. package/src/v4/classic/tests/pickomit.test.ts +211 -0
  183. package/src/v4/classic/tests/pipe.test.ts +101 -0
  184. package/src/v4/classic/tests/prefault.test.ts +144 -0
  185. package/src/v4/classic/tests/preprocess-types.test.ts +44 -0
  186. package/src/v4/classic/tests/preprocess.test.ts +351 -0
  187. package/src/v4/classic/tests/primitive.test.ts +175 -0
  188. package/src/v4/classic/tests/promise.test.ts +81 -0
  189. package/src/v4/classic/tests/properties.test.ts +141 -0
  190. package/src/v4/classic/tests/prototypes.test.ts +23 -0
  191. package/src/v4/classic/tests/readonly.test.ts +184 -0
  192. package/src/v4/classic/tests/record.test.ts +901 -0
  193. package/src/v4/classic/tests/recursive-types.test.ts +707 -0
  194. package/src/v4/classic/tests/redos.test.ts +68 -0
  195. package/src/v4/classic/tests/refine.test.ts +720 -0
  196. package/src/v4/classic/tests/registries.test.ts +243 -0
  197. package/src/v4/classic/tests/set.test.ts +196 -0
  198. package/src/v4/classic/tests/standard-schema.test.ts +153 -0
  199. package/src/v4/classic/tests/string-formats.test.ts +125 -0
  200. package/src/v4/classic/tests/string.test.ts +1440 -0
  201. package/src/v4/classic/tests/stringbool.test.ts +124 -0
  202. package/src/v4/classic/tests/template-literal.test.ts +864 -0
  203. package/src/v4/classic/tests/to-json-schema-methods.test.ts +438 -0
  204. package/src/v4/classic/tests/to-json-schema.test.ts +4811 -0
  205. package/src/v4/classic/tests/transform.test.ts +414 -0
  206. package/src/v4/classic/tests/tuple.test.ts +520 -0
  207. package/src/v4/classic/tests/union.test.ts +358 -0
  208. package/src/v4/classic/tests/update.test.ts +92 -0
  209. package/src/v4/classic/tests/url.test.ts +13 -0
  210. package/src/v4/classic/tests/validate.test.ts +645 -0
  211. package/src/v4/classic/tests/validations.test.ts +283 -0
  212. package/src/v4/classic/tests/void.test.ts +12 -0
  213. package/src/v4/core/api.ts +1875 -0
  214. package/src/v4/core/checks.ts +1266 -0
  215. package/src/v4/core/compile.ts +2323 -0
  216. package/src/v4/core/core.ts +230 -0
  217. package/src/v4/core/doc.ts +52 -0
  218. package/src/v4/core/errors.ts +543 -0
  219. package/src/v4/core/index.ts +19 -0
  220. package/src/v4/core/json-schema-generator.ts +131 -0
  221. package/src/v4/core/json-schema-processors.ts +944 -0
  222. package/src/v4/core/json-schema.ts +148 -0
  223. package/src/v4/core/memoizer.ts +404 -0
  224. package/src/v4/core/parse.ts +309 -0
  225. package/src/v4/core/regexes.ts +217 -0
  226. package/src/v4/core/registries.ts +105 -0
  227. package/src/v4/core/schemas.ts +5271 -0
  228. package/src/v4/core/standard-schema.ts +159 -0
  229. package/src/v4/core/tests/cached.test.ts +54 -0
  230. package/src/v4/core/tests/compile-differential.test.ts +1004 -0
  231. package/src/v4/core/tests/compile-generative.test.ts +445 -0
  232. package/src/v4/core/tests/compile-global.test.ts +89 -0
  233. package/src/v4/core/tests/compile-off-by-default.test.ts +26 -0
  234. package/src/v4/core/tests/compile-stats.test.ts +34 -0
  235. package/src/v4/core/tests/compile.test.ts +2056 -0
  236. package/src/v4/core/tests/email-regex.test.ts +140 -0
  237. package/src/v4/core/tests/extend.test.ts +59 -0
  238. package/src/v4/core/tests/index.test.ts +46 -0
  239. package/src/v4/core/tests/locales/be.test.ts +124 -0
  240. package/src/v4/core/tests/locales/el.test.ts +214 -0
  241. package/src/v4/core/tests/locales/en.test.ts +22 -0
  242. package/src/v4/core/tests/locales/es.test.ts +180 -0
  243. package/src/v4/core/tests/locales/fr.test.ts +72 -0
  244. package/src/v4/core/tests/locales/he.test.ts +391 -0
  245. package/src/v4/core/tests/locales/hr.test.ts +163 -0
  246. package/src/v4/core/tests/locales/hy.test.ts +15 -0
  247. package/src/v4/core/tests/locales/nl.test.ts +46 -0
  248. package/src/v4/core/tests/locales/parity.test.ts +67 -0
  249. package/src/v4/core/tests/locales/ru.test.ts +128 -0
  250. package/src/v4/core/tests/locales/tg.test.ts +85 -0
  251. package/src/v4/core/tests/locales/tk.test.ts +69 -0
  252. package/src/v4/core/tests/locales/tr.test.ts +69 -0
  253. package/src/v4/core/tests/locales/uz.test.ts +105 -0
  254. package/src/v4/core/tests/polyfill-collision.test.ts +46 -0
  255. package/src/v4/core/tests/record-constructor.test.ts +125 -0
  256. package/src/v4/core/tests/recursive-tuples.test.ts +45 -0
  257. package/src/v4/core/tests/treeshake.test.ts +51 -0
  258. package/src/v4/core/tests/url-no-canparse.test.ts +133 -0
  259. package/src/v4/core/to-json-schema.ts +857 -0
  260. package/src/v4/core/util.ts +1322 -0
  261. package/src/v4/core/versions.ts +5 -0
  262. package/src/v4/core/visit.ts +193 -0
  263. package/src/v4/core/zsf.ts +323 -0
  264. package/src/v4/index.ts +4 -0
  265. package/src/v4/locales/ar.ts +120 -0
  266. package/src/v4/locales/az.ts +116 -0
  267. package/src/v4/locales/be.ts +188 -0
  268. package/src/v4/locales/bg.ts +133 -0
  269. package/src/v4/locales/bn.ts +123 -0
  270. package/src/v4/locales/ca.ts +121 -0
  271. package/src/v4/locales/ckb.ts +140 -0
  272. package/src/v4/locales/cs.ts +123 -0
  273. package/src/v4/locales/da.ts +128 -0
  274. package/src/v4/locales/de.ts +121 -0
  275. package/src/v4/locales/el.ts +124 -0
  276. package/src/v4/locales/en.ts +136 -0
  277. package/src/v4/locales/eo.ts +123 -0
  278. package/src/v4/locales/es.ts +146 -0
  279. package/src/v4/locales/fa.ts +131 -0
  280. package/src/v4/locales/fi.ts +126 -0
  281. package/src/v4/locales/fr-CA.ts +121 -0
  282. package/src/v4/locales/fr.ts +140 -0
  283. package/src/v4/locales/gu.ts +123 -0
  284. package/src/v4/locales/he.ts +254 -0
  285. package/src/v4/locales/hi.ts +126 -0
  286. package/src/v4/locales/hr.ts +136 -0
  287. package/src/v4/locales/hu.ts +122 -0
  288. package/src/v4/locales/hy.ts +175 -0
  289. package/src/v4/locales/id.ts +120 -0
  290. package/src/v4/locales/index.ts +63 -0
  291. package/src/v4/locales/is.ts +124 -0
  292. package/src/v4/locales/it.ts +121 -0
  293. package/src/v4/locales/ja.ts +119 -0
  294. package/src/v4/locales/ka.ts +128 -0
  295. package/src/v4/locales/kh.ts +7 -0
  296. package/src/v4/locales/km.ts +124 -0
  297. package/src/v4/locales/kn.ts +126 -0
  298. package/src/v4/locales/ko.ts +126 -0
  299. package/src/v4/locales/lt.ts +260 -0
  300. package/src/v4/locales/mk.ts +123 -0
  301. package/src/v4/locales/ms.ts +120 -0
  302. package/src/v4/locales/ne.ts +128 -0
  303. package/src/v4/locales/nl.ts +126 -0
  304. package/src/v4/locales/nn.ts +121 -0
  305. package/src/v4/locales/no.ts +121 -0
  306. package/src/v4/locales/ota.ts +122 -0
  307. package/src/v4/locales/pl.ts +123 -0
  308. package/src/v4/locales/ps.ts +131 -0
  309. package/src/v4/locales/pt-BR.ts +161 -0
  310. package/src/v4/locales/pt.ts +159 -0
  311. package/src/v4/locales/ro.ts +132 -0
  312. package/src/v4/locales/ru.ts +188 -0
  313. package/src/v4/locales/sk.ts +123 -0
  314. package/src/v4/locales/sl.ts +123 -0
  315. package/src/v4/locales/sv.ts +124 -0
  316. package/src/v4/locales/ta.ts +123 -0
  317. package/src/v4/locales/tg.ts +135 -0
  318. package/src/v4/locales/th.ts +124 -0
  319. package/src/v4/locales/tk.ts +113 -0
  320. package/src/v4/locales/tr.ts +116 -0
  321. package/src/v4/locales/ua.ts +7 -0
  322. package/src/v4/locales/uk.ts +122 -0
  323. package/src/v4/locales/ur.ts +124 -0
  324. package/src/v4/locales/uz.ts +120 -0
  325. package/src/v4/locales/vi.ts +122 -0
  326. package/src/v4/locales/yo.ts +129 -0
  327. package/src/v4/locales/zh-CN.ts +121 -0
  328. package/src/v4/locales/zh-TW.ts +120 -0
  329. package/src/v4/mini/checks.ts +33 -0
  330. package/src/v4/mini/coerce.ts +27 -0
  331. package/src/v4/mini/deep-partial.ts +73 -0
  332. package/src/v4/mini/external.ts +52 -0
  333. package/src/v4/mini/in-out.ts +45 -0
  334. package/src/v4/mini/index.ts +3 -0
  335. package/src/v4/mini/iso.ts +66 -0
  336. package/src/v4/mini/parse.ts +16 -0
  337. package/src/v4/mini/schemas.ts +2051 -0
  338. package/src/v4/mini/tests/apply.test.ts +57 -0
  339. package/src/v4/mini/tests/assignability.test.ts +190 -0
  340. package/src/v4/mini/tests/brand.test.ts +94 -0
  341. package/src/v4/mini/tests/checks.test.ts +158 -0
  342. package/src/v4/mini/tests/codec.test.ts +582 -0
  343. package/src/v4/mini/tests/computed.test.ts +30 -0
  344. package/src/v4/mini/tests/cyclic-data.test.ts +97 -0
  345. package/src/v4/mini/tests/deep-partial.test.ts +54 -0
  346. package/src/v4/mini/tests/error.test.ts +41 -0
  347. package/src/v4/mini/tests/functions.test.ts +14 -0
  348. package/src/v4/mini/tests/index.test.ts +1175 -0
  349. package/src/v4/mini/tests/number.test.ts +109 -0
  350. package/src/v4/mini/tests/object.test.ts +292 -0
  351. package/src/v4/mini/tests/prototypes.test.ts +43 -0
  352. package/src/v4/mini/tests/recursive-types.test.ts +341 -0
  353. package/src/v4/mini/tests/standard-schema.test.ts +50 -0
  354. package/src/v4/mini/tests/string.test.ts +396 -0
  355. package/src/v4-mini/index.ts +3 -0
  356. package/v3/ZodError.cjs +167 -0
  357. package/v3/ZodError.d.cts +164 -0
  358. package/v3/ZodError.d.ts +164 -0
  359. package/v3/ZodError.js +143 -0
  360. package/v3/errors.cjs +36 -0
  361. package/v3/errors.d.cts +5 -0
  362. package/v3/errors.d.ts +5 -0
  363. package/v3/errors.js +9 -0
  364. package/v3/external.cjs +41 -0
  365. package/v3/external.d.cts +6 -0
  366. package/v3/external.d.ts +6 -0
  367. package/v3/external.js +6 -0
  368. package/v3/helpers/enumUtil.cjs +21 -0
  369. package/v3/helpers/enumUtil.d.cts +8 -0
  370. package/v3/helpers/enumUtil.d.ts +8 -0
  371. package/v3/helpers/enumUtil.js +1 -0
  372. package/v3/helpers/errorUtil.cjs +28 -0
  373. package/v3/helpers/errorUtil.d.cts +9 -0
  374. package/v3/helpers/errorUtil.d.ts +9 -0
  375. package/v3/helpers/errorUtil.js +6 -0
  376. package/v3/helpers/parseUtil.cjs +143 -0
  377. package/v3/helpers/parseUtil.d.cts +78 -0
  378. package/v3/helpers/parseUtil.d.ts +78 -0
  379. package/v3/helpers/parseUtil.js +109 -0
  380. package/v3/helpers/partialUtil.cjs +21 -0
  381. package/v3/helpers/partialUtil.d.cts +8 -0
  382. package/v3/helpers/partialUtil.d.ts +8 -0
  383. package/v3/helpers/partialUtil.js +1 -0
  384. package/v3/helpers/typeAliases.cjs +21 -0
  385. package/v3/helpers/typeAliases.d.cts +2 -0
  386. package/v3/helpers/typeAliases.d.ts +2 -0
  387. package/v3/helpers/typeAliases.js +1 -0
  388. package/v3/helpers/util.cjs +156 -0
  389. package/v3/helpers/util.d.cts +85 -0
  390. package/v3/helpers/util.d.ts +85 -0
  391. package/v3/helpers/util.js +133 -0
  392. package/v3/index.cjs +52 -0
  393. package/v3/index.d.cts +4 -0
  394. package/v3/index.d.ts +4 -0
  395. package/v3/index.js +4 -0
  396. package/v3/locales/en.cjs +112 -0
  397. package/v3/locales/en.d.cts +3 -0
  398. package/v3/locales/en.d.ts +3 -0
  399. package/v3/locales/en.js +109 -0
  400. package/v3/package.json +7 -0
  401. package/v3/standard-schema.cjs +21 -0
  402. package/v3/standard-schema.d.cts +102 -0
  403. package/v3/standard-schema.d.ts +102 -0
  404. package/v3/standard-schema.js +1 -0
  405. package/v3/types.cjs +3806 -0
  406. package/v3/types.d.cts +1034 -0
  407. package/v3/types.d.ts +1034 -0
  408. package/v3/types.js +3705 -0
  409. package/v4/classic/checks.cjs +53 -0
  410. package/v4/classic/checks.d.cts +1 -0
  411. package/v4/classic/checks.d.ts +1 -0
  412. package/v4/classic/checks.js +1 -0
  413. package/v4/classic/coerce.cjs +66 -0
  414. package/v4/classic/coerce.d.cts +17 -0
  415. package/v4/classic/coerce.d.ts +17 -0
  416. package/v4/classic/coerce.js +17 -0
  417. package/v4/classic/compat.cjs +80 -0
  418. package/v4/classic/compat.d.cts +48 -0
  419. package/v4/classic/compat.d.ts +48 -0
  420. package/v4/classic/compat.js +31 -0
  421. package/v4/classic/deep-partial.cjs +58 -0
  422. package/v4/classic/deep-partial.d.cts +14 -0
  423. package/v4/classic/deep-partial.d.ts +14 -0
  424. package/v4/classic/deep-partial.js +13 -0
  425. package/v4/classic/errors.cjs +102 -0
  426. package/v4/classic/errors.d.cts +29 -0
  427. package/v4/classic/errors.d.ts +29 -0
  428. package/v4/classic/errors.js +57 -0
  429. package/v4/classic/external.cjs +97 -0
  430. package/v4/classic/external.d.cts +19 -0
  431. package/v4/classic/external.d.ts +19 -0
  432. package/v4/classic/external.js +17 -0
  433. package/v4/classic/from-json-schema.cjs +926 -0
  434. package/v4/classic/from-json-schema.d.cts +12 -0
  435. package/v4/classic/from-json-schema.d.ts +12 -0
  436. package/v4/classic/from-json-schema.js +881 -0
  437. package/v4/classic/in-out.cjs +82 -0
  438. package/v4/classic/in-out.d.cts +8 -0
  439. package/v4/classic/in-out.d.ts +8 -0
  440. package/v4/classic/in-out.js +36 -0
  441. package/v4/classic/index.cjs +52 -0
  442. package/v4/classic/index.d.cts +3 -0
  443. package/v4/classic/index.d.ts +3 -0
  444. package/v4/classic/index.js +4 -0
  445. package/v4/classic/iso.cjs +68 -0
  446. package/v4/classic/iso.d.cts +7 -0
  447. package/v4/classic/iso.d.ts +7 -0
  448. package/v4/classic/iso.js +15 -0
  449. package/v4/classic/package.json +7 -0
  450. package/v4/classic/parse.cjs +63 -0
  451. package/v4/classic/parse.d.cts +44 -0
  452. package/v4/classic/parse.d.ts +44 -0
  453. package/v4/classic/parse.js +16 -0
  454. package/v4/classic/schemas.cjs +1750 -0
  455. package/v4/classic/schemas.d.cts +819 -0
  456. package/v4/classic/schemas.d.ts +819 -0
  457. package/v4/classic/schemas.js +1609 -0
  458. package/v4/core/api.cjs +1269 -0
  459. package/v4/core/api.d.cts +335 -0
  460. package/v4/core/api.d.ts +335 -0
  461. package/v4/core/api.js +1107 -0
  462. package/v4/core/checks.cjs +561 -0
  463. package/v4/core/checks.d.cts +294 -0
  464. package/v4/core/checks.d.ts +294 -0
  465. package/v4/core/checks.js +516 -0
  466. package/v4/core/compile.cjs +1915 -0
  467. package/v4/core/compile.d.cts +65 -0
  468. package/v4/core/compile.d.ts +65 -0
  469. package/v4/core/compile.js +1865 -0
  470. package/v4/core/core.cjs +167 -0
  471. package/v4/core/core.d.cts +82 -0
  472. package/v4/core/core.d.ts +82 -0
  473. package/v4/core/core.js +141 -0
  474. package/v4/core/doc.cjs +61 -0
  475. package/v4/core/doc.d.cts +16 -0
  476. package/v4/core/doc.d.ts +16 -0
  477. package/v4/core/doc.js +38 -0
  478. package/v4/core/errors.cjs +317 -0
  479. package/v4/core/errors.d.cts +225 -0
  480. package/v4/core/errors.d.ts +225 -0
  481. package/v4/core/errors.js +267 -0
  482. package/v4/core/index.cjs +70 -0
  483. package/v4/core/index.d.cts +19 -0
  484. package/v4/core/index.d.ts +19 -0
  485. package/v4/core/index.js +19 -0
  486. package/v4/core/json-schema-generator.cjs +122 -0
  487. package/v4/core/json-schema-generator.d.cts +65 -0
  488. package/v4/core/json-schema-generator.d.ts +65 -0
  489. package/v4/core/json-schema-generator.js +99 -0
  490. package/v4/core/json-schema-processors.cjs +945 -0
  491. package/v4/core/json-schema-processors.d.cts +65 -0
  492. package/v4/core/json-schema-processors.d.ts +65 -0
  493. package/v4/core/json-schema-processors.js +859 -0
  494. package/v4/core/json-schema.cjs +21 -0
  495. package/v4/core/json-schema.d.cts +89 -0
  496. package/v4/core/json-schema.d.ts +89 -0
  497. package/v4/core/json-schema.js +1 -0
  498. package/v4/core/memoizer.cjs +424 -0
  499. package/v4/core/memoizer.d.cts +15 -0
  500. package/v4/core/memoizer.d.ts +15 -0
  501. package/v4/core/memoizer.js +375 -0
  502. package/v4/core/package.json +7 -0
  503. package/v4/core/parse.cjs +233 -0
  504. package/v4/core/parse.d.cts +52 -0
  505. package/v4/core/parse.d.ts +52 -0
  506. package/v4/core/parse.js +175 -0
  507. package/v4/core/regexes.cjs +209 -0
  508. package/v4/core/regexes.d.cts +90 -0
  509. package/v4/core/regexes.d.ts +90 -0
  510. package/v4/core/regexes.js +156 -0
  511. package/v4/core/registries.cjs +75 -0
  512. package/v4/core/registries.d.cts +35 -0
  513. package/v4/core/registries.d.ts +35 -0
  514. package/v4/core/registries.js +51 -0
  515. package/v4/core/schemas.cjs +2754 -0
  516. package/v4/core/schemas.d.cts +1277 -0
  517. package/v4/core/schemas.d.ts +1277 -0
  518. package/v4/core/schemas.js +2691 -0
  519. package/v4/core/standard-schema.cjs +21 -0
  520. package/v4/core/standard-schema.d.cts +126 -0
  521. package/v4/core/standard-schema.d.ts +126 -0
  522. package/v4/core/standard-schema.js +1 -0
  523. package/v4/core/to-json-schema.cjs +681 -0
  524. package/v4/core/to-json-schema.d.cts +154 -0
  525. package/v4/core/to-json-schema.d.ts +154 -0
  526. package/v4/core/to-json-schema.js +651 -0
  527. package/v4/core/util.cjs +997 -0
  528. package/v4/core/util.d.cts +284 -0
  529. package/v4/core/util.d.ts +284 -0
  530. package/v4/core/util.js +904 -0
  531. package/v4/core/versions.cjs +27 -0
  532. package/v4/core/versions.d.cts +5 -0
  533. package/v4/core/versions.d.ts +5 -0
  534. package/v4/core/versions.js +5 -0
  535. package/v4/core/visit.cjs +214 -0
  536. package/v4/core/visit.d.cts +30 -0
  537. package/v4/core/visit.d.ts +30 -0
  538. package/v4/core/visit.js +169 -0
  539. package/v4/index.cjs +51 -0
  540. package/v4/index.d.cts +3 -0
  541. package/v4/index.d.ts +3 -0
  542. package/v4/index.js +4 -0
  543. package/v4/locales/ar.cjs +138 -0
  544. package/v4/locales/ar.d.cts +5 -0
  545. package/v4/locales/ar.d.ts +4 -0
  546. package/v4/locales/ar.js +111 -0
  547. package/v4/locales/az.cjs +137 -0
  548. package/v4/locales/az.d.cts +5 -0
  549. package/v4/locales/az.d.ts +4 -0
  550. package/v4/locales/az.js +110 -0
  551. package/v4/locales/be.cjs +195 -0
  552. package/v4/locales/be.d.cts +5 -0
  553. package/v4/locales/be.d.ts +4 -0
  554. package/v4/locales/be.js +168 -0
  555. package/v4/locales/bg.cjs +152 -0
  556. package/v4/locales/bg.d.cts +5 -0
  557. package/v4/locales/bg.d.ts +4 -0
  558. package/v4/locales/bg.js +125 -0
  559. package/v4/locales/bn.cjs +140 -0
  560. package/v4/locales/bn.d.cts +5 -0
  561. package/v4/locales/bn.d.ts +4 -0
  562. package/v4/locales/bn.js +113 -0
  563. package/v4/locales/ca.cjs +139 -0
  564. package/v4/locales/ca.d.cts +5 -0
  565. package/v4/locales/ca.d.ts +4 -0
  566. package/v4/locales/ca.js +112 -0
  567. package/v4/locales/ckb.cjs +160 -0
  568. package/v4/locales/ckb.d.cts +5 -0
  569. package/v4/locales/ckb.d.ts +4 -0
  570. package/v4/locales/ckb.js +133 -0
  571. package/v4/locales/cs.cjs +143 -0
  572. package/v4/locales/cs.d.cts +5 -0
  573. package/v4/locales/cs.d.ts +4 -0
  574. package/v4/locales/cs.js +116 -0
  575. package/v4/locales/da.cjs +147 -0
  576. package/v4/locales/da.d.cts +5 -0
  577. package/v4/locales/da.d.ts +4 -0
  578. package/v4/locales/da.js +120 -0
  579. package/v4/locales/de.cjs +140 -0
  580. package/v4/locales/de.d.cts +5 -0
  581. package/v4/locales/de.d.ts +4 -0
  582. package/v4/locales/de.js +113 -0
  583. package/v4/locales/el.cjs +139 -0
  584. package/v4/locales/el.d.cts +5 -0
  585. package/v4/locales/el.d.ts +4 -0
  586. package/v4/locales/el.js +112 -0
  587. package/v4/locales/en.cjs +152 -0
  588. package/v4/locales/en.d.cts +5 -0
  589. package/v4/locales/en.d.ts +4 -0
  590. package/v4/locales/en.js +125 -0
  591. package/v4/locales/eo.cjs +141 -0
  592. package/v4/locales/eo.d.cts +5 -0
  593. package/v4/locales/eo.d.ts +4 -0
  594. package/v4/locales/eo.js +114 -0
  595. package/v4/locales/es.cjs +164 -0
  596. package/v4/locales/es.d.cts +5 -0
  597. package/v4/locales/es.d.ts +4 -0
  598. package/v4/locales/es.js +137 -0
  599. package/v4/locales/fa.cjs +146 -0
  600. package/v4/locales/fa.d.cts +5 -0
  601. package/v4/locales/fa.d.ts +4 -0
  602. package/v4/locales/fa.js +119 -0
  603. package/v4/locales/fi.cjs +144 -0
  604. package/v4/locales/fi.d.cts +5 -0
  605. package/v4/locales/fi.d.ts +4 -0
  606. package/v4/locales/fi.js +117 -0
  607. package/v4/locales/fr-CA.cjs +139 -0
  608. package/v4/locales/fr-CA.d.cts +5 -0
  609. package/v4/locales/fr-CA.d.ts +4 -0
  610. package/v4/locales/fr-CA.js +112 -0
  611. package/v4/locales/fr.cjs +157 -0
  612. package/v4/locales/fr.d.cts +5 -0
  613. package/v4/locales/fr.d.ts +4 -0
  614. package/v4/locales/fr.js +130 -0
  615. package/v4/locales/gu.cjs +140 -0
  616. package/v4/locales/gu.d.cts +5 -0
  617. package/v4/locales/gu.d.ts +4 -0
  618. package/v4/locales/gu.js +113 -0
  619. package/v4/locales/he.cjs +249 -0
  620. package/v4/locales/he.d.cts +5 -0
  621. package/v4/locales/he.d.ts +4 -0
  622. package/v4/locales/he.js +222 -0
  623. package/v4/locales/hi.cjs +138 -0
  624. package/v4/locales/hi.d.cts +5 -0
  625. package/v4/locales/hi.d.ts +4 -0
  626. package/v4/locales/hi.js +111 -0
  627. package/v4/locales/hr.cjs +154 -0
  628. package/v4/locales/hr.d.cts +5 -0
  629. package/v4/locales/hr.d.ts +4 -0
  630. package/v4/locales/hr.js +127 -0
  631. package/v4/locales/hu.cjs +140 -0
  632. package/v4/locales/hu.d.cts +5 -0
  633. package/v4/locales/hu.d.ts +4 -0
  634. package/v4/locales/hu.js +113 -0
  635. package/v4/locales/hy.cjs +185 -0
  636. package/v4/locales/hy.d.cts +5 -0
  637. package/v4/locales/hy.d.ts +4 -0
  638. package/v4/locales/hy.js +158 -0
  639. package/v4/locales/id.cjs +138 -0
  640. package/v4/locales/id.d.cts +5 -0
  641. package/v4/locales/id.d.ts +4 -0
  642. package/v4/locales/id.js +111 -0
  643. package/v4/locales/index.cjs +152 -0
  644. package/v4/locales/index.d.cts +63 -0
  645. package/v4/locales/index.d.ts +63 -0
  646. package/v4/locales/index.js +63 -0
  647. package/v4/locales/is.cjs +141 -0
  648. package/v4/locales/is.d.cts +5 -0
  649. package/v4/locales/is.d.ts +4 -0
  650. package/v4/locales/is.js +114 -0
  651. package/v4/locales/it.cjs +140 -0
  652. package/v4/locales/it.d.cts +5 -0
  653. package/v4/locales/it.d.ts +4 -0
  654. package/v4/locales/it.js +113 -0
  655. package/v4/locales/ja.cjs +139 -0
  656. package/v4/locales/ja.d.cts +5 -0
  657. package/v4/locales/ja.d.ts +4 -0
  658. package/v4/locales/ja.js +112 -0
  659. package/v4/locales/ka.cjs +144 -0
  660. package/v4/locales/ka.d.cts +5 -0
  661. package/v4/locales/ka.d.ts +4 -0
  662. package/v4/locales/ka.js +117 -0
  663. package/v4/locales/kh.cjs +12 -0
  664. package/v4/locales/kh.d.cts +5 -0
  665. package/v4/locales/kh.d.ts +5 -0
  666. package/v4/locales/kh.js +5 -0
  667. package/v4/locales/km.cjs +142 -0
  668. package/v4/locales/km.d.cts +5 -0
  669. package/v4/locales/km.d.ts +4 -0
  670. package/v4/locales/km.js +115 -0
  671. package/v4/locales/kn.cjs +143 -0
  672. package/v4/locales/kn.d.cts +5 -0
  673. package/v4/locales/kn.d.ts +4 -0
  674. package/v4/locales/kn.js +116 -0
  675. package/v4/locales/ko.cjs +143 -0
  676. package/v4/locales/ko.d.cts +5 -0
  677. package/v4/locales/ko.d.ts +4 -0
  678. package/v4/locales/ko.js +116 -0
  679. package/v4/locales/lt.cjs +251 -0
  680. package/v4/locales/lt.d.cts +5 -0
  681. package/v4/locales/lt.d.ts +4 -0
  682. package/v4/locales/lt.js +224 -0
  683. package/v4/locales/mk.cjs +141 -0
  684. package/v4/locales/mk.d.cts +5 -0
  685. package/v4/locales/mk.d.ts +4 -0
  686. package/v4/locales/mk.js +114 -0
  687. package/v4/locales/ms.cjs +139 -0
  688. package/v4/locales/ms.d.cts +5 -0
  689. package/v4/locales/ms.d.ts +4 -0
  690. package/v4/locales/ms.js +112 -0
  691. package/v4/locales/ne.cjs +138 -0
  692. package/v4/locales/ne.d.cts +5 -0
  693. package/v4/locales/ne.d.ts +4 -0
  694. package/v4/locales/ne.js +111 -0
  695. package/v4/locales/nl.cjs +142 -0
  696. package/v4/locales/nl.d.cts +5 -0
  697. package/v4/locales/nl.d.ts +4 -0
  698. package/v4/locales/nl.js +115 -0
  699. package/v4/locales/nn.cjs +140 -0
  700. package/v4/locales/nn.d.cts +5 -0
  701. package/v4/locales/nn.d.ts +4 -0
  702. package/v4/locales/nn.js +113 -0
  703. package/v4/locales/no.cjs +140 -0
  704. package/v4/locales/no.d.cts +5 -0
  705. package/v4/locales/no.d.ts +4 -0
  706. package/v4/locales/no.js +113 -0
  707. package/v4/locales/ota.cjs +141 -0
  708. package/v4/locales/ota.d.cts +5 -0
  709. package/v4/locales/ota.d.ts +4 -0
  710. package/v4/locales/ota.js +114 -0
  711. package/v4/locales/package.json +7 -0
  712. package/v4/locales/pl.cjs +141 -0
  713. package/v4/locales/pl.d.cts +5 -0
  714. package/v4/locales/pl.d.ts +4 -0
  715. package/v4/locales/pl.js +114 -0
  716. package/v4/locales/ps.cjs +146 -0
  717. package/v4/locales/ps.d.cts +5 -0
  718. package/v4/locales/ps.d.ts +4 -0
  719. package/v4/locales/ps.js +119 -0
  720. package/v4/locales/pt-BR.cjs +170 -0
  721. package/v4/locales/pt-BR.d.cts +5 -0
  722. package/v4/locales/pt-BR.d.ts +4 -0
  723. package/v4/locales/pt-BR.js +143 -0
  724. package/v4/locales/pt.cjs +169 -0
  725. package/v4/locales/pt.d.cts +5 -0
  726. package/v4/locales/pt.d.ts +4 -0
  727. package/v4/locales/pt.js +142 -0
  728. package/v4/locales/ro.cjs +149 -0
  729. package/v4/locales/ro.d.cts +5 -0
  730. package/v4/locales/ro.d.ts +4 -0
  731. package/v4/locales/ro.js +122 -0
  732. package/v4/locales/ru.cjs +195 -0
  733. package/v4/locales/ru.d.cts +5 -0
  734. package/v4/locales/ru.d.ts +4 -0
  735. package/v4/locales/ru.js +168 -0
  736. package/v4/locales/sk.cjs +143 -0
  737. package/v4/locales/sk.d.cts +5 -0
  738. package/v4/locales/sk.d.ts +4 -0
  739. package/v4/locales/sk.js +116 -0
  740. package/v4/locales/sl.cjs +141 -0
  741. package/v4/locales/sl.d.cts +5 -0
  742. package/v4/locales/sl.d.ts +4 -0
  743. package/v4/locales/sl.js +114 -0
  744. package/v4/locales/sv.cjs +142 -0
  745. package/v4/locales/sv.d.cts +5 -0
  746. package/v4/locales/sv.d.ts +4 -0
  747. package/v4/locales/sv.js +115 -0
  748. package/v4/locales/ta.cjs +142 -0
  749. package/v4/locales/ta.d.cts +5 -0
  750. package/v4/locales/ta.d.ts +4 -0
  751. package/v4/locales/ta.js +115 -0
  752. package/v4/locales/tg.cjs +144 -0
  753. package/v4/locales/tg.d.cts +5 -0
  754. package/v4/locales/tg.d.ts +4 -0
  755. package/v4/locales/tg.js +117 -0
  756. package/v4/locales/th.cjs +142 -0
  757. package/v4/locales/th.d.cts +5 -0
  758. package/v4/locales/th.d.ts +4 -0
  759. package/v4/locales/th.js +115 -0
  760. package/v4/locales/tk.cjs +134 -0
  761. package/v4/locales/tk.d.cts +5 -0
  762. package/v4/locales/tk.d.ts +4 -0
  763. package/v4/locales/tk.js +107 -0
  764. package/v4/locales/tr.cjs +137 -0
  765. package/v4/locales/tr.d.cts +5 -0
  766. package/v4/locales/tr.d.ts +4 -0
  767. package/v4/locales/tr.js +110 -0
  768. package/v4/locales/ua.cjs +12 -0
  769. package/v4/locales/ua.d.cts +5 -0
  770. package/v4/locales/ua.d.ts +5 -0
  771. package/v4/locales/ua.js +5 -0
  772. package/v4/locales/uk.cjs +140 -0
  773. package/v4/locales/uk.d.cts +5 -0
  774. package/v4/locales/uk.d.ts +4 -0
  775. package/v4/locales/uk.js +113 -0
  776. package/v4/locales/ur.cjs +142 -0
  777. package/v4/locales/ur.d.cts +5 -0
  778. package/v4/locales/ur.d.ts +4 -0
  779. package/v4/locales/ur.js +115 -0
  780. package/v4/locales/uz.cjs +140 -0
  781. package/v4/locales/uz.d.cts +5 -0
  782. package/v4/locales/uz.d.ts +4 -0
  783. package/v4/locales/uz.js +113 -0
  784. package/v4/locales/vi.cjs +140 -0
  785. package/v4/locales/vi.d.cts +5 -0
  786. package/v4/locales/vi.d.ts +4 -0
  787. package/v4/locales/vi.js +113 -0
  788. package/v4/locales/yo.cjs +139 -0
  789. package/v4/locales/yo.d.cts +5 -0
  790. package/v4/locales/yo.d.ts +4 -0
  791. package/v4/locales/yo.js +112 -0
  792. package/v4/locales/zh-CN.cjs +141 -0
  793. package/v4/locales/zh-CN.d.cts +5 -0
  794. package/v4/locales/zh-CN.d.ts +4 -0
  795. package/v4/locales/zh-CN.js +114 -0
  796. package/v4/locales/zh-TW.cjs +139 -0
  797. package/v4/locales/zh-TW.d.cts +5 -0
  798. package/v4/locales/zh-TW.d.ts +4 -0
  799. package/v4/locales/zh-TW.js +112 -0
  800. package/v4/mini/checks.cjs +54 -0
  801. package/v4/mini/checks.d.cts +1 -0
  802. package/v4/mini/checks.d.ts +1 -0
  803. package/v4/mini/checks.js +1 -0
  804. package/v4/mini/coerce.cjs +71 -0
  805. package/v4/mini/coerce.d.cts +7 -0
  806. package/v4/mini/coerce.d.ts +7 -0
  807. package/v4/mini/coerce.js +22 -0
  808. package/v4/mini/deep-partial.cjs +58 -0
  809. package/v4/mini/deep-partial.d.cts +14 -0
  810. package/v4/mini/deep-partial.d.ts +14 -0
  811. package/v4/mini/deep-partial.js +13 -0
  812. package/v4/mini/external.cjs +95 -0
  813. package/v4/mini/external.d.cts +16 -0
  814. package/v4/mini/external.d.ts +16 -0
  815. package/v4/mini/external.js +16 -0
  816. package/v4/mini/in-out.cjs +82 -0
  817. package/v4/mini/in-out.d.cts +8 -0
  818. package/v4/mini/in-out.d.ts +8 -0
  819. package/v4/mini/in-out.js +36 -0
  820. package/v4/mini/index.cjs +51 -0
  821. package/v4/mini/index.d.cts +3 -0
  822. package/v4/mini/index.d.ts +3 -0
  823. package/v4/mini/index.js +3 -0
  824. package/v4/mini/iso.cjs +83 -0
  825. package/v4/mini/iso.d.cts +22 -0
  826. package/v4/mini/iso.d.ts +22 -0
  827. package/v4/mini/iso.js +34 -0
  828. package/v4/mini/package.json +7 -0
  829. package/v4/mini/parse.cjs +37 -0
  830. package/v4/mini/parse.d.cts +1 -0
  831. package/v4/mini/parse.d.ts +1 -0
  832. package/v4/mini/parse.js +1 -0
  833. package/v4/mini/schemas.cjs +1147 -0
  834. package/v4/mini/schemas.d.cts +480 -0
  835. package/v4/mini/schemas.d.ts +480 -0
  836. package/v4/mini/schemas.js +1002 -0
  837. package/v4/package.json +7 -0
  838. package/v4-mini/index.cjs +51 -0
  839. package/v4-mini/index.d.cts +3 -0
  840. package/v4-mini/index.d.ts +3 -0
  841. package/v4-mini/index.js +3 -0
  842. package/v4-mini/package.json +7 -0
@@ -0,0 +1,4811 @@
1
+ import { Validator } from "@seriousme/openapi-schema-validator";
2
+ import { describe, expect, test, vi } from "vitest";
3
+ import * as z from "zod";
4
+ // import * as zCore from "zod/v4/core";
5
+
6
+ const openAPI30Validator = new Validator();
7
+ /** @see https://github.com/colinhacks/zod/issues/5147 */
8
+ const validateOpenAPI30Schema = async (zodJSONSchema: Record<string, unknown>): Promise<true> => {
9
+ const res = await openAPI30Validator.validate({
10
+ openapi: "3.0.0",
11
+ info: {
12
+ title: "SampleApi",
13
+ description: "Sample backend service",
14
+ version: "1.0.0",
15
+ },
16
+ components: { schemas: { test: zodJSONSchema } },
17
+ paths: {},
18
+ });
19
+
20
+ if (!res.valid) {
21
+ // `console.error` should make `vitest` trow an unhandled error printing the validation messages in consoles
22
+ console.error(
23
+ `OpenAPI schema is not valid against ${openAPI30Validator.version}`,
24
+ JSON.stringify(res.errors, null, 2)
25
+ );
26
+ }
27
+
28
+ return true;
29
+ };
30
+
31
+ describe("toJSONSchema", () => {
32
+ test("primitive types", () => {
33
+ expect(z.toJSONSchema(z.string())).toMatchInlineSnapshot(`
34
+ {
35
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
36
+ "type": "string",
37
+ }
38
+ `);
39
+ expect(z.toJSONSchema(z.number())).toMatchInlineSnapshot(`
40
+ {
41
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
42
+ "type": "number",
43
+ }
44
+ `);
45
+ expect(z.toJSONSchema(z.boolean())).toMatchInlineSnapshot(`
46
+ {
47
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
48
+ "type": "boolean",
49
+ }
50
+ `);
51
+ expect(z.toJSONSchema(z.null())).toMatchInlineSnapshot(`
52
+ {
53
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
54
+ "type": "null",
55
+ }
56
+ `);
57
+ expect(z.toJSONSchema(z.undefined(), { unrepresentable: "any" })).toMatchInlineSnapshot(`
58
+ {
59
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
60
+ }
61
+ `);
62
+ expect(z.toJSONSchema(z.any())).toMatchInlineSnapshot(`
63
+ {
64
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
65
+ }
66
+ `);
67
+ expect(z.toJSONSchema(z.unknown())).toMatchInlineSnapshot(`
68
+ {
69
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
70
+ }
71
+ `);
72
+ expect(z.toJSONSchema(z.never())).toMatchInlineSnapshot(`
73
+ {
74
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
75
+ "not": {},
76
+ }
77
+ `);
78
+ expect(z.toJSONSchema(z.email())).toMatchInlineSnapshot(`
79
+ {
80
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
81
+ "format": "email",
82
+ "pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$",
83
+ "type": "string",
84
+ }
85
+ `);
86
+ expect(z.toJSONSchema(z.iso.datetime())).toMatchInlineSnapshot(`
87
+ {
88
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
89
+ "format": "date-time",
90
+ "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$",
91
+ "type": "string",
92
+ }
93
+ `);
94
+ expect(z.toJSONSchema(z.iso.date())).toMatchInlineSnapshot(`
95
+ {
96
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
97
+ "format": "date",
98
+ "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$",
99
+ "type": "string",
100
+ }
101
+ `);
102
+ // only shapes keeping both an offset and seconds may advertise `date-time`, composed onto a string as well as constructed
103
+ expect(z.toJSONSchema(z.iso.datetime()).format).toEqual("date-time");
104
+ expect(z.toJSONSchema(z.iso.datetime({ offset: true })).format).toEqual("date-time");
105
+ expect(z.toJSONSchema(z.iso.datetime({ precision: 0 })).format).toEqual("date-time");
106
+ expect(z.toJSONSchema(z.iso.datetime({ local: true })).format).toEqual(undefined);
107
+ expect(z.toJSONSchema(z.iso.datetime({ precision: -1 })).format).toEqual(undefined);
108
+ expect(z.toJSONSchema(z.string().check(z.iso.datetime({ local: true }))).format).toEqual(undefined);
109
+ expect(z.toJSONSchema(z.string().check(z.iso.datetime())).format).toEqual("date-time");
110
+ // the pattern still describes what the schema accepts
111
+ expect(z.toJSONSchema(z.iso.datetime({ local: true })).pattern).toBeDefined();
112
+
113
+ expect(z.toJSONSchema(z.iso.time())).toMatchInlineSnapshot(`
114
+ {
115
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
116
+ "pattern": "^(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?$",
117
+ "type": "string",
118
+ }
119
+ `);
120
+ expect(z.toJSONSchema(z.iso.time({ precision: -1 }))).toMatchInlineSnapshot(`
121
+ {
122
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
123
+ "pattern": "^(?:[01]\\d|2[0-3]):[0-5]\\d$",
124
+ "type": "string",
125
+ }
126
+ `);
127
+ expect(z.toJSONSchema(z.iso.time({ precision: 0 }))).toMatchInlineSnapshot(`
128
+ {
129
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
130
+ "pattern": "^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$",
131
+ "type": "string",
132
+ }
133
+ `);
134
+ expect(z.toJSONSchema(z.iso.time({ precision: 3 }))).toMatchInlineSnapshot(`
135
+ {
136
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
137
+ "pattern": "^(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d\\.\\d{3}$",
138
+ "type": "string",
139
+ }
140
+ `);
141
+ expect(z.toJSONSchema(z.iso.duration())).toMatchInlineSnapshot(`
142
+ {
143
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
144
+ "format": "duration",
145
+ "pattern": "^P(?:(\\d+W)|(?!.*W)(?=\\d|T\\d)(\\d+Y)?(\\d+M)?(\\d+D)?(T(?=\\d)(\\d+H)?(\\d+M)?(\\d+([.,]\\d+)?S)?)?)$",
146
+ "type": "string",
147
+ }
148
+ `);
149
+ expect(z.toJSONSchema(z.ipv4())).toMatchInlineSnapshot(`
150
+ {
151
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
152
+ "format": "ipv4",
153
+ "pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$",
154
+ "type": "string",
155
+ }
156
+ `);
157
+ expect(z.toJSONSchema(z.ipv6())).toMatchInlineSnapshot(`
158
+ {
159
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
160
+ "format": "ipv6",
161
+ "pattern": "^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$",
162
+ "type": "string",
163
+ }
164
+ `);
165
+ expect(z.toJSONSchema(z.mac())).toMatchInlineSnapshot(`
166
+ {
167
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
168
+ "format": "mac",
169
+ "pattern": "^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$",
170
+ "type": "string",
171
+ }
172
+ `);
173
+ expect(z.toJSONSchema(z.mac({ delimiter: ":" }))).toMatchInlineSnapshot(`
174
+ {
175
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
176
+ "format": "mac",
177
+ "pattern": "^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$",
178
+ "type": "string",
179
+ }
180
+ `);
181
+ expect(z.toJSONSchema(z.mac({ delimiter: "-" }))).toMatchInlineSnapshot(`
182
+ {
183
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
184
+ "format": "mac",
185
+ "pattern": "^(?:[0-9A-F]{2}-){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}-){5}[0-9a-f]{2}$",
186
+ "type": "string",
187
+ }
188
+ `);
189
+ expect(z.toJSONSchema(z.uuid())).toMatchInlineSnapshot(`
190
+ {
191
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
192
+ "format": "uuid",
193
+ "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
194
+ "type": "string",
195
+ }
196
+ `);
197
+ expect(z.toJSONSchema(z.guid())).toMatchInlineSnapshot(`
198
+ {
199
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
200
+ "format": "uuid",
201
+ "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$",
202
+ "type": "string",
203
+ }
204
+ `);
205
+ expect(z.toJSONSchema(z.url())).toMatchInlineSnapshot(`
206
+ {
207
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
208
+ "format": "uri",
209
+ "type": "string",
210
+ }
211
+ `);
212
+ expect(z.toJSONSchema(z.base64())).toMatchInlineSnapshot(`
213
+ {
214
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
215
+ "contentEncoding": "base64",
216
+ "format": "base64",
217
+ "pattern": "^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$",
218
+ "type": "string",
219
+ }
220
+ `);
221
+ expect(z.toJSONSchema(z.base64url())).toMatchInlineSnapshot(`
222
+ {
223
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
224
+ "contentEncoding": "base64url",
225
+ "format": "base64url",
226
+ "pattern": "^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$",
227
+ "type": "string",
228
+ }
229
+ `);
230
+ expect(z.toJSONSchema(z.cuid())).toMatchInlineSnapshot(`
231
+ {
232
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
233
+ "format": "cuid",
234
+ "pattern": "^[cC][0-9a-z]{6,}$",
235
+ "type": "string",
236
+ }
237
+ `);
238
+ // expect(z.toJSONSchema(z.regex(/asdf/))).toMatchInlineSnapshot();
239
+ expect(z.toJSONSchema(z.emoji())).toMatchInlineSnapshot(`
240
+ {
241
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
242
+ "format": "emoji",
243
+ "pattern": "^(?=[\\s\\S]*[\\p{Extended_Pictographic}\\p{Regional_Indicator}\\u20E3])[\\p{Extended_Pictographic}\\p{Emoji_Component}]+$",
244
+ "type": "string",
245
+ }
246
+ `);
247
+ expect(z.toJSONSchema(z.nanoid())).toMatchInlineSnapshot(`
248
+ {
249
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
250
+ "format": "nanoid",
251
+ "pattern": "^[a-zA-Z0-9_-]{21}$",
252
+ "type": "string",
253
+ }
254
+ `);
255
+ expect(z.toJSONSchema(z.nanoid({ length: 64 }))).toMatchInlineSnapshot(`
256
+ {
257
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
258
+ "format": "nanoid",
259
+ "pattern": "^[a-zA-Z0-9_-]{64}$",
260
+ "type": "string",
261
+ }
262
+ `);
263
+ expect(z.toJSONSchema(z.cuid2())).toMatchInlineSnapshot(`
264
+ {
265
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
266
+ "format": "cuid2",
267
+ "pattern": "^[0-9a-z]+$",
268
+ "type": "string",
269
+ }
270
+ `);
271
+ expect(z.toJSONSchema(z.ulid())).toMatchInlineSnapshot(`
272
+ {
273
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
274
+ "format": "ulid",
275
+ "pattern": "^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$",
276
+ "type": "string",
277
+ }
278
+ `);
279
+ // expect(z.toJSONSchema(z.cidr())).toMatchInlineSnapshot();
280
+ expect(z.toJSONSchema(z.number())).toMatchInlineSnapshot(`
281
+ {
282
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
283
+ "type": "number",
284
+ }
285
+ `);
286
+ expect(z.toJSONSchema(z.int())).toMatchInlineSnapshot(`
287
+ {
288
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
289
+ "maximum": 9007199254740991,
290
+ "minimum": -9007199254740991,
291
+ "type": "integer",
292
+ }
293
+ `);
294
+ expect(z.toJSONSchema(z.int32())).toMatchInlineSnapshot(`
295
+ {
296
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
297
+ "maximum": 2147483647,
298
+ "minimum": -2147483648,
299
+ "type": "integer",
300
+ }
301
+ `);
302
+ expect(z.toJSONSchema(z.float32())).toMatchInlineSnapshot(`
303
+ {
304
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
305
+ "maximum": 3.4028234663852886e+38,
306
+ "minimum": -3.4028234663852886e+38,
307
+ "type": "number",
308
+ }
309
+ `);
310
+ expect(z.toJSONSchema(z.float64())).toMatchInlineSnapshot(`
311
+ {
312
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
313
+ "maximum": 1.7976931348623157e+308,
314
+ "minimum": -1.7976931348623157e+308,
315
+ "type": "number",
316
+ }
317
+ `);
318
+ expect(z.toJSONSchema(z.jwt())).toMatchInlineSnapshot(`
319
+ {
320
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
321
+ "format": "jwt",
322
+ "type": "string",
323
+ }
324
+ `);
325
+ });
326
+
327
+ test("unsupported schema types", () => {
328
+ expect(() => z.toJSONSchema(z.bigint())).toThrow("BigInt cannot be represented in JSON Schema");
329
+ expect(() => z.toJSONSchema(z.int64())).toThrow("BigInt cannot be represented in JSON Schema");
330
+ expect(() => z.toJSONSchema(z.symbol())).toThrow("Symbols cannot be represented in JSON Schema");
331
+ expect(() => z.toJSONSchema(z.void())).toThrow("Void cannot be represented in JSON Schema");
332
+ expect(() => z.toJSONSchema(z.undefined())).toThrow("Undefined cannot be represented in JSON Schema");
333
+ expect(() => z.toJSONSchema(z.date())).toThrow("Date cannot be represented in JSON Schema");
334
+ expect(() => z.toJSONSchema(z.map(z.string(), z.number()))).toThrow("Map cannot be represented in JSON Schema");
335
+ expect(() => z.toJSONSchema(z.set(z.string()))).toThrow("Set cannot be represented in JSON Schema");
336
+ expect(() => z.toJSONSchema(z.custom(() => true))).toThrow("Custom types cannot be represented in JSON Schema");
337
+
338
+ // Transform
339
+ const transformSchema = z.string().transform((val) => Number.parseInt(val));
340
+ expect(() => z.toJSONSchema(transformSchema)).toThrow("Transforms cannot be represented in JSON Schema");
341
+
342
+ // Static catch values
343
+ const staticCatchSchema = z.string().catch(() => "sup");
344
+ expect(z.toJSONSchema(staticCatchSchema)).toMatchInlineSnapshot(`
345
+ {
346
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
347
+ "default": "sup",
348
+ "type": "string",
349
+ }
350
+ `);
351
+
352
+ // Dynamic catch values
353
+ const dynamicCatchSchema = z.string().catch((ctx) => `${ctx.issues.length}`);
354
+ expect(() => z.toJSONSchema(dynamicCatchSchema)).toThrow("Dynamic catch values are not supported in JSON Schema");
355
+ expect(z.toJSONSchema(dynamicCatchSchema, { unrepresentable: "any" })).toMatchInlineSnapshot(`
356
+ {
357
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
358
+ "type": "string",
359
+ }
360
+ `);
361
+ });
362
+
363
+ test("string formats", () => {
364
+ expect(z.toJSONSchema(z.string().email())).toMatchInlineSnapshot(`
365
+ {
366
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
367
+ "format": "email",
368
+ "pattern": "^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$",
369
+ "type": "string",
370
+ }
371
+ `);
372
+ expect(z.toJSONSchema(z.string().uuid())).toMatchInlineSnapshot(`
373
+ {
374
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
375
+ "format": "uuid",
376
+ "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$",
377
+ "type": "string",
378
+ }
379
+ `);
380
+ expect(z.toJSONSchema(z.iso.datetime())).toMatchInlineSnapshot(`
381
+ {
382
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
383
+ "format": "date-time",
384
+ "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z))$",
385
+ "type": "string",
386
+ }
387
+ `);
388
+
389
+ expect(z.toJSONSchema(z.iso.date())).toMatchInlineSnapshot(`
390
+ {
391
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
392
+ "format": "date",
393
+ "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$",
394
+ "type": "string",
395
+ }
396
+ `);
397
+ expect(z.toJSONSchema(z.iso.time())).toMatchInlineSnapshot(`
398
+ {
399
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
400
+ "pattern": "^(?:[01]\\d|2[0-3]):[0-5]\\d(?::[0-5]\\d(?:\\.\\d+)?)?$",
401
+ "type": "string",
402
+ }
403
+ `);
404
+ expect(z.toJSONSchema(z.iso.duration())).toMatchInlineSnapshot(`
405
+ {
406
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
407
+ "format": "duration",
408
+ "pattern": "^P(?:(\\d+W)|(?!.*W)(?=\\d|T\\d)(\\d+Y)?(\\d+M)?(\\d+D)?(T(?=\\d)(\\d+H)?(\\d+M)?(\\d+([.,]\\d+)?S)?)?)$",
409
+ "type": "string",
410
+ }
411
+ `);
412
+ // expect(z.toJSONSchema(z.string().ip())).toMatchInlineSnapshot(`
413
+ // {
414
+ // "pattern": /\\(\\^\\(\\?:\\(\\?:25\\[0-5\\]\\|2\\[0-4\\]\\[0-9\\]\\|1\\[0-9\\]\\[0-9\\]\\|\\[1-9\\]\\[0-9\\]\\|\\[0-9\\]\\)\\\\\\.\\)\\{3\\}\\(\\?:25\\[0-5\\]\\|2\\[0-4\\]\\[0-9\\]\\|1\\[0-9\\]\\[0-9\\]\\|\\[1-9\\]\\[0-9\\]\\|\\[0-9\\]\\)\\$\\)\\|\\(\\^\\(\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{7\\}\\|::\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,6\\}\\|\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{1\\}:\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,5\\}\\|\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{2\\}:\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,4\\}\\|\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{3\\}:\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,3\\}\\|\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{4\\}:\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,2\\}\\|\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{5\\}:\\(\\[a-fA-F0-9\\]\\{1,4\\}:\\)\\{0,1\\}\\)\\(\\[a-fA-F0-9\\]\\{1,4\\}\\|\\(\\(\\(25\\[0-5\\]\\)\\|\\(2\\[0-4\\]\\[0-9\\]\\)\\|\\(1\\[0-9\\]\\{2\\}\\)\\|\\(\\[0-9\\]\\{1,2\\}\\)\\)\\\\\\.\\)\\{3\\}\\(\\(25\\[0-5\\]\\)\\|\\(2\\[0-4\\]\\[0-9\\]\\)\\|\\(1\\[0-9\\]\\{2\\}\\)\\|\\(\\[0-9\\]\\{1,2\\}\\)\\)\\)\\$\\)/,
415
+ // "type": "string",
416
+ // }
417
+ // `);
418
+ expect(z.toJSONSchema(z.ipv4())).toMatchInlineSnapshot(`
419
+ {
420
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
421
+ "format": "ipv4",
422
+ "pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$",
423
+ "type": "string",
424
+ }
425
+ `);
426
+
427
+ expect(z.toJSONSchema(z.ipv6())).toMatchInlineSnapshot(`
428
+ {
429
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
430
+ "format": "ipv6",
431
+ "pattern": "^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$",
432
+ "type": "string",
433
+ }
434
+ `);
435
+
436
+ expect(z.toJSONSchema(z.mac())).toMatchInlineSnapshot(`
437
+ {
438
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
439
+ "format": "mac",
440
+ "pattern": "^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$",
441
+ "type": "string",
442
+ }
443
+ `);
444
+ expect(z.toJSONSchema(z.mac({ delimiter: ":" }))).toMatchInlineSnapshot(`
445
+ {
446
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
447
+ "format": "mac",
448
+ "pattern": "^(?:[0-9A-F]{2}:){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$",
449
+ "type": "string",
450
+ }
451
+ `);
452
+ expect(z.toJSONSchema(z.mac({ delimiter: "-" }))).toMatchInlineSnapshot(`
453
+ {
454
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
455
+ "format": "mac",
456
+ "pattern": "^(?:[0-9A-F]{2}-){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}-){5}[0-9a-f]{2}$",
457
+ "type": "string",
458
+ }
459
+ `);
460
+
461
+ expect(z.toJSONSchema(z.base64())).toMatchInlineSnapshot(`
462
+ {
463
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
464
+ "contentEncoding": "base64",
465
+ "format": "base64",
466
+ "pattern": "^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$",
467
+ "type": "string",
468
+ }
469
+ `);
470
+ expect(z.toJSONSchema(z.url())).toMatchInlineSnapshot(`
471
+ {
472
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
473
+ "format": "uri",
474
+ "type": "string",
475
+ }
476
+ `);
477
+ expect(z.toJSONSchema(z.guid())).toMatchInlineSnapshot(`
478
+ {
479
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
480
+ "format": "uuid",
481
+ "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$",
482
+ "type": "string",
483
+ }
484
+ `);
485
+ expect(z.toJSONSchema(z.string().regex(/asdf/))).toMatchInlineSnapshot(`
486
+ {
487
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
488
+ "pattern": "asdf",
489
+ "type": "string",
490
+ }
491
+ `);
492
+ });
493
+
494
+ test("base64url pattern agrees with parse", () => {
495
+ const schema = z.base64url();
496
+ const pattern = new RegExp(z.toJSONSchema(schema).pattern!);
497
+ // lengths 1 (mod 4) carry no whole byte; the emitted pattern must reject them like parse does
498
+ for (const s of ["", "A", "AA", "AAA", "AAAA", "AAAAA"]) {
499
+ expect(pattern.test(s)).toBe(schema.safeParse(s).success);
500
+ }
501
+ });
502
+
503
+ test("base64 pattern agrees with parse", () => {
504
+ const schema = z.base64();
505
+ const pattern = new RegExp(z.toJSONSchema(schema).pattern!);
506
+ // the runtime def.pattern is lax; the emitted pattern must still carry the block structure parse enforces
507
+ for (const s of ["", "A", "AA", "AAA", "AAAA", "AAAAA", "AA==", "AAA=", "A===", "=", "AAAA=="]) {
508
+ expect(pattern.test(s)).toBe(schema.safeParse(s).success);
509
+ }
510
+ });
511
+
512
+ test("looseRecord with a format key emits the exact pattern", () => {
513
+ // recordProcessor reads bag.patterns directly; it must apply the same lax-to-exact swap as stringProcessor
514
+ const json = z.toJSONSchema(z.looseRecord(z.base64url(), z.number()));
515
+ expect(Object.keys(json.patternProperties!)).toEqual([z.regexes.base64url.source]);
516
+ });
517
+
518
+ test("string patterns", () => {
519
+ expect(
520
+ z.toJSONSchema(
521
+ z
522
+ .string()
523
+ .startsWith("hello")
524
+ .includes("cruel")
525
+ .includes("dark", { position: 10 })
526
+ .endsWith("world")
527
+ .regex(/stuff/)
528
+ )
529
+ ).toMatchInlineSnapshot(`
530
+ {
531
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
532
+ "allOf": [
533
+ {
534
+ "pattern": "^hello.*",
535
+ },
536
+ {
537
+ "pattern": "cruel",
538
+ },
539
+ {
540
+ "pattern": "^.{10,}dark",
541
+ },
542
+ {
543
+ "pattern": ".*world$",
544
+ },
545
+ {
546
+ "pattern": "stuff",
547
+ },
548
+ ],
549
+ "type": "string",
550
+ }
551
+ `);
552
+
553
+ expect(
554
+ z.toJSONSchema(
555
+ z
556
+ .string()
557
+ .startsWith("hello")
558
+ .includes("cruel")
559
+ .includes("dark", { position: 10 })
560
+ .endsWith("world")
561
+ .regex(/stuff/),
562
+ {
563
+ target: "draft-7",
564
+ }
565
+ )
566
+ ).toMatchInlineSnapshot(`
567
+ {
568
+ "$schema": "http://json-schema.org/draft-07/schema#",
569
+ "allOf": [
570
+ {
571
+ "pattern": "^hello.*",
572
+ "type": "string",
573
+ },
574
+ {
575
+ "pattern": "cruel",
576
+ "type": "string",
577
+ },
578
+ {
579
+ "pattern": "^.{10,}dark",
580
+ "type": "string",
581
+ },
582
+ {
583
+ "pattern": ".*world$",
584
+ "type": "string",
585
+ },
586
+ {
587
+ "pattern": "stuff",
588
+ "type": "string",
589
+ },
590
+ ],
591
+ "type": "string",
592
+ }
593
+ `);
594
+ });
595
+
596
+ test("includes, startsWith and endsWith emit only a pattern, not a format", () => {
597
+ expect(z.toJSONSchema(z.string().includes("x"))).toEqual({
598
+ $schema: "https://json-schema.org/draft/2020-12/schema",
599
+ type: "string",
600
+ pattern: "x",
601
+ });
602
+ expect(z.toJSONSchema(z.string().startsWith("x"))).toEqual({
603
+ $schema: "https://json-schema.org/draft/2020-12/schema",
604
+ type: "string",
605
+ pattern: "^x.*",
606
+ });
607
+ expect(z.toJSONSchema(z.string().endsWith("x"))).toEqual({
608
+ $schema: "https://json-schema.org/draft/2020-12/schema",
609
+ type: "string",
610
+ pattern: ".*x$",
611
+ });
612
+
613
+ // a real format survives these checks in either chain order, on every target
614
+ for (const target of ["draft-2020-12", "draft-7", "openapi-3.0"] as const) {
615
+ for (const [format, base, sub] of [
616
+ ["uri", z.url(), z.string()],
617
+ ["email", z.email(), z.string()],
618
+ ] as const) {
619
+ for (const check of ["includes", "startsWith", "endsWith"] as const) {
620
+ expect(z.toJSONSchema(base[check]("x"), { target })).toMatchObject({ format });
621
+ expect(z.toJSONSchema(sub[check]("x")[format === "uri" ? "url" : "email"](), { target })).toMatchObject({
622
+ format,
623
+ });
624
+ }
625
+ }
626
+ }
627
+
628
+ // lowercase and uppercase still emit their own format
629
+ expect(z.toJSONSchema(z.string().lowercase())).toMatchObject({ format: "lowercase" });
630
+ expect(z.toJSONSchema(z.string().uppercase())).toMatchObject({ format: "uppercase" });
631
+ });
632
+
633
+ test("includes with position emits a pattern matching runtime semantics", () => {
634
+ // String.prototype.includes(sub, position) matches `sub` at `position` OR
635
+ // LATER, so the JSON Schema pattern must allow >= position leading chars.
636
+ const schema = z.string().includes("foo", { position: 2 });
637
+ const json = z.toJSONSchema(schema) as { pattern?: string };
638
+ expect(json.pattern).toBe("^.{2,}foo");
639
+
640
+ const re = new RegExp(json.pattern!);
641
+ for (const input of ["xxfoo", "xxxfoo", "xfoo", "ab"]) {
642
+ expect(re.test(input)).toBe(schema.safeParse(input).success);
643
+ }
644
+ });
645
+
646
+ test("number constraints", () => {
647
+ expect(z.toJSONSchema(z.number().min(5).max(10))).toMatchInlineSnapshot(
648
+ `
649
+ {
650
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
651
+ "maximum": 10,
652
+ "minimum": 5,
653
+ "type": "number",
654
+ }
655
+ `
656
+ );
657
+
658
+ expect(z.toJSONSchema(z.number().gt(5).gt(10))).toMatchInlineSnapshot(`
659
+ {
660
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
661
+ "exclusiveMinimum": 10,
662
+ "type": "number",
663
+ }
664
+ `);
665
+
666
+ expect(z.toJSONSchema(z.number().gt(5).gte(10))).toMatchInlineSnapshot(`
667
+ {
668
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
669
+ "minimum": 10,
670
+ "type": "number",
671
+ }
672
+ `);
673
+
674
+ expect(z.toJSONSchema(z.number().lt(5).lt(3))).toMatchInlineSnapshot(`
675
+ {
676
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
677
+ "exclusiveMaximum": 3,
678
+ "type": "number",
679
+ }
680
+ `);
681
+
682
+ expect(z.toJSONSchema(z.number().lt(5).lt(3).lte(2))).toMatchInlineSnapshot(`
683
+ {
684
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
685
+ "maximum": 2,
686
+ "type": "number",
687
+ }
688
+ `);
689
+
690
+ expect(z.toJSONSchema(z.number().lt(5).lte(3))).toMatchInlineSnapshot(`
691
+ {
692
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
693
+ "maximum": 3,
694
+ "type": "number",
695
+ }
696
+ `);
697
+
698
+ expect(z.toJSONSchema(z.number().gt(5).lt(10))).toMatchInlineSnapshot(`
699
+ {
700
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
701
+ "exclusiveMaximum": 10,
702
+ "exclusiveMinimum": 5,
703
+ "type": "number",
704
+ }
705
+ `);
706
+ expect(z.toJSONSchema(z.number().gte(5).lte(10))).toMatchInlineSnapshot(`
707
+ {
708
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
709
+ "maximum": 10,
710
+ "minimum": 5,
711
+ "type": "number",
712
+ }
713
+ `);
714
+ expect(z.toJSONSchema(z.number().positive())).toMatchInlineSnapshot(`
715
+ {
716
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
717
+ "exclusiveMinimum": 0,
718
+ "type": "number",
719
+ }
720
+ `);
721
+ expect(z.toJSONSchema(z.number().negative())).toMatchInlineSnapshot(`
722
+ {
723
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
724
+ "exclusiveMaximum": 0,
725
+ "type": "number",
726
+ }
727
+ `);
728
+ expect(z.toJSONSchema(z.number().nonpositive())).toMatchInlineSnapshot(`
729
+ {
730
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
731
+ "maximum": 0,
732
+ "type": "number",
733
+ }
734
+ `);
735
+ expect(z.toJSONSchema(z.number().nonnegative())).toMatchInlineSnapshot(`
736
+ {
737
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
738
+ "minimum": 0,
739
+ "type": "number",
740
+ }
741
+ `);
742
+ });
743
+
744
+ test("number constraints draft-4", () => {
745
+ expect(z.toJSONSchema(z.number().gt(5).lt(10), { target: "draft-4" })).toMatchInlineSnapshot(`
746
+ {
747
+ "$schema": "http://json-schema.org/draft-04/schema#",
748
+ "exclusiveMaximum": true,
749
+ "exclusiveMinimum": true,
750
+ "maximum": 10,
751
+ "minimum": 5,
752
+ "type": "number",
753
+ }
754
+ `);
755
+ });
756
+
757
+ test("number constraints intersection draft-04", () => {
758
+ // When both minimum (from .int()) and exclusiveMinimum (from .positive()) exist, the more restrictive constraint should be used
759
+ expect(z.toJSONSchema(z.number().int().positive().lte(65535), { target: "draft-04" })).toMatchInlineSnapshot(`
760
+ {
761
+ "$schema": "http://json-schema.org/draft-04/schema#",
762
+ "exclusiveMinimum": true,
763
+ "maximum": 65535,
764
+ "minimum": 0,
765
+ "type": "integer",
766
+ }
767
+ `);
768
+ // Same for openapi-3.0
769
+ expect(z.toJSONSchema(z.number().int().positive().lte(65535), { target: "openapi-3.0" })).toMatchInlineSnapshot(`
770
+ {
771
+ "exclusiveMinimum": true,
772
+ "maximum": 65535,
773
+ "minimum": 0,
774
+ "type": "integer",
775
+ }
776
+ `);
777
+ // When inclusive minimum is more restrictive than exclusive minimum
778
+ expect(z.toJSONSchema(z.number().gt(3).gte(10), { target: "draft-04" })).toMatchInlineSnapshot(`
779
+ {
780
+ "$schema": "http://json-schema.org/draft-04/schema#",
781
+ "minimum": 10,
782
+ "type": "number",
783
+ }
784
+ `);
785
+ // Same logic for maximum constraints
786
+ expect(z.toJSONSchema(z.number().int().negative(), { target: "draft-04" })).toMatchInlineSnapshot(`
787
+ {
788
+ "$schema": "http://json-schema.org/draft-04/schema#",
789
+ "exclusiveMaximum": true,
790
+ "maximum": 0,
791
+ "minimum": -9007199254740991,
792
+ "type": "integer",
793
+ }
794
+ `);
795
+ });
796
+
797
+ test("number constraints order independence with int()", () => {
798
+ // .int() applied last should not overwrite the tighter bounds .min()/.max() already stored
799
+ expect(z.toJSONSchema(z.number().int().min(0).max(23))).toMatchInlineSnapshot(`
800
+ {
801
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
802
+ "maximum": 23,
803
+ "minimum": 0,
804
+ "type": "integer",
805
+ }
806
+ `);
807
+ expect(z.toJSONSchema(z.number().min(0).max(23).int())).toMatchInlineSnapshot(`
808
+ {
809
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
810
+ "maximum": 23,
811
+ "minimum": 0,
812
+ "type": "integer",
813
+ }
814
+ `);
815
+ });
816
+
817
+ test("chained multipleOf divisors emit the whole conjunction", () => {
818
+ // runtime rejects 4 (not a multiple of 3), so the emitted schema must too
819
+ expect(z.toJSONSchema(z.number().multipleOf(2).multipleOf(3))).toMatchInlineSnapshot(`
820
+ {
821
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
822
+ "allOf": [
823
+ {
824
+ "multipleOf": 3,
825
+ },
826
+ ],
827
+ "multipleOf": 2,
828
+ "type": "number",
829
+ }
830
+ `);
831
+ });
832
+
833
+ test("length() does not overwrite a tighter earlier bound", () => {
834
+ // min(8) + length(5) matches nothing at runtime; the emitted bounds stay honest about that
835
+ expect(z.toJSONSchema(z.string().min(8).length(5))).toMatchInlineSnapshot(`
836
+ {
837
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
838
+ "maxLength": 5,
839
+ "minLength": 8,
840
+ "type": "string",
841
+ }
842
+ `);
843
+ });
844
+
845
+ test("an integer format keeps the emitted type integer whatever format lands last", () => {
846
+ // runtime rejects 1.5 in both orders, so both must emit type integer
847
+ const after = z.number().int().check(z.float32());
848
+ expect(after.safeParse(1.5).success).toBe(false);
849
+ expect(z.toJSONSchema(after).type).toBe("integer");
850
+ expect(z.toJSONSchema(z.number().check(z.float32()).int()).type).toBe("integer");
851
+ });
852
+
853
+ test("disjoint mime checks emit a false schema, not an unconstrained file", () => {
854
+ const file = z.file().mime("image/png").mime("text/plain");
855
+ expect(z.toJSONSchema(file)).toMatchInlineSnapshot(`
856
+ {
857
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
858
+ "contentEncoding": "binary",
859
+ "format": "binary",
860
+ "not": {},
861
+ "type": "string",
862
+ }
863
+ `);
864
+ });
865
+
866
+ test("a custom mime narrowing via onattach intersects with built-in mime checks", () => {
867
+ const check = {
868
+ _zod: {
869
+ def: { check: "custom_mime" },
870
+ onattach: [
871
+ (inst: any) => {
872
+ inst._zod.bag.mime = ["text/plain"];
873
+ },
874
+ ],
875
+ check: () => {},
876
+ },
877
+ };
878
+ const file = z
879
+ .file()
880
+ .mime(["image/png", "text/plain"])
881
+ .check(check as any);
882
+ expect(z.toJSONSchema(file).contentMediaType).toBe("text/plain");
883
+ });
884
+
885
+ test("a custom check contributing via onattach still lands", () => {
886
+ // third-party checks have no converter handler; their bag writes are merged in as a fallback
887
+ const check = {
888
+ _zod: {
889
+ def: { check: "custom_range" },
890
+ onattach: [
891
+ (inst: any) => {
892
+ const bag = inst._zod.bag;
893
+ if (bag.minimum === undefined || 5 > bag.minimum) bag.minimum = 5;
894
+ },
895
+ ],
896
+ check: () => {},
897
+ },
898
+ };
899
+ expect(z.toJSONSchema(z.number().check(check as any))).toMatchInlineSnapshot(`
900
+ {
901
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
902
+ "minimum": 5,
903
+ "type": "number",
904
+ }
905
+ `);
906
+ });
907
+
908
+ test("target normalization draft-04 and draft-07", () => {
909
+ // Test that both old (draft-4, draft-7) and new (draft-04, draft-07) target formats work
910
+
911
+ // Test draft-04 / draft-4
912
+ expect(z.toJSONSchema(z.number().gt(5), { target: "draft-04" })).toMatchInlineSnapshot(`
913
+ {
914
+ "$schema": "http://json-schema.org/draft-04/schema#",
915
+ "exclusiveMinimum": true,
916
+ "minimum": 5,
917
+ "type": "number",
918
+ }
919
+ `);
920
+ expect(z.toJSONSchema(z.number().gt(5), { target: "draft-4" })).toMatchInlineSnapshot(`
921
+ {
922
+ "$schema": "http://json-schema.org/draft-04/schema#",
923
+ "exclusiveMinimum": true,
924
+ "minimum": 5,
925
+ "type": "number",
926
+ }
927
+ `);
928
+ // Test draft-07 / draft-7
929
+ expect(z.toJSONSchema(z.number().gt(5), { target: "draft-07" })).toMatchInlineSnapshot(`
930
+ {
931
+ "$schema": "http://json-schema.org/draft-07/schema#",
932
+ "exclusiveMinimum": 5,
933
+ "type": "number",
934
+ }
935
+ `);
936
+ expect(z.toJSONSchema(z.number().gt(5), { target: "draft-7" })).toMatchInlineSnapshot(`
937
+ {
938
+ "$schema": "http://json-schema.org/draft-07/schema#",
939
+ "exclusiveMinimum": 5,
940
+ "type": "number",
941
+ }
942
+ `);
943
+ });
944
+
945
+ test("nullable openapi-3.0", () => {
946
+ const schema = z.string().nullable();
947
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
948
+ validateOpenAPI30Schema(jsonSchema);
949
+ expect(jsonSchema).toMatchInlineSnapshot(`
950
+ {
951
+ "nullable": true,
952
+ "type": "string",
953
+ }
954
+ `);
955
+ });
956
+
957
+ test("union with null openapi-3.0", () => {
958
+ const schema = z.union([z.string(), z.null()]);
959
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
960
+ validateOpenAPI30Schema(jsonSchema);
961
+ expect(jsonSchema).toMatchInlineSnapshot(`
962
+ {
963
+ "anyOf": [
964
+ {
965
+ "type": "string",
966
+ },
967
+ {
968
+ "enum": [
969
+ null,
970
+ ],
971
+ "nullable": true,
972
+ "type": "string",
973
+ },
974
+ ],
975
+ }
976
+ `);
977
+ });
978
+
979
+ test("number with exclusive min-max openapi-3.0", () => {
980
+ const schema = z.number().lt(100).gt(1);
981
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
982
+ validateOpenAPI30Schema(jsonSchema);
983
+ expect(jsonSchema).toMatchInlineSnapshot(`
984
+ {
985
+ "exclusiveMaximum": true,
986
+ "exclusiveMinimum": true,
987
+ "maximum": 100,
988
+ "minimum": 1,
989
+ "type": "number",
990
+ }
991
+ `);
992
+ });
993
+
994
+ test("arrays", () => {
995
+ expect(z.toJSONSchema(z.array(z.string()))).toMatchInlineSnapshot(`
996
+ {
997
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
998
+ "items": {
999
+ "type": "string",
1000
+ },
1001
+ "type": "array",
1002
+ }
1003
+ `);
1004
+ });
1005
+
1006
+ test("unions", () => {
1007
+ const schema = z.union([z.string(), z.number()]);
1008
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1009
+ {
1010
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1011
+ "type": [
1012
+ "string",
1013
+ "number",
1014
+ ],
1015
+ }
1016
+ `);
1017
+ });
1018
+
1019
+ test("nullable compacts to a type array", () => {
1020
+ expect(z.toJSONSchema(z.string().nullable())).toMatchInlineSnapshot(`
1021
+ {
1022
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1023
+ "type": [
1024
+ "string",
1025
+ "null",
1026
+ ],
1027
+ }
1028
+ `);
1029
+
1030
+ // a nested union folds into the outer type array rather than leaving a mixed shape
1031
+ expect(z.toJSONSchema(z.union([z.string(), z.number()]).nullable())).toMatchInlineSnapshot(`
1032
+ {
1033
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1034
+ "type": [
1035
+ "string",
1036
+ "number",
1037
+ "null",
1038
+ ],
1039
+ }
1040
+ `);
1041
+
1042
+ // the null branch is bare but the object branch is not, so this stays an anyOf
1043
+ expect(z.toJSONSchema(z.object({ a: z.string() }).nullable())).toMatchInlineSnapshot(`
1044
+ {
1045
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1046
+ "anyOf": [
1047
+ {
1048
+ "additionalProperties": false,
1049
+ "properties": {
1050
+ "a": {
1051
+ "type": "string",
1052
+ },
1053
+ },
1054
+ "required": [
1055
+ "a",
1056
+ ],
1057
+ "type": "object",
1058
+ },
1059
+ {
1060
+ "type": "null",
1061
+ },
1062
+ ],
1063
+ }
1064
+ `);
1065
+ });
1066
+
1067
+ test("duplicate branches dedupe, single branch stays a bare type", () => {
1068
+ // a `type` array must have unique members, so two branches that erase to the same bare type collapse
1069
+ const refined = z.union([z.string().refine((s) => s.length > 2), z.string().refine((s) => s.length < 9)]);
1070
+ expect(z.toJSONSchema(refined)).toMatchInlineSnapshot(`
1071
+ {
1072
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1073
+ "type": "string",
1074
+ }
1075
+ `);
1076
+ expect(z.toJSONSchema(z.union([z.string()]))).toMatchInlineSnapshot(`
1077
+ {
1078
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1079
+ "type": "string",
1080
+ }
1081
+ `);
1082
+ });
1083
+
1084
+ test("compaction runs after overrides and ref extraction", () => {
1085
+ // an override decorates the string branch, so it is no longer a bare type and must survive in anyOf
1086
+ const decorated = z.toJSONSchema(z.union([z.string(), z.number()]), {
1087
+ override(ctx) {
1088
+ if (ctx.zodSchema._zod.def.type === "string") ctx.jsonSchema.whatever = "sup";
1089
+ },
1090
+ });
1091
+ expect(decorated).toMatchInlineSnapshot(`
1092
+ {
1093
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1094
+ "anyOf": [
1095
+ {
1096
+ "type": "string",
1097
+ "whatever": "sup",
1098
+ },
1099
+ {
1100
+ "type": "number",
1101
+ },
1102
+ ],
1103
+ }
1104
+ `);
1105
+
1106
+ // a branch extracted into $defs is a $ref, not a bare type, so `reused` is not silently bypassed
1107
+ const shared = z.string();
1108
+ expect(
1109
+ z.toJSONSchema(z.object({ a: shared, b: z.union([shared, z.null()]) }), { reused: "ref" })
1110
+ ).toMatchInlineSnapshot(`
1111
+ {
1112
+ "$defs": {
1113
+ "__schema0": {
1114
+ "type": "string",
1115
+ },
1116
+ },
1117
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1118
+ "additionalProperties": false,
1119
+ "properties": {
1120
+ "a": {
1121
+ "$ref": "#/$defs/__schema0",
1122
+ },
1123
+ "b": {
1124
+ "anyOf": [
1125
+ {
1126
+ "$ref": "#/$defs/__schema0",
1127
+ },
1128
+ {
1129
+ "type": "null",
1130
+ },
1131
+ ],
1132
+ },
1133
+ },
1134
+ "required": [
1135
+ "a",
1136
+ "b",
1137
+ ],
1138
+ "type": "object",
1139
+ }
1140
+ `);
1141
+ });
1142
+
1143
+ test("openapi-3.0 keeps a single-string type", () => {
1144
+ // OpenAPI 3.0 has no type arrays; nullability is spelled with `nullable`
1145
+ expect(z.toJSONSchema(z.string().nullable(), { target: "openapi-3.0" })).toMatchInlineSnapshot(`
1146
+ {
1147
+ "nullable": true,
1148
+ "type": "string",
1149
+ }
1150
+ `);
1151
+ });
1152
+
1153
+ test("union with constrained branch is not compacted", () => {
1154
+ const schema = z.union([z.string().min(1), z.number()]);
1155
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1156
+ {
1157
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1158
+ "anyOf": [
1159
+ {
1160
+ "minLength": 1,
1161
+ "type": "string",
1162
+ },
1163
+ {
1164
+ "type": "number",
1165
+ },
1166
+ ],
1167
+ }
1168
+ `);
1169
+ });
1170
+
1171
+ test("union of literals is not compacted", () => {
1172
+ const schema = z.union([z.literal("a"), z.literal("b")]);
1173
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1174
+ {
1175
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1176
+ "anyOf": [
1177
+ {
1178
+ "const": "a",
1179
+ "type": "string",
1180
+ },
1181
+ {
1182
+ "const": "b",
1183
+ "type": "string",
1184
+ },
1185
+ ],
1186
+ }
1187
+ `);
1188
+ });
1189
+
1190
+ test("discriminated unions", () => {
1191
+ const schema = z.discriminatedUnion("type", [
1192
+ z.object({ type: z.literal("success"), data: z.string() }),
1193
+ z.object({ type: z.literal("error"), message: z.string() }),
1194
+ ]);
1195
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1196
+ {
1197
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1198
+ "oneOf": [
1199
+ {
1200
+ "additionalProperties": false,
1201
+ "properties": {
1202
+ "data": {
1203
+ "type": "string",
1204
+ },
1205
+ "type": {
1206
+ "const": "success",
1207
+ "type": "string",
1208
+ },
1209
+ },
1210
+ "required": [
1211
+ "type",
1212
+ "data",
1213
+ ],
1214
+ "type": "object",
1215
+ },
1216
+ {
1217
+ "additionalProperties": false,
1218
+ "properties": {
1219
+ "message": {
1220
+ "type": "string",
1221
+ },
1222
+ "type": {
1223
+ "const": "error",
1224
+ "type": "string",
1225
+ },
1226
+ },
1227
+ "required": [
1228
+ "type",
1229
+ "message",
1230
+ ],
1231
+ "type": "object",
1232
+ },
1233
+ ],
1234
+ }
1235
+ `);
1236
+ });
1237
+
1238
+ test("intersections", () => {
1239
+ const schema = z.intersection(z.object({ name: z.string() }), z.object({ age: z.number() }));
1240
+
1241
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1242
+ {
1243
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1244
+ "additionalProperties": false,
1245
+ "properties": {
1246
+ "age": {
1247
+ "type": "number",
1248
+ },
1249
+ "name": {
1250
+ "type": "string",
1251
+ },
1252
+ },
1253
+ "required": [
1254
+ "name",
1255
+ "age",
1256
+ ],
1257
+ "type": "object",
1258
+ }
1259
+ `);
1260
+ });
1261
+
1262
+ test("record", () => {
1263
+ const schema = z.record(z.string(), z.boolean());
1264
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1265
+ {
1266
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1267
+ "additionalProperties": {
1268
+ "type": "boolean",
1269
+ },
1270
+ "propertyNames": {
1271
+ "type": "string",
1272
+ },
1273
+ "type": "object",
1274
+ }
1275
+ `);
1276
+ });
1277
+
1278
+ test("record openapi-3.0", () => {
1279
+ const schema = z.record(z.string(), z.boolean());
1280
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
1281
+ validateOpenAPI30Schema(jsonSchema);
1282
+ expect(jsonSchema).toMatchInlineSnapshot(`
1283
+ {
1284
+ "additionalProperties": {
1285
+ "type": "boolean",
1286
+ },
1287
+ "type": "object",
1288
+ }
1289
+ `);
1290
+ });
1291
+
1292
+ test("record with enum keys adds required", () => {
1293
+ const schema = z.record(z.enum(["key1", "key2"]), z.number());
1294
+
1295
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1296
+ {
1297
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1298
+ "additionalProperties": {
1299
+ "type": "number",
1300
+ },
1301
+ "propertyNames": {
1302
+ "enum": [
1303
+ "key1",
1304
+ "key2",
1305
+ ],
1306
+ "type": "string",
1307
+ },
1308
+ "required": [
1309
+ "key1",
1310
+ "key2",
1311
+ ],
1312
+ "type": "object",
1313
+ }
1314
+ `);
1315
+ });
1316
+
1317
+ test("record with enum keys drops required for an optional-in value under io: input", () => {
1318
+ const schema = z.record(z.enum(["key1", "key2"]), z.number().default(0));
1319
+
1320
+ expect(z.toJSONSchema(schema, { io: "input" }).required).toBeUndefined();
1321
+ expect(z.toJSONSchema(schema).required).toEqual(["key1", "key2"]);
1322
+ expect(z.toJSONSchema(z.record(z.enum(["key1", "key2"]), z.number()), { io: "input" }).required).toEqual([
1323
+ "key1",
1324
+ "key2",
1325
+ ]);
1326
+ // A caught value keeps required, matching the input type and z.object().
1327
+ expect(z.toJSONSchema(z.record(z.enum(["key1", "key2"]), z.number().catch(0)), { io: "input" }).required).toEqual([
1328
+ "key1",
1329
+ "key2",
1330
+ ]);
1331
+ });
1332
+
1333
+ test("record stringifies numeric enum keys for propertyNames and required", () => {
1334
+ enum NumberEnum {
1335
+ Zero = 0,
1336
+ One = 1,
1337
+ }
1338
+ const schema = z.record(z.enum(NumberEnum), z.string());
1339
+
1340
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1341
+ {
1342
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1343
+ "additionalProperties": {
1344
+ "type": "string",
1345
+ },
1346
+ "propertyNames": {
1347
+ "enum": [
1348
+ "0",
1349
+ "1",
1350
+ ],
1351
+ "type": "string",
1352
+ },
1353
+ "required": [
1354
+ "0",
1355
+ "1",
1356
+ ],
1357
+ "type": "object",
1358
+ }
1359
+ `);
1360
+ });
1361
+
1362
+ test("record with a numeric key emits propertyNames over the numeric-string form", () => {
1363
+ expect(z.toJSONSchema(z.record(z.number(), z.boolean())).propertyNames).toEqual({
1364
+ type: "string",
1365
+ pattern: "^-?\\d+(?:\\.\\d+)?$",
1366
+ });
1367
+ // range checks can't apply to a key, so only the integer shape survives
1368
+ expect(z.toJSONSchema(z.record(z.int32(), z.boolean())).propertyNames).toEqual({
1369
+ type: "string",
1370
+ pattern: "^-?\\d+$",
1371
+ });
1372
+ expect(z.toJSONSchema(z.record(z.literal([1, 2]), z.boolean()))).toMatchObject({
1373
+ propertyNames: { type: "string", enum: ["1", "2"] },
1374
+ required: ["1", "2"],
1375
+ });
1376
+ expect(z.toJSONSchema(z.record(z.literal(1), z.boolean())).propertyNames).toEqual({
1377
+ type: "string",
1378
+ const: "1",
1379
+ });
1380
+ });
1381
+
1382
+ test("record key rewrite reaches through wrappers and union branches", () => {
1383
+ const numericString = { type: "string", pattern: "^-?\\d+(?:\\.\\d+)?$" };
1384
+ // a wrapper only carries its inner type once the refs are flattened, so this is decided after the record itself is emitted
1385
+ expect(
1386
+ z.toJSONSchema(
1387
+ z.record(
1388
+ z.lazy(() => z.number()),
1389
+ z.boolean()
1390
+ )
1391
+ ).propertyNames
1392
+ ).toEqual(numericString);
1393
+ expect(z.toJSONSchema(z.record(z.number().pipe(z.number()), z.boolean())).propertyNames).toEqual(numericString);
1394
+ expect(z.toJSONSchema(z.record(z.number().readonly(), z.boolean())).propertyNames).toMatchObject(numericString);
1395
+ expect(z.toJSONSchema(z.record(z.union([z.literal("Tuna"), z.literal(21)]), z.string()))).toMatchObject({
1396
+ propertyNames: {
1397
+ anyOf: [
1398
+ { type: "string", const: "Tuna" },
1399
+ { type: "string", const: "21" },
1400
+ ],
1401
+ },
1402
+ required: ["Tuna", "21"],
1403
+ });
1404
+ });
1405
+
1406
+ test("record with a numeric key inlines an extracted key, and leaves a string one referenced", () => {
1407
+ expect(z.toJSONSchema(z.record(z.number().meta({ id: "Num" }), z.boolean())).propertyNames).toEqual({
1408
+ type: "string",
1409
+ pattern: "^-?\\d+(?:\\.\\d+)?$",
1410
+ });
1411
+ expect(z.toJSONSchema(z.record(z.string().meta({ id: "Str" }), z.boolean())).propertyNames).toEqual({
1412
+ $ref: "#/$defs/Str",
1413
+ });
1414
+ // the value position still wants the number form, so the two cannot share one def
1415
+ const key = z.number().meta({ id: "Shared" });
1416
+ expect(z.toJSONSchema(z.object({ a: key, b: z.record(key, z.string()) }))).toMatchObject({
1417
+ properties: { a: { $ref: "#/$defs/Shared" }, b: { propertyNames: { type: "string" } } },
1418
+ $defs: { Shared: { type: "number" } },
1419
+ });
1420
+ });
1421
+
1422
+ test("record key rewrite reaches a wrapped record", () => {
1423
+ // the flatten copies a record's properties onto its wrapper by reference, so the rewrite has to find every copy
1424
+ expect(z.toJSONSchema(z.record(z.number(), z.boolean()).optional()).propertyNames).toEqual({
1425
+ type: "string",
1426
+ pattern: "^-?\\d+(?:\\.\\d+)?$",
1427
+ });
1428
+ expect(z.toJSONSchema(z.object({ a: z.record(z.literal([1, 2]), z.boolean()).optional() }))).toMatchObject({
1429
+ properties: { a: { propertyNames: { type: "string", enum: ["1", "2"] }, required: ["1", "2"] } },
1430
+ });
1431
+ });
1432
+
1433
+ test("record with a recursive key converts without looping", () => {
1434
+ const numeric: any = z.lazy(() => z.union([z.number(), numeric]));
1435
+ expect(z.toJSONSchema(z.record(numeric, z.boolean())).propertyNames).toMatchObject({
1436
+ anyOf: [{ type: "string", pattern: "^-?\\d+(?:\\.\\d+)?$" }, { $ref: "#/$defs/__schema0" }],
1437
+ });
1438
+ // a key with nothing to re-express keeps the reference it had
1439
+ const stringy: any = z.lazy(() => z.union([z.string(), stringy]));
1440
+ expect(z.toJSONSchema(z.record(stringy, z.boolean())).propertyNames).toEqual({ $ref: "#/$defs/__schema0" });
1441
+ expect(
1442
+ z.toJSONSchema(z.record(z.union([z.literal("a"), z.literal("b")]).meta({ id: "Keys" }), z.boolean()))
1443
+ .propertyNames
1444
+ ).toEqual({ $ref: "#/$defs/Keys" });
1445
+ });
1446
+
1447
+ test("record with a heterogeneous key stringifies only its numeric members", () => {
1448
+ // a mixed key carries no `type`, so the numeric members are caught by value rather than by type
1449
+ expect(z.toJSONSchema(z.record(z.literal(["a", 1]), z.boolean()))).toMatchObject({
1450
+ propertyNames: { enum: ["a", "1"] },
1451
+ required: ["a", "1"],
1452
+ });
1453
+ // a member no key can spell is left as it was, since the parser only ever retries a key as a number
1454
+ expect(z.toJSONSchema(z.record(z.literal(["a", true]) as any, z.boolean())).propertyNames).toEqual({
1455
+ enum: ["a", true],
1456
+ });
1457
+ });
1458
+
1459
+ test("record stringifies required for every target", () => {
1460
+ const schema = z.record(z.literal([1, 2]), z.boolean());
1461
+ for (const target of ["draft-2020-12", "draft-7", "draft-4", "openapi-3.0"] as const) {
1462
+ expect(z.toJSONSchema(schema, { target }).required).toEqual(["1", "2"]);
1463
+ }
1464
+ });
1465
+
1466
+ test("strict record with regex key uses propertyNames", () => {
1467
+ const schema = z.record(z.string().regex(/^label:[a-z]{2}$/), z.string());
1468
+
1469
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1470
+ {
1471
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1472
+ "additionalProperties": {
1473
+ "type": "string",
1474
+ },
1475
+ "propertyNames": {
1476
+ "pattern": "^label:[a-z]{2}$",
1477
+ "type": "string",
1478
+ },
1479
+ "type": "object",
1480
+ }
1481
+ `);
1482
+ });
1483
+
1484
+ test("looseRecord with regex key uses patternProperties", () => {
1485
+ const schema = z.looseRecord(z.string().regex(/^label:[a-z]{2}$/), z.string());
1486
+
1487
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1488
+ {
1489
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1490
+ "patternProperties": {
1491
+ "^label:[a-z]{2}$": {
1492
+ "type": "string",
1493
+ },
1494
+ },
1495
+ "type": "object",
1496
+ }
1497
+ `);
1498
+ });
1499
+
1500
+ test("looseRecord with multiple regex patterns uses patternProperties", () => {
1501
+ const schema = z.looseRecord(
1502
+ z
1503
+ .string()
1504
+ .regex(/^prefix_/)
1505
+ .regex(/_suffix$/),
1506
+ z.number()
1507
+ );
1508
+
1509
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1510
+ {
1511
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1512
+ "patternProperties": {
1513
+ "^prefix_": {
1514
+ "type": "number",
1515
+ },
1516
+ "_suffix$": {
1517
+ "type": "number",
1518
+ },
1519
+ },
1520
+ "type": "object",
1521
+ }
1522
+ `);
1523
+ });
1524
+
1525
+ test("looseRecord without regex key uses propertyNames", () => {
1526
+ // looseRecord with plain string key should still use propertyNames
1527
+ const schema = z.looseRecord(z.string(), z.boolean());
1528
+
1529
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1530
+ {
1531
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1532
+ "additionalProperties": {
1533
+ "type": "boolean",
1534
+ },
1535
+ "propertyNames": {
1536
+ "type": "string",
1537
+ },
1538
+ "type": "object",
1539
+ }
1540
+ `);
1541
+ });
1542
+
1543
+ test("intersection of object with looseRecord uses patternProperties", () => {
1544
+ const zLabeled = z.object({ label: z.string() });
1545
+ const zLocalizedLabeled = z.looseRecord(z.string().regex(/^label:[a-z]{2}$/), z.string());
1546
+ const schema = zLabeled.and(zLocalizedLabeled);
1547
+
1548
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1549
+ {
1550
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1551
+ "allOf": [
1552
+ {
1553
+ "additionalProperties": false,
1554
+ "properties": {
1555
+ "label": {
1556
+ "type": "string",
1557
+ },
1558
+ },
1559
+ "required": [
1560
+ "label",
1561
+ ],
1562
+ "type": "object",
1563
+ },
1564
+ {
1565
+ "patternProperties": {
1566
+ "^label:[a-z]{2}$": {
1567
+ "type": "string",
1568
+ },
1569
+ },
1570
+ "type": "object",
1571
+ },
1572
+ ],
1573
+ }
1574
+ `);
1575
+ });
1576
+
1577
+ test("tuple", () => {
1578
+ const schema = z.tuple([z.string(), z.number()]);
1579
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1580
+ {
1581
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1582
+ "items": false,
1583
+ "maxItems": 2,
1584
+ "minItems": 2,
1585
+ "prefixItems": [
1586
+ {
1587
+ "type": "string",
1588
+ },
1589
+ {
1590
+ "type": "number",
1591
+ },
1592
+ ],
1593
+ "type": "array",
1594
+ }
1595
+ `);
1596
+ });
1597
+
1598
+ test("tuple with rest", () => {
1599
+ const schema = z.tuple([z.string(), z.number()]).rest(z.boolean());
1600
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1601
+ {
1602
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1603
+ "items": {
1604
+ "type": "boolean",
1605
+ },
1606
+ "minItems": 2,
1607
+ "prefixItems": [
1608
+ {
1609
+ "type": "string",
1610
+ },
1611
+ {
1612
+ "type": "number",
1613
+ },
1614
+ ],
1615
+ "type": "array",
1616
+ }
1617
+ `);
1618
+ });
1619
+
1620
+ test("tuple openapi-3.0", () => {
1621
+ const schema = z.tuple([z.string(), z.number()]);
1622
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
1623
+ validateOpenAPI30Schema(jsonSchema);
1624
+ expect(jsonSchema).toMatchInlineSnapshot(`
1625
+ {
1626
+ "items": {
1627
+ "anyOf": [
1628
+ {
1629
+ "type": "string",
1630
+ },
1631
+ {
1632
+ "type": "number",
1633
+ },
1634
+ ],
1635
+ },
1636
+ "maxItems": 2,
1637
+ "minItems": 2,
1638
+ "type": "array",
1639
+ }
1640
+ `);
1641
+ });
1642
+
1643
+ test("tuple with rest openapi-3.0", () => {
1644
+ const schema = z.tuple([z.string(), z.number()]).rest(z.boolean());
1645
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
1646
+ validateOpenAPI30Schema(jsonSchema);
1647
+ expect(jsonSchema).toMatchInlineSnapshot(`
1648
+ {
1649
+ "items": {
1650
+ "anyOf": [
1651
+ {
1652
+ "type": "string",
1653
+ },
1654
+ {
1655
+ "type": "number",
1656
+ },
1657
+ {
1658
+ "type": "boolean",
1659
+ },
1660
+ ],
1661
+ },
1662
+ "minItems": 2,
1663
+ "type": "array",
1664
+ }
1665
+ `);
1666
+ });
1667
+
1668
+ test("tuple with null openapi-3.0", () => {
1669
+ const schema = z.tuple([z.string(), z.number(), z.null()]);
1670
+ const jsonSchema = z.toJSONSchema(schema, { target: "openapi-3.0" });
1671
+ validateOpenAPI30Schema(jsonSchema);
1672
+ expect(jsonSchema).toMatchInlineSnapshot(`
1673
+ {
1674
+ "items": {
1675
+ "anyOf": [
1676
+ {
1677
+ "type": "string",
1678
+ },
1679
+ {
1680
+ "type": "number",
1681
+ },
1682
+ {
1683
+ "enum": [
1684
+ null,
1685
+ ],
1686
+ "nullable": true,
1687
+ "type": "string",
1688
+ },
1689
+ ],
1690
+ },
1691
+ "maxItems": 3,
1692
+ "minItems": 3,
1693
+ "type": "array",
1694
+ }
1695
+ `);
1696
+ });
1697
+
1698
+ test("tuple draft-7", () => {
1699
+ const schema = z.tuple([z.string(), z.number()]);
1700
+ expect(z.toJSONSchema(schema, { target: "draft-7", io: "input" })).toMatchInlineSnapshot(`
1701
+ {
1702
+ "$schema": "http://json-schema.org/draft-07/schema#",
1703
+ "additionalItems": false,
1704
+ "items": [
1705
+ {
1706
+ "type": "string",
1707
+ },
1708
+ {
1709
+ "type": "number",
1710
+ },
1711
+ ],
1712
+ "maxItems": 2,
1713
+ "minItems": 2,
1714
+ "type": "array",
1715
+ }
1716
+ `);
1717
+ });
1718
+
1719
+ test("tuple with rest draft-7", () => {
1720
+ const schema = z.tuple([z.string(), z.number()]).rest(z.boolean());
1721
+ expect(z.toJSONSchema(schema, { target: "draft-7", io: "input" })).toMatchInlineSnapshot(`
1722
+ {
1723
+ "$schema": "http://json-schema.org/draft-07/schema#",
1724
+ "additionalItems": {
1725
+ "type": "boolean",
1726
+ },
1727
+ "items": [
1728
+ {
1729
+ "type": "string",
1730
+ },
1731
+ {
1732
+ "type": "number",
1733
+ },
1734
+ ],
1735
+ "minItems": 2,
1736
+ "type": "array",
1737
+ }
1738
+ `);
1739
+ });
1740
+
1741
+ test("tuple with rest draft-7 - issue #5151 regression test", () => {
1742
+ // This test addresses issue #5151: tuple with rest elements and ids in draft-7 had incorrect internal path handling affecting complex scenarios
1743
+ const primarySchema = z.string().meta({ id: "primary" });
1744
+ const restSchema = z.number().meta({ id: "rest" });
1745
+ const testSchema = z.tuple([primarySchema], restSchema);
1746
+
1747
+ // Test both final output structure AND internal path handling
1748
+ const capturedPaths: string[] = [];
1749
+ const result = z.toJSONSchema(testSchema, {
1750
+ target: "draft-7",
1751
+ override: (ctx) => capturedPaths.push(ctx.path.join("/")),
1752
+ });
1753
+
1754
+ // Verify correct draft-7 structure with metadata extraction
1755
+ expect(result).toMatchInlineSnapshot(`
1756
+ {
1757
+ "$schema": "http://json-schema.org/draft-07/schema#",
1758
+ "additionalItems": {
1759
+ "$ref": "#/definitions/rest",
1760
+ },
1761
+ "definitions": {
1762
+ "primary": {
1763
+ "type": "string",
1764
+ },
1765
+ "rest": {
1766
+ "type": "number",
1767
+ },
1768
+ },
1769
+ "items": [
1770
+ {
1771
+ "$ref": "#/definitions/primary",
1772
+ },
1773
+ ],
1774
+ "minItems": 1,
1775
+ "type": "array",
1776
+ }
1777
+ `);
1778
+
1779
+ // Verify internal paths are correct (this was the actual bug)
1780
+ expect(capturedPaths).toContain("items/0"); // prefix items should use "items" path
1781
+ expect(capturedPaths).toContain("additionalItems"); // rest should use "additionalItems" path
1782
+ expect(capturedPaths).not.toContain("prefixItems/0"); // should not use draft-2020-12 paths
1783
+
1784
+ // Structural validations
1785
+ expect(Array.isArray(result.items)).toBe(true);
1786
+ expect(result.additionalItems).toBeDefined();
1787
+ });
1788
+
1789
+ test("closed tuple length constraints - issue #6193", () => {
1790
+ const schema = z.tuple([z.string(), z.string(), z.object({ lang: z.string() })]);
1791
+
1792
+ expect(schema.safeParse(["a", "b", { lang: "en" }, "extra"]).success).toBe(false);
1793
+
1794
+ const draft2020 = z.toJSONSchema(schema, { target: "draft-2020-12" });
1795
+ expect(draft2020.items).toBe(false);
1796
+ expect(draft2020.minItems).toBe(3);
1797
+ expect(draft2020.maxItems).toBe(3);
1798
+
1799
+ const draft7 = z.toJSONSchema(schema, { target: "draft-7", io: "input" });
1800
+ expect(draft7.additionalItems).toBe(false);
1801
+ expect(draft7.minItems).toBe(3);
1802
+ expect(draft7.maxItems).toBe(3);
1803
+
1804
+ const openapi = z.toJSONSchema(schema, { target: "openapi-3.0" });
1805
+ validateOpenAPI30Schema(openapi);
1806
+ expect(openapi.minItems).toBe(3);
1807
+ expect(openapi.maxItems).toBe(3);
1808
+ });
1809
+
1810
+ test("closed tuple with optional trailing items", () => {
1811
+ const schema = z.tuple([z.string(), z.number().optional()]);
1812
+
1813
+ const draft2020 = z.toJSONSchema(schema, { target: "draft-2020-12" });
1814
+ expect(draft2020.items).toBe(false);
1815
+ expect(draft2020.minItems).toBe(1);
1816
+ expect(draft2020.maxItems).toBe(2);
1817
+
1818
+ const draft7 = z.toJSONSchema(schema, { target: "draft-7", io: "input" });
1819
+ expect(draft7.additionalItems).toBe(false);
1820
+ expect(draft7.minItems).toBe(1);
1821
+ expect(draft7.maxItems).toBe(2);
1822
+ });
1823
+
1824
+ test("empty closed tuple rejects extra elements", () => {
1825
+ const schema = z.tuple([]);
1826
+
1827
+ const draft2020 = z.toJSONSchema(schema, { target: "draft-2020-12" });
1828
+ expect(draft2020).not.toHaveProperty("prefixItems");
1829
+ expect(draft2020.items).toBe(false);
1830
+ expect(draft2020.maxItems).toBe(0);
1831
+
1832
+ const draft7 = z.toJSONSchema(schema, { target: "draft-7", io: "input" });
1833
+ expect(draft7).not.toHaveProperty("items");
1834
+ expect(draft7).not.toHaveProperty("additionalItems");
1835
+ expect(draft7.maxItems).toBe(0);
1836
+
1837
+ const draft4 = z.toJSONSchema(schema, { target: "draft-4" });
1838
+ expect(draft4).not.toHaveProperty("items");
1839
+ expect(draft4).not.toHaveProperty("additionalItems");
1840
+ expect(draft4.maxItems).toBe(0);
1841
+
1842
+ const openapi = z.toJSONSchema(schema, { target: "openapi-3.0" });
1843
+ validateOpenAPI30Schema(openapi);
1844
+ expect(openapi.items).toEqual({});
1845
+ expect(openapi.maxItems).toBe(0);
1846
+ });
1847
+
1848
+ test("empty tuple with rest applies its schema to every item", () => {
1849
+ const schema = z.tuple([]).rest(z.boolean());
1850
+
1851
+ const draft2020 = z.toJSONSchema(schema, { target: "draft-2020-12" });
1852
+ expect(draft2020).not.toHaveProperty("prefixItems");
1853
+ expect(draft2020.items).toEqual({ type: "boolean" });
1854
+
1855
+ for (const target of ["draft-7", "draft-4"] as const) {
1856
+ const result = z.toJSONSchema(schema, { target });
1857
+ expect(result.items).toEqual({ type: "boolean" });
1858
+ expect(result).not.toHaveProperty("additionalItems");
1859
+ }
1860
+
1861
+ const openapi = z.toJSONSchema(schema, { target: "openapi-3.0" });
1862
+ validateOpenAPI30Schema(openapi);
1863
+ expect(openapi.items).toEqual({ type: "boolean" });
1864
+ });
1865
+
1866
+ test("empty tuple with rest uses the emitted items path", () => {
1867
+ const schema = z.tuple([]).rest(z.string());
1868
+
1869
+ for (const target of ["draft-2020-12", "draft-7", "draft-4", "openapi-3.0"] as const) {
1870
+ const paths: string[] = [];
1871
+ z.toJSONSchema(schema, {
1872
+ target,
1873
+ override: (ctx) => paths.push(ctx.path.join("/")),
1874
+ });
1875
+ expect(paths).toContain("items");
1876
+ expect(paths).not.toContain("additionalItems");
1877
+ expect(paths).not.toContain("items/0");
1878
+ }
1879
+ });
1880
+
1881
+ test("closed tuple length respects io direction with defaults", () => {
1882
+ const schema = z.tuple([z.string(), z.string().default("x")]);
1883
+
1884
+ const input = z.toJSONSchema(schema, { target: "draft-2020-12", io: "input" });
1885
+ expect(input.items).toBe(false);
1886
+ expect(input.minItems).toBe(1);
1887
+ expect(input.maxItems).toBe(2);
1888
+
1889
+ const output = z.toJSONSchema(schema, { target: "draft-2020-12", io: "output" });
1890
+ expect(output.items).toBe(false);
1891
+ expect(output.minItems).toBe(2);
1892
+ expect(output.maxItems).toBe(2);
1893
+ });
1894
+
1895
+ test("closed tuple length resolves input optionality past transform and catch", () => {
1896
+ const minItems = (schema: z.core.$ZodType, io: "input" | "output") =>
1897
+ z.toJSONSchema(schema, { target: "draft-2020-12", io }).minItems;
1898
+
1899
+ // Both let the parser observe an absent slot, but declare a required input.
1900
+ const pre = z.tuple([z.string(), z.preprocess((v) => v, z.string())]);
1901
+ expect(minItems(pre, "input")).toBe(2);
1902
+ expect(minItems(pre, "output")).toBe(2);
1903
+
1904
+ const caught = z.tuple([z.string(), z.string().catch("x")]);
1905
+ expect(minItems(caught, "input")).toBe(2);
1906
+ expect(minItems(caught, "output")).toBe(2);
1907
+
1908
+ // Resolution passes through the wrapper rather than stopping at it.
1909
+ expect(minItems(z.tuple([z.string(), z.string().optional().catch("x")]), "input")).toBe(1);
1910
+ });
1911
+
1912
+ test("promise", () => {
1913
+ const schema = z.promise(z.string());
1914
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1915
+ {
1916
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1917
+ "type": "string",
1918
+ }
1919
+ `);
1920
+ });
1921
+
1922
+ test("lazy", () => {
1923
+ const schema = z.lazy(() => z.string());
1924
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
1925
+ {
1926
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1927
+ "type": "string",
1928
+ }
1929
+ `);
1930
+ });
1931
+
1932
+ // enum
1933
+ test("enum", () => {
1934
+ const a = z.enum(["a", "b", "c"]);
1935
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
1936
+ {
1937
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1938
+ "enum": [
1939
+ "a",
1940
+ "b",
1941
+ "c",
1942
+ ],
1943
+ "type": "string",
1944
+ }
1945
+ `);
1946
+
1947
+ enum B {
1948
+ A = 0,
1949
+ B = 1,
1950
+ C = 2,
1951
+ }
1952
+
1953
+ const b = z.enum(B);
1954
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
1955
+ {
1956
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1957
+ "enum": [
1958
+ 0,
1959
+ 1,
1960
+ 2,
1961
+ ],
1962
+ "type": "number",
1963
+ }
1964
+ `);
1965
+ });
1966
+
1967
+ // literal
1968
+ test("literal", () => {
1969
+ const a = z.literal("hello");
1970
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
1971
+ {
1972
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1973
+ "const": "hello",
1974
+ "type": "string",
1975
+ }
1976
+ `);
1977
+
1978
+ const b = z.literal(7);
1979
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
1980
+ {
1981
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1982
+ "const": 7,
1983
+ "type": "number",
1984
+ }
1985
+ `);
1986
+
1987
+ const c = z.literal(["hello", undefined, null, 5, BigInt(1324)]);
1988
+ expect(() => z.toJSONSchema(c)).toThrow();
1989
+
1990
+ const d = z.literal(["hello", null, 5]);
1991
+ expect(z.toJSONSchema(d)).toMatchInlineSnapshot(`
1992
+ {
1993
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
1994
+ "enum": [
1995
+ "hello",
1996
+ null,
1997
+ 5,
1998
+ ],
1999
+ }
2000
+ `);
2001
+
2002
+ const e = z.literal(["hello", "zod", "v4"]);
2003
+ expect(z.toJSONSchema(e)).toMatchInlineSnapshot(`
2004
+ {
2005
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2006
+ "enum": [
2007
+ "hello",
2008
+ "zod",
2009
+ "v4",
2010
+ ],
2011
+ "type": "string",
2012
+ }
2013
+ `);
2014
+ });
2015
+
2016
+ test("literal draft-4", () => {
2017
+ const a = z.literal("hello");
2018
+ expect(z.toJSONSchema(a, { target: "draft-4" })).toMatchInlineSnapshot(`
2019
+ {
2020
+ "$schema": "http://json-schema.org/draft-04/schema#",
2021
+ "enum": [
2022
+ "hello",
2023
+ ],
2024
+ "type": "string",
2025
+ }
2026
+ `);
2027
+ });
2028
+
2029
+ // pipe
2030
+ test("pipe", () => {
2031
+ const schema = z
2032
+ .string()
2033
+ .transform((val) => Number.parseInt(val))
2034
+ .pipe(z.number());
2035
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
2036
+ {
2037
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2038
+ "type": "number",
2039
+ }
2040
+ `);
2041
+ });
2042
+
2043
+ test("simple objects", () => {
2044
+ const schema = z.object({
2045
+ name: z.string(),
2046
+ age: z.number(),
2047
+ });
2048
+
2049
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(
2050
+ `
2051
+ {
2052
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2053
+ "additionalProperties": false,
2054
+ "properties": {
2055
+ "age": {
2056
+ "type": "number",
2057
+ },
2058
+ "name": {
2059
+ "type": "string",
2060
+ },
2061
+ },
2062
+ "required": [
2063
+ "name",
2064
+ "age",
2065
+ ],
2066
+ "type": "object",
2067
+ }
2068
+ `
2069
+ );
2070
+ });
2071
+
2072
+ test("additionalproperties in z.object", () => {
2073
+ const a = z.object({
2074
+ name: z.string(),
2075
+ });
2076
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
2077
+ {
2078
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2079
+ "additionalProperties": false,
2080
+ "properties": {
2081
+ "name": {
2082
+ "type": "string",
2083
+ },
2084
+ },
2085
+ "required": [
2086
+ "name",
2087
+ ],
2088
+ "type": "object",
2089
+ }
2090
+ `);
2091
+ expect(z.toJSONSchema(a, { io: "input" })).toMatchInlineSnapshot(`
2092
+ {
2093
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2094
+ "properties": {
2095
+ "name": {
2096
+ "type": "string",
2097
+ },
2098
+ },
2099
+ "required": [
2100
+ "name",
2101
+ ],
2102
+ "type": "object",
2103
+ }
2104
+ `);
2105
+ expect(
2106
+ z.toJSONSchema(a, {
2107
+ io: "input",
2108
+ override(ctx) {
2109
+ const def = ctx.zodSchema._zod.def;
2110
+ if (def.type === "object" && !def.catchall) {
2111
+ (ctx.jsonSchema as z.core.JSONSchema.ObjectSchema).additionalProperties = false;
2112
+ }
2113
+ },
2114
+ })
2115
+ ).toMatchInlineSnapshot(`
2116
+ {
2117
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2118
+ "additionalProperties": false,
2119
+ "properties": {
2120
+ "name": {
2121
+ "type": "string",
2122
+ },
2123
+ },
2124
+ "required": [
2125
+ "name",
2126
+ ],
2127
+ "type": "object",
2128
+ }
2129
+ `);
2130
+ });
2131
+
2132
+ test("catchall objects", () => {
2133
+ const a = z.strictObject({
2134
+ name: z.string(),
2135
+ age: z.number(),
2136
+ });
2137
+
2138
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
2139
+ {
2140
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2141
+ "additionalProperties": false,
2142
+ "properties": {
2143
+ "age": {
2144
+ "type": "number",
2145
+ },
2146
+ "name": {
2147
+ "type": "string",
2148
+ },
2149
+ },
2150
+ "required": [
2151
+ "name",
2152
+ "age",
2153
+ ],
2154
+ "type": "object",
2155
+ }
2156
+ `);
2157
+
2158
+ const b = z
2159
+ .object({
2160
+ name: z.string(),
2161
+ })
2162
+ .catchall(z.string());
2163
+
2164
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
2165
+ {
2166
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2167
+ "additionalProperties": {
2168
+ "type": "string",
2169
+ },
2170
+ "properties": {
2171
+ "name": {
2172
+ "type": "string",
2173
+ },
2174
+ },
2175
+ "required": [
2176
+ "name",
2177
+ ],
2178
+ "type": "object",
2179
+ }
2180
+ `);
2181
+
2182
+ const c = z.looseObject({
2183
+ name: z.string(),
2184
+ });
2185
+
2186
+ expect(z.toJSONSchema(c)).toMatchInlineSnapshot(`
2187
+ {
2188
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2189
+ "additionalProperties": {},
2190
+ "properties": {
2191
+ "name": {
2192
+ "type": "string",
2193
+ },
2194
+ },
2195
+ "required": [
2196
+ "name",
2197
+ ],
2198
+ "type": "object",
2199
+ }
2200
+ `);
2201
+ });
2202
+
2203
+ test("optional fields - object", () => {
2204
+ const schema = z.object({
2205
+ required: z.string(),
2206
+ optional: z.string().optional(),
2207
+ nonoptional: z.string().optional().nonoptional(),
2208
+ });
2209
+
2210
+ const result = z.toJSONSchema(schema);
2211
+
2212
+ expect(result).toMatchInlineSnapshot(`
2213
+ {
2214
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2215
+ "additionalProperties": false,
2216
+ "properties": {
2217
+ "nonoptional": {
2218
+ "type": "string",
2219
+ },
2220
+ "optional": {
2221
+ "type": "string",
2222
+ },
2223
+ "required": {
2224
+ "type": "string",
2225
+ },
2226
+ },
2227
+ "required": [
2228
+ "required",
2229
+ "nonoptional",
2230
+ ],
2231
+ "type": "object",
2232
+ }
2233
+ `);
2234
+ });
2235
+
2236
+ test("recursive object", () => {
2237
+ interface Category {
2238
+ name: string;
2239
+ subcategories: Category[];
2240
+ }
2241
+
2242
+ const categorySchema: z.ZodType<Category> = z.object({
2243
+ name: z.string(),
2244
+ subcategories: z.array(z.lazy(() => categorySchema)),
2245
+ });
2246
+
2247
+ const result = z.toJSONSchema(categorySchema);
2248
+ expect(result).toMatchInlineSnapshot(`
2249
+ {
2250
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2251
+ "additionalProperties": false,
2252
+ "properties": {
2253
+ "name": {
2254
+ "type": "string",
2255
+ },
2256
+ "subcategories": {
2257
+ "items": {
2258
+ "$ref": "#",
2259
+ },
2260
+ "type": "array",
2261
+ },
2262
+ },
2263
+ "required": [
2264
+ "name",
2265
+ "subcategories",
2266
+ ],
2267
+ "type": "object",
2268
+ }
2269
+ `);
2270
+ });
2271
+
2272
+ test("simple interface", () => {
2273
+ const userSchema = z.object({
2274
+ name: z.string(),
2275
+ age: z.number().optional(),
2276
+ });
2277
+
2278
+ const result = z.toJSONSchema(userSchema);
2279
+ expect(result).toMatchInlineSnapshot(`
2280
+ {
2281
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2282
+ "additionalProperties": false,
2283
+ "properties": {
2284
+ "age": {
2285
+ "type": "number",
2286
+ },
2287
+ "name": {
2288
+ "type": "string",
2289
+ },
2290
+ },
2291
+ "required": [
2292
+ "name",
2293
+ ],
2294
+ "type": "object",
2295
+ }
2296
+ `);
2297
+ });
2298
+
2299
+ test("catchall interface", () => {
2300
+ const a = z.strictObject({
2301
+ name: z.string(),
2302
+ age: z.number(),
2303
+ });
2304
+
2305
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
2306
+ {
2307
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2308
+ "additionalProperties": false,
2309
+ "properties": {
2310
+ "age": {
2311
+ "type": "number",
2312
+ },
2313
+ "name": {
2314
+ "type": "string",
2315
+ },
2316
+ },
2317
+ "required": [
2318
+ "name",
2319
+ "age",
2320
+ ],
2321
+ "type": "object",
2322
+ }
2323
+ `);
2324
+
2325
+ const b = z
2326
+ .object({
2327
+ name: z.string(),
2328
+ })
2329
+ .catchall(z.string());
2330
+
2331
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
2332
+ {
2333
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2334
+ "additionalProperties": {
2335
+ "type": "string",
2336
+ },
2337
+ "properties": {
2338
+ "name": {
2339
+ "type": "string",
2340
+ },
2341
+ },
2342
+ "required": [
2343
+ "name",
2344
+ ],
2345
+ "type": "object",
2346
+ }
2347
+ `);
2348
+
2349
+ const c = z.looseObject({
2350
+ name: z.string(),
2351
+ });
2352
+
2353
+ expect(z.toJSONSchema(c)).toMatchInlineSnapshot(`
2354
+ {
2355
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2356
+ "additionalProperties": {},
2357
+ "properties": {
2358
+ "name": {
2359
+ "type": "string",
2360
+ },
2361
+ },
2362
+ "required": [
2363
+ "name",
2364
+ ],
2365
+ "type": "object",
2366
+ }
2367
+ `);
2368
+ });
2369
+
2370
+ test("recursive interface schemas", () => {
2371
+ const TreeNodeSchema = z.object({
2372
+ id: z.string(),
2373
+ get children() {
2374
+ return TreeNodeSchema;
2375
+ },
2376
+ });
2377
+
2378
+ const result = z.toJSONSchema(TreeNodeSchema);
2379
+
2380
+ // Should have definitions for recursive schema
2381
+ expect(JSON.stringify(result, null, 2)).toMatchInlineSnapshot(
2382
+ `
2383
+ "{
2384
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2385
+ "type": "object",
2386
+ "properties": {
2387
+ "id": {
2388
+ "type": "string"
2389
+ },
2390
+ "children": {
2391
+ "$ref": "#"
2392
+ }
2393
+ },
2394
+ "required": [
2395
+ "id",
2396
+ "children"
2397
+ ],
2398
+ "additionalProperties": false
2399
+ }"
2400
+ `
2401
+ );
2402
+ });
2403
+
2404
+ test("mutually recursive interface schemas", () => {
2405
+ const FolderSchema = z.object({
2406
+ name: z.string(),
2407
+ get files() {
2408
+ return z.array(FileSchema);
2409
+ },
2410
+ });
2411
+
2412
+ const FileSchema = z.object({
2413
+ name: z.string(),
2414
+ get parent() {
2415
+ return FolderSchema;
2416
+ },
2417
+ });
2418
+
2419
+ const result = z.toJSONSchema(FolderSchema);
2420
+
2421
+ // Should have definitions for both schemas
2422
+ expect(JSON.stringify(result, null, 2)).toMatchInlineSnapshot(
2423
+ `
2424
+ "{
2425
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2426
+ "type": "object",
2427
+ "properties": {
2428
+ "name": {
2429
+ "type": "string"
2430
+ },
2431
+ "files": {
2432
+ "type": "array",
2433
+ "items": {
2434
+ "type": "object",
2435
+ "properties": {
2436
+ "name": {
2437
+ "type": "string"
2438
+ },
2439
+ "parent": {
2440
+ "$ref": "#"
2441
+ }
2442
+ },
2443
+ "required": [
2444
+ "name",
2445
+ "parent"
2446
+ ],
2447
+ "additionalProperties": false
2448
+ }
2449
+ }
2450
+ },
2451
+ "required": [
2452
+ "name",
2453
+ "files"
2454
+ ],
2455
+ "additionalProperties": false
2456
+ }"
2457
+ `
2458
+ );
2459
+ });
2460
+ });
2461
+
2462
+ test("override", () => {
2463
+ const schema = z.toJSONSchema(z.string(), {
2464
+ override: (ctx) => {
2465
+ ctx.zodSchema;
2466
+ ctx.jsonSchema;
2467
+ ctx.jsonSchema.whatever = "sup";
2468
+ },
2469
+ });
2470
+ expect(schema).toMatchInlineSnapshot(`
2471
+ {
2472
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2473
+ "type": "string",
2474
+ "whatever": "sup",
2475
+ }
2476
+ `);
2477
+ });
2478
+
2479
+ test("override: do not run on references", () => {
2480
+ let overrideCount = 0;
2481
+ const schema = z
2482
+ .union([z.string().date(), z.string().datetime(), z.string().datetime({ local: true })])
2483
+ .meta({ a: true })
2484
+ .transform((str) => new Date(str))
2485
+ .meta({ b: true })
2486
+ .pipe(z.date())
2487
+ .meta({ c: true })
2488
+ .brand("dateIn");
2489
+ z.toJSONSchema(schema, {
2490
+ unrepresentable: "any",
2491
+ io: "input",
2492
+ override(_) {
2493
+ overrideCount++;
2494
+ },
2495
+ });
2496
+
2497
+ expect(overrideCount).toBe(12);
2498
+ });
2499
+
2500
+ test("override with refs", () => {
2501
+ const a = z.string().optional();
2502
+ const result = z.toJSONSchema(a, {
2503
+ override(ctx) {
2504
+ if (ctx.zodSchema._zod.def.type === "string") {
2505
+ ctx.jsonSchema.type = "STRING" as "string";
2506
+ }
2507
+ },
2508
+ });
2509
+
2510
+ expect(result).toMatchInlineSnapshot(`
2511
+ {
2512
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2513
+ "type": "STRING",
2514
+ }
2515
+ `);
2516
+ });
2517
+
2518
+ test("override execution order", () => {
2519
+ const schema = z.union([z.string(), z.number()]);
2520
+ let unionSchema!: any;
2521
+ z.toJSONSchema(schema, {
2522
+ override(ctx) {
2523
+ if (ctx.zodSchema._zod.def.type === "union") {
2524
+ unionSchema = ctx.jsonSchema;
2525
+ }
2526
+ },
2527
+ });
2528
+
2529
+ expect(unionSchema).toMatchInlineSnapshot(`
2530
+ {
2531
+ "type": [
2532
+ "string",
2533
+ "number",
2534
+ ],
2535
+ }
2536
+ `);
2537
+ });
2538
+
2539
+ test("override with path", () => {
2540
+ const userSchema = z.object({
2541
+ name: z.string(),
2542
+ age: z.number(),
2543
+ });
2544
+
2545
+ const capturedPaths: (string | number)[][] = [];
2546
+
2547
+ z.toJSONSchema(userSchema, {
2548
+ override(ctx) {
2549
+ capturedPaths.push(ctx.path);
2550
+ },
2551
+ });
2552
+
2553
+ expect(capturedPaths).toMatchInlineSnapshot(`
2554
+ [
2555
+ [
2556
+ "properties",
2557
+ "age",
2558
+ ],
2559
+ [
2560
+ "properties",
2561
+ "name",
2562
+ ],
2563
+ [],
2564
+ ]
2565
+ `);
2566
+ });
2567
+
2568
+ test("pipe", () => {
2569
+ const mySchema = z
2570
+ .string()
2571
+ .transform((val) => val.length)
2572
+ .pipe(z.number());
2573
+ // ZodPipe
2574
+
2575
+ const a = z.toJSONSchema(mySchema);
2576
+ expect(a).toMatchInlineSnapshot(`
2577
+ {
2578
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2579
+ "type": "number",
2580
+ }
2581
+ `);
2582
+ // => { type: "number" }
2583
+
2584
+ const b = z.toJSONSchema(mySchema, { io: "input" });
2585
+ expect(b).toMatchInlineSnapshot(`
2586
+ {
2587
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2588
+ "type": "string",
2589
+ }
2590
+ `);
2591
+ // => { type: "string" }
2592
+ });
2593
+
2594
+ test("passthrough schemas", () => {
2595
+ const Internal = z.object({
2596
+ num: z.number(),
2597
+ str: z.string(),
2598
+ });
2599
+ //.meta({ id: "Internal" });
2600
+
2601
+ const External = z.object({
2602
+ a: Internal,
2603
+ b: Internal.optional(),
2604
+ c: z.lazy(() => Internal),
2605
+ d: z.promise(Internal),
2606
+ e: z.pipe(Internal, Internal),
2607
+ });
2608
+
2609
+ const result = z.toJSONSchema(External, {
2610
+ reused: "ref",
2611
+ });
2612
+ expect(result).toMatchInlineSnapshot(`
2613
+ {
2614
+ "$defs": {
2615
+ "__schema0": {
2616
+ "additionalProperties": false,
2617
+ "properties": {
2618
+ "num": {
2619
+ "type": "number",
2620
+ },
2621
+ "str": {
2622
+ "type": "string",
2623
+ },
2624
+ },
2625
+ "required": [
2626
+ "num",
2627
+ "str",
2628
+ ],
2629
+ "type": "object",
2630
+ },
2631
+ },
2632
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2633
+ "additionalProperties": false,
2634
+ "properties": {
2635
+ "a": {
2636
+ "$ref": "#/$defs/__schema0",
2637
+ },
2638
+ "b": {
2639
+ "$ref": "#/$defs/__schema0",
2640
+ },
2641
+ "c": {
2642
+ "$ref": "#/$defs/__schema0",
2643
+ },
2644
+ "d": {
2645
+ "$ref": "#/$defs/__schema0",
2646
+ },
2647
+ "e": {
2648
+ "$ref": "#/$defs/__schema0",
2649
+ },
2650
+ },
2651
+ "required": [
2652
+ "a",
2653
+ "c",
2654
+ "d",
2655
+ "e",
2656
+ ],
2657
+ "type": "object",
2658
+ }
2659
+ `);
2660
+ });
2661
+
2662
+ test("extract schemas with id", () => {
2663
+ const name = z.string().meta({ id: "name" });
2664
+ const result = z.toJSONSchema(
2665
+ z.object({
2666
+ first_name: name,
2667
+ last_name: name.nullable(),
2668
+ middle_name: name.optional(),
2669
+ age: z.number().meta({ id: "age" }),
2670
+ })
2671
+ );
2672
+ expect(result).toMatchInlineSnapshot(`
2673
+ {
2674
+ "$defs": {
2675
+ "age": {
2676
+ "type": "number",
2677
+ },
2678
+ "name": {
2679
+ "type": "string",
2680
+ },
2681
+ },
2682
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2683
+ "additionalProperties": false,
2684
+ "properties": {
2685
+ "age": {
2686
+ "$ref": "#/$defs/age",
2687
+ },
2688
+ "first_name": {
2689
+ "$ref": "#/$defs/name",
2690
+ },
2691
+ "last_name": {
2692
+ "anyOf": [
2693
+ {
2694
+ "$ref": "#/$defs/name",
2695
+ },
2696
+ {
2697
+ "type": "null",
2698
+ },
2699
+ ],
2700
+ },
2701
+ "middle_name": {
2702
+ "$ref": "#/$defs/name",
2703
+ },
2704
+ },
2705
+ "required": [
2706
+ "first_name",
2707
+ "last_name",
2708
+ "age",
2709
+ ],
2710
+ "type": "object",
2711
+ }
2712
+ `);
2713
+ });
2714
+
2715
+ test("escapes JSON Pointer reserved characters in $ref but not in $defs key", () => {
2716
+ const User = z.object({ name: z.string() }).meta({ id: "Shared/User~" });
2717
+ const result = z.toJSONSchema(z.object({ User }));
2718
+ // the $ref pointer escapes `/` -> `~1` and `~` -> `~0` (RFC 6901),
2719
+ // while the $defs key keeps the original id
2720
+ expect((result.properties!.User as any).$ref).toBe("#/$defs/Shared~1User~0");
2721
+ expect(Object.keys(result.$defs!)).toEqual(["Shared/User~"]);
2722
+ });
2723
+
2724
+ test("escapes JSON Pointer reserved characters in the root $ref", () => {
2725
+ const User = z.object({ name: z.string() }).meta({ id: "Shared/User~" });
2726
+ const result = z.toJSONSchema(User);
2727
+ expect(result.$ref).toBe("#/$defs/Shared~1User~0");
2728
+ expect(Object.keys(result.$defs!)).toEqual(["Shared/User~"]);
2729
+ });
2730
+
2731
+ test("a multipleOf divisor JSON Schema cannot express goes through `unrepresentable`", () => {
2732
+ // the keyword must be strictly greater than zero, and NaN/Infinity do not survive JSON at all
2733
+ for (const divisor of [0, Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]) {
2734
+ expect(() => z.toJSONSchema(z.number().multipleOf(divisor))).toThrow(/cannot be represented in JSON Schema/);
2735
+ expect(z.toJSONSchema(z.number().multipleOf(divisor), { unrepresentable: "any" })).toMatchObject({
2736
+ type: "number",
2737
+ });
2738
+ expect(z.toJSONSchema(z.number().multipleOf(divisor), { unrepresentable: "any" })).not.toHaveProperty("multipleOf");
2739
+ }
2740
+
2741
+ // a negative divisor accepts exactly what its absolute value accepts, so it still maps
2742
+ expect(z.number().multipleOf(-5).safeParse(10).success).toEqual(true);
2743
+ expect(z.number().multipleOf(-5).safeParse(13).success).toEqual(false);
2744
+ expect(z.toJSONSchema(z.number().multipleOf(-5))).toMatchObject({ multipleOf: 5 });
2745
+ expect(z.toJSONSchema(z.number().multipleOf(0.1))).toMatchObject({ multipleOf: 0.1 });
2746
+ });
2747
+
2748
+ test("unrepresentable default values go through `unrepresentable`", () => {
2749
+ // a bigint default has no reliable JSON encoding, so it is dropped rather than approximated
2750
+ expect(z.toJSONSchema(z.bigint().default(0n), { unrepresentable: "any" })).toMatchInlineSnapshot(`
2751
+ {
2752
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2753
+ }
2754
+ `);
2755
+ expect(z.toJSONSchema(z.bigint().prefault(2n), { io: "input", unrepresentable: "any" })).toMatchInlineSnapshot(`
2756
+ {
2757
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2758
+ }
2759
+ `);
2760
+ // including when nested inside an otherwise representable default
2761
+ expect(
2762
+ z.toJSONSchema(z.object({ a: z.bigint() }).default({ a: 1n }), { unrepresentable: "any" })
2763
+ ).toMatchInlineSnapshot(`
2764
+ {
2765
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2766
+ "additionalProperties": false,
2767
+ "properties": {
2768
+ "a": {},
2769
+ },
2770
+ "required": [
2771
+ "a",
2772
+ ],
2773
+ "type": "object",
2774
+ }
2775
+ `);
2776
+
2777
+ // under the default strict mode the inner type throws first; a representable inner type surfaces the default's own error rather than a raw `JSON.stringify` TypeError
2778
+ expect(() => z.toJSONSchema(z.bigint().default(0n))).toThrow("BigInt cannot be represented in JSON Schema");
2779
+ expect(() => z.toJSONSchema(z.unknown().default(1n))).toThrow("BigInt defaults cannot be represented in JSON Schema");
2780
+
2781
+ // representable defaults are untouched
2782
+ expect(z.toJSONSchema(z.object({ a: z.number() }).default({ a: 2 }))).toMatchInlineSnapshot(`
2783
+ {
2784
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2785
+ "additionalProperties": false,
2786
+ "default": {
2787
+ "a": 2,
2788
+ },
2789
+ "properties": {
2790
+ "a": {
2791
+ "type": "number",
2792
+ },
2793
+ },
2794
+ "required": [
2795
+ "a",
2796
+ ],
2797
+ "type": "object",
2798
+ }
2799
+ `);
2800
+ });
2801
+
2802
+ test("an `unrepresentable` handler can represent a bigint default", () => {
2803
+ // one handler covers both the type and its default, so no `unrepresentable: "any"` is needed and every other unrepresentable type still throws
2804
+ expect(
2805
+ z.toJSONSchema(z.object({ startAt: z.coerce.bigint().optional().default(0n) }), {
2806
+ io: "input",
2807
+ unrepresentable: ({ zodSchema }) => {
2808
+ const def = zodSchema._zod.def;
2809
+ if (def.type === "bigint") return { type: "integer", format: "int64" };
2810
+ if (def.type === "default") return { default: String(def.defaultValue) };
2811
+ return "throw";
2812
+ },
2813
+ })
2814
+ ).toMatchInlineSnapshot(`
2815
+ {
2816
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2817
+ "properties": {
2818
+ "startAt": {
2819
+ "default": "0",
2820
+ "format": "int64",
2821
+ "type": "integer",
2822
+ },
2823
+ },
2824
+ "type": "object",
2825
+ }
2826
+ `);
2827
+
2828
+ // a handler may also drop just the default while still representing the type
2829
+ expect(
2830
+ z.toJSONSchema(z.bigint().default(0n), {
2831
+ unrepresentable: ({ zodSchema }) =>
2832
+ zodSchema._zod.def.type === "bigint" ? { type: "integer", format: "int64" } : "any",
2833
+ })
2834
+ ).toMatchInlineSnapshot(`
2835
+ {
2836
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2837
+ "format": "int64",
2838
+ "type": "integer",
2839
+ }
2840
+ `);
2841
+ });
2842
+
2843
+ test("unrepresentable literal values are ignored", () => {
2844
+ const a = z.toJSONSchema(z.literal(["hello", null, 5, BigInt(1324), undefined]), { unrepresentable: "any" });
2845
+ expect(a).toMatchInlineSnapshot(`
2846
+ {
2847
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2848
+ "enum": [
2849
+ "hello",
2850
+ null,
2851
+ 5,
2852
+ 1324,
2853
+ ],
2854
+ }
2855
+ `);
2856
+
2857
+ const b = z.toJSONSchema(z.literal([undefined, null, 5, BigInt(1324)]), {
2858
+ unrepresentable: "any",
2859
+ });
2860
+ expect(b).toMatchInlineSnapshot(`
2861
+ {
2862
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2863
+ "enum": [
2864
+ null,
2865
+ 5,
2866
+ 1324,
2867
+ ],
2868
+ }
2869
+ `);
2870
+
2871
+ const c = z.toJSONSchema(z.literal([undefined]), {
2872
+ unrepresentable: "any",
2873
+ });
2874
+ expect(c).toMatchInlineSnapshot(`
2875
+ {
2876
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2877
+ }
2878
+ `);
2879
+ });
2880
+
2881
+ test("describe with id", () => {
2882
+ const jobId = z.string().meta({ id: "jobId" });
2883
+
2884
+ const a = z.toJSONSchema(
2885
+ z.object({
2886
+ current: jobId.describe("Current job"),
2887
+ previous: jobId.describe("Previous job"),
2888
+ })
2889
+ );
2890
+ expect(a).toMatchInlineSnapshot(`
2891
+ {
2892
+ "$defs": {
2893
+ "jobId": {
2894
+ "type": "string",
2895
+ },
2896
+ },
2897
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2898
+ "additionalProperties": false,
2899
+ "properties": {
2900
+ "current": {
2901
+ "$ref": "#/$defs/jobId",
2902
+ "description": "Current job",
2903
+ },
2904
+ "previous": {
2905
+ "$ref": "#/$defs/jobId",
2906
+ "description": "Previous job",
2907
+ },
2908
+ },
2909
+ "required": [
2910
+ "current",
2911
+ "previous",
2912
+ ],
2913
+ "type": "object",
2914
+ }
2915
+ `);
2916
+ });
2917
+
2918
+ test("id is stripped from $defs entries (draft-2020-12)", () => {
2919
+ // The `id` in `.meta()` is a registration tag — it determines the $defs key but should not leak into the definition body, where it is redundant.
2920
+ const inner = z.string().meta({ id: "Inner" });
2921
+ const result = z.toJSONSchema(z.object({ a: inner, b: inner }));
2922
+ expect(result.$defs?.Inner).toEqual({ type: "string" });
2923
+ expect((result.$defs?.Inner as any).id).toBeUndefined();
2924
+ });
2925
+
2926
+ test("id is stripped from definitions entries (draft-04)", () => {
2927
+ // In draft-04, `id` is a reserved keyword that sets a base URI for the subschema. Leaking Zod's registration tag here is semantically wrong, so ensure it is stripped.
2928
+ const inner = z.string().meta({ id: "Inner" });
2929
+ const result = z.toJSONSchema(z.object({ a: inner, b: inner }), { target: "draft-04" }) as any;
2930
+ expect(result.definitions?.Inner).toEqual({ type: "string" });
2931
+ expect(result.definitions?.Inner?.id).toBeUndefined();
2932
+ });
2933
+
2934
+ test("id is stripped from root schema", () => {
2935
+ // The registration tag should not appear on the root either.
2936
+ const A = z.object({ name: z.string() }).meta({ id: "A" });
2937
+ const result = z.toJSONSchema(A);
2938
+ expect((result as any).id).toBeUndefined();
2939
+ });
2940
+
2941
+ test("root schema with id is hoisted into $defs", () => {
2942
+ const A = z.object({ name: z.string() }).meta({ id: "A" });
2943
+ const result = z.toJSONSchema(A);
2944
+
2945
+ expect(result).toEqual({
2946
+ $schema: "https://json-schema.org/draft/2020-12/schema",
2947
+ $ref: "#/$defs/A",
2948
+ $defs: {
2949
+ A: {
2950
+ type: "object",
2951
+ properties: {
2952
+ name: {
2953
+ type: "string",
2954
+ },
2955
+ },
2956
+ required: ["name"],
2957
+ additionalProperties: false,
2958
+ },
2959
+ },
2960
+ });
2961
+ });
2962
+
2963
+ test("root schema with id uses definitions on legacy targets", () => {
2964
+ const A = z.object({ name: z.string() }).meta({ id: "A" });
2965
+ const result = z.toJSONSchema(A, { target: "draft-07" });
2966
+
2967
+ expect(result).toEqual({
2968
+ $schema: "http://json-schema.org/draft-07/schema#",
2969
+ $ref: "#/definitions/A",
2970
+ definitions: {
2971
+ A: {
2972
+ type: "object",
2973
+ properties: {
2974
+ name: {
2975
+ type: "string",
2976
+ },
2977
+ },
2978
+ required: ["name"],
2979
+ additionalProperties: false,
2980
+ },
2981
+ },
2982
+ });
2983
+ });
2984
+
2985
+ test("id is observable in override callback", () => {
2986
+ // The strip happens after override callbacks run, so userland override code can still read `jsonSchema.id` if it wants to.
2987
+ const inner = z.string().meta({ id: "Inner" });
2988
+ const seenIds: Array<string | undefined> = [];
2989
+ z.toJSONSchema(z.object({ a: inner }), {
2990
+ override: ({ jsonSchema }) => {
2991
+ if (jsonSchema.id !== undefined) seenIds.push(jsonSchema.id as string);
2992
+ },
2993
+ });
2994
+ expect(seenIds).toContain("Inner");
2995
+ });
2996
+
2997
+ test("describe with id on wrapper", () => {
2998
+ // Test that $ref propagation works when processor sets a different ref (readonly -> innerType) but parent was extracted due to having an id
2999
+ const roJobId = z.string().readonly().meta({ id: "roJobId" });
3000
+
3001
+ const a = z.toJSONSchema(
3002
+ z.object({
3003
+ current: roJobId.describe("Current readonly job"),
3004
+ previous: roJobId.describe("Previous readonly job"),
3005
+ })
3006
+ );
3007
+ expect(a).toMatchInlineSnapshot(`
3008
+ {
3009
+ "$defs": {
3010
+ "roJobId": {
3011
+ "readOnly": true,
3012
+ "type": "string",
3013
+ },
3014
+ },
3015
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3016
+ "additionalProperties": false,
3017
+ "properties": {
3018
+ "current": {
3019
+ "$ref": "#/$defs/roJobId",
3020
+ "description": "Current readonly job",
3021
+ },
3022
+ "previous": {
3023
+ "$ref": "#/$defs/roJobId",
3024
+ "description": "Previous readonly job",
3025
+ },
3026
+ },
3027
+ "required": [
3028
+ "current",
3029
+ "previous",
3030
+ ],
3031
+ "type": "object",
3032
+ }
3033
+ `);
3034
+ });
3035
+
3036
+ test("overwrite id", () => {
3037
+ const jobId = z.string().meta({ id: "aaa" });
3038
+
3039
+ const a = z.toJSONSchema(
3040
+ z.object({
3041
+ current: jobId,
3042
+ previous: jobId.meta({ id: "bbb" }),
3043
+ })
3044
+ );
3045
+ expect(a).toMatchInlineSnapshot(`
3046
+ {
3047
+ "$defs": {
3048
+ "aaa": {
3049
+ "type": "string",
3050
+ },
3051
+ "bbb": {
3052
+ "$ref": "#/$defs/aaa",
3053
+ },
3054
+ },
3055
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3056
+ "additionalProperties": false,
3057
+ "properties": {
3058
+ "current": {
3059
+ "$ref": "#/$defs/aaa",
3060
+ },
3061
+ "previous": {
3062
+ "$ref": "#/$defs/bbb",
3063
+ },
3064
+ },
3065
+ "required": [
3066
+ "current",
3067
+ "previous",
3068
+ ],
3069
+ "type": "object",
3070
+ }
3071
+ `);
3072
+
3073
+ const b = z.toJSONSchema(
3074
+ z.object({
3075
+ current: jobId,
3076
+ previous: jobId.meta({ id: "ccc" }),
3077
+ }),
3078
+ {
3079
+ reused: "ref",
3080
+ }
3081
+ );
3082
+ expect(b).toMatchInlineSnapshot(`
3083
+ {
3084
+ "$defs": {
3085
+ "aaa": {
3086
+ "type": "string",
3087
+ },
3088
+ "ccc": {
3089
+ "$ref": "#/$defs/aaa",
3090
+ },
3091
+ },
3092
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3093
+ "additionalProperties": false,
3094
+ "properties": {
3095
+ "current": {
3096
+ "$ref": "#/$defs/aaa",
3097
+ },
3098
+ "previous": {
3099
+ "$ref": "#/$defs/ccc",
3100
+ },
3101
+ },
3102
+ "required": [
3103
+ "current",
3104
+ "previous",
3105
+ ],
3106
+ "type": "object",
3107
+ }
3108
+ `);
3109
+ });
3110
+
3111
+ test("overwrite descriptions", () => {
3112
+ const field = z.string().describe("a").describe("b").describe("c");
3113
+
3114
+ const a = z.toJSONSchema(
3115
+ z.object({
3116
+ d: field.describe("d"),
3117
+ e: field.describe("e"),
3118
+ })
3119
+ );
3120
+ expect(a).toMatchInlineSnapshot(`
3121
+ {
3122
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3123
+ "additionalProperties": false,
3124
+ "properties": {
3125
+ "d": {
3126
+ "description": "d",
3127
+ "type": "string",
3128
+ },
3129
+ "e": {
3130
+ "description": "e",
3131
+ "type": "string",
3132
+ },
3133
+ },
3134
+ "required": [
3135
+ "d",
3136
+ "e",
3137
+ ],
3138
+ "type": "object",
3139
+ }
3140
+ `);
3141
+
3142
+ const b = z.toJSONSchema(
3143
+ z.object({
3144
+ d: field.describe("d"),
3145
+ e: field.describe("e"),
3146
+ }),
3147
+ {
3148
+ reused: "ref",
3149
+ }
3150
+ );
3151
+ expect(b).toMatchInlineSnapshot(`
3152
+ {
3153
+ "$defs": {
3154
+ "__schema0": {
3155
+ "description": "c",
3156
+ "type": "string",
3157
+ },
3158
+ },
3159
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3160
+ "additionalProperties": false,
3161
+ "properties": {
3162
+ "d": {
3163
+ "$ref": "#/$defs/__schema0",
3164
+ "description": "d",
3165
+ },
3166
+ "e": {
3167
+ "$ref": "#/$defs/__schema0",
3168
+ "description": "e",
3169
+ },
3170
+ },
3171
+ "required": [
3172
+ "d",
3173
+ "e",
3174
+ ],
3175
+ "type": "object",
3176
+ }
3177
+ `);
3178
+ });
3179
+
3180
+ test("top-level readonly", () => {
3181
+ const A = z
3182
+ .object({
3183
+ name: z.string(),
3184
+ get b() {
3185
+ return B;
3186
+ },
3187
+ })
3188
+ .readonly()
3189
+ .meta({ id: "A" });
3190
+ // z.globalRegistry.add(A, { id: "A" });
3191
+ // .meta({ id: "A" });
3192
+
3193
+ const B = z
3194
+ .object({
3195
+ name: z.string(),
3196
+ get a() {
3197
+ return A;
3198
+ },
3199
+ })
3200
+ .readonly()
3201
+ .meta({ id: "B" });
3202
+ // z.globalRegistry.add(B, { id: "B" });
3203
+ // .meta({ id: "B" });
3204
+
3205
+ const result = z.toJSONSchema(A);
3206
+ expect(result).toEqual({
3207
+ $schema: "https://json-schema.org/draft/2020-12/schema",
3208
+ $ref: "#/$defs/A",
3209
+ $defs: {
3210
+ A: {
3211
+ additionalProperties: false,
3212
+ properties: {
3213
+ b: {
3214
+ $ref: "#/$defs/B",
3215
+ },
3216
+ name: {
3217
+ type: "string",
3218
+ },
3219
+ },
3220
+ readOnly: true,
3221
+ required: ["name", "b"],
3222
+ type: "object",
3223
+ },
3224
+ B: {
3225
+ additionalProperties: false,
3226
+ properties: {
3227
+ a: {
3228
+ $ref: "#/$defs/A",
3229
+ },
3230
+ name: {
3231
+ type: "string",
3232
+ },
3233
+ },
3234
+ readOnly: true,
3235
+ required: ["name", "a"],
3236
+ type: "object",
3237
+ },
3238
+ },
3239
+ });
3240
+ });
3241
+
3242
+ test("basic registry", () => {
3243
+ const myRegistry = z.registry<{ id: string }>();
3244
+ const User = z.object({
3245
+ name: z.string(),
3246
+ get posts() {
3247
+ return z.array(Post);
3248
+ },
3249
+ });
3250
+
3251
+ const Post = z.object({
3252
+ title: z.string(),
3253
+ content: z.string(),
3254
+ get author() {
3255
+ return User;
3256
+ },
3257
+ });
3258
+
3259
+ myRegistry.add(User, { id: "User" });
3260
+ myRegistry.add(Post, { id: "Post" });
3261
+
3262
+ const result = z.toJSONSchema(myRegistry, {
3263
+ uri: (id) => `https://example.com/${id}.json`,
3264
+ });
3265
+ expect(result).toMatchInlineSnapshot(`
3266
+ {
3267
+ "schemas": {
3268
+ "Post": {
3269
+ "$id": "https://example.com/Post.json",
3270
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3271
+ "additionalProperties": false,
3272
+ "properties": {
3273
+ "author": {
3274
+ "$ref": "https://example.com/User.json",
3275
+ },
3276
+ "content": {
3277
+ "type": "string",
3278
+ },
3279
+ "title": {
3280
+ "type": "string",
3281
+ },
3282
+ },
3283
+ "required": [
3284
+ "title",
3285
+ "content",
3286
+ "author",
3287
+ ],
3288
+ "type": "object",
3289
+ },
3290
+ "User": {
3291
+ "$id": "https://example.com/User.json",
3292
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3293
+ "additionalProperties": false,
3294
+ "properties": {
3295
+ "name": {
3296
+ "type": "string",
3297
+ },
3298
+ "posts": {
3299
+ "items": {
3300
+ "$ref": "https://example.com/Post.json",
3301
+ },
3302
+ "type": "array",
3303
+ },
3304
+ },
3305
+ "required": [
3306
+ "name",
3307
+ "posts",
3308
+ ],
3309
+ "type": "object",
3310
+ },
3311
+ },
3312
+ }
3313
+ `);
3314
+ });
3315
+
3316
+ function countMapScanWork<T>(convert: () => T): { result: T; work: number } {
3317
+ const entries = Map.prototype.entries;
3318
+ const values = Map.prototype.values;
3319
+ let work = 0;
3320
+ const entriesSpy = vi.spyOn(Map.prototype, "entries").mockImplementation(function (this: Map<unknown, unknown>) {
3321
+ work += this.size;
3322
+ return entries.call(this);
3323
+ } as typeof Map.prototype.entries);
3324
+ const valuesSpy = vi.spyOn(Map.prototype, "values").mockImplementation(function (this: Map<unknown, unknown>) {
3325
+ work += this.size;
3326
+ return values.call(this);
3327
+ } as typeof Map.prototype.values);
3328
+ try {
3329
+ return { result: convert(), work };
3330
+ } finally {
3331
+ entriesSpy.mockRestore();
3332
+ valuesSpy.mockRestore();
3333
+ }
3334
+ }
3335
+
3336
+ test("large registry conversion performs linear map-scan work", () => {
3337
+ const count = 64;
3338
+ const convert = (size: number, withIntersection: boolean) => {
3339
+ const registry = z.registry<{ id: string }>();
3340
+ for (let i = 0; i < size; i++) {
3341
+ registry.add(
3342
+ z.object({ id: z.string(), name: z.string(), count: z.number(), nested: z.object({ a: z.boolean() }) }),
3343
+ { id: `Type${i}` }
3344
+ );
3345
+ }
3346
+ if (withIntersection) registry.add(z.object({ a: z.string() }).and(z.object({ b: z.string() })), { id: "Inter" });
3347
+
3348
+ return countMapScanWork(() => z.toJSONSchema(registry, { uri: (id) => `https://example.com/${id}.json` }));
3349
+ };
3350
+
3351
+ const plain = convert(count, false);
3352
+ const plainDouble = convert(count * 2, false);
3353
+ expect(Object.keys(plain.result.schemas)).toHaveLength(count);
3354
+ expect(plain.result.schemas.Type0).toMatchObject({
3355
+ $id: "https://example.com/Type0.json",
3356
+ type: "object",
3357
+ properties: { nested: { type: "object" } },
3358
+ });
3359
+ expect(plain.result.schemas[`Type${count - 1}`]!.$id).toBe(`https://example.com/Type${count - 1}.json`);
3360
+ expect(plain.work).toBeGreaterThan(0);
3361
+ expect(plainDouble.work).toBeLessThanOrEqual(plain.work * 2);
3362
+
3363
+ const folded = convert(count, true);
3364
+ const foldedDouble = convert(count * 2, true);
3365
+ expect(folded.result.schemas.Inter).toMatchObject({ type: "object", properties: { a: {}, b: {} } });
3366
+ expect(folded.work).toBeGreaterThan(0);
3367
+ expect(foldedDouble.work).toBeLessThanOrEqual(folded.work * 2);
3368
+ });
3369
+
3370
+ test("a registry of records with numeric keys performs linear map-scan work", () => {
3371
+ const count = 64;
3372
+ const convert = (size: number, key: (i: number) => z.core.$ZodType) => {
3373
+ const registry = z.registry<{ id: string }>();
3374
+ for (let i = 0; i < size; i++) {
3375
+ registry.add(z.object({ m: z.record(key(i) as z.core.$ZodRecordKey, z.boolean()) }), { id: `Type${i}` });
3376
+ }
3377
+ return countMapScanWork(() => z.toJSONSchema(registry, { uri: (id) => `https://example.com/${id}.json` }));
3378
+ };
3379
+
3380
+ const numeric = convert(count, () => z.number());
3381
+ const numericDouble = convert(count * 2, () => z.number());
3382
+ expect(numeric.result.schemas.Type0).toMatchObject({
3383
+ properties: { m: { propertyNames: { type: "string", pattern: "^-?\\d+(?:\\.\\d+)?$" } } },
3384
+ });
3385
+ expect(numeric.work).toBeGreaterThan(0);
3386
+ expect(numericDouble.work).toBeLessThanOrEqual(numeric.work * 2);
3387
+
3388
+ const extracted = convert(count, (i) => z.number().meta({ id: `Key${i}` }));
3389
+ const extractedDouble = convert(count * 2, (i) => z.number().meta({ id: `Key${i}` }));
3390
+ expect(extracted.result.schemas.Type0).toMatchObject({
3391
+ properties: { m: { propertyNames: { type: "string", pattern: "^-?\\d+(?:\\.\\d+)?$" } } },
3392
+ });
3393
+ expect(extracted.work).toBeGreaterThan(0);
3394
+ expect(extractedDouble.work).toBeLessThanOrEqual(extracted.work * 2);
3395
+ });
3396
+
3397
+ test("registry extracts unregistered subschemas into __shared", () => {
3398
+ const registry = z.registry<{ id: string }>();
3399
+ const address = z.object({ street: z.string() }).meta({ id: "Address" });
3400
+ registry.add(z.object({ home: address, work: address }), { id: "Person" });
3401
+ registry.add(z.object({ hq: address }), { id: "Company" });
3402
+
3403
+ const { schemas } = z.toJSONSchema(registry, { uri: (id) => `https://example.com/${id}.json` });
3404
+
3405
+ expect(schemas.__shared).toMatchInlineSnapshot(`
3406
+ {
3407
+ "$defs": {
3408
+ "Address": {
3409
+ "additionalProperties": false,
3410
+ "properties": {
3411
+ "street": {
3412
+ "type": "string",
3413
+ },
3414
+ },
3415
+ "required": [
3416
+ "street",
3417
+ ],
3418
+ "type": "object",
3419
+ },
3420
+ },
3421
+ }
3422
+ `);
3423
+ expect(schemas.Person).toMatchInlineSnapshot(`
3424
+ {
3425
+ "$id": "https://example.com/Person.json",
3426
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3427
+ "additionalProperties": false,
3428
+ "properties": {
3429
+ "home": {
3430
+ "$ref": "https://example.com/__shared.json#/$defs/Address",
3431
+ },
3432
+ "work": {
3433
+ "$ref": "https://example.com/__shared.json#/$defs/Address",
3434
+ },
3435
+ },
3436
+ "required": [
3437
+ "home",
3438
+ "work",
3439
+ ],
3440
+ "type": "object",
3441
+ }
3442
+ `);
3443
+ // Company is emitted after Person, so it only resolves if the shared $defs built on the first finalize are still reachable — the pass that writes them no longer runs per schema.
3444
+ expect(schemas.Company!.properties!.hq).toEqual({
3445
+ $ref: "https://example.com/__shared.json#/$defs/Address",
3446
+ });
3447
+ });
3448
+
3449
+ test("registry extracts reused subschemas into __shared without an id", () => {
3450
+ const registry = z.registry<{ id: string }>();
3451
+ const shared = z.object({ q: z.string() });
3452
+ registry.add(z.object({ a: shared, b: shared }), { id: "First" });
3453
+ registry.add(z.object({ c: shared }), { id: "Second" });
3454
+
3455
+ const { schemas } = z.toJSONSchema(registry, { uri: (id) => `${id}.json`, reused: "ref" });
3456
+
3457
+ // The id is counter-generated, so this pins that ctx.counter is consumed exactly once.
3458
+ expect(Object.keys(schemas.__shared!.$defs!)).toEqual(["schema0"]);
3459
+ expect(schemas.First!.properties!.a).toEqual({ $ref: "__shared.json#/$defs/schema0" });
3460
+ expect(schemas.Second!.properties!.c).toEqual({ $ref: "__shared.json#/$defs/schema0" });
3461
+ });
3462
+
3463
+ test("JSONSchemaGenerator re-runs shared passes when emit params change", () => {
3464
+ const shared = z.object({ s: z.string() });
3465
+ const a = z.object({ x: shared, y: shared });
3466
+ const registry = z.registry<{ id: string }>();
3467
+ registry.add(a, { id: "A" });
3468
+
3469
+ const gen = new z.core.JSONSchemaGenerator({ target: "draft-2020-12" });
3470
+ gen.process(a);
3471
+ const defs: Record<string, any> = {};
3472
+ const external = { registry, uri: (id: string) => `${id}.json`, defs };
3473
+
3474
+ gen.emit(a, { external, reused: "inline" });
3475
+ // Same `external`, different `reused` — the second emit must still extract `shared`.
3476
+ const second: any = gen.emit(a, { external, reused: "ref" });
3477
+
3478
+ expect(Object.keys(defs)).toEqual(["schema0"]);
3479
+ expect(second.properties.x).toEqual({ $ref: "__shared.json#/$defs/schema0" });
3480
+ expect(second.properties.y).toEqual({ $ref: "__shared.json#/$defs/schema0" });
3481
+ });
3482
+
3483
+ test("JSONSchemaGenerator still throws on cycles when a later emit asks for it", () => {
3484
+ const Node: any = z.object({
3485
+ v: z.string(),
3486
+ get next() {
3487
+ return Node;
3488
+ },
3489
+ });
3490
+ const registry = z.registry<{ id: string }>();
3491
+ registry.add(Node, { id: "Node" });
3492
+
3493
+ const gen = new z.core.JSONSchemaGenerator({ target: "draft-2020-12" });
3494
+ gen.process(Node);
3495
+ const external = { registry, uri: (id: string) => `${id}.json`, defs: {} };
3496
+
3497
+ gen.emit(Node, { external, cycles: "ref" });
3498
+ expect(() => gen.emit(Node, { external, cycles: "throw" })).toThrow(/Cycle detected/);
3499
+ });
3500
+
3501
+ test("JSONSchemaGenerator re-runs shared passes on a no-params emit", () => {
3502
+ const shared = z.object({ s: z.string() });
3503
+ const a = z.object({ x: shared });
3504
+ const registry = z.registry<{ id: string }>();
3505
+ registry.add(a, { id: "A" });
3506
+
3507
+ const gen = new z.core.JSONSchemaGenerator({ target: "draft-2020-12" });
3508
+ gen.process(a);
3509
+ const defs: Record<string, any> = {};
3510
+ gen.emit(a, { external: { registry, uri: (id: string) => `${id}.json`, defs }, reused: "ref" });
3511
+
3512
+ // Re-processing an already-seen schema bumps `seen.count`, which `extractDefs` branches on under `reused: "ref"` — and it returns early, so it cannot clear the guards itself.
3513
+ gen.process(shared);
3514
+ const second: any = gen.emit(a);
3515
+
3516
+ expect(Object.keys(defs)).toEqual(["schema0"]);
3517
+ expect(second.properties.x).toEqual({ $ref: "__shared.json#/$defs/schema0" });
3518
+ });
3519
+
3520
+ test("_ref", () => {
3521
+ // const a = z.promise(z.string().describe("a"));
3522
+ const a = z.toJSONSchema(z.promise(z.string().describe("a")));
3523
+ expect(a).toMatchInlineSnapshot(`
3524
+ {
3525
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3526
+ "description": "a",
3527
+ "type": "string",
3528
+ }
3529
+ `);
3530
+
3531
+ const b = z.toJSONSchema(z.lazy(() => z.string().describe("a")));
3532
+ expect(b).toMatchInlineSnapshot(`
3533
+ {
3534
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3535
+ "description": "a",
3536
+ "type": "string",
3537
+ }
3538
+ `);
3539
+
3540
+ const c = z.toJSONSchema(z.optional(z.string().describe("a")));
3541
+ expect(c).toMatchInlineSnapshot(`
3542
+ {
3543
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3544
+ "description": "a",
3545
+ "type": "string",
3546
+ }
3547
+ `);
3548
+
3549
+ const d = z.toJSONSchema(z.string().meta({ id: "foo" }).describe("bar").optional());
3550
+ expect(d).toMatchInlineSnapshot(`
3551
+ {
3552
+ "$defs": {
3553
+ "foo": {
3554
+ "type": "string",
3555
+ },
3556
+ },
3557
+ "$ref": "#/$defs/foo",
3558
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3559
+ "description": "bar",
3560
+ }
3561
+ `);
3562
+ });
3563
+
3564
+ test("defaults/prefaults", () => {
3565
+ const a = z
3566
+ .string()
3567
+ .transform((val) => val.length)
3568
+ .pipe(z.number());
3569
+ const b = a.prefault("hello");
3570
+ const c = a.default(1234);
3571
+
3572
+ // a
3573
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
3574
+ {
3575
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3576
+ "type": "number",
3577
+ }
3578
+ `);
3579
+ expect(z.toJSONSchema(a, { io: "input" })).toMatchInlineSnapshot(`
3580
+ {
3581
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3582
+ "type": "string",
3583
+ }
3584
+ `);
3585
+
3586
+ // b
3587
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
3588
+ {
3589
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3590
+ "type": "number",
3591
+ }
3592
+ `);
3593
+ expect(z.toJSONSchema(b, { io: "input" })).toMatchInlineSnapshot(`
3594
+ {
3595
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3596
+ "default": "hello",
3597
+ "type": "string",
3598
+ }
3599
+ `);
3600
+ // c
3601
+ expect(z.toJSONSchema(c)).toMatchInlineSnapshot(`
3602
+ {
3603
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3604
+ "default": 1234,
3605
+ "type": "number",
3606
+ }
3607
+ `);
3608
+ expect(z.toJSONSchema(c, { io: "input" })).toMatchInlineSnapshot(`
3609
+ {
3610
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3611
+ "type": "string",
3612
+ }
3613
+ `);
3614
+ });
3615
+
3616
+ test("catch on a transforming schema", () => {
3617
+ const a = z
3618
+ .string()
3619
+ .transform((val) => val.length)
3620
+ .pipe(z.number())
3621
+ .catch(0);
3622
+
3623
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
3624
+ {
3625
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3626
+ "default": 0,
3627
+ "type": "number",
3628
+ }
3629
+ `);
3630
+ // catch values are output-typed, so they are not valid input
3631
+ expect(z.toJSONSchema(a, { io: "input" })).toMatchInlineSnapshot(`
3632
+ {
3633
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3634
+ "type": "string",
3635
+ }
3636
+ `);
3637
+
3638
+ // the catch no longer hides the inner transform from ancestors, so their output-typed metadata is stripped too — matching a bare nested transform
3639
+ expect(z.toJSONSchema(z.object({ a }).meta({ examples: [{ a: 1 }] }), { io: "input" })).toMatchInlineSnapshot(`
3640
+ {
3641
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3642
+ "properties": {
3643
+ "a": {
3644
+ "type": "string",
3645
+ },
3646
+ },
3647
+ "required": [
3648
+ "a",
3649
+ ],
3650
+ "type": "object",
3651
+ }
3652
+ `);
3653
+ });
3654
+
3655
+ test("falsy prefaults (false, 0, empty string)", () => {
3656
+ // boolean prefault false
3657
+ const a = z.boolean().prefault(false);
3658
+ expect(z.toJSONSchema(a, { io: "input" })).toMatchInlineSnapshot(`
3659
+ {
3660
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3661
+ "default": false,
3662
+ "type": "boolean",
3663
+ }
3664
+ `);
3665
+
3666
+ // number prefault 0
3667
+ const b = z.number().prefault(0);
3668
+ expect(z.toJSONSchema(b, { io: "input" })).toMatchInlineSnapshot(`
3669
+ {
3670
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3671
+ "default": 0,
3672
+ "type": "number",
3673
+ }
3674
+ `);
3675
+
3676
+ // string prefault empty string
3677
+ const c = z.string().prefault("");
3678
+ expect(z.toJSONSchema(c, { io: "input" })).toMatchInlineSnapshot(`
3679
+ {
3680
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3681
+ "default": "",
3682
+ "type": "string",
3683
+ }
3684
+ `);
3685
+ });
3686
+
3687
+ test("input type", () => {
3688
+ const schema = z.object({
3689
+ a: z.string(),
3690
+ b: z.string().optional(),
3691
+ c: z.string().default("hello"),
3692
+ d: z.string().nullable(),
3693
+ e: z.string().prefault("hello"),
3694
+ f: z.string().catch("hello"),
3695
+ g: z.never(),
3696
+ h: z.union([z.string(), z.number().default(2)]),
3697
+ i: z.union([z.string(), z.string().optional()]),
3698
+ });
3699
+ expect(z.toJSONSchema(schema, { io: "input" })).toMatchInlineSnapshot(`
3700
+ {
3701
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3702
+ "properties": {
3703
+ "a": {
3704
+ "type": "string",
3705
+ },
3706
+ "b": {
3707
+ "type": "string",
3708
+ },
3709
+ "c": {
3710
+ "default": "hello",
3711
+ "type": "string",
3712
+ },
3713
+ "d": {
3714
+ "type": [
3715
+ "string",
3716
+ "null",
3717
+ ],
3718
+ },
3719
+ "e": {
3720
+ "default": "hello",
3721
+ "type": "string",
3722
+ },
3723
+ "f": {
3724
+ "default": "hello",
3725
+ "type": "string",
3726
+ },
3727
+ "g": {
3728
+ "not": {},
3729
+ },
3730
+ "h": {
3731
+ "anyOf": [
3732
+ {
3733
+ "type": "string",
3734
+ },
3735
+ {
3736
+ "default": 2,
3737
+ "type": "number",
3738
+ },
3739
+ ],
3740
+ },
3741
+ "i": {
3742
+ "type": "string",
3743
+ },
3744
+ },
3745
+ "required": [
3746
+ "a",
3747
+ "d",
3748
+ "f",
3749
+ "g",
3750
+ ],
3751
+ "type": "object",
3752
+ }
3753
+ `);
3754
+ expect(z.toJSONSchema(schema, { io: "output" })).toMatchInlineSnapshot(`
3755
+ {
3756
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3757
+ "additionalProperties": false,
3758
+ "properties": {
3759
+ "a": {
3760
+ "type": "string",
3761
+ },
3762
+ "b": {
3763
+ "type": "string",
3764
+ },
3765
+ "c": {
3766
+ "default": "hello",
3767
+ "type": "string",
3768
+ },
3769
+ "d": {
3770
+ "type": [
3771
+ "string",
3772
+ "null",
3773
+ ],
3774
+ },
3775
+ "e": {
3776
+ "type": "string",
3777
+ },
3778
+ "f": {
3779
+ "default": "hello",
3780
+ "type": "string",
3781
+ },
3782
+ "g": {
3783
+ "not": {},
3784
+ },
3785
+ "h": {
3786
+ "anyOf": [
3787
+ {
3788
+ "type": "string",
3789
+ },
3790
+ {
3791
+ "default": 2,
3792
+ "type": "number",
3793
+ },
3794
+ ],
3795
+ },
3796
+ "i": {
3797
+ "type": "string",
3798
+ },
3799
+ },
3800
+ "required": [
3801
+ "a",
3802
+ "c",
3803
+ "d",
3804
+ "e",
3805
+ "f",
3806
+ "g",
3807
+ "h",
3808
+ ],
3809
+ "type": "object",
3810
+ }
3811
+ `);
3812
+ });
3813
+
3814
+ test("examples on pipe", () => {
3815
+ const schema = z
3816
+ .string()
3817
+ .meta({ examples: ["test"] })
3818
+ .transform(Number)
3819
+ // .pipe(z.transform(Number).meta({ examples: [4] }))
3820
+ .meta({ examples: [4] });
3821
+
3822
+ const i = z.toJSONSchema(schema, { io: "input", unrepresentable: "any" });
3823
+ expect(i).toMatchInlineSnapshot(`
3824
+ {
3825
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3826
+ "examples": [
3827
+ "test",
3828
+ ],
3829
+ "type": "string",
3830
+ }
3831
+ `);
3832
+ const o = z.toJSONSchema(schema, { io: "output", unrepresentable: "any" });
3833
+ expect(o).toMatchInlineSnapshot(`
3834
+ {
3835
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3836
+ "examples": [
3837
+ 4,
3838
+ ],
3839
+ }
3840
+ `);
3841
+ });
3842
+
3843
+ // test("number checks", () => {
3844
+ // expect(z.toJSONSchema(z.number().int())).toMatchInlineSnapshot(`
3845
+ // {
3846
+ // "maximum": 9007199254740991,
3847
+ // "minimum": -9007199254740991,
3848
+ // "type": "integer",
3849
+ // }
3850
+ // `);
3851
+ // expect(z.toJSONSchema(z.int())).toMatchInlineSnapshot(`
3852
+ // {
3853
+ // "maximum": 9007199254740991,
3854
+ // "minimum": -9007199254740991,
3855
+ // "type": "integer",
3856
+ // }
3857
+ // `);
3858
+ // expect(z.toJSONSchema(z.int().positive())).toMatchInlineSnapshot(`
3859
+ // {
3860
+ // "exclusiveMinimum": 0,
3861
+ // "maximum": 9007199254740991,
3862
+ // "minimum": -9007199254740991,
3863
+ // "type": "integer",
3864
+ // }
3865
+ // `);
3866
+ // expect(z.toJSONSchema(z.int().nonnegative())).toMatchInlineSnapshot(`
3867
+ // {
3868
+ // "maximum": 9007199254740991,
3869
+ // "minimum": 0,
3870
+ // "type": "integer",
3871
+ // }
3872
+ // `);
3873
+ // expect(z.toJSONSchema(z.int().gt(0))).toMatchInlineSnapshot(`
3874
+ // {
3875
+ // "exclusiveMinimum": 0,
3876
+ // "maximum": 9007199254740991,
3877
+ // "minimum": -9007199254740991,
3878
+ // "type": "integer",
3879
+ // }
3880
+ // `);
3881
+ // expect(z.toJSONSchema(z.int().gte(0))).toMatchInlineSnapshot(`
3882
+ // {
3883
+ // "maximum": 9007199254740991,
3884
+ // "minimum": 0,
3885
+ // "type": "integer",
3886
+ // }
3887
+ // `);
3888
+
3889
+ // });
3890
+
3891
+ test("use output type for preprocess", () => {
3892
+ const a = z.preprocess((val) => String(val), z.string());
3893
+
3894
+ expect(z.toJSONSchema(a, { io: "input" })).toMatchInlineSnapshot(`
3895
+ {
3896
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3897
+ "type": "string",
3898
+ }
3899
+ `);
3900
+ });
3901
+
3902
+ test("object property with preprocess stays required in input JSON schema", () => {
3903
+ const schema = z.object({
3904
+ noPreprocess: z.string(),
3905
+ withPreprocess: z.preprocess((v) => v, z.string()),
3906
+ optionalPreprocess: z.preprocess((v) => v, z.string().optional()),
3907
+ });
3908
+
3909
+ // A preprocessed property is only optional when its inner schema is — the
3910
+ // transform wrapper must not make a required property optional in the input
3911
+ // JSON schema (matches runtime: `schema.parse({})` rejects `withPreprocess`).
3912
+ expect(z.toJSONSchema(schema, { io: "input" })).toMatchInlineSnapshot(`
3913
+ {
3914
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3915
+ "properties": {
3916
+ "noPreprocess": {
3917
+ "type": "string",
3918
+ },
3919
+ "optionalPreprocess": {
3920
+ "type": "string",
3921
+ },
3922
+ "withPreprocess": {
3923
+ "type": "string",
3924
+ },
3925
+ },
3926
+ "required": [
3927
+ "noPreprocess",
3928
+ "withPreprocess",
3929
+ ],
3930
+ "type": "object",
3931
+ }
3932
+ `);
3933
+ });
3934
+
3935
+ test("input JSON schema resolves requiredness past transform and catch wrappers", () => {
3936
+ const required = (schema: z.ZodObject) =>
3937
+ (z.toJSONSchema(schema, { io: "input" }) as z.core.JSONSchema.ObjectSchema).required;
3938
+ const id = (v: unknown) => v;
3939
+
3940
+ // catch observes an absent key at runtime, but its declared input type stays required (#5003)
3941
+ expect(required(z.object({ a: z.string().catch("x") }))).toEqual(["a"]);
3942
+ expect(required(z.object({ a: z.string().optional().catch("x") }))).toBeUndefined();
3943
+ // nested preprocess and the legacy transform-pipe form both resolve through to the inner schema
3944
+ expect(required(z.object({ a: z.preprocess(id, z.preprocess(id, z.string())) }))).toEqual(["a"]);
3945
+ expect(required(z.object({ a: z.transform((v: unknown) => String(v)).pipe(z.string()) }))).toEqual(["a"]);
3946
+ });
3947
+
3948
+ test("object required keys preserve shape order and direction", () => {
3949
+ const symbol = Symbol("symbol");
3950
+ const shape: z.ZodRawShape = {
3951
+ 10: z.string(),
3952
+ 2: z.string().optional(),
3953
+ plain: z.string(),
3954
+ optional: z.string().optional(),
3955
+ defaulted: z.string().default("x"),
3956
+ caught: z.string().catch("x"),
3957
+ [symbol]: z.string(),
3958
+ };
3959
+ Object.defineProperty(shape, "__proto__", { value: z.string(), enumerable: true });
3960
+ Object.defineProperty(shape, "hidden", { value: z.string(), enumerable: false });
3961
+
3962
+ const input = z.toJSONSchema(z.object(shape), { io: "input", unrepresentable: "any" });
3963
+ const output = z.toJSONSchema(z.object(shape), { io: "output", unrepresentable: "any" });
3964
+
3965
+ expect(Object.keys(input.properties!)).toEqual(["2", "10", "plain", "optional", "defaulted", "caught", "__proto__"]);
3966
+ expect(input.required).toEqual(["10", "plain", "caught", "__proto__"]);
3967
+ expect(output.required).toEqual(["10", "plain", "defaulted", "caught", "__proto__"]);
3968
+ expect(Object.prototype.hasOwnProperty.call(input.properties!, "__proto__")).toBe(true);
3969
+ expect(input.properties).not.toHaveProperty("hidden");
3970
+ expect(Object.getOwnPropertySymbols(input.properties!)).toEqual([]);
3971
+ });
3972
+
3973
+ test("strip output-side examples from input JSON schema for codec", () => {
3974
+ const codec = z
3975
+ .codec(z.string(), z.number(), { decode: (s) => Number(s), encode: (n) => String(n) })
3976
+ .meta({ examples: [42] });
3977
+
3978
+ expect(z.toJSONSchema(codec, { io: "input" })).toMatchInlineSnapshot(`
3979
+ {
3980
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3981
+ "type": "string",
3982
+ }
3983
+ `);
3984
+ expect(z.toJSONSchema(codec, { io: "output" })).toMatchInlineSnapshot(`
3985
+ {
3986
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3987
+ "examples": [
3988
+ 42,
3989
+ ],
3990
+ "type": "number",
3991
+ }
3992
+ `);
3993
+ });
3994
+
3995
+ // test("isTransforming", () => {
3996
+ // const tx = z.core.isTransforming;
3997
+ // expect(tx(z.string())).toEqual(false);
3998
+ // expect(tx(z.string().transform((val) => val))).toEqual(true);
3999
+ // expect(tx(z.string().pipe(z.string()))).toEqual(false);
4000
+ // expect(
4001
+ // tx(
4002
+ // z
4003
+ // .string()
4004
+ // .transform((val) => val)
4005
+ // .pipe(z.string())
4006
+ // )
4007
+ // ).toEqual(true);
4008
+
4009
+ // const a = z.transform((val) => val);
4010
+ // expect(tx(z.transform((val) => val))).toEqual(true);
4011
+ // expect(tx(a.optional())).toEqual(true);
4012
+
4013
+ // const b = z.string().optional();
4014
+ // expect(tx(b)).toEqual(false);
4015
+
4016
+ // const c = z.string().prefault("hello");
4017
+ // expect(tx(c)).toEqual(false);
4018
+
4019
+ // const d = z.string().default("hello");
4020
+ // expect(tx(d)).toEqual(false);
4021
+ // });
4022
+
4023
+ test("flatten simple intersections", () => {
4024
+ const FirstSchema = z.object({
4025
+ testNum: z.number(),
4026
+ });
4027
+
4028
+ const SecondSchema = z.object({
4029
+ testStr: z.string(),
4030
+ });
4031
+
4032
+ const ThirdSchema = z.object({
4033
+ testBool: z.boolean(),
4034
+ });
4035
+
4036
+ const HelloSchema = FirstSchema.and(SecondSchema).and(ThirdSchema).describe("123");
4037
+
4038
+ // Zod 3
4039
+ // console.log(JSON.stringify(zodToJsonSchema(HelloSchema), null, 2));
4040
+
4041
+ // Zod 4
4042
+ const result = z.toJSONSchema(HelloSchema, { target: "draft-7" });
4043
+ expect(result).toMatchInlineSnapshot(`
4044
+ {
4045
+ "$schema": "http://json-schema.org/draft-07/schema#",
4046
+ "additionalProperties": false,
4047
+ "description": "123",
4048
+ "properties": {
4049
+ "testBool": {
4050
+ "type": "boolean",
4051
+ },
4052
+ "testNum": {
4053
+ "type": "number",
4054
+ },
4055
+ "testStr": {
4056
+ "type": "string",
4057
+ },
4058
+ },
4059
+ "required": [
4060
+ "testNum",
4061
+ "testStr",
4062
+ "testBool",
4063
+ ],
4064
+ "type": "object",
4065
+ }
4066
+ `);
4067
+ });
4068
+
4069
+ test("z.file()", () => {
4070
+ const a = z.file();
4071
+ expect(z.toJSONSchema(a)).toMatchInlineSnapshot(`
4072
+ {
4073
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4074
+ "contentEncoding": "binary",
4075
+ "format": "binary",
4076
+ "type": "string",
4077
+ }
4078
+ `);
4079
+
4080
+ const b = z.file().mime("image/png").min(1000).max(10000);
4081
+ expect(z.toJSONSchema(b)).toMatchInlineSnapshot(`
4082
+ {
4083
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4084
+ "contentEncoding": "binary",
4085
+ "contentMediaType": "image/png",
4086
+ "format": "binary",
4087
+ "maxLength": 10000,
4088
+ "minLength": 1000,
4089
+ "type": "string",
4090
+ }
4091
+ `);
4092
+
4093
+ const c = z.file().mime(["image/png", "image/jpg"]).min(1000).max(10000);
4094
+ expect(z.toJSONSchema(c)).toMatchInlineSnapshot(`
4095
+ {
4096
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4097
+ "anyOf": [
4098
+ {
4099
+ "contentMediaType": "image/png",
4100
+ },
4101
+ {
4102
+ "contentMediaType": "image/jpg",
4103
+ },
4104
+ ],
4105
+ "contentEncoding": "binary",
4106
+ "format": "binary",
4107
+ "maxLength": 10000,
4108
+ "minLength": 1000,
4109
+ "type": "string",
4110
+ }
4111
+ `);
4112
+ });
4113
+
4114
+ test("custom toJSONSchema", () => {
4115
+ const schema = z.instanceof(Date);
4116
+ schema._zod.toJSONSchema = () => ({
4117
+ type: "string",
4118
+ format: "date-time",
4119
+ });
4120
+
4121
+ expect(z.toJSONSchema(schema)).toMatchInlineSnapshot(`
4122
+ {
4123
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4124
+ "format": "date-time",
4125
+ "type": "string",
4126
+ }
4127
+ `);
4128
+ });
4129
+
4130
+ test("cycle detection - root", () => {
4131
+ const schema = z.object({
4132
+ name: z.string(),
4133
+ get subcategories() {
4134
+ return z.array(schema);
4135
+ },
4136
+ });
4137
+
4138
+ expect(() => z.toJSONSchema(schema, { cycles: "throw" })).toThrowErrorMatchingInlineSnapshot(`
4139
+ [Error: Cycle detected: #/properties/subcategories/items/<root>
4140
+
4141
+ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.]
4142
+ `);
4143
+ });
4144
+
4145
+ test("cycle detection - mutual recursion", () => {
4146
+ const A = z.object({
4147
+ name: z.string(),
4148
+ get subcategories() {
4149
+ return z.array(B);
4150
+ },
4151
+ });
4152
+
4153
+ const B = z.object({
4154
+ name: z.string(),
4155
+ get subcategories() {
4156
+ return z.array(A);
4157
+ },
4158
+ });
4159
+
4160
+ expect(() => z.toJSONSchema(A, { cycles: "throw" })).toThrowErrorMatchingInlineSnapshot(`
4161
+ [Error: Cycle detected: #/properties/subcategories/items/properties/subcategories/items/<root>
4162
+
4163
+ Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.]
4164
+ `);
4165
+ });
4166
+
4167
+ test("recursive lazy with describe does not stack overflow", () => {
4168
+ const NodeSchema: z.ZodType = z.lazy(() =>
4169
+ z
4170
+ .object({
4171
+ value: z.string().describe("node value"),
4172
+ children: z.array(NodeSchema.describe("child node")).optional().describe("child list"),
4173
+ })
4174
+ .describe("tree node")
4175
+ );
4176
+
4177
+ const result = z.toJSONSchema(NodeSchema, { cycles: "ref", reused: "ref" });
4178
+ expect(result).toBeDefined();
4179
+ expect(result.$defs).toBeDefined();
4180
+ });
4181
+
4182
+ test("__proto__ shape key is emitted as an own property", () => {
4183
+ const schema = z.object({ ["__proto__"]: z.literal("admin"), role: z.string() });
4184
+ const result = z.toJSONSchema(schema, { io: "input" });
4185
+
4186
+ expect(result.required).toEqual(["__proto__", "role"]);
4187
+ // every required key needs a matching entry in properties
4188
+ for (const key of result.required!) {
4189
+ expect(Object.prototype.hasOwnProperty.call(result.properties, key)).toBe(true);
4190
+ }
4191
+ expect(JSON.parse(JSON.stringify(result)).properties.__proto__).toEqual({ type: "string", const: "admin" });
4192
+ });
4193
+
4194
+ test("__proto__ registry id is emitted as an own entry", () => {
4195
+ const myRegistry = z.registry<{ id: string }>();
4196
+ myRegistry.add(z.object({ a: z.string() }), { id: "__proto__" });
4197
+ myRegistry.add(z.object({ b: z.string() }), { id: "normal" });
4198
+
4199
+ expect(Object.keys(z.toJSONSchema(myRegistry).schemas)).toEqual(["__proto__", "normal"]);
4200
+ });
4201
+
4202
+ test("__proto__ def id emits a resolvable $ref", () => {
4203
+ const myRegistry = z.registry<{ id: string }>();
4204
+ const Inner = z.object({ x: z.string() });
4205
+ myRegistry.add(Inner, { id: "__proto__" });
4206
+
4207
+ const json = JSON.parse(JSON.stringify(z.toJSONSchema(z.object({ a: Inner, b: Inner }), { metadata: myRegistry })));
4208
+ expect(json.properties.a.$ref).toBe("#/$defs/__proto__");
4209
+ expect(json.$defs.__proto__).toBeDefined();
4210
+ });
4211
+
4212
+ test("__proto__ patternProperties key is emitted as an own property", () => {
4213
+ const result = z.toJSONSchema(z.looseRecord(z.string().regex(/__proto__/), z.string()));
4214
+
4215
+ expect(Object.getPrototypeOf(result.patternProperties)).toBe(Object.prototype);
4216
+ expect(Object.prototype.hasOwnProperty.call(result.patternProperties, "__proto__")).toBe(true);
4217
+ expect(result.patternProperties?.__proto__).toEqual({ type: "string" });
4218
+ });
4219
+
4220
+ test("__proto__ metadata survives direct and wrapper metadata merges", () => {
4221
+ for (const schema of [z.string(), z.readonly(z.string())]) {
4222
+ const registry = z.registry<Record<string, unknown>>();
4223
+ registry.add(schema, Object.fromEntries([["__proto__", { marker: true }]]));
4224
+
4225
+ const result: any = z.toJSONSchema(schema, { metadata: registry });
4226
+ expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
4227
+ expect(Object.prototype.hasOwnProperty.call(result, "__proto__")).toBe(true);
4228
+ expect(result.__proto__).toEqual({ marker: true });
4229
+ expect(JSON.parse(JSON.stringify(result)).__proto__).toEqual({ marker: true });
4230
+ }
4231
+ });
4232
+
4233
+ test("partialRecord does not require finite keys", () => {
4234
+ const result = z.toJSONSchema(z.partialRecord(z.enum(["__proto__", "b"]), z.string()));
4235
+
4236
+ expect(result.required).toBeUndefined();
4237
+ expect(result.propertyNames).toEqual({ type: "string", enum: ["__proto__", "b"] });
4238
+ expect(result.additionalProperties).toEqual({ type: "string" });
4239
+ });
4240
+
4241
+ describe("unrepresentable callback", () => {
4242
+ test("is consulted for every unrepresentable type", () => {
4243
+ const seen: string[] = [];
4244
+ const collect: z.core.UnrepresentableHandler<z.core.$ZodTypes> = ({ zodSchema }) => {
4245
+ seen.push(zodSchema._zod.def.type);
4246
+ return "any";
4247
+ };
4248
+
4249
+ const schemas = [
4250
+ z.bigint(),
4251
+ z.symbol(),
4252
+ z.undefined(),
4253
+ z.void(),
4254
+ z.date(),
4255
+ z.nan(),
4256
+ z.custom<string>(),
4257
+ z.map(z.string(), z.string()),
4258
+ z.set(z.string()),
4259
+ z.transform((x: unknown) => x),
4260
+ z.literal([undefined]),
4261
+ z.literal([1n]),
4262
+ z.string().catch(() => {
4263
+ throw new Error("dynamic");
4264
+ }),
4265
+ // z.function() is not a ZodType, but its processor is reachable through the same path
4266
+ z.function() as unknown as z.ZodType,
4267
+ ];
4268
+ for (const schema of schemas) z.toJSONSchema(schema, { unrepresentable: collect });
4269
+
4270
+ expect(seen).toEqual([
4271
+ "bigint",
4272
+ "symbol",
4273
+ "undefined",
4274
+ "void",
4275
+ "date",
4276
+ "nan",
4277
+ "custom",
4278
+ "map",
4279
+ "set",
4280
+ "transform",
4281
+ "literal",
4282
+ "literal",
4283
+ "catch",
4284
+ "function",
4285
+ ]);
4286
+ });
4287
+
4288
+ test("returned JSON Schema replaces the unrepresentable node", () => {
4289
+ // the motivating case: represent dates, keep throwing for everything else
4290
+ const params: z.core.ToJSONSchemaParams = {
4291
+ unrepresentable: ({ zodSchema }) =>
4292
+ zodSchema._zod.def.type === "date" ? { type: "string", format: "date-time" } : "throw",
4293
+ };
4294
+ expect(z.toJSONSchema(z.object({ when: z.date() }), params)).toMatchInlineSnapshot(`
4295
+ {
4296
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4297
+ "additionalProperties": false,
4298
+ "properties": {
4299
+ "when": {
4300
+ "format": "date-time",
4301
+ "type": "string",
4302
+ },
4303
+ },
4304
+ "required": [
4305
+ "when",
4306
+ ],
4307
+ "type": "object",
4308
+ }
4309
+ `);
4310
+ expect(() => z.toJSONSchema(z.object({ id: z.bigint() }), params)).toThrow(
4311
+ "BigInt cannot be represented in JSON Schema"
4312
+ );
4313
+ });
4314
+
4315
+ test("`throw` and `undefined` returns produce the default error", () => {
4316
+ expect(() => z.toJSONSchema(z.date(), { unrepresentable: () => "throw" })).toThrow(
4317
+ "Date cannot be represented in JSON Schema"
4318
+ );
4319
+ expect(() => z.toJSONSchema(z.date(), { unrepresentable: () => undefined })).toThrow(
4320
+ "Date cannot be represented in JSON Schema"
4321
+ );
4322
+ });
4323
+
4324
+ test("`any` return matches the string option", () => {
4325
+ expect(z.toJSONSchema(z.date(), { unrepresentable: () => "any" })).toEqual(
4326
+ z.toJSONSchema(z.date(), { unrepresentable: "any" })
4327
+ );
4328
+ });
4329
+
4330
+ test("`message` distinguishes sites that share a schema", () => {
4331
+ const seen: string[] = [];
4332
+ expect(
4333
+ z.toJSONSchema(z.literal([undefined, 1n, "a"]), {
4334
+ unrepresentable: ({ zodSchema, message }) => {
4335
+ seen.push(`${zodSchema._zod.def.type}: ${message}`);
4336
+ return "any";
4337
+ },
4338
+ })
4339
+ ).toMatchInlineSnapshot(`
4340
+ {
4341
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4342
+ "enum": [
4343
+ 1,
4344
+ "a",
4345
+ ],
4346
+ }
4347
+ `);
4348
+ // same `zodSchema`, different message -- the only way to tell the two literal sites apart
4349
+ expect(seen).toEqual([
4350
+ "literal: Literal `undefined` cannot be represented in JSON Schema",
4351
+ "literal: BigInt literals cannot be represented in JSON Schema",
4352
+ ]);
4353
+ });
4354
+
4355
+ test("errors thrown by the callback propagate", () => {
4356
+ expect(() =>
4357
+ z.toJSONSchema(z.object({ when: z.date() }), {
4358
+ unrepresentable: ({ zodSchema, path }) => {
4359
+ throw new Error(`${zodSchema._zod.def.type} at /${path.join("/")}`);
4360
+ },
4361
+ })
4362
+ ).toThrow("date at /properties/when");
4363
+ });
4364
+
4365
+ test("runs before `override`", () => {
4366
+ const order: string[] = [];
4367
+ expect(
4368
+ z.toJSONSchema(z.date(), {
4369
+ unrepresentable: () => {
4370
+ order.push("unrepresentable");
4371
+ return { type: "string" };
4372
+ },
4373
+ override: (ctx) => {
4374
+ order.push("override");
4375
+ if (ctx.jsonSchema.type === "string") ctx.jsonSchema.format = "date-time";
4376
+ },
4377
+ })
4378
+ ).toMatchInlineSnapshot(`
4379
+ {
4380
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4381
+ "format": "date-time",
4382
+ "type": "string",
4383
+ }
4384
+ `);
4385
+ expect(order).toEqual(["unrepresentable", "override"]);
4386
+ });
4387
+
4388
+ test("a returned schema replaces the whole literal", () => {
4389
+ expect(
4390
+ z.toJSONSchema(z.literal(["a", 1n]), {
4391
+ unrepresentable: () => ({ type: "string", pattern: "^\\d+$" }),
4392
+ })
4393
+ ).toMatchInlineSnapshot(`
4394
+ {
4395
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4396
+ "pattern": "^\\d+$",
4397
+ "type": "string",
4398
+ }
4399
+ `);
4400
+
4401
+ // "any" keeps the existing per-value behavior: undefined dropped, bigint coerced
4402
+ expect(z.toJSONSchema(z.literal(["a", 1n, undefined]), { unrepresentable: () => "any" })).toEqual(
4403
+ z.toJSONSchema(z.literal(["a", 1n, undefined]), { unrepresentable: "any" })
4404
+ );
4405
+ });
4406
+
4407
+ const dateToString: z.core.ToJSONSchemaParams["unrepresentable"] = ({ zodSchema }) =>
4408
+ zodSchema._zod.def.type === "date" ? { type: "string", format: "date-time" } : "throw";
4409
+
4410
+ test("emits a valid OpenAPI 3.0 schema", async () => {
4411
+ const jsonSchema = z.toJSONSchema(z.object({ start: z.date() }), {
4412
+ target: "openapi-3.0",
4413
+ unrepresentable: dateToString,
4414
+ });
4415
+ expect(jsonSchema).toMatchInlineSnapshot(`
4416
+ {
4417
+ "additionalProperties": false,
4418
+ "properties": {
4419
+ "start": {
4420
+ "format": "date-time",
4421
+ "type": "string",
4422
+ },
4423
+ },
4424
+ "required": [
4425
+ "start",
4426
+ ],
4427
+ "type": "object",
4428
+ }
4429
+ `);
4430
+ await expect(validateOpenAPI30Schema(jsonSchema)).resolves.toBe(true);
4431
+ });
4432
+
4433
+ test("the returned schema survives extraction into $defs", () => {
4434
+ const When = z.date().meta({ id: "When" });
4435
+ expect(
4436
+ z.toJSONSchema(z.object({ start: When, end: When }), { unrepresentable: dateToString })
4437
+ ).toMatchInlineSnapshot(`
4438
+ {
4439
+ "$defs": {
4440
+ "When": {
4441
+ "format": "date-time",
4442
+ "type": "string",
4443
+ },
4444
+ },
4445
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4446
+ "additionalProperties": false,
4447
+ "properties": {
4448
+ "end": {
4449
+ "$ref": "#/$defs/When",
4450
+ },
4451
+ "start": {
4452
+ "$ref": "#/$defs/When",
4453
+ },
4454
+ },
4455
+ "required": [
4456
+ "start",
4457
+ "end",
4458
+ ],
4459
+ "type": "object",
4460
+ }
4461
+ `);
4462
+ });
4463
+
4464
+ test("applies to dynamic catch values", () => {
4465
+ const schema = z.string().catch(() => {
4466
+ throw new Error("dynamic");
4467
+ });
4468
+ expect(
4469
+ z.toJSONSchema(schema, {
4470
+ unrepresentable: () => ({ default: "fallback" }),
4471
+ })
4472
+ ).toMatchInlineSnapshot(`
4473
+ {
4474
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
4475
+ "default": "fallback",
4476
+ "type": "string",
4477
+ }
4478
+ `);
4479
+ });
4480
+ });
4481
+
4482
+ describe("intersection folding", () => {
4483
+ const TARGETS = ["draft-2020-12", "draft-07", "draft-04", "openapi-3.0"] as const;
4484
+ const body = (schema: z.ZodType, params?: Parameters<typeof z.toJSONSchema>[1]) => {
4485
+ const { $schema, ...rest } = z.toJSONSchema(schema, params) as Record<string, unknown>;
4486
+ return rest;
4487
+ };
4488
+
4489
+ test("two objects become one object", () => {
4490
+ expect(body(z.object({ name: z.string() }).and(z.object({ age: z.number() })))).toMatchInlineSnapshot(`
4491
+ {
4492
+ "additionalProperties": false,
4493
+ "properties": {
4494
+ "age": {
4495
+ "type": "number",
4496
+ },
4497
+ "name": {
4498
+ "type": "string",
4499
+ },
4500
+ },
4501
+ "required": [
4502
+ "name",
4503
+ "age",
4504
+ ],
4505
+ "type": "object",
4506
+ }
4507
+ `);
4508
+ });
4509
+
4510
+ test("the folded shape is identical on every target", () => {
4511
+ const schema = z.object({ name: z.string() }).and(z.object({ age: z.number() }));
4512
+ const bodies = TARGETS.map((target) => body(schema, { target }));
4513
+ for (const emitted of bodies) expect(emitted).toEqual(bodies[0]);
4514
+ });
4515
+
4516
+ test("closed only when every member is closed", () => {
4517
+ expect(body(z.strictObject({ a: z.string() }).and(z.strictObject({ b: z.number() })))).toMatchObject({
4518
+ additionalProperties: false,
4519
+ });
4520
+ // A loose member keeps the intersection open, which is what the parser does: it merges both results, so the loose side's extra keys survive.
4521
+ expect(body(z.looseObject({ a: z.string() }).and(z.object({ b: z.number() })))).not.toHaveProperty(
4522
+ "additionalProperties"
4523
+ );
4524
+ });
4525
+
4526
+ test("chained and nested intersections fold flat", () => {
4527
+ const chained = z
4528
+ .object({ a: z.string() })
4529
+ .and(z.object({ b: z.string() }))
4530
+ .and(z.object({ c: z.string() }));
4531
+ expect(body(chained)).toMatchObject({ properties: { a: {}, b: {}, c: {} }, required: ["a", "b", "c"] });
4532
+ const nested = z.object({ a: z.string() }).and(z.object({ b: z.string() }).and(z.object({ c: z.string() })));
4533
+ expect(body(nested)).toEqual(body(chained));
4534
+ });
4535
+
4536
+ test("required is the union, and an optional key stays optional", () => {
4537
+ expect(body(z.object({ a: z.string().optional() }).and(z.object({ b: z.string() })))).toMatchObject({
4538
+ properties: { a: { type: "string" }, b: { type: "string" } },
4539
+ required: ["b"],
4540
+ });
4541
+ });
4542
+
4543
+ test("a key both members declare has to satisfy both", () => {
4544
+ // identical declarations collapse
4545
+ expect(body(z.object({ k: z.string() }).and(z.object({ k: z.string() })))).toMatchObject({
4546
+ properties: { k: { type: "string" } },
4547
+ });
4548
+ // object declarations fold, matching the parser's deep merge
4549
+ expect(
4550
+ body(z.object({ k: z.object({ a: z.string() }) }).and(z.object({ k: z.object({ b: z.string() }) })))
4551
+ ).toMatchObject({
4552
+ properties: { k: { type: "object", properties: { a: {}, b: {} }, required: ["a", "b"] } },
4553
+ });
4554
+ // anything else stays an intersection one level down
4555
+ expect(body(z.object({ k: z.string() }).and(z.object({ k: z.number() })))).toMatchObject({
4556
+ properties: { k: { allOf: [{ type: "string" }, { type: "number" }] } },
4557
+ });
4558
+ });
4559
+
4560
+ test("an intersection distributes over a union", () => {
4561
+ const schema = z
4562
+ .object({ name: z.string() })
4563
+ .and(
4564
+ z.discriminatedUnion("type", [
4565
+ z.object({ type: z.literal("a"), value: z.string() }),
4566
+ z.object({ type: z.literal("b"), count: z.number() }),
4567
+ ])
4568
+ );
4569
+ expect(body(schema)).toMatchObject({
4570
+ oneOf: [
4571
+ { type: "object", properties: { name: {}, type: {}, value: {} }, additionalProperties: false },
4572
+ { type: "object", properties: { name: {}, type: {}, count: {} }, additionalProperties: false },
4573
+ ],
4574
+ });
4575
+ const inclusive = z
4576
+ .object({ name: z.string() })
4577
+ .and(z.union([z.object({ v: z.string() }), z.object({ c: z.number() })]));
4578
+ expect(body(inclusive)).toMatchObject({
4579
+ anyOf: [{ properties: { name: {}, v: {} } }, { properties: { name: {}, c: {} } }],
4580
+ });
4581
+ });
4582
+
4583
+ test("input conversion has nothing to close", () => {
4584
+ expect(body(z.object({ a: z.string() }).and(z.object({ b: z.string() })), { io: "input" })).toEqual({
4585
+ type: "object",
4586
+ properties: { a: { type: "string" }, b: { type: "string" } },
4587
+ required: ["a", "b"],
4588
+ });
4589
+ });
4590
+
4591
+ test("folds through a property and through z.lazy", () => {
4592
+ expect(body(z.object({ inner: z.object({ a: z.string() }).and(z.object({ b: z.string() })) }))).toMatchObject({
4593
+ properties: { inner: { type: "object", properties: { a: {}, b: {} } } },
4594
+ });
4595
+ const self: z.ZodType = z.lazy(() => z.object({ a: z.string() }).and(z.object({ next: z.optional(self) })));
4596
+ expect(body(self)).toMatchObject({ type: "object", properties: { a: {}, next: { $ref: "#" } } });
4597
+ });
4598
+
4599
+ test("metadata on the intersection itself survives", () => {
4600
+ expect(
4601
+ body(
4602
+ z
4603
+ .object({ a: z.string() })
4604
+ .and(z.object({ b: z.string() }))
4605
+ .meta({ title: "T" })
4606
+ )
4607
+ ).toMatchObject({
4608
+ title: "T",
4609
+ type: "object",
4610
+ properties: { a: {}, b: {} },
4611
+ });
4612
+ });
4613
+
4614
+ test("a catchall constrains the sibling's keys too", () => {
4615
+ // The catchall member does not declare `b`, so its `additionalProperties` is what it demands of `b`. Folding `b` into `properties` has to carry that demand across, or the key would escape it.
4616
+ const schema = z
4617
+ .object({ a: z.string() })
4618
+ .catchall(z.number())
4619
+ .and(z.object({ b: z.string() }));
4620
+ expect(body(schema)).toEqual({
4621
+ type: "object",
4622
+ properties: { a: { type: "string" }, b: { allOf: [{ type: "number" }, { type: "string" }] } },
4623
+ required: ["a", "b"],
4624
+ additionalProperties: { type: "number" },
4625
+ });
4626
+ });
4627
+
4628
+ test("two catchalls both constrain the keys neither declares", () => {
4629
+ const schema = z
4630
+ .object({})
4631
+ .catchall(z.number())
4632
+ .and(z.object({}).catchall(z.number().min(0)));
4633
+ expect(body(schema)).toEqual({
4634
+ type: "object",
4635
+ properties: {},
4636
+ additionalProperties: { allOf: [{ type: "number" }, { type: "number", minimum: 0 }] },
4637
+ });
4638
+ expect(schema.safeParse({ zz: 5 }).success).toBe(true);
4639
+ expect(schema.safeParse({ zz: -1 }).success).toBe(false);
4640
+ });
4641
+
4642
+ test("the emitted openness agrees with the parser for every pair of object modes", () => {
4643
+ const modes = {
4644
+ strip: (shape: z.ZodRawShape) => z.object(shape),
4645
+ strict: (shape: z.ZodRawShape) => z.strictObject(shape),
4646
+ loose: (shape: z.ZodRawShape) => z.looseObject(shape),
4647
+ catchall: (shape: z.ZodRawShape) => z.object(shape).catchall(z.number()),
4648
+ };
4649
+ const grid: Record<string, unknown> = {};
4650
+ for (const [leftName, left] of Object.entries(modes)) {
4651
+ for (const [rightName, right] of Object.entries(modes)) {
4652
+ const schema = left({ a: z.string() }).and(right({ b: z.number() }));
4653
+ const emitted = body(schema) as { additionalProperties?: unknown };
4654
+ grid[`${leftName} & ${rightName}`] = emitted.additionalProperties ?? "open";
4655
+
4656
+ // Whatever the emitted keyword says, it has to agree with what the parser does with a key neither member declares.
4657
+ const parsed = schema.safeParse({ a: "s", b: 1, zz: 7 });
4658
+ const keepsUnknown = parsed.success && Object.prototype.hasOwnProperty.call(parsed.data, "zz");
4659
+ if (keepsUnknown) expect(emitted.additionalProperties).not.toBe(false);
4660
+ }
4661
+ }
4662
+ expect(grid).toMatchInlineSnapshot(`
4663
+ {
4664
+ "catchall & catchall": {
4665
+ "type": "number",
4666
+ },
4667
+ "catchall & loose": {
4668
+ "type": "number",
4669
+ },
4670
+ "catchall & strict": {
4671
+ "type": "number",
4672
+ },
4673
+ "catchall & strip": {
4674
+ "type": "number",
4675
+ },
4676
+ "loose & catchall": {
4677
+ "type": "number",
4678
+ },
4679
+ "loose & loose": "open",
4680
+ "loose & strict": "open",
4681
+ "loose & strip": "open",
4682
+ "strict & catchall": {
4683
+ "type": "number",
4684
+ },
4685
+ "strict & loose": "open",
4686
+ "strict & strict": false,
4687
+ "strict & strip": false,
4688
+ "strip & catchall": {
4689
+ "type": "number",
4690
+ },
4691
+ "strip & loose": "open",
4692
+ "strip & strict": false,
4693
+ "strip & strip": false,
4694
+ }
4695
+ `);
4696
+ });
4697
+
4698
+ test("a __proto__ key stays an own property", () => {
4699
+ const folded = body(z.object({ ["__proto__"]: z.string() }).and(z.object({ b: z.number() }))) as any;
4700
+ expect(Object.prototype.hasOwnProperty.call(folded.properties, "__proto__")).toBe(true);
4701
+ expect(folded.required).toEqual(["__proto__", "b"]);
4702
+ });
4703
+ });
4704
+
4705
+ describe("intersection folding declines", () => {
4706
+ // Every case here keeps the `allOf` it produces today. The fold only understands the four object keywords, so a member carrying anything else is left alone rather than having a constraint dropped or an annotation re-scoped.
4707
+ const allOf = (schema: z.ZodType, params?: Parameters<typeof z.toJSONSchema>[1]) =>
4708
+ (z.toJSONSchema(schema, params) as any).allOf;
4709
+
4710
+ test("a member that is a reference keeps its reference", () => {
4711
+ const named = z.object({ a: z.string() }).meta({ id: "Named" });
4712
+ expect(allOf(named.and(z.object({ b: z.string() })))[0]).toEqual({ $ref: "#/$defs/Named" });
4713
+
4714
+ const shared = z.object({ c: z.string() });
4715
+ const reused = z.toJSONSchema(z.object({ x: shared.and(z.object({ d: z.string() })), y: shared }), {
4716
+ reused: "ref",
4717
+ }) as any;
4718
+ expect(reused.properties.x.allOf[0]).toEqual({ $ref: "#/$defs/__schema0" });
4719
+
4720
+ const cyclic: any = z.object({
4721
+ get next() {
4722
+ return z.optional(cyclic);
4723
+ },
4724
+ n: z.string(),
4725
+ });
4726
+ expect(allOf(cyclic.and(z.object({ m: z.string() })))[0].$ref).toBe("#/$defs/__schema0");
4727
+ });
4728
+
4729
+ test("an annotated member keeps its place", () => {
4730
+ expect(
4731
+ allOf(
4732
+ z
4733
+ .object({ a: z.string() })
4734
+ .describe("A")
4735
+ .and(z.object({ b: z.string() }))
4736
+ )[0]
4737
+ ).toMatchObject({
4738
+ description: "A",
4739
+ });
4740
+ });
4741
+
4742
+ test("members that are not plain objects are left alone", () => {
4743
+ expect(allOf(z.intersection(z.string().min(2), z.string().max(5)))).toHaveLength(2);
4744
+ expect(allOf(z.object({ a: z.string() }).and(z.nullable(z.object({ b: z.string() }))))).toHaveLength(2);
4745
+ expect(
4746
+ allOf(z.object({ label: z.string() }).and(z.looseRecord(z.string().regex(/^label:[a-z]{2}$/), z.string())))
4747
+ ).toHaveLength(2);
4748
+ });
4749
+
4750
+ test("a shared member is never rewritten for its other uses", () => {
4751
+ const shared = z.object({ c: z.string() });
4752
+ const result = z.toJSONSchema(z.object({ x: shared.and(z.object({ d: z.string() })), y: shared })) as any;
4753
+ expect(result.properties.x).toMatchObject({ properties: { c: {}, d: {} }, additionalProperties: false });
4754
+ // `y` is the same Zod schema, emitted separately, and keeps the closedness it had on its own.
4755
+ expect(result.properties.y).toEqual({
4756
+ type: "object",
4757
+ properties: { c: { type: "string" } },
4758
+ required: ["c"],
4759
+ additionalProperties: false,
4760
+ });
4761
+ });
4762
+
4763
+ test("registry conversion keeps its cross-references", () => {
4764
+ const registry = z.registry<{ id: string }>();
4765
+ const left = z.object({ p: z.string() });
4766
+ const right = z.object({ q: z.string() });
4767
+ registry.add(left, { id: "P" });
4768
+ registry.add(right, { id: "Q" });
4769
+ registry.add(left.and(right), { id: "PQ" });
4770
+ expect((z.toJSONSchema(registry) as any).schemas.PQ.allOf).toEqual([{ $ref: "P" }, { $ref: "Q" }]);
4771
+ });
4772
+
4773
+ test("override still runs, and sees the intersection before it folds", () => {
4774
+ const schema = z.object({ a: z.string() }).and(z.object({ b: z.string() }));
4775
+ const result = z.toJSONSchema(schema, {
4776
+ override(ctx) {
4777
+ if ((ctx.jsonSchema as any).allOf) (ctx.jsonSchema as any).title = "intersection";
4778
+ },
4779
+ }) as any;
4780
+ expect(result).toMatchObject({ title: "intersection", type: "object", properties: { a: {}, b: {} } });
4781
+ });
4782
+
4783
+ test("an override that writes object keywords wins over the fold", () => {
4784
+ // The override runs first, so anything it puts on the intersection is deliberate. Folding would overwrite it silently, so the fold stands down instead.
4785
+ const result = z.toJSONSchema(z.object({ a: z.string() }).and(z.object({ b: z.string() })), {
4786
+ override(ctx) {
4787
+ if ((ctx.jsonSchema as any).allOf) (ctx.jsonSchema as any).additionalProperties = true;
4788
+ },
4789
+ }) as any;
4790
+ expect(result.additionalProperties).toBe(true);
4791
+ expect(result.allOf).toHaveLength(2);
4792
+ });
4793
+
4794
+ test("more than one union member is left alone rather than multiplied out", () => {
4795
+ // The second union lands among the members the first is distributed across, and a union is not an object, so every branch fails to fold and the whole intersection stands down.
4796
+ const schema = z
4797
+ .object({ a: z.string() })
4798
+ .and(z.union([z.object({ v: z.string() }), z.object({ w: z.string() })]))
4799
+ .and(z.union([z.object({ x: z.string() }), z.object({ y: z.string() })]));
4800
+ expect(allOf(schema)).toHaveLength(3);
4801
+ });
4802
+ });
4803
+
4804
+ test("currency code", () => {
4805
+ expect(z.toJSONSchema(z.currencyCode())).toEqual({
4806
+ $schema: "https://json-schema.org/draft/2020-12/schema",
4807
+ type: "string",
4808
+ format: "currency_code",
4809
+ pattern: z.regexes.currencyCode.source,
4810
+ });
4811
+ });