zod-compiler 0.1.0 → 1.0.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 (451) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +381 -28
  3. package/dist/cli/commands/check.d.ts +9 -0
  4. package/dist/cli/commands/check.d.ts.map +1 -0
  5. package/dist/cli/commands/check.js +150 -0
  6. package/dist/cli/commands/check.js.map +1 -0
  7. package/dist/cli/commands/generate.d.ts +34 -0
  8. package/dist/cli/commands/generate.d.ts.map +1 -0
  9. package/dist/cli/commands/generate.js +135 -0
  10. package/dist/cli/commands/generate.js.map +1 -0
  11. package/dist/cli/commands/watch.d.ts +33 -0
  12. package/dist/cli/commands/watch.d.ts.map +1 -0
  13. package/dist/cli/commands/watch.js +163 -0
  14. package/dist/cli/commands/watch.js.map +1 -0
  15. package/dist/cli/emitter.d.ts +21 -0
  16. package/dist/cli/emitter.d.ts.map +1 -0
  17. package/dist/cli/emitter.js +64 -0
  18. package/dist/cli/emitter.js.map +1 -0
  19. package/dist/cli/errors.d.ts +2 -0
  20. package/dist/cli/errors.d.ts.map +1 -0
  21. package/dist/cli/errors.js +4 -0
  22. package/dist/cli/errors.js.map +1 -0
  23. package/dist/cli/index.d.ts +3 -0
  24. package/dist/cli/index.d.ts.map +1 -0
  25. package/dist/cli/index.js +181 -0
  26. package/dist/cli/index.js.map +1 -0
  27. package/dist/cli/logger.d.ts +18 -0
  28. package/dist/cli/logger.d.ts.map +1 -0
  29. package/dist/cli/logger.js +46 -0
  30. package/dist/cli/logger.js.map +1 -0
  31. package/dist/core/codegen/context.d.ts +138 -0
  32. package/dist/core/codegen/context.d.ts.map +1 -0
  33. package/dist/core/codegen/context.js +213 -0
  34. package/dist/core/codegen/context.js.map +1 -0
  35. package/dist/core/codegen/emit-issue.d.ts +74 -0
  36. package/dist/core/codegen/emit-issue.d.ts.map +1 -0
  37. package/dist/core/codegen/emit-issue.js +116 -0
  38. package/dist/core/codegen/emit-issue.js.map +1 -0
  39. package/dist/core/codegen/emit.d.ts +8 -0
  40. package/dist/core/codegen/emit.d.ts.map +1 -0
  41. package/dist/core/codegen/emit.js +16 -0
  42. package/dist/core/codegen/emit.js.map +1 -0
  43. package/dist/core/codegen/fast-path.d.ts +17 -0
  44. package/dist/core/codegen/fast-path.d.ts.map +1 -0
  45. package/dist/core/codegen/fast-path.js +114 -0
  46. package/dist/core/codegen/fast-path.js.map +1 -0
  47. package/dist/core/codegen/index.d.ts +19 -0
  48. package/dist/core/codegen/index.d.ts.map +1 -0
  49. package/dist/core/codegen/index.js +54 -0
  50. package/dist/core/codegen/index.js.map +1 -0
  51. package/dist/core/codegen/issue-decls.d.ts +37 -0
  52. package/dist/core/codegen/issue-decls.d.ts.map +1 -0
  53. package/dist/core/codegen/issue-decls.js +47 -0
  54. package/dist/core/codegen/issue-decls.js.map +1 -0
  55. package/dist/core/codegen/schemas/any.d.ts +5 -0
  56. package/dist/core/codegen/schemas/any.d.ts.map +1 -0
  57. package/dist/core/codegen/schemas/any.js +7 -0
  58. package/dist/core/codegen/schemas/any.js.map +1 -0
  59. package/dist/core/codegen/schemas/array.d.ts +7 -0
  60. package/dist/core/codegen/schemas/array.d.ts.map +1 -0
  61. package/dist/core/codegen/schemas/array.js +86 -0
  62. package/dist/core/codegen/schemas/array.js.map +1 -0
  63. package/dist/core/codegen/schemas/bigint.d.ts +5 -0
  64. package/dist/core/codegen/schemas/bigint.d.ts.map +1 -0
  65. package/dist/core/codegen/schemas/bigint.js +80 -0
  66. package/dist/core/codegen/schemas/bigint.js.map +1 -0
  67. package/dist/core/codegen/schemas/boolean.d.ts +5 -0
  68. package/dist/core/codegen/schemas/boolean.d.ts.map +1 -0
  69. package/dist/core/codegen/schemas/boolean.js +20 -0
  70. package/dist/core/codegen/schemas/boolean.js.map +1 -0
  71. package/dist/core/codegen/schemas/catch.d.ts +7 -0
  72. package/dist/core/codegen/schemas/catch.d.ts.map +1 -0
  73. package/dist/core/codegen/schemas/catch.js +26 -0
  74. package/dist/core/codegen/schemas/catch.js.map +1 -0
  75. package/dist/core/codegen/schemas/date.d.ts +5 -0
  76. package/dist/core/codegen/schemas/date.d.ts.map +1 -0
  77. package/dist/core/codegen/schemas/date.js +79 -0
  78. package/dist/core/codegen/schemas/date.js.map +1 -0
  79. package/dist/core/codegen/schemas/default.d.ts +5 -0
  80. package/dist/core/codegen/schemas/default.d.ts.map +1 -0
  81. package/dist/core/codegen/schemas/default.js +17 -0
  82. package/dist/core/codegen/schemas/default.js.map +1 -0
  83. package/dist/core/codegen/schemas/discriminated-union.d.ts +7 -0
  84. package/dist/core/codegen/schemas/discriminated-union.d.ts.map +1 -0
  85. package/dist/core/codegen/schemas/discriminated-union.js +45 -0
  86. package/dist/core/codegen/schemas/discriminated-union.js.map +1 -0
  87. package/dist/core/codegen/schemas/effect.d.ts +17 -0
  88. package/dist/core/codegen/schemas/effect.d.ts.map +1 -0
  89. package/dist/core/codegen/schemas/effect.js +35 -0
  90. package/dist/core/codegen/schemas/effect.js.map +1 -0
  91. package/dist/core/codegen/schemas/enum.d.ts +5 -0
  92. package/dist/core/codegen/schemas/enum.d.ts.map +1 -0
  93. package/dist/core/codegen/schemas/enum.js +33 -0
  94. package/dist/core/codegen/schemas/enum.js.map +1 -0
  95. package/dist/core/codegen/schemas/fallback.d.ts +4 -0
  96. package/dist/core/codegen/schemas/fallback.d.ts.map +1 -0
  97. package/dist/core/codegen/schemas/fallback.js +23 -0
  98. package/dist/core/codegen/schemas/fallback.js.map +1 -0
  99. package/dist/core/codegen/schemas/file.d.ts +5 -0
  100. package/dist/core/codegen/schemas/file.d.ts.map +1 -0
  101. package/dist/core/codegen/schemas/file.js +78 -0
  102. package/dist/core/codegen/schemas/file.js.map +1 -0
  103. package/dist/core/codegen/schemas/intersection.d.ts +7 -0
  104. package/dist/core/codegen/schemas/intersection.d.ts.map +1 -0
  105. package/dist/core/codegen/schemas/intersection.js +13 -0
  106. package/dist/core/codegen/schemas/intersection.js.map +1 -0
  107. package/dist/core/codegen/schemas/literal.d.ts +5 -0
  108. package/dist/core/codegen/schemas/literal.d.ts.map +1 -0
  109. package/dist/core/codegen/schemas/literal.js +31 -0
  110. package/dist/core/codegen/schemas/literal.js.map +1 -0
  111. package/dist/core/codegen/schemas/map.d.ts +7 -0
  112. package/dist/core/codegen/schemas/map.d.ts.map +1 -0
  113. package/dist/core/codegen/schemas/map.js +46 -0
  114. package/dist/core/codegen/schemas/map.js.map +1 -0
  115. package/dist/core/codegen/schemas/nan.d.ts +5 -0
  116. package/dist/core/codegen/schemas/nan.d.ts.map +1 -0
  117. package/dist/core/codegen/schemas/nan.js +13 -0
  118. package/dist/core/codegen/schemas/nan.js.map +1 -0
  119. package/dist/core/codegen/schemas/never.d.ts +5 -0
  120. package/dist/core/codegen/schemas/never.d.ts.map +1 -0
  121. package/dist/core/codegen/schemas/never.js +11 -0
  122. package/dist/core/codegen/schemas/never.js.map +1 -0
  123. package/dist/core/codegen/schemas/null.d.ts +5 -0
  124. package/dist/core/codegen/schemas/null.d.ts.map +1 -0
  125. package/dist/core/codegen/schemas/null.js +13 -0
  126. package/dist/core/codegen/schemas/null.js.map +1 -0
  127. package/dist/core/codegen/schemas/nullable.d.ts +7 -0
  128. package/dist/core/codegen/schemas/nullable.d.ts.map +1 -0
  129. package/dist/core/codegen/schemas/nullable.js +15 -0
  130. package/dist/core/codegen/schemas/nullable.js.map +1 -0
  131. package/dist/core/codegen/schemas/number.d.ts +5 -0
  132. package/dist/core/codegen/schemas/number.d.ts.map +1 -0
  133. package/dist/core/codegen/schemas/number.js +186 -0
  134. package/dist/core/codegen/schemas/number.js.map +1 -0
  135. package/dist/core/codegen/schemas/object.d.ts +7 -0
  136. package/dist/core/codegen/schemas/object.d.ts.map +1 -0
  137. package/dist/core/codegen/schemas/object.js +65 -0
  138. package/dist/core/codegen/schemas/object.js.map +1 -0
  139. package/dist/core/codegen/schemas/optional.d.ts +7 -0
  140. package/dist/core/codegen/schemas/optional.d.ts.map +1 -0
  141. package/dist/core/codegen/schemas/optional.js +15 -0
  142. package/dist/core/codegen/schemas/optional.js.map +1 -0
  143. package/dist/core/codegen/schemas/pipe.d.ts +7 -0
  144. package/dist/core/codegen/schemas/pipe.d.ts.map +1 -0
  145. package/dist/core/codegen/schemas/pipe.js +18 -0
  146. package/dist/core/codegen/schemas/pipe.js.map +1 -0
  147. package/dist/core/codegen/schemas/readonly.d.ts +7 -0
  148. package/dist/core/codegen/schemas/readonly.d.ts.map +1 -0
  149. package/dist/core/codegen/schemas/readonly.js +7 -0
  150. package/dist/core/codegen/schemas/readonly.js.map +1 -0
  151. package/dist/core/codegen/schemas/record.d.ts +7 -0
  152. package/dist/core/codegen/schemas/record.d.ts.map +1 -0
  153. package/dist/core/codegen/schemas/record.js +53 -0
  154. package/dist/core/codegen/schemas/record.js.map +1 -0
  155. package/dist/core/codegen/schemas/recursive-ref.d.ts +5 -0
  156. package/dist/core/codegen/schemas/recursive-ref.d.ts.map +1 -0
  157. package/dist/core/codegen/schemas/recursive-ref.js +24 -0
  158. package/dist/core/codegen/schemas/recursive-ref.js.map +1 -0
  159. package/dist/core/codegen/schemas/set.d.ts +7 -0
  160. package/dist/core/codegen/schemas/set.d.ts.map +1 -0
  161. package/dist/core/codegen/schemas/set.js +88 -0
  162. package/dist/core/codegen/schemas/set.js.map +1 -0
  163. package/dist/core/codegen/schemas/string-bool.d.ts +4 -0
  164. package/dist/core/codegen/schemas/string-bool.d.ts.map +1 -0
  165. package/dist/core/codegen/schemas/string-bool.js +39 -0
  166. package/dist/core/codegen/schemas/string-bool.js.map +1 -0
  167. package/dist/core/codegen/schemas/string.d.ts +5 -0
  168. package/dist/core/codegen/schemas/string.d.ts.map +1 -0
  169. package/dist/core/codegen/schemas/string.js +226 -0
  170. package/dist/core/codegen/schemas/string.js.map +1 -0
  171. package/dist/core/codegen/schemas/symbol.d.ts +5 -0
  172. package/dist/core/codegen/schemas/symbol.d.ts.map +1 -0
  173. package/dist/core/codegen/schemas/symbol.js +13 -0
  174. package/dist/core/codegen/schemas/symbol.js.map +1 -0
  175. package/dist/core/codegen/schemas/template-literal.d.ts +5 -0
  176. package/dist/core/codegen/schemas/template-literal.d.ts.map +1 -0
  177. package/dist/core/codegen/schemas/template-literal.js +16 -0
  178. package/dist/core/codegen/schemas/template-literal.js.map +1 -0
  179. package/dist/core/codegen/schemas/tuple.d.ts +15 -0
  180. package/dist/core/codegen/schemas/tuple.d.ts.map +1 -0
  181. package/dist/core/codegen/schemas/tuple.js +111 -0
  182. package/dist/core/codegen/schemas/tuple.js.map +1 -0
  183. package/dist/core/codegen/schemas/undefined.d.ts +5 -0
  184. package/dist/core/codegen/schemas/undefined.d.ts.map +1 -0
  185. package/dist/core/codegen/schemas/undefined.js +13 -0
  186. package/dist/core/codegen/schemas/undefined.js.map +1 -0
  187. package/dist/core/codegen/schemas/union.d.ts +7 -0
  188. package/dist/core/codegen/schemas/union.d.ts.map +1 -0
  189. package/dist/core/codegen/schemas/union.js +95 -0
  190. package/dist/core/codegen/schemas/union.js.map +1 -0
  191. package/dist/core/codegen/schemas/unknown.d.ts +5 -0
  192. package/dist/core/codegen/schemas/unknown.d.ts.map +1 -0
  193. package/dist/core/codegen/schemas/unknown.js +7 -0
  194. package/dist/core/codegen/schemas/unknown.js.map +1 -0
  195. package/dist/core/codegen/schemas/void.d.ts +5 -0
  196. package/dist/core/codegen/schemas/void.d.ts.map +1 -0
  197. package/dist/core/codegen/schemas/void.js +13 -0
  198. package/dist/core/codegen/schemas/void.js.map +1 -0
  199. package/dist/core/codegen/slow-path.d.ts +5 -0
  200. package/dist/core/codegen/slow-path.d.ts.map +1 -0
  201. package/dist/core/codegen/slow-path.js +113 -0
  202. package/dist/core/codegen/slow-path.js.map +1 -0
  203. package/dist/core/codegen/well-known-regex.d.ts +27 -0
  204. package/dist/core/codegen/well-known-regex.d.ts.map +1 -0
  205. package/dist/core/codegen/well-known-regex.js +51 -0
  206. package/dist/core/codegen/well-known-regex.js.map +1 -0
  207. package/dist/core/compile.d.ts +19 -0
  208. package/dist/core/compile.d.ts.map +1 -0
  209. package/dist/core/compile.js +37 -0
  210. package/dist/core/compile.js.map +1 -0
  211. package/dist/core/diagnostic.d.ts +46 -0
  212. package/dist/core/diagnostic.d.ts.map +1 -0
  213. package/dist/core/diagnostic.js +174 -0
  214. package/dist/core/diagnostic.js.map +1 -0
  215. package/dist/core/extract/checks.d.ts +35 -0
  216. package/dist/core/extract/checks.d.ts.map +1 -0
  217. package/dist/core/extract/checks.js +249 -0
  218. package/dist/core/extract/checks.js.map +1 -0
  219. package/dist/core/extract/effects.d.ts +12 -0
  220. package/dist/core/extract/effects.d.ts.map +1 -0
  221. package/dist/core/extract/effects.js +204 -0
  222. package/dist/core/extract/effects.js.map +1 -0
  223. package/dist/core/extract/extractors/any.d.ts +3 -0
  224. package/dist/core/extract/extractors/any.d.ts.map +1 -0
  225. package/dist/core/extract/extractors/any.js +2 -0
  226. package/dist/core/extract/extractors/any.js.map +1 -0
  227. package/dist/core/extract/extractors/array.d.ts +4 -0
  228. package/dist/core/extract/extractors/array.d.ts.map +1 -0
  229. package/dist/core/extract/extractors/array.js +19 -0
  230. package/dist/core/extract/extractors/array.js.map +1 -0
  231. package/dist/core/extract/extractors/bigint.d.ts +4 -0
  232. package/dist/core/extract/extractors/bigint.d.ts.map +1 -0
  233. package/dist/core/extract/extractors/bigint.js +70 -0
  234. package/dist/core/extract/extractors/bigint.js.map +1 -0
  235. package/dist/core/extract/extractors/boolean.d.ts +3 -0
  236. package/dist/core/extract/extractors/boolean.d.ts.map +1 -0
  237. package/dist/core/extract/extractors/boolean.js +5 -0
  238. package/dist/core/extract/extractors/boolean.js.map +1 -0
  239. package/dist/core/extract/extractors/catch.d.ts +11 -0
  240. package/dist/core/extract/extractors/catch.d.ts.map +1 -0
  241. package/dist/core/extract/extractors/catch.js +20 -0
  242. package/dist/core/extract/extractors/catch.js.map +1 -0
  243. package/dist/core/extract/extractors/date.d.ts +4 -0
  244. package/dist/core/extract/extractors/date.d.ts.map +1 -0
  245. package/dist/core/extract/extractors/date.js +49 -0
  246. package/dist/core/extract/extractors/date.js.map +1 -0
  247. package/dist/core/extract/extractors/default.d.ts +4 -0
  248. package/dist/core/extract/extractors/default.d.ts.map +1 -0
  249. package/dist/core/extract/extractors/default.js +11 -0
  250. package/dist/core/extract/extractors/default.js.map +1 -0
  251. package/dist/core/extract/extractors/enum.d.ts +3 -0
  252. package/dist/core/extract/extractors/enum.d.ts.map +1 -0
  253. package/dist/core/extract/extractors/enum.js +15 -0
  254. package/dist/core/extract/extractors/enum.js.map +1 -0
  255. package/dist/core/extract/extractors/file.d.ts +4 -0
  256. package/dist/core/extract/extractors/file.d.ts.map +1 -0
  257. package/dist/core/extract/extractors/file.js +24 -0
  258. package/dist/core/extract/extractors/file.js.map +1 -0
  259. package/dist/core/extract/extractors/index.d.ts +32 -0
  260. package/dist/core/extract/extractors/index.d.ts.map +1 -0
  261. package/dist/core/extract/extractors/index.js +32 -0
  262. package/dist/core/extract/extractors/index.js.map +1 -0
  263. package/dist/core/extract/extractors/intersection.d.ts +3 -0
  264. package/dist/core/extract/extractors/intersection.d.ts.map +1 -0
  265. package/dist/core/extract/extractors/intersection.js +14 -0
  266. package/dist/core/extract/extractors/intersection.js.map +1 -0
  267. package/dist/core/extract/extractors/lazy.d.ts +4 -0
  268. package/dist/core/extract/extractors/lazy.d.ts.map +1 -0
  269. package/dist/core/extract/extractors/lazy.js +23 -0
  270. package/dist/core/extract/extractors/lazy.js.map +1 -0
  271. package/dist/core/extract/extractors/literal.d.ts +3 -0
  272. package/dist/core/extract/extractors/literal.d.ts.map +1 -0
  273. package/dist/core/extract/extractors/literal.js +2 -0
  274. package/dist/core/extract/extractors/literal.js.map +1 -0
  275. package/dist/core/extract/extractors/map.d.ts +4 -0
  276. package/dist/core/extract/extractors/map.d.ts.map +1 -0
  277. package/dist/core/extract/extractors/map.js +6 -0
  278. package/dist/core/extract/extractors/map.js.map +1 -0
  279. package/dist/core/extract/extractors/nan.d.ts +3 -0
  280. package/dist/core/extract/extractors/nan.d.ts.map +1 -0
  281. package/dist/core/extract/extractors/nan.js +2 -0
  282. package/dist/core/extract/extractors/nan.js.map +1 -0
  283. package/dist/core/extract/extractors/never.d.ts +3 -0
  284. package/dist/core/extract/extractors/never.d.ts.map +1 -0
  285. package/dist/core/extract/extractors/never.js +2 -0
  286. package/dist/core/extract/extractors/never.js.map +1 -0
  287. package/dist/core/extract/extractors/null.d.ts +3 -0
  288. package/dist/core/extract/extractors/null.d.ts.map +1 -0
  289. package/dist/core/extract/extractors/null.js +2 -0
  290. package/dist/core/extract/extractors/null.js.map +1 -0
  291. package/dist/core/extract/extractors/nullable.d.ts +3 -0
  292. package/dist/core/extract/extractors/nullable.d.ts.map +1 -0
  293. package/dist/core/extract/extractors/nullable.js +5 -0
  294. package/dist/core/extract/extractors/nullable.js.map +1 -0
  295. package/dist/core/extract/extractors/number.d.ts +4 -0
  296. package/dist/core/extract/extractors/number.d.ts.map +1 -0
  297. package/dist/core/extract/extractors/number.js +38 -0
  298. package/dist/core/extract/extractors/number.js.map +1 -0
  299. package/dist/core/extract/extractors/object.d.ts +4 -0
  300. package/dist/core/extract/extractors/object.d.ts.map +1 -0
  301. package/dist/core/extract/extractors/object.js +43 -0
  302. package/dist/core/extract/extractors/object.js.map +1 -0
  303. package/dist/core/extract/extractors/optional.d.ts +3 -0
  304. package/dist/core/extract/extractors/optional.d.ts.map +1 -0
  305. package/dist/core/extract/extractors/optional.js +14 -0
  306. package/dist/core/extract/extractors/optional.js.map +1 -0
  307. package/dist/core/extract/extractors/pipe.d.ts +4 -0
  308. package/dist/core/extract/extractors/pipe.d.ts.map +1 -0
  309. package/dist/core/extract/extractors/pipe.js +31 -0
  310. package/dist/core/extract/extractors/pipe.js.map +1 -0
  311. package/dist/core/extract/extractors/readonly.d.ts +9 -0
  312. package/dist/core/extract/extractors/readonly.d.ts.map +1 -0
  313. package/dist/core/extract/extractors/readonly.js +8 -0
  314. package/dist/core/extract/extractors/readonly.js.map +1 -0
  315. package/dist/core/extract/extractors/record.d.ts +4 -0
  316. package/dist/core/extract/extractors/record.d.ts.map +1 -0
  317. package/dist/core/extract/extractors/record.js +39 -0
  318. package/dist/core/extract/extractors/record.js.map +1 -0
  319. package/dist/core/extract/extractors/set.d.ts +4 -0
  320. package/dist/core/extract/extractors/set.d.ts.map +1 -0
  321. package/dist/core/extract/extractors/set.js +33 -0
  322. package/dist/core/extract/extractors/set.js.map +1 -0
  323. package/dist/core/extract/extractors/string-bool.d.ts +10 -0
  324. package/dist/core/extract/extractors/string-bool.d.ts.map +1 -0
  325. package/dist/core/extract/extractors/string-bool.js +92 -0
  326. package/dist/core/extract/extractors/string-bool.js.map +1 -0
  327. package/dist/core/extract/extractors/string.d.ts +4 -0
  328. package/dist/core/extract/extractors/string.d.ts.map +1 -0
  329. package/dist/core/extract/extractors/string.js +41 -0
  330. package/dist/core/extract/extractors/string.js.map +1 -0
  331. package/dist/core/extract/extractors/symbol.d.ts +3 -0
  332. package/dist/core/extract/extractors/symbol.d.ts.map +1 -0
  333. package/dist/core/extract/extractors/symbol.js +2 -0
  334. package/dist/core/extract/extractors/symbol.js.map +1 -0
  335. package/dist/core/extract/extractors/template-literal.d.ts +4 -0
  336. package/dist/core/extract/extractors/template-literal.d.ts.map +1 -0
  337. package/dist/core/extract/extractors/template-literal.js +8 -0
  338. package/dist/core/extract/extractors/template-literal.js.map +1 -0
  339. package/dist/core/extract/extractors/tuple.d.ts +4 -0
  340. package/dist/core/extract/extractors/tuple.d.ts.map +1 -0
  341. package/dist/core/extract/extractors/tuple.js +6 -0
  342. package/dist/core/extract/extractors/tuple.js.map +1 -0
  343. package/dist/core/extract/extractors/undefined.d.ts +3 -0
  344. package/dist/core/extract/extractors/undefined.d.ts.map +1 -0
  345. package/dist/core/extract/extractors/undefined.js +2 -0
  346. package/dist/core/extract/extractors/undefined.js.map +1 -0
  347. package/dist/core/extract/extractors/union.d.ts +4 -0
  348. package/dist/core/extract/extractors/union.d.ts.map +1 -0
  349. package/dist/core/extract/extractors/union.js +44 -0
  350. package/dist/core/extract/extractors/union.js.map +1 -0
  351. package/dist/core/extract/extractors/unknown.d.ts +3 -0
  352. package/dist/core/extract/extractors/unknown.d.ts.map +1 -0
  353. package/dist/core/extract/extractors/unknown.js +2 -0
  354. package/dist/core/extract/extractors/unknown.js.map +1 -0
  355. package/dist/core/extract/extractors/void.d.ts +3 -0
  356. package/dist/core/extract/extractors/void.d.ts.map +1 -0
  357. package/dist/core/extract/extractors/void.js +2 -0
  358. package/dist/core/extract/extractors/void.js.map +1 -0
  359. package/dist/core/extract/fallback.d.ts +4 -0
  360. package/dist/core/extract/fallback.d.ts.map +1 -0
  361. package/dist/core/extract/fallback.js +9 -0
  362. package/dist/core/extract/fallback.js.map +1 -0
  363. package/dist/core/extract/index.d.ts +11 -0
  364. package/dist/core/extract/index.d.ts.map +1 -0
  365. package/dist/core/extract/index.js +11 -0
  366. package/dist/core/extract/index.js.map +1 -0
  367. package/dist/core/extract/registry.d.ts +59 -0
  368. package/dist/core/extract/registry.d.ts.map +1 -0
  369. package/dist/core/extract/registry.js +152 -0
  370. package/dist/core/extract/registry.js.map +1 -0
  371. package/dist/core/extract/types.d.ts +114 -0
  372. package/dist/core/extract/types.d.ts.map +1 -0
  373. package/dist/core/extract/types.js +2 -0
  374. package/dist/core/extract/types.js.map +1 -0
  375. package/dist/core/iife.d.ts +38 -0
  376. package/dist/core/iife.d.ts.map +1 -0
  377. package/dist/core/iife.js +59 -0
  378. package/dist/core/iife.js.map +1 -0
  379. package/dist/core/pipeline.d.ts +26 -0
  380. package/dist/core/pipeline.d.ts.map +1 -0
  381. package/dist/core/pipeline.js +42 -0
  382. package/dist/core/pipeline.js.map +1 -0
  383. package/dist/core/types.d.ts +352 -0
  384. package/dist/core/types.d.ts.map +1 -0
  385. package/dist/core/types.js +6 -0
  386. package/dist/core/types.js.map +1 -0
  387. package/dist/discovery.d.ts +15 -0
  388. package/dist/discovery.d.ts.map +1 -0
  389. package/dist/discovery.js +40 -0
  390. package/dist/discovery.js.map +1 -0
  391. package/dist/index.d.ts +4 -670
  392. package/dist/index.d.ts.map +1 -0
  393. package/dist/index.js +2 -2550
  394. package/dist/index.js.map +1 -0
  395. package/dist/loader.d.ts +12 -0
  396. package/dist/loader.d.ts.map +1 -0
  397. package/dist/loader.js +76 -0
  398. package/dist/loader.js.map +1 -0
  399. package/dist/unplugin/bun.d.ts +3 -0
  400. package/dist/unplugin/bun.d.ts.map +1 -0
  401. package/dist/unplugin/bun.js +3 -0
  402. package/dist/unplugin/bun.js.map +1 -0
  403. package/dist/unplugin/esbuild.d.ts +3 -0
  404. package/dist/unplugin/esbuild.d.ts.map +1 -0
  405. package/dist/unplugin/esbuild.js +3 -0
  406. package/dist/unplugin/esbuild.js.map +1 -0
  407. package/dist/unplugin/farm.d.ts +3 -0
  408. package/dist/unplugin/farm.d.ts.map +1 -0
  409. package/dist/unplugin/farm.js +3 -0
  410. package/dist/unplugin/farm.js.map +1 -0
  411. package/dist/unplugin/index.d.ts +4 -0
  412. package/dist/unplugin/index.d.ts.map +1 -0
  413. package/dist/unplugin/index.js +75 -0
  414. package/dist/unplugin/index.js.map +1 -0
  415. package/dist/unplugin/rolldown.d.ts +3 -0
  416. package/dist/unplugin/rolldown.d.ts.map +1 -0
  417. package/dist/unplugin/rolldown.js +3 -0
  418. package/dist/unplugin/rolldown.js.map +1 -0
  419. package/dist/unplugin/rollup.d.ts +3 -0
  420. package/dist/unplugin/rollup.d.ts.map +1 -0
  421. package/dist/unplugin/rollup.js +3 -0
  422. package/dist/unplugin/rollup.js.map +1 -0
  423. package/dist/unplugin/rspack.d.ts +3 -0
  424. package/dist/unplugin/rspack.d.ts.map +1 -0
  425. package/dist/unplugin/rspack.js +3 -0
  426. package/dist/unplugin/rspack.js.map +1 -0
  427. package/dist/unplugin/transform.d.ts +36 -0
  428. package/dist/unplugin/transform.d.ts.map +1 -0
  429. package/dist/unplugin/transform.js +285 -0
  430. package/dist/unplugin/transform.js.map +1 -0
  431. package/dist/unplugin/types.d.ts +54 -0
  432. package/dist/unplugin/types.d.ts.map +1 -0
  433. package/dist/unplugin/types.js +19 -0
  434. package/dist/unplugin/types.js.map +1 -0
  435. package/dist/unplugin/virtual.d.ts +24 -0
  436. package/dist/unplugin/virtual.d.ts.map +1 -0
  437. package/dist/unplugin/virtual.js +62 -0
  438. package/dist/unplugin/virtual.js.map +1 -0
  439. package/dist/unplugin/vite.d.ts +3 -0
  440. package/dist/unplugin/vite.d.ts.map +1 -0
  441. package/dist/unplugin/vite.js +3 -0
  442. package/dist/unplugin/vite.js.map +1 -0
  443. package/dist/unplugin/webpack.d.ts +3 -0
  444. package/dist/unplugin/webpack.d.ts.map +1 -0
  445. package/dist/unplugin/webpack.js +3 -0
  446. package/dist/unplugin/webpack.js.map +1 -0
  447. package/package.json +110 -51
  448. package/dist/index.mjs +0 -2536
  449. package/dist/standalone.d.ts +0 -294
  450. package/dist/standalone.js +0 -523
  451. package/dist/standalone.mjs +0 -514
package/dist/index.js CHANGED
@@ -1,2550 +1,2 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
-
3
- var ts = require('typescript');
4
- var standalone = require('./standalone.js');
5
- var _apply_decs_2203_r = require('@swc/helpers/_/_apply_decs_2203_r');
6
- var z = require('zod');
7
- var _identity = require('@swc/helpers/_/_identity');
8
-
9
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
-
11
- var ts__default = /*#__PURE__*/_interopDefault(ts);
12
- var standalone__default = /*#__PURE__*/_interopDefault(standalone);
13
- var z__default = /*#__PURE__*/_interopDefault(z);
14
-
15
- function uniqueIdentifier(name) {
16
- return ts.factory.createUniqueName(name, ts__default.default.GeneratedIdentifierFlags.ReservedInNestedScopes);
17
- }
18
- function local(ident, initializer, modifiable = false) {
19
- return ts.factory.createVariableStatement([], ts.factory.createVariableDeclarationList([
20
- ts.factory.createVariableDeclaration(ident, undefined, undefined, initializer)
21
- ], !modifiable ? ts.NodeFlags.Const : ts.NodeFlags.Let));
22
- }
23
- function objectLiteral(obj) {
24
- const entries = Object.entries(obj).filter(([k, v])=>!!v);
25
- return ts.factory.createObjectLiteralExpression(entries.map(([name, expr])=>ts.factory.createPropertyAssignment(ts.factory.createStringLiteral(name), expr)), entries.length > 1);
26
- }
27
- const IDENT_REGEX = /^[$A-Z_a-z][\w$]*$/;
28
- function identifierOrStringLiteral(value) {
29
- if (IDENT_REGEX.test(value)) {
30
- return ts.factory.createIdentifier(value);
31
- } else {
32
- return ts.factory.createStringLiteral(value);
33
- }
34
- }
35
- function propertyChain(start, elements) {
36
- for (let element of elements){
37
- if (typeof element === 'string') {
38
- if (IDENT_REGEX.test(element)) {
39
- start = ts.factory.createPropertyAccessExpression(start, element);
40
- continue;
41
- }
42
- element = ts.factory.createStringLiteral(element);
43
- }
44
- start = ts.factory.createElementAccessExpression(start, element);
45
- }
46
- return start;
47
- }
48
- function print(node, hint = ts.EmitHint.Unspecified) {
49
- const file = ts__default.default.createSourceFile('print.ts', '', ts.ScriptTarget.ES2022, false, ts.ScriptKind.TS);
50
- const printer = ts__default.default.createPrinter();
51
- return printer.printNode(hint, node, file);
52
- }
53
- function callHelper(verifierContext, helper, ...args) {
54
- return ts.factory.createCallExpression(propertyChain(verifierContext, [
55
- 'helpers',
56
- helper
57
- ]), undefined, args);
58
- }
59
- class Path {
60
- constructor(parts){
61
- this.parts = parts;
62
- }
63
- static empty() {
64
- return new Path([]);
65
- }
66
- get isEmpty() {
67
- return this.parts.length === 0;
68
- }
69
- clone() {
70
- return new Path([
71
- ...this.parts
72
- ]);
73
- }
74
- push(fragment) {
75
- const parts = [
76
- ...this.parts
77
- ];
78
- if (typeof fragment === 'string') {
79
- parts.push(ts.factory.createStringLiteral(fragment));
80
- } else if (typeof fragment === 'number') {
81
- parts.push(ts.factory.createNumericLiteral(fragment, ts.TokenFlags.None));
82
- } else {
83
- parts.push(fragment);
84
- }
85
- return new Path(parts);
86
- }
87
- serialize() {
88
- return ts.factory.createArrayLiteralExpression(this.parts, false);
89
- }
90
- }
91
-
92
- /**
93
- * In JavaScript, there exist two main types of values - primitives like strings, numbers, and booleans; and objects like
94
- * arrays, `Map`s, `Set`s, and, well, *objects*. Two primitives with equal value are equal to each other: `42 === 42` and
95
- * `"foo" === "foo"`, however ***two objects will never be equal to each other***: `{} !== {}`, `[] !== []`.
96
- *
97
- * Since `zod-compiler` outputs source code which is then {@linkcode eval}uated, it has to get values defined in types
98
- * like `z.literal()` or `.default()` or `.catch()` from *somewhere*; for primitive types this is fine, and the value can
99
- * be directly pasted into the source code. However, for objects, this can lead to unexpected behavior if you, for example,
100
- * expect values returned by `.default()` to be equal to the original value provided in the schema definition.
101
- *
102
- * The default inlining mode is {@linkcode Default}, which inlines *primitives*, but not *objects*. This means that
103
- * any object values are defined as a **dependency**; they are the *exact same values* taken from the schema definition
104
- * that the compiled parser can use via a reference. This behavior provides the best compatibility for in-source usage of
105
- * `zod-compiler`.
106
- *
107
- * For **standalone** builds, however, that would mean that you'd have to provide these dependency references to the parser.
108
- * `zc.compile()` returns a dependency array which you could then pass to `standalone()`, though you'd have to figure
109
- * out where those values come from and extract them out of your source tree. Alternatively, the {@linkcode InliningMode.Aggressive Aggressive}
110
- * inlining mode *will* attempt to inline objects, arrays, `Map`s, and `Set`s. This does mean that these values would
111
- * no longer be equivalent to their definition, but there's a good chance you don't depend on that behavior anyway.
112
- */ var InliningMode = /*#__PURE__*/ function(InliningMode) {
113
- /** Does not inline any values; they will all be added as dependencies. */ InliningMode[InliningMode["None"] = 0] = "None";
114
- /** Inlines most primitives: strings, numbers, `BigInt`s, booleans, and `null`/`undefined`. */ InliningMode[InliningMode["Default"] = 1] = "Default";
115
- /**
116
- * Like {@linkcode InliningMode.Default Default}, but also inlines objects, arrays, `Map`s and `Set`s, and regular
117
- * expressions.
118
- *
119
- * Symbols and functions cannot be inlined.
120
- */ InliningMode[InliningMode["Aggressive"] = 2] = "Aggressive";
121
- return InliningMode;
122
- }({});
123
- class Dependencies {
124
- constructor(verifierContext, inliningMode){
125
- this.verifierContext = verifierContext;
126
- this.inliningMode = inliningMode;
127
- this._dependencies = [];
128
- }
129
- add(value) {
130
- const i = this._dependencies.push(value) - 1;
131
- return propertyChain(this.verifierContext, [
132
- 'dependencies',
133
- i
134
- ]);
135
- }
136
- addOrInline(value) {
137
- if (this.inliningMode === 0) {
138
- return this.add(value);
139
- }
140
- const expr = toLiteral(value, this.inliningMode);
141
- if (expr === null) {
142
- return this.add(value);
143
- }
144
- return expr;
145
- }
146
- get dependencies() {
147
- return this._dependencies;
148
- }
149
- }
150
- class AbstractGeneratorContextWithExprs {
151
- constructor(input, output, verifierContext, dependencies){
152
- this._input = input;
153
- this._output = output;
154
- this._verifierContext = verifierContext;
155
- this._dependencies = dependencies;
156
- }
157
- get input() {
158
- return this._input;
159
- }
160
- get output() {
161
- return this._output;
162
- }
163
- get dependencies() {
164
- return this._dependencies;
165
- }
166
- get verifierContext() {
167
- return this._verifierContext;
168
- }
169
- *prelude() {}
170
- *postlude() {}
171
- *outputs(expr) {
172
- yield ts.factory.createExpressionStatement(ts.factory.createAssignment(this._output, expr));
173
- }
174
- *report(issue, input = this.input) {
175
- yield ts.factory.createExpressionStatement(ts.factory.createCallExpression(propertyChain(this.verifierContext, [
176
- 'reportIssue'
177
- ]), undefined, [
178
- issue,
179
- input
180
- ]));
181
- }
182
- }
183
- class AbstractGeneratorContextWithExprsAndStatusVar extends AbstractGeneratorContextWithExprs {
184
- *prelude() {
185
- yield ts.factory.createVariableStatement([], ts.factory.createVariableDeclarationList([
186
- ts.factory.createVariableDeclaration(this.statusVar, undefined, undefined, ts.factory.createNumericLiteral(standalone.ParseStatus.VALID))
187
- ], ts.NodeFlags.Let));
188
- }
189
- constructor(...args){
190
- super(...args), this.statusVar = uniqueIdentifier('status');
191
- }
192
- }
193
- class FunctionalGeneratorContext extends AbstractGeneratorContextWithExprsAndStatusVar {
194
- *postlude() {
195
- yield ts.factory.createReturnStatement(this.statusVar);
196
- }
197
- *status(status, allowShortCircuiting = true) {
198
- if (typeof status === 'number') {
199
- if (status === standalone.ParseStatus.INVALID && allowShortCircuiting) {
200
- yield ts.factory.createReturnStatement(ts.factory.createNumericLiteral(status));
201
- } else {
202
- yield ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(this.statusVar, ts.SyntaxKind.BarEqualsToken, ts.factory.createNumericLiteral(status)));
203
- }
204
- } else {
205
- if (allowShortCircuiting) {
206
- yield ts.factory.createIfStatement(ts.factory.createBitwiseAnd(status, ts.factory.createNumericLiteral(standalone.ParseStatus.INVALID)), ts.factory.createReturnStatement(status));
207
- }
208
- yield ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(this.statusVar, ts.SyntaxKind.BarEqualsToken, status));
209
- }
210
- }
211
- withInput(expr) {
212
- const x = new FunctionalGeneratorContext(expr, this.output, this.verifierContext, this.dependencies);
213
- x.statusVar = this.statusVar;
214
- return x;
215
- }
216
- }
217
- var LabeledShortCircuitMode = /*#__PURE__*/ function(LabeledShortCircuitMode) {
218
- LabeledShortCircuitMode[LabeledShortCircuitMode["Continue"] = 0] = "Continue";
219
- LabeledShortCircuitMode[LabeledShortCircuitMode["Break"] = 1] = "Break";
220
- return LabeledShortCircuitMode;
221
- }({});
222
- class LabeledBlockScopeGeneratorContext extends AbstractGeneratorContextWithExprsAndStatusVar {
223
- constructor(mode, label, input, output, verifierContext, dependencies){
224
- super(input, output, verifierContext, dependencies), this.mode = mode, this.label = label;
225
- }
226
- *status(status, allowShortCircuiting = true) {
227
- yield ts.factory.createExpressionStatement(ts.factory.createBinaryExpression(this.statusVar, ts.SyntaxKind.BarEqualsToken, typeof status === 'number' ? ts.factory.createNumericLiteral(status) : status));
228
- if (allowShortCircuiting) {
229
- const shortCircuit = ()=>{
230
- switch(this.mode){
231
- case 1:
232
- return ts.factory.createBreakStatement(this.label);
233
- case 0:
234
- return ts.factory.createContinueStatement(this.label);
235
- }
236
- };
237
- if (status === standalone.ParseStatus.INVALID) {
238
- yield shortCircuit();
239
- } else {
240
- yield ts.factory.createIfStatement(ts.factory.createBitwiseAnd(this.statusVar, ts.factory.createNumericLiteral(standalone.ParseStatus.INVALID)), shortCircuit());
241
- }
242
- }
243
- }
244
- withInput(expr) {
245
- const x = new LabeledBlockScopeGeneratorContext(this.mode, this.label, expr, this.output, this.verifierContext, this.dependencies);
246
- x.statusVar = this.statusVar;
247
- return x;
248
- }
249
- }
250
- function toLiteral(value, mode) {
251
- switch(typeof value){
252
- case 'string':
253
- return ts.factory.createStringLiteral(value);
254
- case 'number':
255
- return ts.factory.createNumericLiteral(value);
256
- case 'bigint':
257
- return ts.factory.createBigIntLiteral(value.toString());
258
- case 'undefined':
259
- return ts.factory.createIdentifier('undefined');
260
- case 'boolean':
261
- return value ? ts.factory.createTrue() : ts.factory.createFalse();
262
- case 'object':
263
- if (value === null) {
264
- return ts.factory.createNull();
265
- }
266
- if (mode === 2) {
267
- if (value instanceof RegExp) {
268
- return ts.factory.createRegularExpressionLiteral(value.toString());
269
- } else if (value instanceof Map) {
270
- return ts.factory.createNewExpression(ts.factory.createIdentifier('Map'), undefined, [
271
- ts.factory.createArrayLiteralExpression([
272
- ...value.entries()
273
- ].map(([k, v])=>ts.factory.createArrayLiteralExpression([
274
- toLiteral(k, mode),
275
- toLiteral(v, mode)
276
- ])))
277
- ]);
278
- } else if (value instanceof Set) {
279
- return ts.factory.createNewExpression(ts.factory.createIdentifier('Set'), undefined, [
280
- ts.factory.createArrayLiteralExpression([
281
- ...value.values()
282
- ].map((v)=>toLiteral(v, mode)))
283
- ]);
284
- } else if (Array.isArray(value)) {
285
- return ts.factory.createArrayLiteralExpression(value.map((v)=>toLiteral(v, mode)));
286
- } else if (value instanceof Date) {
287
- return ts.factory.createNewExpression(ts.factory.createIdentifier('Date'), undefined, [
288
- ts.factory.createNumericLiteral(value.getTime())
289
- ]);
290
- } else if (value.constructor === Object) {
291
- return ts.factory.createObjectLiteralExpression(Object.entries(value).map(([k, v])=>ts.factory.createPropertyAssignment(identifierOrStringLiteral(k), toLiteral(v, mode))));
292
- } else {
293
- throw new Error(`Value cannot be inlined: ${value}`);
294
- }
295
- }
296
- break;
297
- case 'function':
298
- case 'symbol':
299
- if (mode === 2) {
300
- throw new Error(`${typeof value === 'function' ? 'Functions' : 'Symbols'} cannot be inlined`);
301
- }
302
- break;
303
- }
304
- return null;
305
- }
306
-
307
- class AbstractCompiledType {
308
- constructor(type){
309
- this.type = type;
310
- }
311
- }
312
- const registry = new Map();
313
- function register(zodType) {
314
- return function(constructor, _context) {
315
- registry.set(zodType, constructor);
316
- };
317
- }
318
- function compilable(type) {
319
- const typeName = type._def.typeName;
320
- if (typeName === undefined) {
321
- throw new TypeError('Third-party Zod types are not supported');
322
- }
323
- const base = registry.get(typeName);
324
- if (!base) {
325
- throw new TypeError(`Unimplemented Zod type \`z.${typeName}\``);
326
- }
327
- return new base(type);
328
- }
329
-
330
- // Mirrors ZodParsedType from src/helpers/util.ts
331
- // Mirrors getParsedType from src/helpers/util.ts
332
- function typeOf(data) {
333
- switch(typeof data){
334
- case 'undefined':
335
- return "undefined";
336
- case 'string':
337
- return "string";
338
- case 'number':
339
- return isNaN(data) ? "nan" : "number";
340
- case 'boolean':
341
- return "boolean";
342
- case 'function':
343
- return "function";
344
- case 'bigint':
345
- return "bigint";
346
- case 'symbol':
347
- return "symbol";
348
- case 'object':
349
- if (Array.isArray(data)) {
350
- return "array";
351
- }
352
- if (data === null) {
353
- return "null";
354
- }
355
- if (typeof data.then === 'function' && typeof data.catch === 'function') {
356
- return "promise";
357
- }
358
- if (data instanceof Map) {
359
- return "map";
360
- }
361
- if (data instanceof Set) {
362
- return "set";
363
- }
364
- if (data instanceof Date) {
365
- return "date";
366
- }
367
- return "object";
368
- default:
369
- return "unknown";
370
- }
371
- }
372
- function stringify(obj) {
373
- return JSON.stringify(obj, (_, value)=>{
374
- if (typeof value === 'bigint') {
375
- return value.toString();
376
- }
377
- return value;
378
- }, 2 /* ugggghhhhhhhh */ );
379
- }
380
- function joinValues(array, separator = ' | ') {
381
- return array.map((val)=>typeof val === 'string' ? `'${val}'` : val).join(separator);
382
- }
383
-
384
- class ZcError extends Error {
385
- get errors() {
386
- return this.issues;
387
- }
388
- constructor(issues){
389
- super(), this.issues = issues;
390
- // TODO: why is this required?
391
- const actualProto = new.target.prototype;
392
- Object.setPrototypeOf(this, actualProto);
393
- this.name = 'ZcError';
394
- }
395
- format(_mapper) {
396
- const mapper = _mapper ?? ((issue)=>issue.message);
397
- const fieldErrors = {
398
- _errors: []
399
- };
400
- const processError = (error)=>{
401
- for (const issue of error.issues){
402
- if (issue.code === "invalid_union") {
403
- issue.unionErrors.map(processError);
404
- } else if (issue.code === "invalid_return_type") {
405
- processError(issue.returnTypeError);
406
- } else if (issue.code === "invalid_arguments") {
407
- processError(issue.argumentsError);
408
- } else if (issue.path.length === 0) {
409
- fieldErrors._errors.push(mapper(issue));
410
- } else {
411
- let curr = fieldErrors;
412
- let i = 0;
413
- while(i < issue.path.length){
414
- const el = issue.path[i];
415
- curr[el] ||= {
416
- _errors: []
417
- };
418
- const terminal = i === issue.path.length - 1;
419
- if (terminal) {
420
- curr[el]._errors.push(mapper(issue));
421
- }
422
- curr = curr[el];
423
- i++;
424
- }
425
- }
426
- }
427
- };
428
- processError(this);
429
- return fieldErrors;
430
- }
431
- static create(issues) {
432
- return new ZcError(issues);
433
- }
434
- toString() {
435
- return this.message;
436
- }
437
- get message() {
438
- return stringify(this.issues);
439
- }
440
- get isEmpty() {
441
- return this.issues.length === 0;
442
- }
443
- addIssue(sub) {
444
- // why not push...?
445
- this.issues = [
446
- ...this.issues,
447
- sub
448
- ];
449
- }
450
- addIssues(subs = []) {
451
- this.issues = [
452
- ...this.issues,
453
- ...subs
454
- ];
455
- }
456
- flatten(mapper = (issue)=>issue.message) {
457
- const fieldErrors = {};
458
- const formErrors = [];
459
- for (const sub of this.issues){
460
- if (sub.path.length > 0) {
461
- fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
462
- fieldErrors[sub.path[0]].push(mapper(sub));
463
- } else {
464
- formErrors.push(mapper(sub));
465
- }
466
- }
467
- return {
468
- formErrors,
469
- fieldErrors
470
- };
471
- }
472
- get formErrors() {
473
- return this.flatten(); //.formErrors?;
474
- }
475
- }
476
-
477
- var _dec$u, _initClass$u, _AbstractCompiledType$u;
478
- exports.ZcAny = void 0;
479
- _dec$u = register(z__default.default.ZodFirstPartyTypeKind.ZodAny);
480
- class ZcAny extends (_AbstractCompiledType$u = AbstractCompiledType) {
481
- static{
482
- ({ c: [exports.ZcAny, _initClass$u] } = _apply_decs_2203_r._(this, [], [
483
- _dec$u
484
- ], _AbstractCompiledType$u));
485
- }
486
- compileType() {
487
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
488
- }
489
- *compileParser(ctx, path) {
490
- yield* ctx.outputs(ctx.input);
491
- }
492
- static{
493
- _initClass$u();
494
- }
495
- }
496
-
497
- var _dec$t, _initClass$t, _AbstractCompiledType$t;
498
- class ArrayGeneratorContext extends LabeledBlockScopeGeneratorContext {
499
- constructor(input, output, verifierContext, dependencies, parentStatusSetter){
500
- super(LabeledShortCircuitMode.Continue, undefined, input, output, verifierContext, dependencies), this.parentStatusSetter = parentStatusSetter;
501
- }
502
- *prelude() {}
503
- *status(status, allowShortCircuiting = true) {
504
- yield* this.parentStatusSetter(typeof status === 'number' ? ts.factory.createNumericLiteral(status) : status);
505
- if (allowShortCircuiting) {
506
- if (status === standalone.ParseStatus.INVALID) {
507
- yield ts.factory.createContinueStatement();
508
- } else if (typeof status !== 'number') {
509
- yield ts.factory.createIfStatement(ts.factory.createBitwiseAnd(status, ts.factory.createNumericLiteral(standalone.ParseStatus.INVALID)), ts.factory.createContinueStatement());
510
- }
511
- }
512
- }
513
- withInput(expr) {
514
- const x = new ArrayGeneratorContext(expr, this.output, this.verifierContext, this.dependencies, this.parentStatusSetter);
515
- x.statusVar = this.statusVar;
516
- return x;
517
- }
518
- }
519
- exports.ZcArray = void 0;
520
- _dec$t = register(z__default.default.ZodFirstPartyTypeKind.ZodArray);
521
- class ZcArray extends (_AbstractCompiledType$t = AbstractCompiledType) {
522
- static{
523
- ({ c: [exports.ZcArray, _initClass$t] } = _apply_decs_2203_r._(this, [], [
524
- _dec$t
525
- ], _AbstractCompiledType$t));
526
- }
527
- compileType() {
528
- return ts.factory.createArrayTypeNode(compilable(this.type._def.type).compileType());
529
- }
530
- *compileParser(ctx, path) {
531
- yield ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Array'), 'isArray'), undefined, [
532
- ctx.input
533
- ])), ts.factory.createBlock([
534
- ...ctx.report(objectLiteral({
535
- path: path.serialize(),
536
- code: ts.factory.createStringLiteral('invalid_type'),
537
- expected: ts.factory.createStringLiteral('array'),
538
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
539
- })),
540
- ...ctx.status(standalone.ParseStatus.INVALID)
541
- ], true));
542
- if (this.type._def.exactLength !== null) {
543
- const value = ts.factory.createNumericLiteral(this.type._def.exactLength.value);
544
- const lengthAccessor = ts.factory.createPropertyAccessExpression(ctx.input, 'length');
545
- const tooBigIdent = uniqueIdentifier('tooBig');
546
- const tooSmallIdent = uniqueIdentifier('tooSmall');
547
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(lengthAccessor, value), ts.factory.createBlock([
548
- local(tooBigIdent, ts.factory.createGreaterThan(lengthAccessor, value)),
549
- local(tooSmallIdent, ts.factory.createLessThan(lengthAccessor, value)),
550
- ...ctx.report(objectLiteral({
551
- path: path.serialize(),
552
- code: ts.factory.createConditionalExpression(tooBigIdent, undefined, ts.factory.createStringLiteral('too_big'), undefined, ts.factory.createStringLiteral('too_small')),
553
- minimum: ts.factory.createConditionalExpression(tooSmallIdent, undefined, value, undefined, ts.factory.createIdentifier('undefined')),
554
- maximum: ts.factory.createConditionalExpression(tooBigIdent, undefined, value, undefined, ts.factory.createIdentifier('undefined')),
555
- type: ts.factory.createStringLiteral('array'),
556
- inclusive: ts.factory.createTrue(),
557
- exact: ts.factory.createTrue(),
558
- message: this.type._def.exactLength.message ? ts.factory.createStringLiteral(this.type._def.exactLength.message) : undefined
559
- })),
560
- ...ctx.status(standalone.ParseStatus.DIRTY)
561
- ], true));
562
- }
563
- if (this.type._def.minLength !== null) {
564
- yield ts.factory.createIfStatement(ts.factory.createLessThan(ts.factory.createPropertyAccessExpression(ctx.input, 'length'), ts.factory.createNumericLiteral(this.type._def.minLength.value)), ts.factory.createBlock([
565
- ...ctx.report(objectLiteral({
566
- path: path.serialize(),
567
- code: ts.factory.createStringLiteral('too_small'),
568
- minimum: ts.factory.createNumericLiteral(this.type._def.minLength.value),
569
- type: ts.factory.createStringLiteral('array'),
570
- inclusive: ts.factory.createTrue(),
571
- exact: ts.factory.createFalse(),
572
- message: this.type._def.minLength.message ? ts.factory.createStringLiteral(this.type._def.minLength.message) : undefined
573
- })),
574
- ...ctx.status(standalone.ParseStatus.DIRTY)
575
- ], true));
576
- }
577
- if (this.type._def.maxLength !== null) {
578
- yield ts.factory.createIfStatement(ts.factory.createGreaterThan(ts.factory.createPropertyAccessExpression(ctx.input, 'length'), ts.factory.createNumericLiteral(this.type._def.maxLength.value)), ts.factory.createBlock([
579
- ...ctx.report(objectLiteral({
580
- path: path.serialize(),
581
- code: ts.factory.createStringLiteral('too_big'),
582
- minimum: ts.factory.createNumericLiteral(this.type._def.maxLength.value),
583
- type: ts.factory.createStringLiteral('array'),
584
- inclusive: ts.factory.createTrue(),
585
- exact: ts.factory.createFalse(),
586
- message: this.type._def.maxLength.message ? ts.factory.createStringLiteral(this.type._def.maxLength.message) : undefined
587
- })),
588
- ...ctx.status(standalone.ParseStatus.DIRTY)
589
- ], true));
590
- }
591
- const output = uniqueIdentifier('outputArray');
592
- yield local(output, ts.factory.createNewExpression(ts.factory.createIdentifier('Array'), undefined, [
593
- ts.factory.createPropertyAccessExpression(ctx.input, 'length')
594
- ]));
595
- const indexInitializer = ts.factory.createLoopVariable(true);
596
- const elInitializer = uniqueIdentifier('el');
597
- const elOutput = ts.factory.createElementAccessExpression(output, indexInitializer);
598
- const childCtx = new ArrayGeneratorContext(elInitializer, elOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
599
- yield ts.factory.createForOfStatement(undefined, ts.factory.createVariableDeclarationList([
600
- ts.factory.createVariableDeclaration(ts.factory.createArrayBindingPattern([
601
- ts.factory.createBindingElement(undefined, undefined, indexInitializer),
602
- ts.factory.createBindingElement(undefined, undefined, elInitializer)
603
- ]))
604
- ], ts.NodeFlags.Const), ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ctx.input, 'entries'), undefined, undefined), ts.factory.createBlock([
605
- ...childCtx.prelude(),
606
- ...compilable(this.type._def.type).compileParser(childCtx, path.push(indexInitializer)),
607
- ...childCtx.postlude()
608
- ]));
609
- yield* ctx.outputs(output);
610
- }
611
- static{
612
- _initClass$t();
613
- }
614
- }
615
-
616
- var _dec$s, _initClass$s, _AbstractCompiledType$s;
617
- exports.ZcBigInt = void 0;
618
- _dec$s = register(z__default.default.ZodFirstPartyTypeKind.ZodBigInt);
619
- class ZcBigInt extends (_AbstractCompiledType$s = AbstractCompiledType) {
620
- static{
621
- ({ c: [exports.ZcBigInt, _initClass$s] } = _apply_decs_2203_r._(this, [], [
622
- _dec$s
623
- ], _AbstractCompiledType$s));
624
- }
625
- compileType() {
626
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword);
627
- }
628
- *compileParser(ctx, path) {
629
- let input = ctx.input;
630
- if (this.type._def.coerce) {
631
- const ident = uniqueIdentifier('coercedInput');
632
- yield local(ident, undefined, true);
633
- input = ident;
634
- yield ts.factory.createTryStatement(ts.factory.createBlock([
635
- ts.factory.createExpressionStatement(ts.factory.createAssignment(ident, ts.factory.createCallExpression(ts.factory.createIdentifier('BigInt'), undefined, [
636
- ctx.input
637
- ])))
638
- ], true), ts.factory.createCatchClause(uniqueIdentifier('e'), ts.factory.createBlock([
639
- ...ctx.report(objectLiteral({
640
- path: path.serialize(),
641
- code: ts.factory.createStringLiteral('invalid_type'),
642
- expected: ts.factory.createStringLiteral('bigint'),
643
- received: callHelper(ctx.verifierContext, 'typeOf', input)
644
- }), input),
645
- // hopefully this causes a short circuit and the rest of the body doesn't get executed...
646
- ...ctx.status(standalone.ParseStatus.INVALID)
647
- ], true)), undefined);
648
- }
649
- let stmt = ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(input), ts.factory.createStringLiteral('bigint')), ts.factory.createBlock([
650
- ...ctx.report(objectLiteral({
651
- path: path.serialize(),
652
- code: ts.factory.createStringLiteral('invalid_type'),
653
- expected: ts.factory.createStringLiteral('bigint'),
654
- received: callHelper(ctx.verifierContext, 'typeOf', input)
655
- }), input),
656
- ...ctx.status(standalone.ParseStatus.INVALID)
657
- ], true));
658
- const checkStmts = [];
659
- for (const check of this.type._def.checks){
660
- switch(check.kind){
661
- case 'min':
662
- checkStmts.push(ts.factory.createIfStatement(ts.factory[check.inclusive ? 'createLessThan' : 'createLessThanEquals'](input, ts.factory.createBigIntLiteral(check.value.toString())), ts.factory.createBlock([
663
- ...ctx.report(objectLiteral({
664
- path: path.serialize(),
665
- code: ts.factory.createStringLiteral('too_small'),
666
- minimum: ts.factory.createBigIntLiteral(check.value.toString()),
667
- type: ts.factory.createStringLiteral('bigint'),
668
- inclusive: check.inclusive ? ts.factory.createTrue() : ts.factory.createFalse(),
669
- exact: ts.factory.createFalse(),
670
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
671
- }), input),
672
- ...ctx.status(standalone.ParseStatus.DIRTY)
673
- ], true)));
674
- break;
675
- case 'max':
676
- checkStmts.push(ts.factory.createIfStatement(ts.factory[check.inclusive ? 'createGreaterThan' : 'createGreaterThanEquals'](input, ts.factory.createBigIntLiteral(check.value.toString())), ts.factory.createBlock([
677
- ...ctx.report(objectLiteral({
678
- path: path.serialize(),
679
- code: ts.factory.createStringLiteral('too_big'),
680
- minimum: ts.factory.createBigIntLiteral(check.value.toString()),
681
- type: ts.factory.createStringLiteral('bigint'),
682
- inclusive: check.inclusive ? ts.factory.createTrue() : ts.factory.createFalse(),
683
- exact: ts.factory.createFalse(),
684
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
685
- }), input),
686
- ...ctx.status(standalone.ParseStatus.DIRTY)
687
- ], true)));
688
- break;
689
- case 'multipleOf':
690
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createModulo(input, ts.factory.createBigIntLiteral(check.value.toString())), ts.factory.createBigIntLiteral('0')), ts.factory.createBlock([
691
- ...ctx.report(objectLiteral({
692
- path: path.serialize(),
693
- code: ts.factory.createStringLiteral('not_multiple_of'),
694
- multipleOf: ts.factory.createBigIntLiteral(check.value.toString()),
695
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
696
- }), input),
697
- ...ctx.status(standalone.ParseStatus.DIRTY)
698
- ], true)));
699
- break;
700
- default:
701
- throw new Error(`Unsupported ZcBigInt check: ${check}`);
702
- }
703
- }
704
- if (checkStmts.length === 0) {
705
- yield stmt;
706
- } else {
707
- stmt = ts.factory.updateIfStatement(stmt, stmt.expression, stmt.thenStatement, ts.factory.createBlock([
708
- ...checkStmts
709
- ], true));
710
- yield stmt;
711
- }
712
- yield* ctx.outputs(input);
713
- }
714
- static{
715
- _initClass$s();
716
- }
717
- }
718
-
719
- var _dec$r, _initClass$r, _AbstractCompiledType$r;
720
- exports.ZcBoolean = void 0;
721
- _dec$r = register(z__default.default.ZodFirstPartyTypeKind.ZodBoolean);
722
- class ZcBoolean extends (_AbstractCompiledType$r = AbstractCompiledType) {
723
- static{
724
- ({ c: [exports.ZcBoolean, _initClass$r] } = _apply_decs_2203_r._(this, [], [
725
- _dec$r
726
- ], _AbstractCompiledType$r));
727
- }
728
- compileType() {
729
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
730
- }
731
- *compileParser(ctx, path) {
732
- let input = ctx.input;
733
- if (this.type._def.coerce) {
734
- const ident = uniqueIdentifier('coercedInput');
735
- yield local(ident, ts.factory.createCallExpression(ts.factory.createIdentifier('Boolean'), [], [
736
- ctx.input
737
- ]), false);
738
- input = ident;
739
- }
740
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(input), ts.factory.createStringLiteral('boolean')), ts.factory.createBlock([
741
- ...ctx.report(objectLiteral({
742
- path: path.serialize(),
743
- code: ts.factory.createStringLiteral('invalid_type'),
744
- expected: ts.factory.createStringLiteral('boolean'),
745
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
746
- }), input),
747
- ...ctx.status(standalone.ParseStatus.INVALID)
748
- ], true));
749
- yield* ctx.outputs(ctx.input);
750
- }
751
- static{
752
- _initClass$r();
753
- }
754
- }
755
-
756
- var _dec$q, _initClass$q, _AbstractCompiledType$q;
757
- exports.ZcBranded = void 0;
758
- _dec$q = register(z__default.default.ZodFirstPartyTypeKind.ZodBranded);
759
- class ZcBranded extends (_AbstractCompiledType$q = AbstractCompiledType) {
760
- static{
761
- ({ c: [exports.ZcBranded, _initClass$q] } = _apply_decs_2203_r._(this, [], [
762
- _dec$q
763
- ], _AbstractCompiledType$q));
764
- }
765
- compileType() {
766
- return compilable(this.type._def.type).compileType();
767
- }
768
- *compileParser(ctx, path) {
769
- yield* compilable(this.type._def.type).compileParser(ctx, path);
770
- }
771
- static{
772
- _initClass$q();
773
- }
774
- }
775
-
776
- var _dec$p, _initClass$p, _AbstractCompiledType$p;
777
- class CatchGeneratorContext extends LabeledBlockScopeGeneratorContext {
778
- constructor(label, input, output, verifierContext, dependencies, statusVar){
779
- super(LabeledShortCircuitMode.Break, label, input, output, verifierContext, dependencies);
780
- this.statusVar = statusVar;
781
- }
782
- *prelude() {}
783
- withInput(expr) {
784
- const x = new CatchGeneratorContext(this.label, expr, this.output, this.verifierContext, this.dependencies, this.statusVar);
785
- x.statusVar = this.statusVar;
786
- return x;
787
- }
788
- }
789
- exports.ZcCatch = void 0;
790
- _dec$p = register(z__default.default.ZodFirstPartyTypeKind.ZodCatch);
791
- class ZcCatch extends (_AbstractCompiledType$p = AbstractCompiledType) {
792
- static{
793
- ({ c: [exports.ZcCatch, _initClass$p] } = _apply_decs_2203_r._(this, [], [
794
- _dec$p
795
- ], _AbstractCompiledType$p));
796
- }
797
- compileType() {
798
- return compilable(this.type._def.innerType).compileType();
799
- }
800
- *compileParser(ctx, path) {
801
- const caughtStatus = uniqueIdentifier('caughtStatus');
802
- yield local(caughtStatus, ts.factory.createNumericLiteral(standalone.ParseStatus.VALID), true);
803
- const output = uniqueIdentifier('caughtOutput');
804
- yield local(output, undefined, true);
805
- const verifierCtxVar = uniqueIdentifier('variantCtx');
806
- yield local(verifierCtxVar, ts.factory.createObjectLiteralExpression([
807
- ts.factory.createSpreadAssignment(ctx.verifierContext),
808
- ts.factory.createPropertyAssignment('issues', ts.factory.createArrayLiteralExpression())
809
- ], true));
810
- const label = uniqueIdentifier('catch');
811
- const childCtx = new CatchGeneratorContext(label, ctx.input, output, verifierCtxVar, ctx.dependencies, caughtStatus);
812
- const catchValue = ctx.dependencies.addOrInline(this.type._def.catchValue(null));
813
- yield ts.factory.createLabeledStatement(label, ts.factory.createBlock([
814
- ...childCtx.prelude(),
815
- ...compilable(this.type._def.innerType).compileParser(childCtx, path),
816
- ...childCtx.postlude()
817
- ]));
818
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(caughtStatus, ts.factory.createNumericLiteral(standalone.ParseStatus.VALID)), ts.factory.createBlock([
819
- ...ctx.outputs(catchValue)
820
- ], true), ts.factory.createBlock([
821
- ...ctx.outputs(output)
822
- ], true));
823
- }
824
- static{
825
- _initClass$p();
826
- }
827
- }
828
-
829
- var _dec$o, _initClass$o, _AbstractCompiledType$o;
830
- exports.ZcDate = void 0;
831
- _dec$o = register(z__default.default.ZodFirstPartyTypeKind.ZodDate);
832
- class ZcDate extends (_AbstractCompiledType$o = AbstractCompiledType) {
833
- static{
834
- ({ c: [exports.ZcDate, _initClass$o] } = _apply_decs_2203_r._(this, [], [
835
- _dec$o
836
- ], _AbstractCompiledType$o));
837
- }
838
- compileType() {
839
- return ts.factory.createTypeReferenceNode('Date');
840
- }
841
- *compileParser(ctx, path) {
842
- let input = ctx.input;
843
- if (this.type._def.coerce) {
844
- const ident = uniqueIdentifier('coercedInput');
845
- yield local(ident, ts.factory.createNewExpression(ts.factory.createIdentifier('Date'), [], [
846
- ctx.input
847
- ]), false);
848
- input = ident;
849
- }
850
- let stmt = ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createBinaryExpression(input, ts.SyntaxKind.InstanceOfKeyword, ts.factory.createIdentifier('Date'))), ts.factory.createBlock([
851
- ...ctx.report(objectLiteral({
852
- path: path.serialize(),
853
- code: ts.factory.createStringLiteral('invalid_type'),
854
- expected: ts.factory.createStringLiteral('date'),
855
- received: callHelper(ctx.verifierContext, 'typeOf', input)
856
- }), input),
857
- ...ctx.status(standalone.ParseStatus.INVALID)
858
- ], true));
859
- const timeVar = uniqueIdentifier('time');
860
- const checkStmts = [
861
- local(timeVar, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(input, 'getTime'), undefined, [])),
862
- ts.factory.createIfStatement(ts.factory.createCallExpression(ts.factory.createIdentifier('isNaN'), undefined, [
863
- timeVar
864
- ]), ts.factory.createBlock([
865
- ...ctx.report(objectLiteral({
866
- path: path.serialize(),
867
- code: ts.factory.createStringLiteral('invalid_date')
868
- }), input),
869
- ...ctx.status(standalone.ParseStatus.INVALID)
870
- ], true))
871
- ];
872
- for (const check of this.type._def.checks){
873
- switch(check.kind){
874
- case 'min':
875
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLessThan(timeVar, ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
876
- ...ctx.report(objectLiteral({
877
- path: path.serialize(),
878
- code: ts.factory.createStringLiteral('too_small'),
879
- minimum: ts.factory.createNumericLiteral(check.value),
880
- type: ts.factory.createStringLiteral('date'),
881
- inclusive: ts.factory.createTrue(),
882
- exact: ts.factory.createFalse(),
883
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
884
- }), input),
885
- ...ctx.status(standalone.ParseStatus.DIRTY)
886
- ], true)));
887
- break;
888
- case 'max':
889
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createGreaterThan(timeVar, ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
890
- ...ctx.report(objectLiteral({
891
- path: path.serialize(),
892
- code: ts.factory.createStringLiteral('too_big'),
893
- minimum: ts.factory.createNumericLiteral(check.value),
894
- type: ts.factory.createStringLiteral('date'),
895
- inclusive: ts.factory.createTrue(),
896
- exact: ts.factory.createFalse(),
897
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
898
- }), input),
899
- ...ctx.status(standalone.ParseStatus.DIRTY)
900
- ], true)));
901
- break;
902
- default:
903
- throw new Error(`Unsupported ZcDate check: ${check}`);
904
- }
905
- }
906
- yield ts.factory.updateIfStatement(stmt, stmt.expression, stmt.thenStatement, ts.factory.createBlock([
907
- ...checkStmts
908
- ], true));
909
- yield* ctx.outputs(input);
910
- }
911
- static{
912
- _initClass$o();
913
- }
914
- }
915
-
916
- var _dec$n, _initClass$n, _AbstractCompiledType$n;
917
- exports.ZcDefault = void 0;
918
- _dec$n = register(z__default.default.ZodFirstPartyTypeKind.ZodDefault);
919
- class ZcDefault extends (_AbstractCompiledType$n = AbstractCompiledType) {
920
- static{
921
- ({ c: [exports.ZcDefault, _initClass$n] } = _apply_decs_2203_r._(this, [], [
922
- _dec$n
923
- ], _AbstractCompiledType$n));
924
- }
925
- compileType() {
926
- return compilable(this.type._def.innerType).compileType();
927
- }
928
- *compileParser(ctx, path) {
929
- const newInput = uniqueIdentifier('defaultInput');
930
- yield local(newInput, ctx.input, true);
931
- const defaultValue = ctx.dependencies.addOrInline(this.type._def.defaultValue());
932
- yield ts.factory.createIfStatement(ts.factory.createStrictEquality(ts.factory.createTypeOfExpression(newInput), ts.factory.createStringLiteral('undefined')), ts.factory.createBlock([
933
- ts.factory.createExpressionStatement(ts.factory.createAssignment(newInput, defaultValue))
934
- ], true));
935
- yield* compilable(this.type._def.innerType).compileParser(ctx.withInput(newInput), path);
936
- }
937
- static{
938
- _initClass$n();
939
- }
940
- }
941
-
942
- var _dec$m, _initClass$m, _AbstractCompiledType$m;
943
- class ObjectGeneratorContext extends LabeledBlockScopeGeneratorContext {
944
- constructor(label, input, output, verifierContext, dependencies, parentStatusSetter){
945
- super(LabeledShortCircuitMode.Break, label, input, output, verifierContext, dependencies), this.parentStatusSetter = parentStatusSetter;
946
- }
947
- *prelude() {}
948
- *status(status, allowShortCircuiting = true) {
949
- yield* this.parentStatusSetter(typeof status === 'number' ? ts.factory.createNumericLiteral(status) : status);
950
- if (allowShortCircuiting) {
951
- if (status === standalone.ParseStatus.INVALID) {
952
- yield ts.factory.createBreakStatement(this.label);
953
- } else if (typeof status !== 'number') {
954
- yield ts.factory.createIfStatement(ts.factory.createBitwiseAnd(status, ts.factory.createNumericLiteral(standalone.ParseStatus.INVALID)), ts.factory.createBreakStatement(this.label));
955
- }
956
- }
957
- }
958
- withInput(expr) {
959
- const x = new ObjectGeneratorContext(this.label, expr, this.output, this.verifierContext, this.dependencies, this.parentStatusSetter);
960
- x.statusVar = this.statusVar;
961
- return x;
962
- }
963
- }
964
- exports.ZcObject = void 0;
965
- _dec$m = register(z__default.default.ZodFirstPartyTypeKind.ZodObject);
966
- class ZcObject extends (_AbstractCompiledType$m = AbstractCompiledType) {
967
- static{
968
- ({ c: [exports.ZcObject, _initClass$m] } = _apply_decs_2203_r._(this, [], [
969
- _dec$m
970
- ], _AbstractCompiledType$m));
971
- }
972
- compileType() {
973
- return ts.factory.createTypeLiteralNode(Object.entries(this.type._def.shape()).map(([key, value])=>{
974
- const type = compilable(value);
975
- const isOptional = value._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodOptional || value.isOptional();
976
- const node = ts.factory.createPropertySignature(undefined, identifierOrStringLiteral(key), isOptional && value._def.typeName !== z__default.default.ZodFirstPartyTypeKind.ZodDefault ? ts.factory.createToken(ts.SyntaxKind.QuestionToken) : undefined, type.compileType());
977
- if (value.description) {
978
- ts__default.default.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `* ${value.description} `, true);
979
- }
980
- return node;
981
- }));
982
- }
983
- *compileParser(ctx, path) {
984
- if (!this.canSkipTypeCheck) {
985
- const typeIdentifier = uniqueIdentifier('type');
986
- yield local(typeIdentifier, callHelper(ctx.verifierContext, 'typeOf', ctx.input));
987
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(typeIdentifier, ts.factory.createStringLiteral('object')), ts.factory.createBlock([
988
- ...ctx.report(objectLiteral({
989
- path: path.serialize(),
990
- code: ts.factory.createStringLiteral('invalid_type'),
991
- expected: ts.factory.createStringLiteral('object'),
992
- received: typeIdentifier
993
- })),
994
- ...ctx.status(standalone.ParseStatus.INVALID)
995
- ], true));
996
- }
997
- const shape = this.type._def.shape();
998
- // TODO: catchall/passthrough/strict
999
- // const shapeKeysIdent = uniqueIdentifier('shapeKeys');
1000
- // yield local(
1001
- // shapeKeysIdent,
1002
- // factory.createNewExpression(
1003
- // factory.createIdentifier('Set'),
1004
- // [],
1005
- // [ factory.createArrayLiteralExpression(Object.keys(shape).map(key => factory.createStringLiteral(key))) ]
1006
- // )
1007
- // );
1008
- const output = uniqueIdentifier('outputObject');
1009
- yield local(output, ts.factory.createObjectLiteralExpression());
1010
- for (const [i, [key, value]] of Object.entries(shape).entries()){
1011
- const label = uniqueIdentifier(`prop${i}`);
1012
- const inputExpr = propertyChain(ctx.input, [
1013
- key
1014
- ]);
1015
- const outputExpr = propertyChain(output, [
1016
- key
1017
- ]);
1018
- const childCtx = new ObjectGeneratorContext(label, inputExpr, outputExpr, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1019
- yield ts.factory.createLabeledStatement(label, ts.factory.createBlock([
1020
- ...compilable(value).compileParser(childCtx, path.push(key))
1021
- ]));
1022
- }
1023
- yield* ctx.outputs(output);
1024
- }
1025
- static{
1026
- _initClass$m();
1027
- }
1028
- constructor(...args){
1029
- super(...args), // In the case of discriminated unions, we already check that the type is an object before passing off parsing
1030
- // to a ZcObject.
1031
- this.canSkipTypeCheck = false;
1032
- }
1033
- }
1034
-
1035
- var _dec$l, _initClass$l, _AbstractCompiledType$l;
1036
- exports.ZcDiscriminatedUnion = void 0;
1037
- _dec$l = register(z__default.default.ZodFirstPartyTypeKind.ZodDiscriminatedUnion);
1038
- class ZcDiscriminatedUnion extends (_AbstractCompiledType$l = AbstractCompiledType) {
1039
- static{
1040
- ({ c: [exports.ZcDiscriminatedUnion, _initClass$l] } = _apply_decs_2203_r._(this, [], [
1041
- _dec$l
1042
- ], _AbstractCompiledType$l));
1043
- }
1044
- compileType() {
1045
- return ts.factory.createUnionTypeNode(this.type._def.options.map((ty)=>compilable(ty).compileType()));
1046
- }
1047
- *compileParser(ctx, path) {
1048
- const typeIdentifier = uniqueIdentifier('type');
1049
- yield local(typeIdentifier, callHelper(ctx.verifierContext, 'typeOf', ctx.input));
1050
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(typeIdentifier, ts.factory.createStringLiteral('object')), ts.factory.createBlock([
1051
- ...ctx.report(objectLiteral({
1052
- path: path.serialize(),
1053
- code: ts.factory.createStringLiteral('invalid_type'),
1054
- expected: ts.factory.createStringLiteral('object'),
1055
- received: typeIdentifier
1056
- })),
1057
- ...ctx.status(standalone.ParseStatus.INVALID)
1058
- ], true));
1059
- yield ts.factory.createSwitchStatement(propertyChain(ctx.input, [
1060
- this.type._def.discriminator
1061
- ]), ts.factory.createCaseBlock([
1062
- ...[
1063
- ...this.type._def.optionsMap.entries()
1064
- ].map(([discriminator, zodTy])=>{
1065
- const ty = compilable(zodTy);
1066
- if (ty instanceof exports.ZcObject) {
1067
- // We've already checked that the input is an object.
1068
- ty.canSkipTypeCheck = true;
1069
- }
1070
- return ts.factory.createCaseClause(ctx.dependencies.addOrInline(discriminator), [
1071
- ...ty.compileParser(ctx, path),
1072
- ts.factory.createBreakStatement()
1073
- ]);
1074
- }),
1075
- ts.factory.createDefaultClause([
1076
- ...ctx.report(objectLiteral({
1077
- path: path.push(this.type._def.discriminator).serialize(),
1078
- code: ts.factory.createStringLiteral('invalid_union_discriminator'),
1079
- options: ctx.dependencies.add(Array.from(this.type._def.optionsMap.keys()))
1080
- })),
1081
- ...ctx.status(standalone.ParseStatus.INVALID)
1082
- ])
1083
- ]));
1084
- }
1085
- static{
1086
- _initClass$l();
1087
- }
1088
- }
1089
-
1090
- var _dec$k, _initClass$k, _AbstractCompiledType$k;
1091
- exports.ZcEnum = void 0;
1092
- _dec$k = register(z__default.default.ZodFirstPartyTypeKind.ZodEnum);
1093
- class ZcEnum extends (_AbstractCompiledType$k = AbstractCompiledType) {
1094
- static{
1095
- ({ c: [exports.ZcEnum, _initClass$k] } = _apply_decs_2203_r._(this, [], [
1096
- _dec$k
1097
- ], _AbstractCompiledType$k));
1098
- }
1099
- compileType() {
1100
- return ts.factory.createUnionTypeNode(this.type._def.values.map((ty)=>ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(ty))));
1101
- }
1102
- *compileParser(ctx, path) {
1103
- const clauses = this.type._def.values.map((value)=>ts.factory.createCaseClause(ts.factory.createStringLiteral(value), []));
1104
- const lastClause = clauses[clauses.length - 1];
1105
- clauses[clauses.length - 1] = ts.factory.updateCaseClause(lastClause, lastClause.expression, [
1106
- ...ctx.outputs(ctx.input),
1107
- ts.factory.createBreakStatement()
1108
- ]);
1109
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('string')), ts.factory.createBlock([
1110
- ...ctx.report(objectLiteral({
1111
- path: path.serialize(),
1112
- code: ts.factory.createStringLiteral('invalid_type'),
1113
- expected: ts.factory.createStringLiteral(this.type._def.values.map((value)=>`"${value}"`).join(' | ')),
1114
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
1115
- })),
1116
- ...ctx.status(standalone.ParseStatus.INVALID)
1117
- ], true), ts.factory.createBlock([
1118
- ts.factory.createSwitchStatement(ctx.input, ts.factory.createCaseBlock([
1119
- ...clauses,
1120
- ts.factory.createDefaultClause([
1121
- ...ctx.report(objectLiteral({
1122
- path: path.serialize(),
1123
- code: ts.factory.createStringLiteral('invalid_enum_value'),
1124
- options: ts.factory.createArrayLiteralExpression(this.type._def.values.map((value)=>ts.factory.createStringLiteral(value))),
1125
- received: ctx.input
1126
- })),
1127
- ...ctx.status(standalone.ParseStatus.INVALID)
1128
- ])
1129
- ]))
1130
- ], true));
1131
- }
1132
- static{
1133
- _initClass$k();
1134
- }
1135
- }
1136
-
1137
- var _dec$j, _initClass$j, _AbstractCompiledType$j;
1138
- class IntersectionGeneratorContext extends LabeledBlockScopeGeneratorContext {
1139
- constructor(parentLabel, label, input, output, verifierContext, dependencies, parentStatusSetter){
1140
- super(LabeledShortCircuitMode.Break, label, input, output, verifierContext, dependencies), this.parentLabel = parentLabel, this.parentStatusSetter = parentStatusSetter;
1141
- }
1142
- *status(status, allowShortCircuiting = true) {
1143
- yield* this.parentStatusSetter(typeof status === 'number' ? ts.factory.createNumericLiteral(status) : status);
1144
- if (allowShortCircuiting) {
1145
- if (status === standalone.ParseStatus.INVALID) {
1146
- yield ts.factory.createBreakStatement(this.label);
1147
- } else if (typeof status !== 'number') {
1148
- yield ts.factory.createIfStatement(ts.factory.createBitwiseAnd(status, ts.factory.createNumericLiteral(standalone.ParseStatus.INVALID)), ts.factory.createBreakStatement(this.label));
1149
- }
1150
- }
1151
- }
1152
- withInput(expr) {
1153
- const x = new IntersectionGeneratorContext(this.parentLabel, this.label, expr, this.output, this.verifierContext, this.dependencies, this.parentStatusSetter);
1154
- x.statusVar = this.statusVar;
1155
- return x;
1156
- }
1157
- }
1158
- exports.ZcIntersection = void 0;
1159
- _dec$j = register(z__default.default.ZodFirstPartyTypeKind.ZodIntersection);
1160
- class ZcIntersection extends (_AbstractCompiledType$j = AbstractCompiledType) {
1161
- static{
1162
- ({ c: [exports.ZcIntersection, _initClass$j] } = _apply_decs_2203_r._(this, [], [
1163
- _dec$j
1164
- ], _AbstractCompiledType$j));
1165
- }
1166
- compileType() {
1167
- return ts.factory.createIntersectionTypeNode([
1168
- compilable(this.type._def.left).compileType(),
1169
- compilable(this.type._def.right).compileType()
1170
- ]);
1171
- }
1172
- *compileParser(ctx, path) {
1173
- const mergeOutput = uniqueIdentifier('intersectionOutput');
1174
- const outputA = uniqueIdentifier('outputA');
1175
- const outputB = uniqueIdentifier('outputB');
1176
- const label = uniqueIdentifier('intersection');
1177
- yield ts.factory.createLabeledStatement(label, ts.factory.createBlock([
1178
- ...this._generateSide(ctx, path, label, 'left', outputA),
1179
- ...this._generateSide(ctx, path, label, 'right', outputB),
1180
- // TODO: We only break one side when parsing fails so that the other can continue to parse; this causes `mergeValues`
1181
- // (and probably the `invalid_intersection_types` issue) to always be called if either side fails. Zod does not do this.
1182
- // We need some way to short circuit here if the status is invalid; context should allow inspecting status instead of
1183
- // being setter-only.
1184
- local(mergeOutput, callHelper(ctx.verifierContext, 'mergeValues', outputA, outputB)),
1185
- ts.factory.createIfStatement(ts.factory.createPropertyAccessExpression(mergeOutput, 'valid'), ts.factory.createBlock([
1186
- ...ctx.outputs(ts.factory.createPropertyAccessExpression(mergeOutput, 'data'))
1187
- ], true), ts.factory.createBlock([
1188
- ...ctx.report(objectLiteral({
1189
- path: path.serialize(),
1190
- code: ts.factory.createStringLiteral('invalid_intersection_types')
1191
- })),
1192
- ...ctx.status(standalone.ParseStatus.INVALID)
1193
- ]))
1194
- ]));
1195
- }
1196
- *_generateSide(ctx, path, parentLabel, side, output) {
1197
- const name = side === 'left' ? 'A' : 'B';
1198
- const label = uniqueIdentifier(`type${name}`);
1199
- yield local(output, undefined, true);
1200
- const childCtx = new IntersectionGeneratorContext(parentLabel, label, ctx.input, output, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1201
- yield ts.factory.createLabeledStatement(label, ts.factory.createBlock([
1202
- ...compilable(this.type._def[side]).compileParser(childCtx, path)
1203
- ]));
1204
- }
1205
- static{
1206
- _initClass$j();
1207
- }
1208
- }
1209
-
1210
- var _dec$i, _initClass$i, _AbstractCompiledType$i;
1211
- exports.ZcLiteral = void 0;
1212
- _dec$i = register(z__default.default.ZodFirstPartyTypeKind.ZodLiteral);
1213
- class ZcLiteral extends (_AbstractCompiledType$i = AbstractCompiledType) {
1214
- static{
1215
- ({ c: [exports.ZcLiteral, _initClass$i] } = _apply_decs_2203_r._(this, [], [
1216
- _dec$i
1217
- ], _AbstractCompiledType$i));
1218
- }
1219
- compileType() {
1220
- const value = this.type._def.value;
1221
- let n;
1222
- switch(typeof value){
1223
- case 'string':
1224
- n = ts.factory.createStringLiteral(value);
1225
- break;
1226
- case 'number':
1227
- n = ts.factory.createNumericLiteral(value);
1228
- break;
1229
- case 'bigint':
1230
- n = ts.factory.createBigIntLiteral(value.toString());
1231
- break;
1232
- case 'undefined':
1233
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
1234
- case 'boolean':
1235
- n = value ? ts.factory.createTrue() : ts.factory.createFalse();
1236
- case 'object':
1237
- if (value === null) {
1238
- n = ts.factory.createNull();
1239
- }
1240
- // fallthrough
1241
- default:
1242
- throw new Error(`\`zc.ZcLiteral\` only supports primitives (string, number, BigInt, boolean, null, or undefined); got '${typeOf(value)}'`);
1243
- }
1244
- return ts.factory.createLiteralTypeNode(n);
1245
- }
1246
- *compileParser(ctx, path) {
1247
- const value = ctx.dependencies.addOrInline(this.type._def.value);
1248
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ctx.input, value), ts.factory.createBlock([
1249
- ...ctx.report(objectLiteral({
1250
- path: path.serialize(),
1251
- code: ts.factory.createStringLiteral('invalid_literal'),
1252
- expected: value,
1253
- received: ctx.input
1254
- })),
1255
- ...ctx.status(standalone.ParseStatus.INVALID)
1256
- ], true));
1257
- yield* ctx.outputs(ctx.input);
1258
- }
1259
- static{
1260
- _initClass$i();
1261
- }
1262
- }
1263
-
1264
- var _dec$h, _initClass$h, _AbstractCompiledType$h;
1265
- exports.ZcMap = void 0;
1266
- _dec$h = register(z__default.default.ZodFirstPartyTypeKind.ZodMap);
1267
- class ZcMap extends (_AbstractCompiledType$h = AbstractCompiledType) {
1268
- static{
1269
- ({ c: [exports.ZcMap, _initClass$h] } = _apply_decs_2203_r._(this, [], [
1270
- _dec$h
1271
- ], _AbstractCompiledType$h));
1272
- }
1273
- compileType() {
1274
- const [k, v] = [
1275
- compilable(this.type._def.keyType),
1276
- compilable(this.type._def.valueType)
1277
- ];
1278
- return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Map'), [
1279
- k.compileType(),
1280
- v.compileType()
1281
- ]);
1282
- }
1283
- *compileParser(ctx, path) {
1284
- const typeIdentifier = uniqueIdentifier('type');
1285
- yield local(typeIdentifier, callHelper(ctx.verifierContext, 'typeOf', ctx.input));
1286
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(typeIdentifier, ts.factory.createStringLiteral('map')), ts.factory.createBlock([
1287
- ...ctx.report(objectLiteral({
1288
- path: path.serialize(),
1289
- code: ts.factory.createStringLiteral('invalid_type'),
1290
- expected: ts.factory.createStringLiteral('map'),
1291
- received: typeIdentifier
1292
- })),
1293
- ...ctx.status(standalone.ParseStatus.INVALID)
1294
- ], true));
1295
- const output = uniqueIdentifier('outputMap');
1296
- yield local(output, ts.factory.createNewExpression(ts.factory.createIdentifier('Map'), undefined, []));
1297
- const keyLabel = uniqueIdentifier('parseKey');
1298
- const keyInput = uniqueIdentifier('key');
1299
- const keyOutput = uniqueIdentifier('keyOut');
1300
- const valueLabel = uniqueIdentifier('parseValue');
1301
- const valueInput = uniqueIdentifier('value');
1302
- const valueOutput = uniqueIdentifier('valueOut');
1303
- const keyCtx = new ObjectGeneratorContext(keyLabel, keyInput, keyOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1304
- const valueCtx = new ObjectGeneratorContext(valueLabel, valueInput, valueOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1305
- yield ts.factory.createForOfStatement(undefined, ts.factory.createVariableDeclarationList([
1306
- ts.factory.createVariableDeclaration(ts.factory.createArrayBindingPattern([
1307
- ts.factory.createBindingElement(undefined, undefined, keyInput),
1308
- ts.factory.createBindingElement(undefined, undefined, valueInput)
1309
- ]))
1310
- ], ts.NodeFlags.Const), ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ctx.input, 'entries'), undefined, []), ts.factory.createBlock([
1311
- local(keyOutput, keyInput, true),
1312
- local(valueOutput, undefined, true),
1313
- ts.factory.createLabeledStatement(keyLabel, ts.factory.createBlock([
1314
- ...compilable(this.type._def.keyType).compileParser(keyCtx, path)
1315
- ])),
1316
- ts.factory.createLabeledStatement(valueLabel, ts.factory.createBlock([
1317
- ...compilable(this.type._def.valueType).compileParser(valueCtx, path.push(keyOutput))
1318
- ])),
1319
- ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(output, 'set'), undefined, [
1320
- keyOutput,
1321
- valueOutput
1322
- ]))
1323
- ]));
1324
- yield* ctx.outputs(output);
1325
- }
1326
- static{
1327
- _initClass$h();
1328
- }
1329
- }
1330
-
1331
- var _dec$g, _initClass$g, _AbstractCompiledType$g;
1332
- exports.ZcNaN = void 0;
1333
- _dec$g = register(z__default.default.ZodFirstPartyTypeKind.ZodNaN);
1334
- class ZcNaN extends (_AbstractCompiledType$g = AbstractCompiledType) {
1335
- static{
1336
- ({ c: [exports.ZcNaN, _initClass$g] } = _apply_decs_2203_r._(this, [], [
1337
- _dec$g
1338
- ], _AbstractCompiledType$g));
1339
- }
1340
- compileType() {
1341
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
1342
- }
1343
- *compileParser(ctx, path) {
1344
- yield ts.factory.createIfStatement(ts.factory.createLogicalOr(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('number')), ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createIdentifier('isNaN'), undefined, [
1345
- ctx.input
1346
- ]))), ts.factory.createBlock([
1347
- ...ctx.report(objectLiteral({
1348
- path: path.serialize(),
1349
- code: ts.factory.createStringLiteral('invalid_type'),
1350
- expected: ts.factory.createStringLiteral('nan'),
1351
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
1352
- })),
1353
- ...ctx.status(standalone.ParseStatus.INVALID)
1354
- ], true));
1355
- yield* ctx.outputs(ctx.input);
1356
- }
1357
- static{
1358
- _initClass$g();
1359
- }
1360
- }
1361
-
1362
- var _dec$f, _initClass$f, _AbstractCompiledType$f;
1363
- function getValidEnumValues(obj) {
1364
- const validKeys = Object.keys(obj).filter((k)=>typeof obj[obj[k]] !== 'number');
1365
- const filtered = {};
1366
- for (const k of validKeys){
1367
- filtered[k] = obj[k];
1368
- }
1369
- return [
1370
- ...new Set(Object.values(filtered))
1371
- ];
1372
- }
1373
- exports.ZcNativeEnum = void 0;
1374
- _dec$f = register(z__default.default.ZodFirstPartyTypeKind.ZodNativeEnum);
1375
- class ZcNativeEnum extends (_AbstractCompiledType$f = AbstractCompiledType) {
1376
- static{
1377
- ({ c: [exports.ZcNativeEnum, _initClass$f] } = _apply_decs_2203_r._(this, [], [
1378
- _dec$f
1379
- ], _AbstractCompiledType$f));
1380
- }
1381
- compileType() {
1382
- return ts.factory.createUnionTypeNode(getValidEnumValues(this.type._def.values).map((value)=>ts.factory.createLiteralTypeNode(typeof value === 'string' ? ts.factory.createStringLiteral(value) : ts.factory.createNumericLiteral(value))));
1383
- }
1384
- *compileParser(ctx, path) {
1385
- const values = getValidEnumValues(this.type._def.values);
1386
- const clauses = values.map((value)=>ts.factory.createCaseClause(typeof value === 'string' ? ts.factory.createStringLiteral(value) : ts.factory.createNumericLiteral(value), []));
1387
- const lastClause = clauses[clauses.length - 1];
1388
- clauses[clauses.length - 1] = ts.factory.updateCaseClause(lastClause, lastClause.expression, [
1389
- ...ctx.outputs(ctx.input),
1390
- ts.factory.createBreakStatement()
1391
- ]);
1392
- yield ts.factory.createIfStatement(ts.factory.createLogicalAnd(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('string')), ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('number'))), ts.factory.createBlock([
1393
- ...ctx.report(objectLiteral({
1394
- path: path.serialize(),
1395
- code: ts.factory.createStringLiteral('invalid_type'),
1396
- expected: ts.factory.createStringLiteral(joinValues(values)),
1397
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
1398
- })),
1399
- ...ctx.status(standalone.ParseStatus.INVALID)
1400
- ], true), ts.factory.createBlock([
1401
- ts.factory.createSwitchStatement(ctx.input, ts.factory.createCaseBlock([
1402
- ...clauses,
1403
- ts.factory.createDefaultClause([
1404
- ...ctx.report(objectLiteral({
1405
- path: path.serialize(),
1406
- code: ts.factory.createStringLiteral('invalid_enum_value'),
1407
- options: ts.factory.createArrayLiteralExpression(values.map((value)=>typeof value === 'string' ? ts.factory.createStringLiteral(value) : ts.factory.createNumericLiteral(value))),
1408
- received: ctx.input
1409
- })),
1410
- ...ctx.status(standalone.ParseStatus.INVALID)
1411
- ])
1412
- ]))
1413
- ], true));
1414
- }
1415
- static{
1416
- _initClass$f();
1417
- }
1418
- }
1419
-
1420
- var _dec$e, _initClass$e, _AbstractCompiledType$e;
1421
- exports.ZcNever = void 0;
1422
- _dec$e = register(z__default.default.ZodFirstPartyTypeKind.ZodNever);
1423
- class ZcNever extends (_AbstractCompiledType$e = AbstractCompiledType) {
1424
- static{
1425
- ({ c: [exports.ZcNever, _initClass$e] } = _apply_decs_2203_r._(this, [], [
1426
- _dec$e
1427
- ], _AbstractCompiledType$e));
1428
- }
1429
- compileType() {
1430
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword);
1431
- }
1432
- *compileParser(ctx, path) {
1433
- yield* ctx.report(objectLiteral({
1434
- path: path.serialize(),
1435
- code: ts.factory.createStringLiteral('invalid_type'),
1436
- expected: ts.factory.createStringLiteral('never'),
1437
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
1438
- }));
1439
- yield* ctx.status(standalone.ParseStatus.INVALID);
1440
- }
1441
- static{
1442
- _initClass$e();
1443
- }
1444
- }
1445
-
1446
- var _dec$d, _initClass$d, _AbstractCompiledType$d;
1447
- exports.ZcNull = void 0;
1448
- _dec$d = register(z__default.default.ZodFirstPartyTypeKind.ZodNull);
1449
- class ZcNull extends (_AbstractCompiledType$d = AbstractCompiledType) {
1450
- static{
1451
- ({ c: [exports.ZcNull, _initClass$d] } = _apply_decs_2203_r._(this, [], [
1452
- _dec$d
1453
- ], _AbstractCompiledType$d));
1454
- }
1455
- compileType() {
1456
- return ts.factory.createLiteralTypeNode(ts.factory.createNull());
1457
- }
1458
- *compileParser(ctx, path) {
1459
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ctx.input, ts.factory.createNull()), ts.factory.createBlock([
1460
- ...ctx.report(objectLiteral({
1461
- path: path.serialize(),
1462
- code: ts.factory.createStringLiteral('invalid_type'),
1463
- expected: ts.factory.createStringLiteral('null'),
1464
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
1465
- })),
1466
- ...ctx.status(standalone.ParseStatus.INVALID)
1467
- ], true));
1468
- yield* ctx.outputs(ctx.input);
1469
- }
1470
- static{
1471
- _initClass$d();
1472
- }
1473
- }
1474
-
1475
- var _dec$c, _initClass$c, _AbstractCompiledType$c;
1476
- exports.ZcNullable = void 0;
1477
- _dec$c = register(z__default.default.ZodFirstPartyTypeKind.ZodNullable);
1478
- class ZcNullable extends (_AbstractCompiledType$c = AbstractCompiledType) {
1479
- static{
1480
- ({ c: [exports.ZcNullable, _initClass$c] } = _apply_decs_2203_r._(this, [], [
1481
- _dec$c
1482
- ], _AbstractCompiledType$c));
1483
- }
1484
- compileType() {
1485
- return ts.factory.createUnionTypeNode([
1486
- compilable(this.type._def.innerType).compileType(),
1487
- ts.factory.createLiteralTypeNode(ts.factory.createNull())
1488
- ]);
1489
- }
1490
- *compileParser(ctx, path) {
1491
- yield ts.factory.createIfStatement(ts.factory.createStrictEquality(ctx.input, ts.factory.createNull()), ts.factory.createBlock([
1492
- ...ctx.outputs(ts.factory.createNull())
1493
- ], true), ts.factory.createBlock([
1494
- ...compilable(this.type._def.innerType).compileParser(ctx, path)
1495
- ], true));
1496
- }
1497
- static{
1498
- _initClass$c();
1499
- }
1500
- }
1501
-
1502
- var _dec$b, _initClass$b, _AbstractCompiledType$b;
1503
- exports.ZcNumber = void 0;
1504
- _dec$b = register(z__default.default.ZodFirstPartyTypeKind.ZodNumber);
1505
- class ZcNumber extends (_AbstractCompiledType$b = AbstractCompiledType) {
1506
- static{
1507
- ({ c: [exports.ZcNumber, _initClass$b] } = _apply_decs_2203_r._(this, [], [
1508
- _dec$b
1509
- ], _AbstractCompiledType$b));
1510
- }
1511
- compileType() {
1512
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
1513
- }
1514
- *compileParser(ctx, path) {
1515
- let input = ctx.input;
1516
- if (this.type._def.coerce) {
1517
- const ident = uniqueIdentifier('coercedInput');
1518
- yield local(ident, ts.factory.createCallExpression(ts.factory.createIdentifier('Number'), [], [
1519
- ctx.input
1520
- ]), false);
1521
- input = ident;
1522
- }
1523
- let stmt = ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(input), ts.factory.createStringLiteral('number')), ts.factory.createBlock([
1524
- ...ctx.report(objectLiteral({
1525
- path: path.serialize(),
1526
- code: ts.factory.createStringLiteral('invalid_type'),
1527
- expected: ts.factory.createStringLiteral('number'),
1528
- received: callHelper(ctx.verifierContext, 'typeOf', input)
1529
- }), input),
1530
- ...ctx.status(standalone.ParseStatus.INVALID)
1531
- ], true));
1532
- const checkStmts = [];
1533
- for (const check of this.type._def.checks){
1534
- switch(check.kind){
1535
- case 'min':
1536
- checkStmts.push(ts.factory.createIfStatement(ts.factory[check.inclusive ? 'createLessThan' : 'createLessThanEquals'](input, ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
1537
- ...ctx.report(objectLiteral({
1538
- path: path.serialize(),
1539
- code: ts.factory.createStringLiteral('too_small'),
1540
- minimum: ts.factory.createNumericLiteral(check.value),
1541
- type: ts.factory.createStringLiteral('number'),
1542
- inclusive: check.inclusive ? ts.factory.createTrue() : ts.factory.createFalse(),
1543
- exact: ts.factory.createFalse(),
1544
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1545
- }), input),
1546
- ...ctx.status(standalone.ParseStatus.DIRTY)
1547
- ], true)));
1548
- break;
1549
- case 'max':
1550
- checkStmts.push(ts.factory.createIfStatement(ts.factory[check.inclusive ? 'createGreaterThan' : 'createGreaterThanEquals'](input, ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
1551
- ...ctx.report(objectLiteral({
1552
- path: path.serialize(),
1553
- code: ts.factory.createStringLiteral('too_big'),
1554
- minimum: ts.factory.createNumericLiteral(check.value),
1555
- type: ts.factory.createStringLiteral('number'),
1556
- inclusive: check.inclusive ? ts.factory.createTrue() : ts.factory.createFalse(),
1557
- exact: ts.factory.createFalse(),
1558
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1559
- }), input),
1560
- ...ctx.status(standalone.ParseStatus.DIRTY)
1561
- ], true)));
1562
- break;
1563
- case 'int':
1564
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Number'), 'isInteger'), undefined, [
1565
- input
1566
- ])), ts.factory.createBlock([
1567
- ...ctx.report(objectLiteral({
1568
- path: path.serialize(),
1569
- code: ts.factory.createStringLiteral('invalid_type'),
1570
- expected: ts.factory.createStringLiteral('integer'),
1571
- received: ts.factory.createStringLiteral('float'),
1572
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1573
- }), input),
1574
- ...ctx.status(standalone.ParseStatus.DIRTY)
1575
- ], true)));
1576
- break;
1577
- case 'finite':
1578
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Number'), 'isFinite'), undefined, [
1579
- input
1580
- ])), ts.factory.createBlock([
1581
- ...ctx.report(objectLiteral({
1582
- path: path.serialize(),
1583
- code: ts.factory.createStringLiteral('not_finite'),
1584
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1585
- }), input),
1586
- ...ctx.status(standalone.ParseStatus.DIRTY)
1587
- ], true)));
1588
- break;
1589
- case 'multipleOf':
1590
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createStrictInequality(callHelper(ctx.verifierContext, 'floatSafeRemainder', input, ts.factory.createNumericLiteral(check.value)), ts.factory.createNumericLiteral(0)), ts.factory.createBlock([
1591
- ...ctx.report(objectLiteral({
1592
- path: path.serialize(),
1593
- code: ts.factory.createStringLiteral('not_multiple_of'),
1594
- multipleOf: ts.factory.createNumericLiteral(check.value),
1595
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1596
- }), input),
1597
- ...ctx.status(standalone.ParseStatus.DIRTY)
1598
- ], true)));
1599
- break;
1600
- default:
1601
- throw new Error(`Unsupported ZcNumber check: ${check}`);
1602
- }
1603
- }
1604
- if (checkStmts.length === 0) {
1605
- yield stmt;
1606
- } else {
1607
- stmt = ts.factory.updateIfStatement(stmt, stmt.expression, stmt.thenStatement, ts.factory.createBlock([
1608
- ...checkStmts
1609
- ], true));
1610
- yield stmt;
1611
- }
1612
- yield* ctx.outputs(input);
1613
- }
1614
- static{
1615
- _initClass$b();
1616
- }
1617
- }
1618
-
1619
- var _dec$a, _initClass$a, _AbstractCompiledType$a;
1620
- exports.ZcOptional = void 0;
1621
- _dec$a = register(z__default.default.ZodFirstPartyTypeKind.ZodOptional);
1622
- class ZcOptional extends (_AbstractCompiledType$a = AbstractCompiledType) {
1623
- static{
1624
- ({ c: [exports.ZcOptional, _initClass$a] } = _apply_decs_2203_r._(this, [], [
1625
- _dec$a
1626
- ], _AbstractCompiledType$a));
1627
- }
1628
- compileType() {
1629
- return ts.factory.createUnionTypeNode([
1630
- compilable(this.type._def.innerType).compileType(),
1631
- ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword)
1632
- ]);
1633
- }
1634
- *compileParser(ctx, path) {
1635
- yield ts.factory.createIfStatement(ts.factory.createStrictEquality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('undefined')), ts.factory.createBlock([
1636
- ...ctx.outputs(ts.factory.createIdentifier('undefined'))
1637
- ], true), ts.factory.createBlock([
1638
- ...compilable(this.type._def.innerType).compileParser(ctx, path)
1639
- ], true));
1640
- }
1641
- static{
1642
- _initClass$a();
1643
- }
1644
- }
1645
-
1646
- var _dec$9, _initClass$9, _AbstractCompiledType$9;
1647
- class ReadonlyGeneratorContext {
1648
- constructor(ctx){
1649
- this.ctx = ctx;
1650
- }
1651
- get dependencies() {
1652
- return this.ctx.dependencies;
1653
- }
1654
- get input() {
1655
- return this.ctx.input;
1656
- }
1657
- get verifierContext() {
1658
- return this.ctx.verifierContext;
1659
- }
1660
- withInput(expr) {
1661
- return new ReadonlyGeneratorContext(this.ctx.withInput(expr));
1662
- }
1663
- prelude() {
1664
- return this.ctx.prelude();
1665
- }
1666
- postlude() {
1667
- return this.ctx.postlude();
1668
- }
1669
- report(expr) {
1670
- return this.ctx.report(expr);
1671
- }
1672
- status(status, allowShortCircuiting) {
1673
- return this.ctx.status(status, allowShortCircuiting);
1674
- }
1675
- *outputs(expr) {
1676
- yield* this.ctx.outputs(ts.factory.createCallExpression(propertyChain(ts.factory.createIdentifier('Object'), [
1677
- 'freeze'
1678
- ]), undefined, [
1679
- expr
1680
- ]));
1681
- }
1682
- }
1683
- exports.ZcReadonly = void 0;
1684
- _dec$9 = register(z__default.default.ZodFirstPartyTypeKind.ZodReadonly);
1685
- class ZcReadonly extends (_AbstractCompiledType$9 = AbstractCompiledType) {
1686
- static{
1687
- ({ c: [exports.ZcReadonly, _initClass$9] } = _apply_decs_2203_r._(this, [], [
1688
- _dec$9
1689
- ], _AbstractCompiledType$9));
1690
- }
1691
- compileType() {
1692
- let innerType = compilable(this.type._def.innerType).compileType();
1693
- if (ts__default.default.isArrayTypeNode(innerType)) {
1694
- return ts.factory.createTypeOperatorNode(ts__default.default.SyntaxKind.ReadonlyKeyword, innerType);
1695
- } else if (ts__default.default.isObjectLiteralExpression(innerType) || ts__default.default.isUnionTypeNode(innerType) || ts__default.default.isIntersectionTypeNode(innerType)) {
1696
- return ts.factory.createTypeReferenceNode('Readonly', [
1697
- innerType
1698
- ]);
1699
- } else if (ts__default.default.isTypeReferenceNode(innerType) && ts__default.default.isIdentifier(innerType.typeName)) {
1700
- switch(innerType.typeName.text){
1701
- case 'Map':
1702
- case 'Set':
1703
- innerType = ts.factory.updateTypeReferenceNode(innerType, ts.factory.createIdentifier(`Readonly${innerType.typeName.text}`), innerType.typeArguments);
1704
- break;
1705
- case 'Record':
1706
- innerType = ts.factory.createTypeReferenceNode('Readonly', [
1707
- innerType
1708
- ]);
1709
- break;
1710
- }
1711
- }
1712
- return innerType;
1713
- }
1714
- *compileParser(ctx, path) {
1715
- yield* compilable(this.type._def.innerType).compileParser(new ReadonlyGeneratorContext(ctx), path);
1716
- }
1717
- static{
1718
- _initClass$9();
1719
- }
1720
- }
1721
-
1722
- var _dec$8, _initClass$8, _AbstractCompiledType$8;
1723
- exports.ZcRecord = void 0;
1724
- _dec$8 = register(z__default.default.ZodFirstPartyTypeKind.ZodRecord);
1725
- class ZcRecord extends (_AbstractCompiledType$8 = AbstractCompiledType) {
1726
- static{
1727
- ({ c: [exports.ZcRecord, _initClass$8] } = _apply_decs_2203_r._(this, [], [
1728
- _dec$8
1729
- ], _AbstractCompiledType$8));
1730
- }
1731
- compileType() {
1732
- const [k, v] = [
1733
- compilable(this.type._def.keyType),
1734
- compilable(this.type._def.valueType)
1735
- ];
1736
- return ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Record'), [
1737
- k.compileType(),
1738
- v.compileType()
1739
- ]);
1740
- }
1741
- *compileParser(ctx, path) {
1742
- const typeIdentifier = uniqueIdentifier('type');
1743
- yield local(typeIdentifier, callHelper(ctx.verifierContext, 'typeOf', ctx.input));
1744
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(typeIdentifier, ts.factory.createStringLiteral('object')), ts.factory.createBlock([
1745
- ...ctx.report(objectLiteral({
1746
- path: path.serialize(),
1747
- code: ts.factory.createStringLiteral('invalid_type'),
1748
- expected: ts.factory.createStringLiteral('object'),
1749
- received: typeIdentifier
1750
- })),
1751
- ...ctx.status(standalone.ParseStatus.INVALID)
1752
- ], true));
1753
- const output = uniqueIdentifier('outputObject');
1754
- yield local(output, ts.factory.createObjectLiteralExpression());
1755
- const keyLabel = uniqueIdentifier('parseKey');
1756
- const keyInput = uniqueIdentifier('key');
1757
- const keyOutput = uniqueIdentifier('keyOut');
1758
- const valueLabel = uniqueIdentifier('parseValue');
1759
- const valueInput = uniqueIdentifier('value');
1760
- const valueOutput = uniqueIdentifier('valueOut');
1761
- const keyCtx = new ObjectGeneratorContext(keyLabel, keyInput, keyOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1762
- const valueCtx = new ObjectGeneratorContext(valueLabel, valueInput, valueOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1763
- yield ts.factory.createForInStatement(ts.factory.createVariableDeclarationList([
1764
- ts.factory.createVariableDeclaration(keyInput)
1765
- ], ts.NodeFlags.Const), ctx.input, ts.factory.createBlock([
1766
- local(keyOutput, keyInput, true),
1767
- local(valueInput, ts.factory.createElementAccessExpression(ctx.input, keyInput)),
1768
- local(valueOutput, undefined, true),
1769
- ts.factory.createLabeledStatement(keyLabel, ts.factory.createBlock([
1770
- ...compilable(this.type._def.keyType).compileParser(keyCtx, path.push(keyInput))
1771
- ])),
1772
- ts.factory.createLabeledStatement(valueLabel, ts.factory.createBlock([
1773
- ...compilable(this.type._def.valueType).compileParser(valueCtx, path.push(keyOutput))
1774
- ])),
1775
- ts.factory.createExpressionStatement(ts.factory.createAssignment(ts.factory.createElementAccessExpression(output, keyOutput), valueOutput))
1776
- ]));
1777
- yield* ctx.outputs(output);
1778
- }
1779
- static{
1780
- _initClass$8();
1781
- }
1782
- }
1783
-
1784
- var _dec$7, _initClass$7, _AbstractCompiledType$7;
1785
- exports.ZcSet = void 0;
1786
- _dec$7 = register(z__default.default.ZodFirstPartyTypeKind.ZodSet);
1787
- class ZcSet extends (_AbstractCompiledType$7 = AbstractCompiledType) {
1788
- static{
1789
- ({ c: [exports.ZcSet, _initClass$7] } = _apply_decs_2203_r._(this, [], [
1790
- _dec$7
1791
- ], _AbstractCompiledType$7));
1792
- }
1793
- compileType() {
1794
- return ts.factory.createTypeReferenceNode('Set', [
1795
- compilable(this.type._def.valueType).compileType()
1796
- ]);
1797
- }
1798
- *compileParser(ctx, path) {
1799
- const typeIdentifier = uniqueIdentifier('type');
1800
- yield local(typeIdentifier, callHelper(ctx.verifierContext, 'typeOf', ctx.input));
1801
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(typeIdentifier, ts.factory.createStringLiteral('set')), ts.factory.createBlock([
1802
- ...ctx.report(objectLiteral({
1803
- path: path.serialize(),
1804
- code: ts.factory.createStringLiteral('invalid_type'),
1805
- expected: ts.factory.createStringLiteral('set'),
1806
- received: typeIdentifier
1807
- })),
1808
- ...ctx.status(standalone.ParseStatus.INVALID)
1809
- ], true));
1810
- if (this.type._def.minSize !== null) {
1811
- yield ts.factory.createIfStatement(ts.factory.createLessThan(ts.factory.createPropertyAccessExpression(ctx.input, 'size'), ts.factory.createNumericLiteral(this.type._def.minSize.value)), ts.factory.createBlock([
1812
- ...ctx.report(objectLiteral({
1813
- path: path.serialize(),
1814
- code: ts.factory.createStringLiteral('too_small'),
1815
- minimum: ts.factory.createNumericLiteral(this.type._def.minSize.value),
1816
- type: ts.factory.createStringLiteral('set'),
1817
- inclusive: ts.factory.createTrue(),
1818
- exact: ts.factory.createFalse(),
1819
- message: this.type._def.minSize.message ? ts.factory.createStringLiteral(this.type._def.minSize.message) : undefined
1820
- })),
1821
- ...ctx.status(standalone.ParseStatus.DIRTY)
1822
- ], true));
1823
- }
1824
- if (this.type._def.maxSize !== null) {
1825
- yield ts.factory.createIfStatement(ts.factory.createGreaterThan(ts.factory.createPropertyAccessExpression(ctx.input, 'size'), ts.factory.createNumericLiteral(this.type._def.maxSize.value)), ts.factory.createBlock([
1826
- ...ctx.report(objectLiteral({
1827
- path: path.serialize(),
1828
- code: ts.factory.createStringLiteral('too_big'),
1829
- minimum: ts.factory.createNumericLiteral(this.type._def.maxSize.value),
1830
- type: ts.factory.createStringLiteral('set'),
1831
- inclusive: ts.factory.createTrue(),
1832
- exact: ts.factory.createFalse(),
1833
- message: this.type._def.maxSize.message ? ts.factory.createStringLiteral(this.type._def.maxSize.message) : undefined
1834
- })),
1835
- ...ctx.status(standalone.ParseStatus.DIRTY)
1836
- ], true));
1837
- }
1838
- const output = uniqueIdentifier('outputSet');
1839
- yield local(output, ts.factory.createNewExpression(ts.factory.createIdentifier('Set'), undefined, []));
1840
- const indexInitializer = ts.factory.createLoopVariable(true);
1841
- const elInitializer = uniqueIdentifier('el');
1842
- const elOutput = uniqueIdentifier('elOut');
1843
- const childCtx = new ArrayGeneratorContext(elInitializer, elOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
1844
- yield ts.factory.createForOfStatement(undefined, ts.factory.createVariableDeclarationList([
1845
- ts.factory.createVariableDeclaration(ts.factory.createArrayBindingPattern([
1846
- ts.factory.createBindingElement(undefined, undefined, indexInitializer),
1847
- ts.factory.createBindingElement(undefined, undefined, elInitializer)
1848
- ]))
1849
- ], ts.NodeFlags.Const), ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createArrayLiteralExpression([
1850
- ts.factory.createSpreadElement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ctx.input, 'values'), undefined, []))
1851
- ]), 'entries'), undefined, undefined), ts.factory.createBlock([
1852
- ...childCtx.prelude(),
1853
- local(elOutput, undefined, true),
1854
- ...compilable(this.type._def.valueType).compileParser(childCtx, path.push(indexInitializer)),
1855
- ...childCtx.postlude(),
1856
- ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(output, 'add'), undefined, [
1857
- elOutput
1858
- ]))
1859
- ]));
1860
- yield* ctx.outputs(output);
1861
- }
1862
- static{
1863
- _initClass$7();
1864
- }
1865
- }
1866
-
1867
- const DATE = /^((\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])))$/;
1868
-
1869
- var _dec$6, _initClass$6, _AbstractCompiledType$6;
1870
- function timeRegexSource(args) {
1871
- let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
1872
- if (args.precision) {
1873
- regex = `${regex}\\.\\d{${args.precision}}`;
1874
- } else if (args.precision == null) {
1875
- regex = `${regex}(\\.\\d+)?`;
1876
- }
1877
- return regex;
1878
- }
1879
- function createTimeRegex(args) {
1880
- return `/^${timeRegexSource(args)}$/`;
1881
- }
1882
- function createDatetimeRegex(args) {
1883
- let regex = `${DATE.source.slice(1, -1)}T${timeRegexSource(args)}`;
1884
- const opts = [];
1885
- opts.push(args.local ? 'Z?' : 'Z');
1886
- if (args.offset) {
1887
- opts.push(`([+-]\\d{2}:?\\d{2})`);
1888
- }
1889
- regex = `${regex}(${opts.join('|')})`;
1890
- return `/^${regex}$/`;
1891
- }
1892
- exports.ZcString = void 0;
1893
- _dec$6 = register(z__default.default.ZodFirstPartyTypeKind.ZodString);
1894
- new class extends _identity._ {
1895
- constructor(){
1896
- super(exports.ZcString), _initClass$6();
1897
- }
1898
- static{
1899
- class ZcString extends (_AbstractCompiledType$6 = AbstractCompiledType) {
1900
- static{
1901
- ({ c: [exports.ZcString, _initClass$6] } = _apply_decs_2203_r._(this, [], [
1902
- _dec$6
1903
- ], _AbstractCompiledType$6));
1904
- }
1905
- compileType() {
1906
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
1907
- }
1908
- *compileParser(ctx, path) {
1909
- let input = ctx.input;
1910
- const hasModifyingChecks = this.type._def.checks.some((a)=>a.kind === 'toLowerCase' || a.kind === 'toUpperCase' || a.kind === 'trim');
1911
- if (this.type._def.coerce) {
1912
- const ident = uniqueIdentifier('coercedInput');
1913
- yield local(ident, ts.factory.createCallExpression(ts.factory.createIdentifier('String'), [], [
1914
- ctx.input
1915
- ]), hasModifyingChecks);
1916
- input = ident;
1917
- } else if (hasModifyingChecks) {
1918
- const ident = uniqueIdentifier('input');
1919
- yield local(ident, input, true);
1920
- input = ident;
1921
- }
1922
- let stmt = ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(input), ts.factory.createStringLiteral('string')), ts.factory.createBlock([
1923
- ...ctx.report(objectLiteral({
1924
- path: path.serialize(),
1925
- code: ts.factory.createStringLiteral('invalid_type'),
1926
- expected: ts.factory.createStringLiteral('string'),
1927
- received: callHelper(ctx.verifierContext, 'typeOf', input)
1928
- }), input),
1929
- ...ctx.status(standalone.ParseStatus.INVALID)
1930
- ], true));
1931
- const checkStmts = [];
1932
- for (const check of this.type._def.checks){
1933
- switch(check.kind){
1934
- case 'min':
1935
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLessThan(propertyChain(input, [
1936
- 'length'
1937
- ]), ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
1938
- ...ctx.report(objectLiteral({
1939
- path: path.serialize(),
1940
- code: ts.factory.createStringLiteral('too_small'),
1941
- minimum: ts.factory.createNumericLiteral(check.value),
1942
- type: ts.factory.createStringLiteral('string'),
1943
- inclusive: ts.factory.createTrue(),
1944
- exact: ts.factory.createFalse(),
1945
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1946
- }), input),
1947
- ...ctx.status(standalone.ParseStatus.DIRTY)
1948
- ], true)));
1949
- break;
1950
- case 'max':
1951
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createGreaterThan(propertyChain(input, [
1952
- 'length'
1953
- ]), ts.factory.createNumericLiteral(check.value)), ts.factory.createBlock([
1954
- ...ctx.report(objectLiteral({
1955
- path: path.serialize(),
1956
- code: ts.factory.createStringLiteral('too_big'),
1957
- minimum: ts.factory.createNumericLiteral(check.value),
1958
- type: ts.factory.createStringLiteral('string'),
1959
- inclusive: ts.factory.createTrue(),
1960
- exact: ts.factory.createFalse(),
1961
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1962
- }), input),
1963
- ...ctx.status(standalone.ParseStatus.DIRTY)
1964
- ], true)));
1965
- break;
1966
- case 'length':
1967
- const value = ts.factory.createNumericLiteral(check.value);
1968
- const lengthAccessor = ts.factory.createPropertyAccessExpression(ctx.input, 'length');
1969
- const tooBigIdent = uniqueIdentifier('tooBig');
1970
- const tooSmallIdent = uniqueIdentifier('tooSmall');
1971
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createStrictInequality(lengthAccessor, value), ts.factory.createBlock([
1972
- local(tooBigIdent, ts.factory.createGreaterThan(lengthAccessor, value)),
1973
- local(tooSmallIdent, ts.factory.createLessThan(lengthAccessor, value)),
1974
- ...ctx.report(objectLiteral({
1975
- path: path.serialize(),
1976
- code: ts.factory.createConditionalExpression(tooBigIdent, undefined, ts.factory.createStringLiteral('too_big'), undefined, ts.factory.createStringLiteral('too_small')),
1977
- minimum: ts.factory.createConditionalExpression(tooSmallIdent, undefined, value, undefined, ts.factory.createIdentifier('undefined')),
1978
- maximum: ts.factory.createConditionalExpression(tooBigIdent, undefined, value, undefined, ts.factory.createIdentifier('undefined')),
1979
- type: ts.factory.createStringLiteral('string'),
1980
- inclusive: ts.factory.createTrue(),
1981
- exact: ts.factory.createTrue(),
1982
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
1983
- }), input),
1984
- ...ctx.status(standalone.ParseStatus.DIRTY)
1985
- ], true)));
1986
- break;
1987
- case 'email':
1988
- case 'emoji':
1989
- case 'uuid':
1990
- case 'nanoid':
1991
- case 'cuid':
1992
- case 'cuid2':
1993
- case 'ulid':
1994
- case 'date':
1995
- case 'duration':
1996
- case 'base64':
1997
- case 'base64url':
1998
- checkStmts.push(ZcString._basicCheck(ctx, path, input, check.kind.toUpperCase(), check.kind, check.message));
1999
- break;
2000
- case 'regex':
2001
- checkStmts.push(ZcString._basicCheck(ctx, path, input, ts.factory.createRegularExpressionLiteral(check.regex.toString()), check.kind, check.message));
2002
- break;
2003
- case 'datetime':
2004
- {
2005
- const regex = ts.factory.createRegularExpressionLiteral(createDatetimeRegex(check));
2006
- checkStmts.push(ZcString._basicCheck(ctx, path, input, regex, check.kind, check.message));
2007
- break;
2008
- }
2009
- case 'time':
2010
- {
2011
- const regex = ts.factory.createRegularExpressionLiteral(createTimeRegex(check));
2012
- checkStmts.push(ZcString._basicCheck(ctx, path, input, regex, check.kind, check.message));
2013
- break;
2014
- }
2015
- case 'ip':
2016
- case 'cidr':
2017
- {
2018
- let cidrSuffix = check.kind === 'cidr' ? '_CIDR' : '';
2019
- if (!check.version) {
2020
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalAnd(ts.factory.createLogicalNot(ts.factory.createCallExpression(propertyChain(ctx.verifierContext, [
2021
- 'regex',
2022
- `IPV4${cidrSuffix}`,
2023
- 'test'
2024
- ]), undefined, [
2025
- input
2026
- ])), ts.factory.createLogicalNot(ts.factory.createCallExpression(propertyChain(ctx.verifierContext, [
2027
- 'regex',
2028
- `IPV6${cidrSuffix}`,
2029
- 'test'
2030
- ]), undefined, [
2031
- input
2032
- ]))), ts.factory.createBlock([
2033
- ...ctx.report(objectLiteral({
2034
- path: path.serialize(),
2035
- code: ts.factory.createStringLiteral('invalid_string'),
2036
- validation: ts.factory.createStringLiteral(check.kind),
2037
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
2038
- }), input),
2039
- ...ctx.status(standalone.ParseStatus.DIRTY)
2040
- ], true)));
2041
- } else {
2042
- checkStmts.push(ZcString._basicCheck(ctx, path, input, `IP${check.version.toUpperCase()}${cidrSuffix}`, check.kind, check.message));
2043
- }
2044
- break;
2045
- }
2046
- case 'url':
2047
- checkStmts.push(ts.factory.createTryStatement(ts.factory.createBlock([
2048
- ts.factory.createExpressionStatement(ts.factory.createNewExpression(ts.factory.createIdentifier('URL'), undefined, [
2049
- input
2050
- ]))
2051
- ], true), ts.factory.createCatchClause(uniqueIdentifier('e'), ts.factory.createBlock([
2052
- ...ctx.report(objectLiteral({
2053
- path: path.serialize(),
2054
- code: ts.factory.createStringLiteral('invalid_string'),
2055
- validation: ts.factory.createStringLiteral('url'),
2056
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
2057
- }), input),
2058
- ...ctx.status(standalone.ParseStatus.DIRTY)
2059
- ], true)), undefined));
2060
- break;
2061
- case 'jwt':
2062
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalNot(callHelper(ctx.verifierContext, 'isValidJWT', input, check.alg ? ts.factory.createStringLiteral(check.alg) : ts.factory.createIdentifier('undefined'))), ts.factory.createBlock([
2063
- ...ctx.report(objectLiteral({
2064
- path: path.serialize(),
2065
- code: ts.factory.createStringLiteral('invalid_string'),
2066
- validation: ts.factory.createStringLiteral('url'),
2067
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
2068
- }), input),
2069
- ...ctx.status(standalone.ParseStatus.DIRTY)
2070
- ], true)));
2071
- break;
2072
- case 'startsWith':
2073
- case 'endsWith':
2074
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(input, check.kind), undefined, [
2075
- ts.factory.createStringLiteral(check.value)
2076
- ])), ts.factory.createBlock([
2077
- ...ctx.report(objectLiteral({
2078
- path: path.serialize(),
2079
- code: ts.factory.createStringLiteral('invalid_string'),
2080
- validation: objectLiteral({
2081
- [check.kind]: ts.factory.createStringLiteral(check.value)
2082
- }),
2083
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
2084
- }), input),
2085
- ...ctx.status(standalone.ParseStatus.DIRTY)
2086
- ], true)));
2087
- break;
2088
- case 'includes':
2089
- checkStmts.push(ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(input, 'includes'), undefined, [
2090
- ts.factory.createStringLiteral(check.value),
2091
- check.position ? ts.factory.createNumericLiteral(check.position) : ts.factory.createIdentifier('undefined')
2092
- ])), ts.factory.createBlock([
2093
- ...ctx.report(objectLiteral({
2094
- path: path.serialize(),
2095
- code: ts.factory.createStringLiteral('invalid_string'),
2096
- validation: objectLiteral({
2097
- includes: ts.factory.createStringLiteral(check.value),
2098
- position: check.position ? ts.factory.createNumericLiteral(check.position) : undefined
2099
- }),
2100
- message: check.message ? ts.factory.createStringLiteral(check.message) : undefined
2101
- }), input),
2102
- ...ctx.status(standalone.ParseStatus.DIRTY)
2103
- ], true)));
2104
- break;
2105
- case 'trim':
2106
- case 'toLowerCase':
2107
- case 'toUpperCase':
2108
- {
2109
- checkStmts.push(ts.factory.createExpressionStatement(ts.factory.createAssignment(input, ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(input, check.kind), undefined, []))));
2110
- break;
2111
- }
2112
- default:
2113
- throw new Error(`Unsupported ZcString check: ${check}`);
2114
- }
2115
- }
2116
- if (checkStmts.length === 0) {
2117
- yield stmt;
2118
- } else {
2119
- stmt = ts.factory.updateIfStatement(stmt, stmt.expression, stmt.thenStatement, ts.factory.createBlock([
2120
- ...checkStmts
2121
- ], true));
2122
- yield stmt;
2123
- }
2124
- yield* ctx.outputs(input);
2125
- }
2126
- static _basicCheck(ctx, path, input, regexOrTester, checkKind, message) {
2127
- const testFunction = typeof regexOrTester === 'string' ? propertyChain(ctx.verifierContext, [
2128
- 'regex',
2129
- regexOrTester,
2130
- 'test'
2131
- ]) : ts__default.default.isRegularExpressionLiteral(regexOrTester) ? ts.factory.createPropertyAccessExpression(regexOrTester, 'test') : regexOrTester;
2132
- return ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(testFunction, undefined, [
2133
- input
2134
- ])), ts.factory.createBlock([
2135
- ...ctx.report(objectLiteral({
2136
- path: path.serialize(),
2137
- code: ts.factory.createStringLiteral('invalid_string'),
2138
- validation: ts.factory.createStringLiteral(checkKind),
2139
- message: message ? ts.factory.createStringLiteral(message) : undefined
2140
- }), input),
2141
- ...ctx.status(standalone.ParseStatus.DIRTY)
2142
- ], true));
2143
- }
2144
- }
2145
- }
2146
- }();
2147
-
2148
- var _dec$5, _initClass$5, _AbstractCompiledType$5;
2149
- exports.ZcSymbol = void 0;
2150
- _dec$5 = register(z__default.default.ZodFirstPartyTypeKind.ZodSymbol);
2151
- class ZcSymbol extends (_AbstractCompiledType$5 = AbstractCompiledType) {
2152
- static{
2153
- ({ c: [exports.ZcSymbol, _initClass$5] } = _apply_decs_2203_r._(this, [], [
2154
- _dec$5
2155
- ], _AbstractCompiledType$5));
2156
- }
2157
- compileType() {
2158
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.SymbolKeyword);
2159
- }
2160
- *compileParser(ctx, path) {
2161
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('symbol')), ts.factory.createBlock([
2162
- ...ctx.report(objectLiteral({
2163
- path: path.serialize(),
2164
- code: ts.factory.createStringLiteral('invalid_type'),
2165
- expected: ts.factory.createStringLiteral('symbol'),
2166
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
2167
- })),
2168
- ...ctx.status(standalone.ParseStatus.INVALID)
2169
- ], true));
2170
- yield* ctx.outputs(ctx.input);
2171
- }
2172
- static{
2173
- _initClass$5();
2174
- }
2175
- }
2176
-
2177
- var _dec$4, _initClass$4, _AbstractCompiledType$4;
2178
- exports.ZcTuple = void 0;
2179
- _dec$4 = register(z__default.default.ZodFirstPartyTypeKind.ZodTuple);
2180
- class ZcTuple extends (_AbstractCompiledType$4 = AbstractCompiledType) {
2181
- static{
2182
- ({ c: [exports.ZcTuple, _initClass$4] } = _apply_decs_2203_r._(this, [], [
2183
- _dec$4
2184
- ], _AbstractCompiledType$4));
2185
- }
2186
- compileType() {
2187
- const items = [];
2188
- for (const item of this.type._def.items){
2189
- items.push(compilable(item).compileType());
2190
- }
2191
- if (this.type._def.rest) {
2192
- items.push(ts.factory.createRestTypeNode(compilable(this.type._def.rest).compileType()));
2193
- }
2194
- return ts.factory.createTupleTypeNode(items);
2195
- }
2196
- *compileParser(ctx, path) {
2197
- yield ts.factory.createIfStatement(ts.factory.createLogicalNot(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier('Array'), 'isArray'), undefined, [
2198
- ctx.input
2199
- ])), ts.factory.createBlock([
2200
- ...ctx.report(objectLiteral({
2201
- path: path.serialize(),
2202
- code: ts.factory.createStringLiteral('invalid_type'),
2203
- expected: ts.factory.createStringLiteral('array'),
2204
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
2205
- })),
2206
- ...ctx.status(standalone.ParseStatus.INVALID)
2207
- ], true));
2208
- yield ts.factory.createIfStatement(ts.factory.createLessThan(ts.factory.createPropertyAccessExpression(ctx.input, 'length'), ts.factory.createNumericLiteral(this.type._def.items.length)), ts.factory.createBlock([
2209
- ...ctx.report(objectLiteral({
2210
- path: path.serialize(),
2211
- code: ts.factory.createStringLiteral('too_small'),
2212
- minimum: ts.factory.createNumericLiteral(this.type._def.items.length),
2213
- type: ts.factory.createStringLiteral('array'),
2214
- inclusive: ts.factory.createTrue(),
2215
- exact: ts.factory.createFalse()
2216
- })),
2217
- ...ctx.status(standalone.ParseStatus.INVALID)
2218
- ], true));
2219
- if (!this.type._def.rest) {
2220
- yield ts.factory.createIfStatement(ts.factory.createGreaterThan(ts.factory.createPropertyAccessExpression(ctx.input, 'length'), ts.factory.createNumericLiteral(this.type._def.items.length)), ts.factory.createBlock([
2221
- ...ctx.report(objectLiteral({
2222
- path: path.serialize(),
2223
- code: ts.factory.createStringLiteral('too_big'),
2224
- minimum: ts.factory.createNumericLiteral(this.type._def.items.length),
2225
- type: ts.factory.createStringLiteral('array'),
2226
- inclusive: ts.factory.createTrue(),
2227
- exact: ts.factory.createFalse()
2228
- })),
2229
- ...ctx.status(standalone.ParseStatus.DIRTY)
2230
- ], true));
2231
- }
2232
- const output = uniqueIdentifier('outputTuple');
2233
- yield local(output, ts.factory.createNewExpression(ts.factory.createIdentifier('Array'), undefined, [
2234
- ts.factory.createPropertyAccessExpression(ctx.input, 'length')
2235
- ]));
2236
- for (const [i, ty] of this.type._def.items.entries()){
2237
- const label = uniqueIdentifier(`item${i}`);
2238
- const inputExpr = propertyChain(ctx.input, [
2239
- i
2240
- ]);
2241
- const outputExpr = propertyChain(output, [
2242
- i
2243
- ]);
2244
- const childCtx = new ObjectGeneratorContext(label, inputExpr, outputExpr, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
2245
- yield ts.factory.createLabeledStatement(label, ts.factory.createBlock([
2246
- ...compilable(ty).compileParser(childCtx, path.push(i))
2247
- ]));
2248
- }
2249
- if (this.type._def.rest) {
2250
- const indexInitializer = ts.factory.createLoopVariable(true);
2251
- const elInitializer = uniqueIdentifier('el');
2252
- const elOutput = ts.factory.createElementAccessExpression(output, indexInitializer);
2253
- const childCtx = new ArrayGeneratorContext(elInitializer, elOutput, ctx.verifierContext, ctx.dependencies, (status)=>ctx.status(status, false));
2254
- yield ts.factory.createForStatement(local(indexInitializer, ts.factory.createNumericLiteral(this.type._def.items.length), true).declarationList, ts.factory.createLessThan(indexInitializer, ts.factory.createPropertyAccessExpression(ctx.input, 'length')), ts.factory.createPostfixIncrement(indexInitializer), ts.factory.createBlock([
2255
- local(elInitializer, ts.factory.createElementAccessExpression(ctx.input, indexInitializer)),
2256
- ...childCtx.prelude(),
2257
- ...compilable(this.type._def.rest).compileParser(childCtx, path.push(indexInitializer)),
2258
- ...childCtx.postlude()
2259
- ]));
2260
- }
2261
- yield* ctx.outputs(output);
2262
- }
2263
- static{
2264
- _initClass$4();
2265
- }
2266
- }
2267
-
2268
- var _dec$3, _initClass$3, _AbstractCompiledType$3;
2269
- exports.ZcUndefined = void 0;
2270
- _dec$3 = register(z__default.default.ZodFirstPartyTypeKind.ZodUndefined);
2271
- class ZcUndefined extends (_AbstractCompiledType$3 = AbstractCompiledType) {
2272
- static{
2273
- ({ c: [exports.ZcUndefined, _initClass$3] } = _apply_decs_2203_r._(this, [], [
2274
- _dec$3
2275
- ], _AbstractCompiledType$3));
2276
- }
2277
- compileType() {
2278
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
2279
- }
2280
- *compileParser(ctx, path) {
2281
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('undefined')), ts.factory.createBlock([
2282
- ...ctx.report(objectLiteral({
2283
- path: path.serialize(),
2284
- code: ts.factory.createStringLiteral('invalid_type'),
2285
- expected: ts.factory.createStringLiteral('undefined'),
2286
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
2287
- })),
2288
- ...ctx.status(standalone.ParseStatus.INVALID)
2289
- ], true));
2290
- yield* ctx.outputs(ctx.input);
2291
- }
2292
- static{
2293
- _initClass$3();
2294
- }
2295
- }
2296
-
2297
- var _dec$2, _initClass$2, _AbstractCompiledType$2;
2298
- class UnionGeneratorContext extends LabeledBlockScopeGeneratorContext {
2299
- constructor(label, input, output, verifierContext, dependencies, parent){
2300
- super(LabeledShortCircuitMode.Break, label, input, output, verifierContext, dependencies), this.parent = parent;
2301
- }
2302
- *postlude() {
2303
- yield ts.factory.createIfStatement(ts.factory.createStrictEquality(this.statusVar, ts.factory.createNumericLiteral(standalone.ParseStatus.VALID)), ts.factory.createBlock([
2304
- ...this.parent.ctx.outputs(this.output),
2305
- ts.factory.createBreakStatement(this.parent.label)
2306
- ], true), ts.factory.createIfStatement(ts.factory.createLogicalAnd(ts.factory.createStrictEquality(this.statusVar, ts.factory.createNumericLiteral(standalone.ParseStatus.DIRTY)), ts.factory.createLogicalNot(this.parent.dirtyResultVar)), ts.factory.createBlock([
2307
- ts.factory.createExpressionStatement(ts.factory.createAssignment(this.parent.dirtyResultVar, this.output)),
2308
- ts.factory.createExpressionStatement(ts.factory.createAssignment(this.parent.dirtyCtxVar, this.verifierContext))
2309
- ])));
2310
- // imho, this shouldn't be behind a condition, but it's how zod does it and I promise parity
2311
- const childIssues = ts.factory.createPropertyAccessExpression(this.verifierContext, 'issues');
2312
- yield ts.factory.createIfStatement(ts.factory.createPropertyAccessExpression(childIssues, 'length'), ts.factory.createBlock([
2313
- ts.factory.createExpressionStatement(ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(this.parent.issues, 'push'), undefined, [
2314
- childIssues
2315
- ]))
2316
- ], true));
2317
- }
2318
- withInput(expr) {
2319
- const x = new UnionGeneratorContext(this.label, expr, this.output, this.verifierContext, this.dependencies, this.parent);
2320
- x.statusVar = this.statusVar;
2321
- return x;
2322
- }
2323
- }
2324
- exports.ZcUnion = void 0;
2325
- _dec$2 = register(z__default.default.ZodFirstPartyTypeKind.ZodUnion);
2326
- class ZcUnion extends (_AbstractCompiledType$2 = AbstractCompiledType) {
2327
- static{
2328
- ({ c: [exports.ZcUnion, _initClass$2] } = _apply_decs_2203_r._(this, [], [
2329
- _dec$2
2330
- ], _AbstractCompiledType$2));
2331
- }
2332
- compileType() {
2333
- return ts.factory.createUnionTypeNode(this.type._def.options.map((ty)=>compilable(ty).compileType()));
2334
- }
2335
- *compileParser(ctx, path) {
2336
- const dirtyResultVar = uniqueIdentifier('unionDirtyResult');
2337
- const dirtyCtxVar = uniqueIdentifier('unionDirtyCtx');
2338
- const issuesVar = uniqueIdentifier('unionIssues');
2339
- yield local(dirtyResultVar, undefined, true);
2340
- yield local(dirtyCtxVar, undefined, true);
2341
- yield local(issuesVar, ts.factory.createArrayLiteralExpression(), false);
2342
- const mainLabel = uniqueIdentifier('union');
2343
- yield ts.factory.createLabeledStatement(mainLabel, ts.factory.createBlock([
2344
- ...this.type._def.options.flatMap((opt, i)=>{
2345
- function* generateBody() {
2346
- const ty = compilable(opt);
2347
- const variantLabel = uniqueIdentifier(`unionVariant_${i}`);
2348
- const tmpOutput = uniqueIdentifier('variantOutput');
2349
- const childVerifierCtxVar = uniqueIdentifier('variantCtx');
2350
- yield local(tmpOutput, undefined, true);
2351
- yield local(childVerifierCtxVar, ts.factory.createObjectLiteralExpression([
2352
- ts.factory.createSpreadAssignment(ctx.verifierContext),
2353
- ts.factory.createPropertyAssignment('issues', ts.factory.createArrayLiteralExpression())
2354
- ], true));
2355
- const childCtx = new UnionGeneratorContext(variantLabel, ctx.input, tmpOutput, childVerifierCtxVar, ctx.dependencies, {
2356
- ctx,
2357
- label: mainLabel,
2358
- dirtyCtxVar,
2359
- dirtyResultVar,
2360
- issues: issuesVar
2361
- });
2362
- yield* childCtx.prelude();
2363
- yield ts.factory.createLabeledStatement(variantLabel, ts.factory.createBlock([
2364
- ...ty.compileParser(childCtx, path)
2365
- ], true));
2366
- yield* childCtx.postlude();
2367
- }
2368
- return [
2369
- ...generateBody()
2370
- ];
2371
- }),
2372
- ts.factory.createIfStatement(dirtyCtxVar, ts.factory.createBlock([
2373
- ...ctx.report(ts.factory.createSpreadElement(ts.factory.createPropertyAccessExpression(dirtyCtxVar, 'issues'))),
2374
- ...ctx.status(standalone.ParseStatus.DIRTY)
2375
- ], true), ts.factory.createBlock([
2376
- ...ctx.report(objectLiteral({
2377
- path: path.serialize(),
2378
- code: ts.factory.createStringLiteral('invalid_union'),
2379
- unionErrors: ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(issuesVar, 'map'), undefined, [
2380
- ts.factory.createArrowFunction(undefined, undefined, [
2381
- ts.factory.createParameterDeclaration(undefined, undefined, 'issues')
2382
- ], undefined, undefined, ts.factory.createNewExpression(ts.factory.createPropertyAccessExpression(ctx.verifierContext, 'ZcError'), undefined, [
2383
- ts.factory.createIdentifier('issues')
2384
- ]))
2385
- ])
2386
- })),
2387
- ...ctx.status(standalone.ParseStatus.INVALID)
2388
- ], true))
2389
- ]));
2390
- }
2391
- static{
2392
- _initClass$2();
2393
- }
2394
- }
2395
-
2396
- var _dec$1, _initClass$1, _AbstractCompiledType$1;
2397
- exports.ZcUnknown = void 0;
2398
- _dec$1 = register(z__default.default.ZodFirstPartyTypeKind.ZodUnknown);
2399
- class ZcUnknown extends (_AbstractCompiledType$1 = AbstractCompiledType) {
2400
- static{
2401
- ({ c: [exports.ZcUnknown, _initClass$1] } = _apply_decs_2203_r._(this, [], [
2402
- _dec$1
2403
- ], _AbstractCompiledType$1));
2404
- }
2405
- compileType() {
2406
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
2407
- }
2408
- *compileParser(ctx, path) {
2409
- yield* ctx.outputs(ctx.input);
2410
- }
2411
- static{
2412
- _initClass$1();
2413
- }
2414
- }
2415
-
2416
- var _dec, _initClass, _AbstractCompiledType;
2417
- exports.ZcVoid = void 0;
2418
- _dec = register(z__default.default.ZodFirstPartyTypeKind.ZodVoid);
2419
- class ZcVoid extends (_AbstractCompiledType = AbstractCompiledType) {
2420
- static{
2421
- ({ c: [exports.ZcVoid, _initClass] } = _apply_decs_2203_r._(this, [], [
2422
- _dec
2423
- ], _AbstractCompiledType));
2424
- }
2425
- compileType() {
2426
- return ts.factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword);
2427
- }
2428
- *compileParser(ctx, path) {
2429
- yield ts.factory.createIfStatement(ts.factory.createStrictInequality(ts.factory.createTypeOfExpression(ctx.input), ts.factory.createStringLiteral('undefined')), ts.factory.createBlock([
2430
- ...ctx.report(objectLiteral({
2431
- path: path.serialize(),
2432
- code: ts.factory.createStringLiteral('invalid_type'),
2433
- expected: ts.factory.createStringLiteral('undefined'),
2434
- received: callHelper(ctx.verifierContext, 'typeOf', ctx.input)
2435
- })),
2436
- ...ctx.status(standalone.ParseStatus.INVALID)
2437
- ], true));
2438
- yield* ctx.outputs(ctx.input);
2439
- }
2440
- static{
2441
- _initClass();
2442
- }
2443
- }
2444
-
2445
- function compile(schema, options) {
2446
- const type = compilable(schema);
2447
- const verifierContext = ts.factory.createIdentifier('ctx');
2448
- const dependencies = new Dependencies(verifierContext, options?.inlining ?? InliningMode.Default);
2449
- const ctx = new FunctionalGeneratorContext(ts.factory.createIdentifier('input'), propertyChain(ts.factory.createIdentifier('ctx'), [
2450
- 'output'
2451
- ]), verifierContext, dependencies);
2452
- const functionBody = [
2453
- ...ctx.prelude(),
2454
- ...type.compileParser(ctx, Path.empty()),
2455
- ...ctx.postlude()
2456
- ];
2457
- if (options?.standalone) {
2458
- return {
2459
- source: print(ts.factory.createFunctionExpression(undefined, undefined, undefined, undefined, [
2460
- ts.factory.createParameterDeclaration(undefined, undefined, 'input'),
2461
- ts.factory.createParameterDeclaration(undefined, undefined, 'ctx')
2462
- ], undefined, ts.factory.createBlock(functionBody, true))),
2463
- dependencies: dependencies.dependencies,
2464
- get hasDependencies () {
2465
- return dependencies.dependencies.length > 0;
2466
- }
2467
- };
2468
- }
2469
- const sourceFile = ts.factory.createSourceFile(functionBody, ts.factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None);
2470
- // Usage of `factory.createUniqueName` requires that `identifiers` is defined and a read-only `Map`. This is usually
2471
- // done after a source file is parsed, but we never do that, so this map doesn't get created and emitting throws errors.
2472
- // Since this is an internal property, this may break on future/older TypeScript versions.
2473
- sourceFile.identifiers = new Map();
2474
- const source = print(sourceFile);
2475
- const parser = new Function('input', 'ctx', source);
2476
- return standalone__default.default(parser, dependencies.dependencies);
2477
- }
2478
- /**
2479
- * Exports the `schema` to a TypeScript type definition.
2480
- *
2481
- * ```ts
2482
- * import z from 'zod';
2483
- * import zc from 'zod-compiler';
2484
- *
2485
- * const schema = z.string();
2486
- *
2487
- * console.log(zc.types(schema));
2488
- * // export type Schema = string;
2489
- * ```
2490
- */ function types(schema, options) {
2491
- const typeDef = compilable(schema).compileType();
2492
- if (options?.asExport ?? true) {
2493
- return print(ts.factory.createSourceFile([
2494
- ts.factory.createTypeAliasDeclaration([
2495
- ts.factory.createToken(ts.SyntaxKind.ExportKeyword)
2496
- ], options?.schemaName ?? 'Schema', undefined, typeDef)
2497
- ], ts.factory.createToken(ts.SyntaxKind.EndOfFileToken), ts.NodeFlags.None));
2498
- } else {
2499
- return print(typeDef);
2500
- }
2501
- }
2502
-
2503
- var zc = {
2504
- __proto__: null,
2505
- AbstractCompiledType: AbstractCompiledType,
2506
- InliningMode: InliningMode,
2507
- get ZcAny () { return exports.ZcAny; },
2508
- get ZcArray () { return exports.ZcArray; },
2509
- get ZcBigInt () { return exports.ZcBigInt; },
2510
- get ZcBoolean () { return exports.ZcBoolean; },
2511
- get ZcBranded () { return exports.ZcBranded; },
2512
- get ZcCatch () { return exports.ZcCatch; },
2513
- get ZcDate () { return exports.ZcDate; },
2514
- get ZcDefault () { return exports.ZcDefault; },
2515
- get ZcDiscriminatedUnion () { return exports.ZcDiscriminatedUnion; },
2516
- get ZcEnum () { return exports.ZcEnum; },
2517
- ZcError: ZcError,
2518
- get ZcIntersection () { return exports.ZcIntersection; },
2519
- get ZcLiteral () { return exports.ZcLiteral; },
2520
- get ZcMap () { return exports.ZcMap; },
2521
- get ZcNaN () { return exports.ZcNaN; },
2522
- get ZcNativeEnum () { return exports.ZcNativeEnum; },
2523
- get ZcNever () { return exports.ZcNever; },
2524
- get ZcNull () { return exports.ZcNull; },
2525
- get ZcNullable () { return exports.ZcNullable; },
2526
- get ZcNumber () { return exports.ZcNumber; },
2527
- get ZcObject () { return exports.ZcObject; },
2528
- get ZcOptional () { return exports.ZcOptional; },
2529
- get ZcReadonly () { return exports.ZcReadonly; },
2530
- get ZcRecord () { return exports.ZcRecord; },
2531
- get ZcSet () { return exports.ZcSet; },
2532
- get ZcString () { return exports.ZcString; },
2533
- get ZcSymbol () { return exports.ZcSymbol; },
2534
- get ZcTuple () { return exports.ZcTuple; },
2535
- get ZcUndefined () { return exports.ZcUndefined; },
2536
- get ZcUnion () { return exports.ZcUnion; },
2537
- get ZcUnknown () { return exports.ZcUnknown; },
2538
- get ZcVoid () { return exports.ZcVoid; },
2539
- compilable: compilable,
2540
- compile: compile,
2541
- types: types
2542
- };
2543
-
2544
- exports.AbstractCompiledType = AbstractCompiledType;
2545
- exports.InliningMode = InliningMode;
2546
- exports.ZcError = ZcError;
2547
- exports.compilable = compilable;
2548
- exports.compile = compile;
2549
- exports.default = zc;
2550
- exports.types = types;
1
+ export { compile, isCompiledSchema } from "#src/core/compile.js";
2
+ //# sourceMappingURL=index.js.map