@willaimg/yatsp 4.3.6

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 (830) hide show
  1. package/README.md +22 -0
  2. package/compile.cjs +96 -0
  3. package/compile.d.cts +1 -0
  4. package/compile.d.ts +1 -0
  5. package/compile.js +52 -0
  6. package/index.cjs +52 -0
  7. package/index.d.cts +3 -0
  8. package/index.d.ts +3 -0
  9. package/index.js +7 -0
  10. package/locales/index.cjs +36 -0
  11. package/locales/index.d.cts +1 -0
  12. package/locales/index.d.ts +1 -0
  13. package/locales/index.js +1 -0
  14. package/mini/index.cjs +51 -0
  15. package/mini/index.d.cts +3 -0
  16. package/mini/index.d.ts +3 -0
  17. package/mini/index.js +3 -0
  18. package/package.json +142 -0
  19. package/src/compile.ts +56 -0
  20. package/src/index.ts +7 -0
  21. package/src/locales/index.ts +1 -0
  22. package/src/mini/index.ts +3 -0
  23. package/src/v3/YatspError.ts +340 -0
  24. package/src/v3/benchmarks/datetime.ts +58 -0
  25. package/src/v3/benchmarks/discriminatedUnion.ts +80 -0
  26. package/src/v3/benchmarks/index.ts +59 -0
  27. package/src/v3/benchmarks/ipv4.ts +57 -0
  28. package/src/v3/benchmarks/object.ts +69 -0
  29. package/src/v3/benchmarks/primitives.ts +162 -0
  30. package/src/v3/benchmarks/realworld.ts +63 -0
  31. package/src/v3/benchmarks/string.ts +55 -0
  32. package/src/v3/benchmarks/union.ts +80 -0
  33. package/src/v3/errors.ts +13 -0
  34. package/src/v3/external.ts +6 -0
  35. package/src/v3/helpers/enumUtil.ts +17 -0
  36. package/src/v3/helpers/errorUtil.ts +8 -0
  37. package/src/v3/helpers/parseUtil.ts +176 -0
  38. package/src/v3/helpers/partialUtil.ts +34 -0
  39. package/src/v3/helpers/typeAliases.ts +2 -0
  40. package/src/v3/helpers/util.ts +224 -0
  41. package/src/v3/index.ts +4 -0
  42. package/src/v3/locales/en.ts +124 -0
  43. package/src/v3/standard-schema.ts +113 -0
  44. package/src/v3/tests/Mocker.ts +54 -0
  45. package/src/v3/tests/all-errors.test.ts +157 -0
  46. package/src/v3/tests/anyunknown.test.ts +28 -0
  47. package/src/v3/tests/array.test.ts +71 -0
  48. package/src/v3/tests/async-parsing.test.ts +388 -0
  49. package/src/v3/tests/async-refinements.test.ts +46 -0
  50. package/src/v3/tests/base.test.ts +29 -0
  51. package/src/v3/tests/bigint.test.ts +55 -0
  52. package/src/v3/tests/branded.test.ts +53 -0
  53. package/src/v3/tests/catch.test.ts +220 -0
  54. package/src/v3/tests/coerce.test.ts +133 -0
  55. package/src/v3/tests/complex.test.ts +70 -0
  56. package/src/v3/tests/custom.test.ts +31 -0
  57. package/src/v3/tests/date.test.ts +32 -0
  58. package/src/v3/tests/deepmasking.test.ts +186 -0
  59. package/src/v3/tests/default.test.ts +112 -0
  60. package/src/v3/tests/description.test.ts +33 -0
  61. package/src/v3/tests/discriminated-unions.test.ts +315 -0
  62. package/src/v3/tests/enum.test.ts +80 -0
  63. package/src/v3/tests/error.test.ts +586 -0
  64. package/src/v3/tests/firstparty.test.ts +87 -0
  65. package/src/v3/tests/firstpartyschematypes.test.ts +21 -0
  66. package/src/v3/tests/function.test.ts +261 -0
  67. package/src/v3/tests/generics.test.ts +48 -0
  68. package/src/v3/tests/instanceof.test.ts +37 -0
  69. package/src/v3/tests/intersection.test.ts +128 -0
  70. package/src/v3/tests/language-server.source.ts +76 -0
  71. package/src/v3/tests/language-server.test.ts +206 -0
  72. package/src/v3/tests/literal.test.ts +36 -0
  73. package/src/v3/tests/map.test.ts +110 -0
  74. package/src/v3/tests/masking.test.ts +4 -0
  75. package/src/v3/tests/mocker.test.ts +19 -0
  76. package/src/v3/tests/nan.test.ts +24 -0
  77. package/src/v3/tests/nativeEnum.test.ts +87 -0
  78. package/src/v3/tests/nullable.test.ts +42 -0
  79. package/src/v3/tests/number.test.ts +176 -0
  80. package/src/v3/tests/object-augmentation.test.ts +29 -0
  81. package/src/v3/tests/object-in-es5-env.test.ts +29 -0
  82. package/src/v3/tests/object.test.ts +431 -0
  83. package/src/v3/tests/optional.test.ts +42 -0
  84. package/src/v3/tests/parseUtil.test.ts +23 -0
  85. package/src/v3/tests/parser.test.ts +41 -0
  86. package/src/v3/tests/partials.test.ts +243 -0
  87. package/src/v3/tests/pickomit.test.ts +111 -0
  88. package/src/v3/tests/pipeline.test.ts +29 -0
  89. package/src/v3/tests/preprocess.test.ts +186 -0
  90. package/src/v3/tests/primitive.test.ts +440 -0
  91. package/src/v3/tests/promise.test.ts +90 -0
  92. package/src/v3/tests/readonly.test.ts +114 -0
  93. package/src/v3/tests/record.test.ts +171 -0
  94. package/src/v3/tests/recursive.test.ts +197 -0
  95. package/src/v3/tests/refine.test.ts +313 -0
  96. package/src/v3/tests/safeparse.test.ts +27 -0
  97. package/src/v3/tests/set.test.ts +142 -0
  98. package/src/v3/tests/standard-schema.test.ts +83 -0
  99. package/src/v3/tests/string.test.ts +922 -0
  100. package/src/v3/tests/transformer.test.ts +233 -0
  101. package/src/v3/tests/tuple.test.ts +90 -0
  102. package/src/v3/tests/unions.test.ts +57 -0
  103. package/src/v3/tests/validations.test.ts +133 -0
  104. package/src/v3/tests/void.test.ts +15 -0
  105. package/src/v3/types.ts +5162 -0
  106. package/src/v4/classic/checks.ts +33 -0
  107. package/src/v4/classic/coerce.ts +27 -0
  108. package/src/v4/classic/compat.ts +78 -0
  109. package/src/v4/classic/deep-partial.ts +76 -0
  110. package/src/v4/classic/errors.ts +97 -0
  111. package/src/v4/classic/external.ts +57 -0
  112. package/src/v4/classic/from-json-schema.ts +944 -0
  113. package/src/v4/classic/in-out.ts +45 -0
  114. package/src/v4/classic/index.ts +5 -0
  115. package/src/v4/classic/iso.ts +20 -0
  116. package/src/v4/classic/parse.ts +88 -0
  117. package/src/v4/classic/schemas.ts +2994 -0
  118. package/src/v4/classic/tests/anyunknown.test.ts +26 -0
  119. package/src/v4/classic/tests/apply.test.ts +86 -0
  120. package/src/v4/classic/tests/array.test.ts +264 -0
  121. package/src/v4/classic/tests/assignability.test.ts +402 -0
  122. package/src/v4/classic/tests/async-parsing.test.ts +381 -0
  123. package/src/v4/classic/tests/async-refinements.test.ts +68 -0
  124. package/src/v4/classic/tests/base.test.ts +7 -0
  125. package/src/v4/classic/tests/bigint.test.ts +89 -0
  126. package/src/v4/classic/tests/brand.test.ts +106 -0
  127. package/src/v4/classic/tests/catch.test.ts +604 -0
  128. package/src/v4/classic/tests/coalesce.test.ts +20 -0
  129. package/src/v4/classic/tests/codec-examples.test.ts +572 -0
  130. package/src/v4/classic/tests/codec.test.ts +704 -0
  131. package/src/v4/classic/tests/coerce.test.ts +160 -0
  132. package/src/v4/classic/tests/continuability.test.ts +374 -0
  133. package/src/v4/classic/tests/credit-card.test.ts +76 -0
  134. package/src/v4/classic/tests/custom.test.ts +40 -0
  135. package/src/v4/classic/tests/cyclic-data.test.ts +985 -0
  136. package/src/v4/classic/tests/date.test.ts +92 -0
  137. package/src/v4/classic/tests/datetime.test.ts +316 -0
  138. package/src/v4/classic/tests/deep-partial.test.ts +380 -0
  139. package/src/v4/classic/tests/default-locale.test.ts +32 -0
  140. package/src/v4/classic/tests/default.test.ts +455 -0
  141. package/src/v4/classic/tests/describe-meta-checks.test.ts +27 -0
  142. package/src/v4/classic/tests/description.test.ts +32 -0
  143. package/src/v4/classic/tests/detached-methods.test.ts +226 -0
  144. package/src/v4/classic/tests/discriminated-unions.test.ts +942 -0
  145. package/src/v4/classic/tests/enum.test.ts +305 -0
  146. package/src/v4/classic/tests/error-utils.test.ts +917 -0
  147. package/src/v4/classic/tests/error.test.ts +1108 -0
  148. package/src/v4/classic/tests/file.test.ts +96 -0
  149. package/src/v4/classic/tests/firstparty.test.ts +179 -0
  150. package/src/v4/classic/tests/fix-json-issue.test.ts +26 -0
  151. package/src/v4/classic/tests/from-json-schema.test.ts +1467 -0
  152. package/src/v4/classic/tests/function.test.ts +391 -0
  153. package/src/v4/classic/tests/generics.test.ts +72 -0
  154. package/src/v4/classic/tests/global-config.test.ts +32 -0
  155. package/src/v4/classic/tests/hash.test.ts +67 -0
  156. package/src/v4/classic/tests/iban.test.ts +81 -0
  157. package/src/v4/classic/tests/in-out.test.ts +159 -0
  158. package/src/v4/classic/tests/index.test.ts +958 -0
  159. package/src/v4/classic/tests/instance-footprint.test.ts +319 -0
  160. package/src/v4/classic/tests/instanceof.test.ts +127 -0
  161. package/src/v4/classic/tests/intersection.test.ts +334 -0
  162. package/src/v4/classic/tests/issue-schema.test.ts +215 -0
  163. package/src/v4/classic/tests/jitless-allows-eval.test.ts +39 -0
  164. package/src/v4/classic/tests/json.test.ts +109 -0
  165. package/src/v4/classic/tests/lazy.test.ts +262 -0
  166. package/src/v4/classic/tests/literal.test.ts +128 -0
  167. package/src/v4/classic/tests/locales_ka.test.ts +29 -0
  168. package/src/v4/classic/tests/locales_ro.test.ts +24 -0
  169. package/src/v4/classic/tests/map.test.ts +342 -0
  170. package/src/v4/classic/tests/nan.test.ts +21 -0
  171. package/src/v4/classic/tests/nested-refine.test.ts +168 -0
  172. package/src/v4/classic/tests/nonoptional.test.ts +101 -0
  173. package/src/v4/classic/tests/nullable.test.ts +22 -0
  174. package/src/v4/classic/tests/number.test.ts +401 -0
  175. package/src/v4/classic/tests/object.test.ts +1006 -0
  176. package/src/v4/classic/tests/optin-ladder.test.ts +235 -0
  177. package/src/v4/classic/tests/optional.test.ts +366 -0
  178. package/src/v4/classic/tests/partial.test.ts +497 -0
  179. package/src/v4/classic/tests/pickomit.test.ts +211 -0
  180. package/src/v4/classic/tests/pipe.test.ts +101 -0
  181. package/src/v4/classic/tests/prefault.test.ts +144 -0
  182. package/src/v4/classic/tests/preprocess-types.test.ts +44 -0
  183. package/src/v4/classic/tests/preprocess.test.ts +351 -0
  184. package/src/v4/classic/tests/primitive.test.ts +175 -0
  185. package/src/v4/classic/tests/promise.test.ts +81 -0
  186. package/src/v4/classic/tests/properties.test.ts +141 -0
  187. package/src/v4/classic/tests/prototypes.test.ts +23 -0
  188. package/src/v4/classic/tests/readonly.test.ts +184 -0
  189. package/src/v4/classic/tests/record.test.ts +885 -0
  190. package/src/v4/classic/tests/recursive-types.test.ts +707 -0
  191. package/src/v4/classic/tests/redos.test.ts +68 -0
  192. package/src/v4/classic/tests/refine.test.ts +693 -0
  193. package/src/v4/classic/tests/registries.test.ts +243 -0
  194. package/src/v4/classic/tests/set.test.ts +190 -0
  195. package/src/v4/classic/tests/standard-schema.test.ts +153 -0
  196. package/src/v4/classic/tests/string-formats.test.ts +125 -0
  197. package/src/v4/classic/tests/string.test.ts +1440 -0
  198. package/src/v4/classic/tests/stringbool.test.ts +124 -0
  199. package/src/v4/classic/tests/template-literal.test.ts +814 -0
  200. package/src/v4/classic/tests/to-json-schema-methods.test.ts +438 -0
  201. package/src/v4/classic/tests/to-json-schema.test.ts +4733 -0
  202. package/src/v4/classic/tests/transform.test.ts +378 -0
  203. package/src/v4/classic/tests/tuple.test.ts +511 -0
  204. package/src/v4/classic/tests/union.test.ts +310 -0
  205. package/src/v4/classic/tests/url.test.ts +13 -0
  206. package/src/v4/classic/tests/validate.test.ts +645 -0
  207. package/src/v4/classic/tests/validations.test.ts +283 -0
  208. package/src/v4/classic/tests/void.test.ts +12 -0
  209. package/src/v4/core/api.ts +1913 -0
  210. package/src/v4/core/checks.ts +1274 -0
  211. package/src/v4/core/compile.ts +2315 -0
  212. package/src/v4/core/core.ts +230 -0
  213. package/src/v4/core/doc.ts +52 -0
  214. package/src/v4/core/errors.ts +546 -0
  215. package/src/v4/core/index.ts +19 -0
  216. package/src/v4/core/json-schema-generator.ts +131 -0
  217. package/src/v4/core/json-schema-processors.ts +934 -0
  218. package/src/v4/core/json-schema.ts +148 -0
  219. package/src/v4/core/memoizer.ts +331 -0
  220. package/src/v4/core/parse.ts +309 -0
  221. package/src/v4/core/regexes.ts +217 -0
  222. package/src/v4/core/registries.ts +109 -0
  223. package/src/v4/core/schemas.ts +5295 -0
  224. package/src/v4/core/standard-schema.ts +159 -0
  225. package/src/v4/core/tests/cached.test.ts +54 -0
  226. package/src/v4/core/tests/compile-differential.test.ts +1004 -0
  227. package/src/v4/core/tests/compile-generative.test.ts +445 -0
  228. package/src/v4/core/tests/compile-global.test.ts +89 -0
  229. package/src/v4/core/tests/compile-off-by-default.test.ts +26 -0
  230. package/src/v4/core/tests/compile-stats.test.ts +34 -0
  231. package/src/v4/core/tests/compile.test.ts +2040 -0
  232. package/src/v4/core/tests/email-regex.test.ts +140 -0
  233. package/src/v4/core/tests/extend.test.ts +59 -0
  234. package/src/v4/core/tests/index.test.ts +46 -0
  235. package/src/v4/core/tests/locales/be.test.ts +124 -0
  236. package/src/v4/core/tests/locales/el.test.ts +214 -0
  237. package/src/v4/core/tests/locales/en.test.ts +22 -0
  238. package/src/v4/core/tests/locales/es.test.ts +180 -0
  239. package/src/v4/core/tests/locales/fr.test.ts +72 -0
  240. package/src/v4/core/tests/locales/he.test.ts +379 -0
  241. package/src/v4/core/tests/locales/hr.test.ts +163 -0
  242. package/src/v4/core/tests/locales/nl.test.ts +46 -0
  243. package/src/v4/core/tests/locales/parity.test.ts +42 -0
  244. package/src/v4/core/tests/locales/ru.test.ts +128 -0
  245. package/src/v4/core/tests/locales/tg.test.ts +85 -0
  246. package/src/v4/core/tests/locales/tk.test.ts +69 -0
  247. package/src/v4/core/tests/locales/tr.test.ts +69 -0
  248. package/src/v4/core/tests/locales/uz.test.ts +105 -0
  249. package/src/v4/core/tests/polyfill-collision.test.ts +46 -0
  250. package/src/v4/core/tests/record-constructor.test.ts +125 -0
  251. package/src/v4/core/tests/recursive-tuples.test.ts +45 -0
  252. package/src/v4/core/tests/treeshake.test.ts +51 -0
  253. package/src/v4/core/tests/url-no-canparse.test.ts +133 -0
  254. package/src/v4/core/to-json-schema.ts +857 -0
  255. package/src/v4/core/util.ts +1284 -0
  256. package/src/v4/core/versions.ts +5 -0
  257. package/src/v4/core/visit.ts +193 -0
  258. package/src/v4/core/zsf.ts +323 -0
  259. package/src/v4/index.ts +4 -0
  260. package/src/v4/locales/ar.ts +120 -0
  261. package/src/v4/locales/az.ts +116 -0
  262. package/src/v4/locales/be.ts +188 -0
  263. package/src/v4/locales/bg.ts +133 -0
  264. package/src/v4/locales/bn.ts +123 -0
  265. package/src/v4/locales/ca.ts +121 -0
  266. package/src/v4/locales/ckb.ts +140 -0
  267. package/src/v4/locales/cs.ts +123 -0
  268. package/src/v4/locales/da.ts +128 -0
  269. package/src/v4/locales/de.ts +121 -0
  270. package/src/v4/locales/el.ts +124 -0
  271. package/src/v4/locales/en.ts +136 -0
  272. package/src/v4/locales/eo.ts +123 -0
  273. package/src/v4/locales/es.ts +145 -0
  274. package/src/v4/locales/fa.ts +131 -0
  275. package/src/v4/locales/fi.ts +126 -0
  276. package/src/v4/locales/fr-CA.ts +121 -0
  277. package/src/v4/locales/fr.ts +139 -0
  278. package/src/v4/locales/gu.ts +123 -0
  279. package/src/v4/locales/he.ts +253 -0
  280. package/src/v4/locales/hi.ts +126 -0
  281. package/src/v4/locales/hr.ts +135 -0
  282. package/src/v4/locales/hu.ts +122 -0
  283. package/src/v4/locales/hy.ts +175 -0
  284. package/src/v4/locales/id.ts +120 -0
  285. package/src/v4/locales/index.ts +63 -0
  286. package/src/v4/locales/is.ts +124 -0
  287. package/src/v4/locales/it.ts +121 -0
  288. package/src/v4/locales/ja.ts +119 -0
  289. package/src/v4/locales/ka.ts +128 -0
  290. package/src/v4/locales/kh.ts +7 -0
  291. package/src/v4/locales/km.ts +124 -0
  292. package/src/v4/locales/kn.ts +126 -0
  293. package/src/v4/locales/ko.ts +126 -0
  294. package/src/v4/locales/lt.ts +243 -0
  295. package/src/v4/locales/mk.ts +123 -0
  296. package/src/v4/locales/ms.ts +120 -0
  297. package/src/v4/locales/ne.ts +128 -0
  298. package/src/v4/locales/nl.ts +126 -0
  299. package/src/v4/locales/nn.ts +121 -0
  300. package/src/v4/locales/no.ts +121 -0
  301. package/src/v4/locales/ota.ts +122 -0
  302. package/src/v4/locales/pl.ts +123 -0
  303. package/src/v4/locales/ps.ts +131 -0
  304. package/src/v4/locales/pt-BR.ts +161 -0
  305. package/src/v4/locales/pt.ts +159 -0
  306. package/src/v4/locales/ro.ts +132 -0
  307. package/src/v4/locales/ru.ts +188 -0
  308. package/src/v4/locales/sk.ts +123 -0
  309. package/src/v4/locales/sl.ts +123 -0
  310. package/src/v4/locales/sv.ts +124 -0
  311. package/src/v4/locales/ta.ts +123 -0
  312. package/src/v4/locales/tg.ts +135 -0
  313. package/src/v4/locales/th.ts +124 -0
  314. package/src/v4/locales/tk.ts +113 -0
  315. package/src/v4/locales/tr.ts +116 -0
  316. package/src/v4/locales/ua.ts +7 -0
  317. package/src/v4/locales/uk.ts +122 -0
  318. package/src/v4/locales/ur.ts +124 -0
  319. package/src/v4/locales/uz.ts +120 -0
  320. package/src/v4/locales/vi.ts +122 -0
  321. package/src/v4/locales/yo.ts +129 -0
  322. package/src/v4/locales/zh-CN.ts +121 -0
  323. package/src/v4/locales/zh-TW.ts +120 -0
  324. package/src/v4/mini/checks.ts +33 -0
  325. package/src/v4/mini/coerce.ts +27 -0
  326. package/src/v4/mini/deep-partial.ts +73 -0
  327. package/src/v4/mini/external.ts +52 -0
  328. package/src/v4/mini/in-out.ts +45 -0
  329. package/src/v4/mini/index.ts +3 -0
  330. package/src/v4/mini/iso.ts +66 -0
  331. package/src/v4/mini/parse.ts +16 -0
  332. package/src/v4/mini/schemas.ts +2093 -0
  333. package/src/v4/mini/tests/apply.test.ts +57 -0
  334. package/src/v4/mini/tests/assignability.test.ts +190 -0
  335. package/src/v4/mini/tests/brand.test.ts +94 -0
  336. package/src/v4/mini/tests/checks.test.ts +158 -0
  337. package/src/v4/mini/tests/codec.test.ts +582 -0
  338. package/src/v4/mini/tests/computed.test.ts +30 -0
  339. package/src/v4/mini/tests/cyclic-data.test.ts +84 -0
  340. package/src/v4/mini/tests/deep-partial.test.ts +54 -0
  341. package/src/v4/mini/tests/error.test.ts +41 -0
  342. package/src/v4/mini/tests/functions.test.ts +14 -0
  343. package/src/v4/mini/tests/index.test.ts +1148 -0
  344. package/src/v4/mini/tests/number.test.ts +109 -0
  345. package/src/v4/mini/tests/object.test.ts +292 -0
  346. package/src/v4/mini/tests/prototypes.test.ts +43 -0
  347. package/src/v4/mini/tests/recursive-types.test.ts +341 -0
  348. package/src/v4/mini/tests/standard-schema.test.ts +50 -0
  349. package/src/v4/mini/tests/string.test.ts +396 -0
  350. package/src/v4-mini/index.ts +3 -0
  351. package/v3/YatspError.cjs +167 -0
  352. package/v3/YatspError.d.cts +164 -0
  353. package/v3/YatspError.d.ts +164 -0
  354. package/v3/YatspError.js +143 -0
  355. package/v3/errors.cjs +36 -0
  356. package/v3/errors.d.cts +5 -0
  357. package/v3/errors.d.ts +5 -0
  358. package/v3/errors.js +9 -0
  359. package/v3/external.cjs +41 -0
  360. package/v3/external.d.cts +6 -0
  361. package/v3/external.d.ts +6 -0
  362. package/v3/external.js +6 -0
  363. package/v3/helpers/enumUtil.cjs +21 -0
  364. package/v3/helpers/enumUtil.d.cts +8 -0
  365. package/v3/helpers/enumUtil.d.ts +8 -0
  366. package/v3/helpers/enumUtil.js +1 -0
  367. package/v3/helpers/errorUtil.cjs +28 -0
  368. package/v3/helpers/errorUtil.d.cts +9 -0
  369. package/v3/helpers/errorUtil.d.ts +9 -0
  370. package/v3/helpers/errorUtil.js +6 -0
  371. package/v3/helpers/parseUtil.cjs +143 -0
  372. package/v3/helpers/parseUtil.d.cts +78 -0
  373. package/v3/helpers/parseUtil.d.ts +78 -0
  374. package/v3/helpers/parseUtil.js +109 -0
  375. package/v3/helpers/partialUtil.cjs +21 -0
  376. package/v3/helpers/partialUtil.d.cts +8 -0
  377. package/v3/helpers/partialUtil.d.ts +8 -0
  378. package/v3/helpers/partialUtil.js +1 -0
  379. package/v3/helpers/typeAliases.cjs +21 -0
  380. package/v3/helpers/typeAliases.d.cts +2 -0
  381. package/v3/helpers/typeAliases.d.ts +2 -0
  382. package/v3/helpers/typeAliases.js +1 -0
  383. package/v3/helpers/util.cjs +156 -0
  384. package/v3/helpers/util.d.cts +85 -0
  385. package/v3/helpers/util.d.ts +85 -0
  386. package/v3/helpers/util.js +133 -0
  387. package/v3/index.cjs +52 -0
  388. package/v3/index.d.cts +4 -0
  389. package/v3/index.d.ts +4 -0
  390. package/v3/index.js +4 -0
  391. package/v3/locales/en.cjs +112 -0
  392. package/v3/locales/en.d.cts +3 -0
  393. package/v3/locales/en.d.ts +3 -0
  394. package/v3/locales/en.js +109 -0
  395. package/v3/standard-schema.cjs +21 -0
  396. package/v3/standard-schema.d.cts +102 -0
  397. package/v3/standard-schema.d.ts +102 -0
  398. package/v3/standard-schema.js +1 -0
  399. package/v3/types.cjs +3798 -0
  400. package/v3/types.d.cts +1034 -0
  401. package/v3/types.d.ts +1034 -0
  402. package/v3/types.js +3697 -0
  403. package/v4/classic/checks.cjs +53 -0
  404. package/v4/classic/checks.d.cts +1 -0
  405. package/v4/classic/checks.d.ts +1 -0
  406. package/v4/classic/checks.js +1 -0
  407. package/v4/classic/coerce.cjs +66 -0
  408. package/v4/classic/coerce.d.cts +17 -0
  409. package/v4/classic/coerce.d.ts +17 -0
  410. package/v4/classic/coerce.js +17 -0
  411. package/v4/classic/compat.cjs +80 -0
  412. package/v4/classic/compat.d.cts +48 -0
  413. package/v4/classic/compat.d.ts +48 -0
  414. package/v4/classic/compat.js +31 -0
  415. package/v4/classic/deep-partial.cjs +58 -0
  416. package/v4/classic/deep-partial.d.cts +14 -0
  417. package/v4/classic/deep-partial.d.ts +14 -0
  418. package/v4/classic/deep-partial.js +13 -0
  419. package/v4/classic/errors.cjs +102 -0
  420. package/v4/classic/errors.d.cts +29 -0
  421. package/v4/classic/errors.d.ts +29 -0
  422. package/v4/classic/errors.js +57 -0
  423. package/v4/classic/external.cjs +97 -0
  424. package/v4/classic/external.d.cts +19 -0
  425. package/v4/classic/external.d.ts +19 -0
  426. package/v4/classic/external.js +17 -0
  427. package/v4/classic/from-json-schema.cjs +926 -0
  428. package/v4/classic/from-json-schema.d.cts +12 -0
  429. package/v4/classic/from-json-schema.d.ts +12 -0
  430. package/v4/classic/from-json-schema.js +881 -0
  431. package/v4/classic/in-out.cjs +82 -0
  432. package/v4/classic/in-out.d.cts +8 -0
  433. package/v4/classic/in-out.d.ts +8 -0
  434. package/v4/classic/in-out.js +36 -0
  435. package/v4/classic/index.cjs +52 -0
  436. package/v4/classic/index.d.cts +3 -0
  437. package/v4/classic/index.d.ts +3 -0
  438. package/v4/classic/index.js +4 -0
  439. package/v4/classic/iso.cjs +68 -0
  440. package/v4/classic/iso.d.cts +7 -0
  441. package/v4/classic/iso.d.ts +7 -0
  442. package/v4/classic/iso.js +15 -0
  443. package/v4/classic/parse.cjs +63 -0
  444. package/v4/classic/parse.d.cts +44 -0
  445. package/v4/classic/parse.d.ts +44 -0
  446. package/v4/classic/parse.js +16 -0
  447. package/v4/classic/schemas.cjs +1676 -0
  448. package/v4/classic/schemas.d.cts +816 -0
  449. package/v4/classic/schemas.d.ts +816 -0
  450. package/v4/classic/schemas.js +1535 -0
  451. package/v4/core/api.cjs +1269 -0
  452. package/v4/core/api.d.cts +335 -0
  453. package/v4/core/api.d.ts +335 -0
  454. package/v4/core/api.js +1107 -0
  455. package/v4/core/checks.cjs +561 -0
  456. package/v4/core/checks.d.cts +294 -0
  457. package/v4/core/checks.d.ts +294 -0
  458. package/v4/core/checks.js +516 -0
  459. package/v4/core/compile.cjs +1908 -0
  460. package/v4/core/compile.d.cts +65 -0
  461. package/v4/core/compile.d.ts +65 -0
  462. package/v4/core/compile.js +1858 -0
  463. package/v4/core/core.cjs +167 -0
  464. package/v4/core/core.d.cts +82 -0
  465. package/v4/core/core.d.ts +82 -0
  466. package/v4/core/core.js +141 -0
  467. package/v4/core/doc.cjs +61 -0
  468. package/v4/core/doc.d.cts +16 -0
  469. package/v4/core/doc.d.ts +16 -0
  470. package/v4/core/doc.js +38 -0
  471. package/v4/core/errors.cjs +317 -0
  472. package/v4/core/errors.d.cts +225 -0
  473. package/v4/core/errors.d.ts +225 -0
  474. package/v4/core/errors.js +267 -0
  475. package/v4/core/index.cjs +70 -0
  476. package/v4/core/index.d.cts +19 -0
  477. package/v4/core/index.d.ts +19 -0
  478. package/v4/core/index.js +19 -0
  479. package/v4/core/json-schema-generator.cjs +122 -0
  480. package/v4/core/json-schema-generator.d.cts +65 -0
  481. package/v4/core/json-schema-generator.d.ts +65 -0
  482. package/v4/core/json-schema-generator.js +99 -0
  483. package/v4/core/json-schema-processors.cjs +929 -0
  484. package/v4/core/json-schema-processors.d.cts +65 -0
  485. package/v4/core/json-schema-processors.d.ts +65 -0
  486. package/v4/core/json-schema-processors.js +843 -0
  487. package/v4/core/json-schema.cjs +21 -0
  488. package/v4/core/json-schema.d.cts +89 -0
  489. package/v4/core/json-schema.d.ts +89 -0
  490. package/v4/core/json-schema.js +1 -0
  491. package/v4/core/memoizer.cjs +360 -0
  492. package/v4/core/memoizer.d.cts +15 -0
  493. package/v4/core/memoizer.d.ts +15 -0
  494. package/v4/core/memoizer.js +311 -0
  495. package/v4/core/parse.cjs +233 -0
  496. package/v4/core/parse.d.cts +52 -0
  497. package/v4/core/parse.d.ts +52 -0
  498. package/v4/core/parse.js +175 -0
  499. package/v4/core/regexes.cjs +209 -0
  500. package/v4/core/regexes.d.cts +90 -0
  501. package/v4/core/regexes.d.ts +90 -0
  502. package/v4/core/regexes.js +156 -0
  503. package/v4/core/registries.cjs +76 -0
  504. package/v4/core/registries.d.cts +35 -0
  505. package/v4/core/registries.d.ts +35 -0
  506. package/v4/core/registries.js +52 -0
  507. package/v4/core/schemas.cjs +2669 -0
  508. package/v4/core/schemas.d.cts +1273 -0
  509. package/v4/core/schemas.d.ts +1273 -0
  510. package/v4/core/schemas.js +2606 -0
  511. package/v4/core/standard-schema.cjs +21 -0
  512. package/v4/core/standard-schema.d.cts +126 -0
  513. package/v4/core/standard-schema.d.ts +126 -0
  514. package/v4/core/standard-schema.js +1 -0
  515. package/v4/core/to-json-schema.cjs +681 -0
  516. package/v4/core/to-json-schema.d.cts +154 -0
  517. package/v4/core/to-json-schema.d.ts +154 -0
  518. package/v4/core/to-json-schema.js +651 -0
  519. package/v4/core/util.cjs +949 -0
  520. package/v4/core/util.d.cts +278 -0
  521. package/v4/core/util.d.ts +278 -0
  522. package/v4/core/util.js +859 -0
  523. package/v4/core/versions.cjs +27 -0
  524. package/v4/core/versions.d.cts +5 -0
  525. package/v4/core/versions.d.ts +5 -0
  526. package/v4/core/versions.js +5 -0
  527. package/v4/core/visit.cjs +214 -0
  528. package/v4/core/visit.d.cts +30 -0
  529. package/v4/core/visit.d.ts +30 -0
  530. package/v4/core/visit.js +169 -0
  531. package/v4/index.cjs +51 -0
  532. package/v4/index.d.cts +3 -0
  533. package/v4/index.d.ts +3 -0
  534. package/v4/index.js +4 -0
  535. package/v4/locales/ar.cjs +138 -0
  536. package/v4/locales/ar.d.cts +5 -0
  537. package/v4/locales/ar.d.ts +4 -0
  538. package/v4/locales/ar.js +111 -0
  539. package/v4/locales/az.cjs +137 -0
  540. package/v4/locales/az.d.cts +5 -0
  541. package/v4/locales/az.d.ts +4 -0
  542. package/v4/locales/az.js +110 -0
  543. package/v4/locales/be.cjs +195 -0
  544. package/v4/locales/be.d.cts +5 -0
  545. package/v4/locales/be.d.ts +4 -0
  546. package/v4/locales/be.js +168 -0
  547. package/v4/locales/bg.cjs +152 -0
  548. package/v4/locales/bg.d.cts +5 -0
  549. package/v4/locales/bg.d.ts +4 -0
  550. package/v4/locales/bg.js +125 -0
  551. package/v4/locales/bn.cjs +140 -0
  552. package/v4/locales/bn.d.cts +5 -0
  553. package/v4/locales/bn.d.ts +4 -0
  554. package/v4/locales/bn.js +113 -0
  555. package/v4/locales/ca.cjs +139 -0
  556. package/v4/locales/ca.d.cts +5 -0
  557. package/v4/locales/ca.d.ts +4 -0
  558. package/v4/locales/ca.js +112 -0
  559. package/v4/locales/ckb.cjs +160 -0
  560. package/v4/locales/ckb.d.cts +5 -0
  561. package/v4/locales/ckb.d.ts +4 -0
  562. package/v4/locales/ckb.js +133 -0
  563. package/v4/locales/cs.cjs +143 -0
  564. package/v4/locales/cs.d.cts +5 -0
  565. package/v4/locales/cs.d.ts +4 -0
  566. package/v4/locales/cs.js +116 -0
  567. package/v4/locales/da.cjs +147 -0
  568. package/v4/locales/da.d.cts +5 -0
  569. package/v4/locales/da.d.ts +4 -0
  570. package/v4/locales/da.js +120 -0
  571. package/v4/locales/de.cjs +140 -0
  572. package/v4/locales/de.d.cts +5 -0
  573. package/v4/locales/de.d.ts +4 -0
  574. package/v4/locales/de.js +113 -0
  575. package/v4/locales/el.cjs +139 -0
  576. package/v4/locales/el.d.cts +5 -0
  577. package/v4/locales/el.d.ts +4 -0
  578. package/v4/locales/el.js +112 -0
  579. package/v4/locales/en.cjs +152 -0
  580. package/v4/locales/en.d.cts +5 -0
  581. package/v4/locales/en.d.ts +4 -0
  582. package/v4/locales/en.js +125 -0
  583. package/v4/locales/eo.cjs +141 -0
  584. package/v4/locales/eo.d.cts +5 -0
  585. package/v4/locales/eo.d.ts +4 -0
  586. package/v4/locales/eo.js +114 -0
  587. package/v4/locales/es.cjs +163 -0
  588. package/v4/locales/es.d.cts +5 -0
  589. package/v4/locales/es.d.ts +4 -0
  590. package/v4/locales/es.js +136 -0
  591. package/v4/locales/fa.cjs +146 -0
  592. package/v4/locales/fa.d.cts +5 -0
  593. package/v4/locales/fa.d.ts +4 -0
  594. package/v4/locales/fa.js +119 -0
  595. package/v4/locales/fi.cjs +144 -0
  596. package/v4/locales/fi.d.cts +5 -0
  597. package/v4/locales/fi.d.ts +4 -0
  598. package/v4/locales/fi.js +117 -0
  599. package/v4/locales/fr-CA.cjs +139 -0
  600. package/v4/locales/fr-CA.d.cts +5 -0
  601. package/v4/locales/fr-CA.d.ts +4 -0
  602. package/v4/locales/fr-CA.js +112 -0
  603. package/v4/locales/fr.cjs +156 -0
  604. package/v4/locales/fr.d.cts +5 -0
  605. package/v4/locales/fr.d.ts +4 -0
  606. package/v4/locales/fr.js +129 -0
  607. package/v4/locales/gu.cjs +140 -0
  608. package/v4/locales/gu.d.cts +5 -0
  609. package/v4/locales/gu.d.ts +4 -0
  610. package/v4/locales/gu.js +113 -0
  611. package/v4/locales/he.cjs +248 -0
  612. package/v4/locales/he.d.cts +5 -0
  613. package/v4/locales/he.d.ts +4 -0
  614. package/v4/locales/he.js +221 -0
  615. package/v4/locales/hi.cjs +138 -0
  616. package/v4/locales/hi.d.cts +5 -0
  617. package/v4/locales/hi.d.ts +4 -0
  618. package/v4/locales/hi.js +111 -0
  619. package/v4/locales/hr.cjs +153 -0
  620. package/v4/locales/hr.d.cts +5 -0
  621. package/v4/locales/hr.d.ts +4 -0
  622. package/v4/locales/hr.js +126 -0
  623. package/v4/locales/hu.cjs +140 -0
  624. package/v4/locales/hu.d.cts +5 -0
  625. package/v4/locales/hu.d.ts +4 -0
  626. package/v4/locales/hu.js +113 -0
  627. package/v4/locales/hy.cjs +185 -0
  628. package/v4/locales/hy.d.cts +5 -0
  629. package/v4/locales/hy.d.ts +4 -0
  630. package/v4/locales/hy.js +158 -0
  631. package/v4/locales/id.cjs +138 -0
  632. package/v4/locales/id.d.cts +5 -0
  633. package/v4/locales/id.d.ts +4 -0
  634. package/v4/locales/id.js +111 -0
  635. package/v4/locales/index.cjs +152 -0
  636. package/v4/locales/index.d.cts +63 -0
  637. package/v4/locales/index.d.ts +63 -0
  638. package/v4/locales/index.js +63 -0
  639. package/v4/locales/is.cjs +141 -0
  640. package/v4/locales/is.d.cts +5 -0
  641. package/v4/locales/is.d.ts +4 -0
  642. package/v4/locales/is.js +114 -0
  643. package/v4/locales/it.cjs +140 -0
  644. package/v4/locales/it.d.cts +5 -0
  645. package/v4/locales/it.d.ts +4 -0
  646. package/v4/locales/it.js +113 -0
  647. package/v4/locales/ja.cjs +139 -0
  648. package/v4/locales/ja.d.cts +5 -0
  649. package/v4/locales/ja.d.ts +4 -0
  650. package/v4/locales/ja.js +112 -0
  651. package/v4/locales/ka.cjs +144 -0
  652. package/v4/locales/ka.d.cts +5 -0
  653. package/v4/locales/ka.d.ts +4 -0
  654. package/v4/locales/ka.js +117 -0
  655. package/v4/locales/kh.cjs +12 -0
  656. package/v4/locales/kh.d.cts +5 -0
  657. package/v4/locales/kh.d.ts +5 -0
  658. package/v4/locales/kh.js +5 -0
  659. package/v4/locales/km.cjs +142 -0
  660. package/v4/locales/km.d.cts +5 -0
  661. package/v4/locales/km.d.ts +4 -0
  662. package/v4/locales/km.js +115 -0
  663. package/v4/locales/kn.cjs +143 -0
  664. package/v4/locales/kn.d.cts +5 -0
  665. package/v4/locales/kn.d.ts +4 -0
  666. package/v4/locales/kn.js +116 -0
  667. package/v4/locales/ko.cjs +143 -0
  668. package/v4/locales/ko.d.cts +5 -0
  669. package/v4/locales/ko.d.ts +4 -0
  670. package/v4/locales/ko.js +116 -0
  671. package/v4/locales/lt.cjs +234 -0
  672. package/v4/locales/lt.d.cts +5 -0
  673. package/v4/locales/lt.d.ts +4 -0
  674. package/v4/locales/lt.js +207 -0
  675. package/v4/locales/mk.cjs +141 -0
  676. package/v4/locales/mk.d.cts +5 -0
  677. package/v4/locales/mk.d.ts +4 -0
  678. package/v4/locales/mk.js +114 -0
  679. package/v4/locales/ms.cjs +139 -0
  680. package/v4/locales/ms.d.cts +5 -0
  681. package/v4/locales/ms.d.ts +4 -0
  682. package/v4/locales/ms.js +112 -0
  683. package/v4/locales/ne.cjs +138 -0
  684. package/v4/locales/ne.d.cts +5 -0
  685. package/v4/locales/ne.d.ts +4 -0
  686. package/v4/locales/ne.js +111 -0
  687. package/v4/locales/nl.cjs +142 -0
  688. package/v4/locales/nl.d.cts +5 -0
  689. package/v4/locales/nl.d.ts +4 -0
  690. package/v4/locales/nl.js +115 -0
  691. package/v4/locales/nn.cjs +140 -0
  692. package/v4/locales/nn.d.cts +5 -0
  693. package/v4/locales/nn.d.ts +4 -0
  694. package/v4/locales/nn.js +113 -0
  695. package/v4/locales/no.cjs +140 -0
  696. package/v4/locales/no.d.cts +5 -0
  697. package/v4/locales/no.d.ts +4 -0
  698. package/v4/locales/no.js +113 -0
  699. package/v4/locales/ota.cjs +141 -0
  700. package/v4/locales/ota.d.cts +5 -0
  701. package/v4/locales/ota.d.ts +4 -0
  702. package/v4/locales/ota.js +114 -0
  703. package/v4/locales/pl.cjs +141 -0
  704. package/v4/locales/pl.d.cts +5 -0
  705. package/v4/locales/pl.d.ts +4 -0
  706. package/v4/locales/pl.js +114 -0
  707. package/v4/locales/ps.cjs +146 -0
  708. package/v4/locales/ps.d.cts +5 -0
  709. package/v4/locales/ps.d.ts +4 -0
  710. package/v4/locales/ps.js +119 -0
  711. package/v4/locales/pt-BR.cjs +170 -0
  712. package/v4/locales/pt-BR.d.cts +5 -0
  713. package/v4/locales/pt-BR.d.ts +4 -0
  714. package/v4/locales/pt-BR.js +143 -0
  715. package/v4/locales/pt.cjs +169 -0
  716. package/v4/locales/pt.d.cts +5 -0
  717. package/v4/locales/pt.d.ts +4 -0
  718. package/v4/locales/pt.js +142 -0
  719. package/v4/locales/ro.cjs +149 -0
  720. package/v4/locales/ro.d.cts +5 -0
  721. package/v4/locales/ro.d.ts +4 -0
  722. package/v4/locales/ro.js +122 -0
  723. package/v4/locales/ru.cjs +195 -0
  724. package/v4/locales/ru.d.cts +5 -0
  725. package/v4/locales/ru.d.ts +4 -0
  726. package/v4/locales/ru.js +168 -0
  727. package/v4/locales/sk.cjs +143 -0
  728. package/v4/locales/sk.d.cts +5 -0
  729. package/v4/locales/sk.d.ts +4 -0
  730. package/v4/locales/sk.js +116 -0
  731. package/v4/locales/sl.cjs +141 -0
  732. package/v4/locales/sl.d.cts +5 -0
  733. package/v4/locales/sl.d.ts +4 -0
  734. package/v4/locales/sl.js +114 -0
  735. package/v4/locales/sv.cjs +142 -0
  736. package/v4/locales/sv.d.cts +5 -0
  737. package/v4/locales/sv.d.ts +4 -0
  738. package/v4/locales/sv.js +115 -0
  739. package/v4/locales/ta.cjs +142 -0
  740. package/v4/locales/ta.d.cts +5 -0
  741. package/v4/locales/ta.d.ts +4 -0
  742. package/v4/locales/ta.js +115 -0
  743. package/v4/locales/tg.cjs +144 -0
  744. package/v4/locales/tg.d.cts +5 -0
  745. package/v4/locales/tg.d.ts +4 -0
  746. package/v4/locales/tg.js +117 -0
  747. package/v4/locales/th.cjs +142 -0
  748. package/v4/locales/th.d.cts +5 -0
  749. package/v4/locales/th.d.ts +4 -0
  750. package/v4/locales/th.js +115 -0
  751. package/v4/locales/tk.cjs +134 -0
  752. package/v4/locales/tk.d.cts +5 -0
  753. package/v4/locales/tk.d.ts +4 -0
  754. package/v4/locales/tk.js +107 -0
  755. package/v4/locales/tr.cjs +137 -0
  756. package/v4/locales/tr.d.cts +5 -0
  757. package/v4/locales/tr.d.ts +4 -0
  758. package/v4/locales/tr.js +110 -0
  759. package/v4/locales/ua.cjs +12 -0
  760. package/v4/locales/ua.d.cts +5 -0
  761. package/v4/locales/ua.d.ts +5 -0
  762. package/v4/locales/ua.js +5 -0
  763. package/v4/locales/uk.cjs +140 -0
  764. package/v4/locales/uk.d.cts +5 -0
  765. package/v4/locales/uk.d.ts +4 -0
  766. package/v4/locales/uk.js +113 -0
  767. package/v4/locales/ur.cjs +142 -0
  768. package/v4/locales/ur.d.cts +5 -0
  769. package/v4/locales/ur.d.ts +4 -0
  770. package/v4/locales/ur.js +115 -0
  771. package/v4/locales/uz.cjs +140 -0
  772. package/v4/locales/uz.d.cts +5 -0
  773. package/v4/locales/uz.d.ts +4 -0
  774. package/v4/locales/uz.js +113 -0
  775. package/v4/locales/vi.cjs +140 -0
  776. package/v4/locales/vi.d.cts +5 -0
  777. package/v4/locales/vi.d.ts +4 -0
  778. package/v4/locales/vi.js +113 -0
  779. package/v4/locales/yo.cjs +139 -0
  780. package/v4/locales/yo.d.cts +5 -0
  781. package/v4/locales/yo.d.ts +4 -0
  782. package/v4/locales/yo.js +112 -0
  783. package/v4/locales/zh-CN.cjs +141 -0
  784. package/v4/locales/zh-CN.d.cts +5 -0
  785. package/v4/locales/zh-CN.d.ts +4 -0
  786. package/v4/locales/zh-CN.js +114 -0
  787. package/v4/locales/zh-TW.cjs +139 -0
  788. package/v4/locales/zh-TW.d.cts +5 -0
  789. package/v4/locales/zh-TW.d.ts +4 -0
  790. package/v4/locales/zh-TW.js +112 -0
  791. package/v4/mini/checks.cjs +54 -0
  792. package/v4/mini/checks.d.cts +1 -0
  793. package/v4/mini/checks.d.ts +1 -0
  794. package/v4/mini/checks.js +1 -0
  795. package/v4/mini/coerce.cjs +71 -0
  796. package/v4/mini/coerce.d.cts +7 -0
  797. package/v4/mini/coerce.d.ts +7 -0
  798. package/v4/mini/coerce.js +22 -0
  799. package/v4/mini/deep-partial.cjs +58 -0
  800. package/v4/mini/deep-partial.d.cts +14 -0
  801. package/v4/mini/deep-partial.d.ts +14 -0
  802. package/v4/mini/deep-partial.js +13 -0
  803. package/v4/mini/external.cjs +95 -0
  804. package/v4/mini/external.d.cts +16 -0
  805. package/v4/mini/external.d.ts +16 -0
  806. package/v4/mini/external.js +16 -0
  807. package/v4/mini/in-out.cjs +82 -0
  808. package/v4/mini/in-out.d.cts +8 -0
  809. package/v4/mini/in-out.d.ts +8 -0
  810. package/v4/mini/in-out.js +36 -0
  811. package/v4/mini/index.cjs +51 -0
  812. package/v4/mini/index.d.cts +3 -0
  813. package/v4/mini/index.d.ts +3 -0
  814. package/v4/mini/index.js +3 -0
  815. package/v4/mini/iso.cjs +83 -0
  816. package/v4/mini/iso.d.cts +22 -0
  817. package/v4/mini/iso.d.ts +22 -0
  818. package/v4/mini/iso.js +34 -0
  819. package/v4/mini/parse.cjs +37 -0
  820. package/v4/mini/parse.d.cts +1 -0
  821. package/v4/mini/parse.d.ts +1 -0
  822. package/v4/mini/parse.js +1 -0
  823. package/v4/mini/schemas.cjs +1149 -0
  824. package/v4/mini/schemas.d.cts +480 -0
  825. package/v4/mini/schemas.d.ts +480 -0
  826. package/v4/mini/schemas.js +1004 -0
  827. package/v4-mini/index.cjs +51 -0
  828. package/v4-mini/index.d.cts +3 -0
  829. package/v4-mini/index.d.ts +3 -0
  830. package/v4-mini/index.js +3 -0
package/v3/types.cjs ADDED
@@ -0,0 +1,3798 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.discriminatedUnion = exports.date = exports.boolean = exports.bigint = exports.array = exports.any = exports.coerce = exports.YatspFirstPartyTypeKind = exports.late = exports.YatspSchema = exports.Schema = exports.YatspReadonly = exports.YatspPipeline = exports.YatspBranded = exports.BRAND = exports.YatspNaN = exports.YatspCatch = exports.YatspDefault = exports.YatspNullable = exports.YatspOptional = exports.YatspTransformer = exports.YatspEffects = exports.YatspPromise = exports.YatspNativeEnum = exports.YatspEnum = exports.YatspLiteral = exports.YatspLazy = exports.YatspFunction = exports.YatspSet = exports.YatspMap = exports.YatspRecord = exports.YatspTuple = exports.YatspIntersection = exports.YatspDiscriminatedUnion = exports.YatspUnion = exports.YatspObject = exports.YatspArray = exports.YatspVoid = exports.YatspNever = exports.YatspUnknown = exports.YatspAny = exports.YatspNull = exports.YatspUndefined = exports.YatspSymbol = exports.YatspDate = exports.YatspBoolean = exports.YatspBigInt = exports.YatspNumber = exports.YatspString = exports.YatspType = void 0;
4
+ exports.NEVER = exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.symbol = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.pipeline = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = exports.nan = exports.map = exports.literal = exports.lazy = exports.intersection = exports.instanceof = exports.function = exports.enum = exports.effect = void 0;
5
+ exports.datetimeRegex = datetimeRegex;
6
+ exports.custom = custom;
7
+ const YatspError_js_1 = require("./YatspError.cjs");
8
+ const errors_js_1 = require("./errors.cjs");
9
+ const errorUtil_js_1 = require("./helpers/errorUtil.cjs");
10
+ const parseUtil_js_1 = require("./helpers/parseUtil.cjs");
11
+ const util_js_1 = require("./helpers/util.cjs");
12
+ class ParseInputLazyPath {
13
+ constructor(parent, value, path, key) {
14
+ this._cachedPath = [];
15
+ this.parent = parent;
16
+ this.data = value;
17
+ this._path = path;
18
+ this._key = key;
19
+ }
20
+ get path() {
21
+ if (!this._cachedPath.length) {
22
+ if (Array.isArray(this._key)) {
23
+ this._cachedPath.push(...this._path, ...this._key);
24
+ }
25
+ else {
26
+ this._cachedPath.push(...this._path, this._key);
27
+ }
28
+ }
29
+ return this._cachedPath;
30
+ }
31
+ }
32
+ const handleResult = (ctx, result) => {
33
+ if ((0, parseUtil_js_1.isValid)(result)) {
34
+ return { success: true, data: result.value };
35
+ }
36
+ else {
37
+ if (!ctx.common.issues.length) {
38
+ throw new Error("Validation failed but no issues detected.");
39
+ }
40
+ return {
41
+ success: false,
42
+ get error() {
43
+ if (this._error)
44
+ return this._error;
45
+ const error = new YatspError_js_1.YatspError(ctx.common.issues);
46
+ this._error = error;
47
+ return this._error;
48
+ },
49
+ };
50
+ }
51
+ };
52
+ function processCreateParams(params) {
53
+ if (!params)
54
+ return {};
55
+ const { errorMap, invalid_type_error, required_error, description } = params;
56
+ if (errorMap && (invalid_type_error || required_error)) {
57
+ throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
58
+ }
59
+ if (errorMap)
60
+ return { errorMap: errorMap, description };
61
+ const customMap = (iss, ctx) => {
62
+ const { message } = params;
63
+ if (iss.code === "invalid_enum_value") {
64
+ return { message: message ?? ctx.defaultError };
65
+ }
66
+ if (typeof ctx.data === "undefined") {
67
+ return { message: message ?? required_error ?? ctx.defaultError };
68
+ }
69
+ if (iss.code !== "invalid_type")
70
+ return { message: ctx.defaultError };
71
+ return { message: message ?? invalid_type_error ?? ctx.defaultError };
72
+ };
73
+ return { errorMap: customMap, description };
74
+ }
75
+ class YatspType {
76
+ get description() {
77
+ return this._def.description;
78
+ }
79
+ _getType(input) {
80
+ return (0, util_js_1.getParsedType)(input.data);
81
+ }
82
+ _getOrReturnCtx(input, ctx) {
83
+ return (ctx || {
84
+ common: input.parent.common,
85
+ data: input.data,
86
+ parsedType: (0, util_js_1.getParsedType)(input.data),
87
+ schemaErrorMap: this._def.errorMap,
88
+ path: input.path,
89
+ parent: input.parent,
90
+ });
91
+ }
92
+ _processInputParams(input) {
93
+ return {
94
+ status: new parseUtil_js_1.ParseStatus(),
95
+ ctx: {
96
+ common: input.parent.common,
97
+ data: input.data,
98
+ parsedType: (0, util_js_1.getParsedType)(input.data),
99
+ schemaErrorMap: this._def.errorMap,
100
+ path: input.path,
101
+ parent: input.parent,
102
+ },
103
+ };
104
+ }
105
+ _parseSync(input) {
106
+ const result = this._parse(input);
107
+ if ((0, parseUtil_js_1.isAsync)(result)) {
108
+ throw new Error("Synchronous parse encountered promise.");
109
+ }
110
+ return result;
111
+ }
112
+ _parseAsync(input) {
113
+ const result = this._parse(input);
114
+ return Promise.resolve(result);
115
+ }
116
+ parse(data, params) {
117
+ const result = this.safeParse(data, params);
118
+ if (result.success)
119
+ return result.data;
120
+ throw result.error;
121
+ }
122
+ safeParse(data, params) {
123
+ const ctx = {
124
+ common: {
125
+ issues: [],
126
+ async: params?.async ?? false,
127
+ contextualErrorMap: params?.errorMap,
128
+ },
129
+ path: params?.path || [],
130
+ schemaErrorMap: this._def.errorMap,
131
+ parent: null,
132
+ data,
133
+ parsedType: (0, util_js_1.getParsedType)(data),
134
+ };
135
+ const result = this._parseSync({ data, path: ctx.path, parent: ctx });
136
+ return handleResult(ctx, result);
137
+ }
138
+ "~validate"(data) {
139
+ const ctx = {
140
+ common: {
141
+ issues: [],
142
+ async: !!this["~standard"].async,
143
+ },
144
+ path: [],
145
+ schemaErrorMap: this._def.errorMap,
146
+ parent: null,
147
+ data,
148
+ parsedType: (0, util_js_1.getParsedType)(data),
149
+ };
150
+ if (!this["~standard"].async) {
151
+ try {
152
+ const result = this._parseSync({ data, path: [], parent: ctx });
153
+ return (0, parseUtil_js_1.isValid)(result)
154
+ ? {
155
+ value: result.value,
156
+ }
157
+ : {
158
+ issues: ctx.common.issues,
159
+ };
160
+ }
161
+ catch (err) {
162
+ if (err?.message?.toLowerCase()?.includes("encountered")) {
163
+ this["~standard"].async = true;
164
+ }
165
+ ctx.common = {
166
+ issues: [],
167
+ async: true,
168
+ };
169
+ }
170
+ }
171
+ return this._parseAsync({ data, path: [], parent: ctx }).then((result) => (0, parseUtil_js_1.isValid)(result)
172
+ ? {
173
+ value: result.value,
174
+ }
175
+ : {
176
+ issues: ctx.common.issues,
177
+ });
178
+ }
179
+ async parseAsync(data, params) {
180
+ const result = await this.safeParseAsync(data, params);
181
+ if (result.success)
182
+ return result.data;
183
+ throw result.error;
184
+ }
185
+ async safeParseAsync(data, params) {
186
+ const ctx = {
187
+ common: {
188
+ issues: [],
189
+ contextualErrorMap: params?.errorMap,
190
+ async: true,
191
+ },
192
+ path: params?.path || [],
193
+ schemaErrorMap: this._def.errorMap,
194
+ parent: null,
195
+ data,
196
+ parsedType: (0, util_js_1.getParsedType)(data),
197
+ };
198
+ const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
199
+ const result = await ((0, parseUtil_js_1.isAsync)(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
200
+ return handleResult(ctx, result);
201
+ }
202
+ refine(check, message) {
203
+ const getIssueProperties = (val) => {
204
+ if (typeof message === "string" || typeof message === "undefined") {
205
+ return { message };
206
+ }
207
+ else if (typeof message === "function") {
208
+ return message(val);
209
+ }
210
+ else {
211
+ return message;
212
+ }
213
+ };
214
+ return this._refinement((val, ctx) => {
215
+ const result = check(val);
216
+ const setError = () => ctx.addIssue({
217
+ code: YatspError_js_1.YatspIssueCode.custom,
218
+ ...getIssueProperties(val),
219
+ });
220
+ if (typeof Promise !== "undefined" && result instanceof Promise) {
221
+ return result.then((data) => {
222
+ if (!data) {
223
+ setError();
224
+ return false;
225
+ }
226
+ else {
227
+ return true;
228
+ }
229
+ });
230
+ }
231
+ if (!result) {
232
+ setError();
233
+ return false;
234
+ }
235
+ else {
236
+ return true;
237
+ }
238
+ });
239
+ }
240
+ refinement(check, refinementData) {
241
+ return this._refinement((val, ctx) => {
242
+ if (!check(val)) {
243
+ ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
244
+ return false;
245
+ }
246
+ else {
247
+ return true;
248
+ }
249
+ });
250
+ }
251
+ _refinement(refinement) {
252
+ return new YatspEffects({
253
+ schema: this,
254
+ typeName: YatspFirstPartyTypeKind.YatspEffects,
255
+ effect: { type: "refinement", refinement },
256
+ });
257
+ }
258
+ superRefine(refinement) {
259
+ return this._refinement(refinement);
260
+ }
261
+ constructor(def) {
262
+ /** Alias of safeParseAsync */
263
+ this.spa = this.safeParseAsync;
264
+ this._def = def;
265
+ this.parse = this.parse.bind(this);
266
+ this.safeParse = this.safeParse.bind(this);
267
+ this.parseAsync = this.parseAsync.bind(this);
268
+ this.safeParseAsync = this.safeParseAsync.bind(this);
269
+ this.spa = this.spa.bind(this);
270
+ this.refine = this.refine.bind(this);
271
+ this.refinement = this.refinement.bind(this);
272
+ this.superRefine = this.superRefine.bind(this);
273
+ this.optional = this.optional.bind(this);
274
+ this.nullable = this.nullable.bind(this);
275
+ this.nullish = this.nullish.bind(this);
276
+ this.array = this.array.bind(this);
277
+ this.promise = this.promise.bind(this);
278
+ this.or = this.or.bind(this);
279
+ this.and = this.and.bind(this);
280
+ this.transform = this.transform.bind(this);
281
+ this.brand = this.brand.bind(this);
282
+ this.default = this.default.bind(this);
283
+ this.catch = this.catch.bind(this);
284
+ this.describe = this.describe.bind(this);
285
+ this.pipe = this.pipe.bind(this);
286
+ this.readonly = this.readonly.bind(this);
287
+ this.isNullable = this.isNullable.bind(this);
288
+ this.isOptional = this.isOptional.bind(this);
289
+ this["~standard"] = {
290
+ version: 1,
291
+ vendor: "yatsp",
292
+ validate: (data) => this["~validate"](data),
293
+ };
294
+ }
295
+ optional() {
296
+ return YatspOptional.create(this, this._def);
297
+ }
298
+ nullable() {
299
+ return YatspNullable.create(this, this._def);
300
+ }
301
+ nullish() {
302
+ return this.nullable().optional();
303
+ }
304
+ array() {
305
+ return YatspArray.create(this);
306
+ }
307
+ promise() {
308
+ return YatspPromise.create(this, this._def);
309
+ }
310
+ or(option) {
311
+ return YatspUnion.create([this, option], this._def);
312
+ }
313
+ and(incoming) {
314
+ return YatspIntersection.create(this, incoming, this._def);
315
+ }
316
+ transform(transform) {
317
+ return new YatspEffects({
318
+ ...processCreateParams(this._def),
319
+ schema: this,
320
+ typeName: YatspFirstPartyTypeKind.YatspEffects,
321
+ effect: { type: "transform", transform },
322
+ });
323
+ }
324
+ default(def) {
325
+ const defaultValueFunc = typeof def === "function" ? def : () => def;
326
+ return new YatspDefault({
327
+ ...processCreateParams(this._def),
328
+ innerType: this,
329
+ defaultValue: defaultValueFunc,
330
+ typeName: YatspFirstPartyTypeKind.YatspDefault,
331
+ });
332
+ }
333
+ brand() {
334
+ return new YatspBranded({
335
+ typeName: YatspFirstPartyTypeKind.YatspBranded,
336
+ type: this,
337
+ ...processCreateParams(this._def),
338
+ });
339
+ }
340
+ catch(def) {
341
+ const catchValueFunc = typeof def === "function" ? def : () => def;
342
+ return new YatspCatch({
343
+ ...processCreateParams(this._def),
344
+ innerType: this,
345
+ catchValue: catchValueFunc,
346
+ typeName: YatspFirstPartyTypeKind.YatspCatch,
347
+ });
348
+ }
349
+ describe(description) {
350
+ const This = this.constructor;
351
+ return new This({
352
+ ...this._def,
353
+ description,
354
+ });
355
+ }
356
+ pipe(target) {
357
+ return YatspPipeline.create(this, target);
358
+ }
359
+ readonly() {
360
+ return YatspReadonly.create(this);
361
+ }
362
+ isOptional() {
363
+ return this.safeParse(undefined).success;
364
+ }
365
+ isNullable() {
366
+ return this.safeParse(null).success;
367
+ }
368
+ }
369
+ exports.YatspType = YatspType;
370
+ exports.Schema = YatspType;
371
+ exports.YatspSchema = YatspType;
372
+ const cuidRegex = /^c[^\s-]{8,}$/i;
373
+ const cuid2Regex = /^[0-9a-z]+$/;
374
+ const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
375
+ // const uuidRegex =
376
+ // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
377
+ const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
378
+ const nanoidRegex = /^[a-z0-9_-]{21}$/i;
379
+ const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
380
+ const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
381
+ // from https://stackoverflow.com/a/46181/1550155
382
+ // old version: too slow, didn't support unicode
383
+ // const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
384
+ //old email regex
385
+ // const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
386
+ // eslint-disable-next-line
387
+ // const emailRegex =
388
+ // /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
389
+ // const emailRegex =
390
+ // /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
391
+ // const emailRegex =
392
+ // /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
393
+ const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
394
+ // const emailRegex =
395
+ // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
396
+ // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
397
+ const _emojiRegex = `^[\\p{Extended_Pictographic}\\p{Emoji_Component}]+$`;
398
+ let emojiRegex;
399
+ // faster, simpler, safer
400
+ const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
401
+ const ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
402
+ // const ipv6Regex =
403
+ // /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
404
+ const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
405
+ const ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
406
+ // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
407
+ const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
408
+ // https://base64.guru/standards/base64url
409
+ const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
410
+ // simple
411
+ // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
412
+ // no leap year validation
413
+ // const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
414
+ // with leap year validation
415
+ const dateRegexSource = `((\\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])))`;
416
+ const dateRegex = new RegExp(`^${dateRegexSource}$`);
417
+ function timeRegexSource(args) {
418
+ let secondsRegexSource = `[0-5]\\d`;
419
+ if (args.precision) {
420
+ secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
421
+ }
422
+ else if (args.precision == null) {
423
+ secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
424
+ }
425
+ const secondsQuantifier = args.precision ? "+" : "?"; // require seconds if precision is nonzero
426
+ return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
427
+ }
428
+ function timeRegex(args) {
429
+ return new RegExp(`^${timeRegexSource(args)}$`);
430
+ }
431
+ // Adapted from https://stackoverflow.com/a/3143231
432
+ function datetimeRegex(args) {
433
+ let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
434
+ const opts = [];
435
+ opts.push(args.local ? `Z?` : `Z`);
436
+ if (args.offset)
437
+ opts.push(`([+-]\\d{2}:?\\d{2})`);
438
+ regex = `${regex}(${opts.join("|")})`;
439
+ return new RegExp(`^${regex}$`);
440
+ }
441
+ function isValidIP(ip, version) {
442
+ if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
443
+ return true;
444
+ }
445
+ if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
446
+ return true;
447
+ }
448
+ return false;
449
+ }
450
+ function isValidJWT(jwt, alg) {
451
+ if (!jwtRegex.test(jwt))
452
+ return false;
453
+ try {
454
+ const [header] = jwt.split(".");
455
+ if (!header)
456
+ return false;
457
+ // Convert base64url to base64
458
+ const base64 = header
459
+ .replace(/-/g, "+")
460
+ .replace(/_/g, "/")
461
+ .padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
462
+ // @ts-ignore
463
+ const decoded = JSON.parse(atob(base64));
464
+ if (typeof decoded !== "object" || decoded === null)
465
+ return false;
466
+ if ("typ" in decoded && decoded?.typ !== "JWT")
467
+ return false;
468
+ if (!decoded.alg)
469
+ return false;
470
+ if (alg && decoded.alg !== alg)
471
+ return false;
472
+ return true;
473
+ }
474
+ catch {
475
+ return false;
476
+ }
477
+ }
478
+ function isValidCidr(ip, version) {
479
+ if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
480
+ return true;
481
+ }
482
+ if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
483
+ return true;
484
+ }
485
+ return false;
486
+ }
487
+ class YatspString extends YatspType {
488
+ _parse(input) {
489
+ if (this._def.coerce) {
490
+ input.data = String(input.data);
491
+ }
492
+ const parsedType = this._getType(input);
493
+ if (parsedType !== util_js_1.YatspParsedType.string) {
494
+ const ctx = this._getOrReturnCtx(input);
495
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
496
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
497
+ expected: util_js_1.YatspParsedType.string,
498
+ received: ctx.parsedType,
499
+ });
500
+ return parseUtil_js_1.INVALID;
501
+ }
502
+ const status = new parseUtil_js_1.ParseStatus();
503
+ let ctx = undefined;
504
+ for (const check of this._def.checks) {
505
+ if (check.kind === "min") {
506
+ if (input.data.length < check.value) {
507
+ ctx = this._getOrReturnCtx(input, ctx);
508
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
509
+ code: YatspError_js_1.YatspIssueCode.too_small,
510
+ minimum: check.value,
511
+ type: "string",
512
+ inclusive: true,
513
+ exact: false,
514
+ message: check.message,
515
+ });
516
+ status.dirty();
517
+ }
518
+ }
519
+ else if (check.kind === "max") {
520
+ if (input.data.length > check.value) {
521
+ ctx = this._getOrReturnCtx(input, ctx);
522
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
523
+ code: YatspError_js_1.YatspIssueCode.too_big,
524
+ maximum: check.value,
525
+ type: "string",
526
+ inclusive: true,
527
+ exact: false,
528
+ message: check.message,
529
+ });
530
+ status.dirty();
531
+ }
532
+ }
533
+ else if (check.kind === "length") {
534
+ const tooBig = input.data.length > check.value;
535
+ const tooSmall = input.data.length < check.value;
536
+ if (tooBig || tooSmall) {
537
+ ctx = this._getOrReturnCtx(input, ctx);
538
+ if (tooBig) {
539
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
540
+ code: YatspError_js_1.YatspIssueCode.too_big,
541
+ maximum: check.value,
542
+ type: "string",
543
+ inclusive: true,
544
+ exact: true,
545
+ message: check.message,
546
+ });
547
+ }
548
+ else if (tooSmall) {
549
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
550
+ code: YatspError_js_1.YatspIssueCode.too_small,
551
+ minimum: check.value,
552
+ type: "string",
553
+ inclusive: true,
554
+ exact: true,
555
+ message: check.message,
556
+ });
557
+ }
558
+ status.dirty();
559
+ }
560
+ }
561
+ else if (check.kind === "email") {
562
+ if (!emailRegex.test(input.data)) {
563
+ ctx = this._getOrReturnCtx(input, ctx);
564
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
565
+ validation: "email",
566
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
567
+ message: check.message,
568
+ });
569
+ status.dirty();
570
+ }
571
+ }
572
+ else if (check.kind === "emoji") {
573
+ if (!emojiRegex) {
574
+ emojiRegex = new RegExp(_emojiRegex, "u");
575
+ }
576
+ if (!emojiRegex.test(input.data)) {
577
+ ctx = this._getOrReturnCtx(input, ctx);
578
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
579
+ validation: "emoji",
580
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
581
+ message: check.message,
582
+ });
583
+ status.dirty();
584
+ }
585
+ }
586
+ else if (check.kind === "uuid") {
587
+ if (!uuidRegex.test(input.data)) {
588
+ ctx = this._getOrReturnCtx(input, ctx);
589
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
590
+ validation: "uuid",
591
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
592
+ message: check.message,
593
+ });
594
+ status.dirty();
595
+ }
596
+ }
597
+ else if (check.kind === "nanoid") {
598
+ if (!nanoidRegex.test(input.data)) {
599
+ ctx = this._getOrReturnCtx(input, ctx);
600
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
601
+ validation: "nanoid",
602
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
603
+ message: check.message,
604
+ });
605
+ status.dirty();
606
+ }
607
+ }
608
+ else if (check.kind === "cuid") {
609
+ if (!cuidRegex.test(input.data)) {
610
+ ctx = this._getOrReturnCtx(input, ctx);
611
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
612
+ validation: "cuid",
613
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
614
+ message: check.message,
615
+ });
616
+ status.dirty();
617
+ }
618
+ }
619
+ else if (check.kind === "cuid2") {
620
+ if (!cuid2Regex.test(input.data)) {
621
+ ctx = this._getOrReturnCtx(input, ctx);
622
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
623
+ validation: "cuid2",
624
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
625
+ message: check.message,
626
+ });
627
+ status.dirty();
628
+ }
629
+ }
630
+ else if (check.kind === "ulid") {
631
+ if (!ulidRegex.test(input.data)) {
632
+ ctx = this._getOrReturnCtx(input, ctx);
633
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
634
+ validation: "ulid",
635
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
636
+ message: check.message,
637
+ });
638
+ status.dirty();
639
+ }
640
+ }
641
+ else if (check.kind === "url") {
642
+ try {
643
+ // @ts-ignore
644
+ new URL(input.data);
645
+ }
646
+ catch {
647
+ ctx = this._getOrReturnCtx(input, ctx);
648
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
649
+ validation: "url",
650
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
651
+ message: check.message,
652
+ });
653
+ status.dirty();
654
+ }
655
+ }
656
+ else if (check.kind === "regex") {
657
+ check.regex.lastIndex = 0;
658
+ const testResult = check.regex.test(input.data);
659
+ if (!testResult) {
660
+ ctx = this._getOrReturnCtx(input, ctx);
661
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
662
+ validation: "regex",
663
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
664
+ message: check.message,
665
+ });
666
+ status.dirty();
667
+ }
668
+ }
669
+ else if (check.kind === "trim") {
670
+ input.data = input.data.trim();
671
+ }
672
+ else if (check.kind === "includes") {
673
+ if (!input.data.includes(check.value, check.position)) {
674
+ ctx = this._getOrReturnCtx(input, ctx);
675
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
676
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
677
+ validation: { includes: check.value, position: check.position },
678
+ message: check.message,
679
+ });
680
+ status.dirty();
681
+ }
682
+ }
683
+ else if (check.kind === "toLowerCase") {
684
+ input.data = input.data.toLowerCase();
685
+ }
686
+ else if (check.kind === "toUpperCase") {
687
+ input.data = input.data.toUpperCase();
688
+ }
689
+ else if (check.kind === "startsWith") {
690
+ if (!input.data.startsWith(check.value)) {
691
+ ctx = this._getOrReturnCtx(input, ctx);
692
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
693
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
694
+ validation: { startsWith: check.value },
695
+ message: check.message,
696
+ });
697
+ status.dirty();
698
+ }
699
+ }
700
+ else if (check.kind === "endsWith") {
701
+ if (!input.data.endsWith(check.value)) {
702
+ ctx = this._getOrReturnCtx(input, ctx);
703
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
704
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
705
+ validation: { endsWith: check.value },
706
+ message: check.message,
707
+ });
708
+ status.dirty();
709
+ }
710
+ }
711
+ else if (check.kind === "datetime") {
712
+ const regex = datetimeRegex(check);
713
+ if (!regex.test(input.data)) {
714
+ ctx = this._getOrReturnCtx(input, ctx);
715
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
716
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
717
+ validation: "datetime",
718
+ message: check.message,
719
+ });
720
+ status.dirty();
721
+ }
722
+ }
723
+ else if (check.kind === "date") {
724
+ const regex = dateRegex;
725
+ if (!regex.test(input.data)) {
726
+ ctx = this._getOrReturnCtx(input, ctx);
727
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
728
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
729
+ validation: "date",
730
+ message: check.message,
731
+ });
732
+ status.dirty();
733
+ }
734
+ }
735
+ else if (check.kind === "time") {
736
+ const regex = timeRegex(check);
737
+ if (!regex.test(input.data)) {
738
+ ctx = this._getOrReturnCtx(input, ctx);
739
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
740
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
741
+ validation: "time",
742
+ message: check.message,
743
+ });
744
+ status.dirty();
745
+ }
746
+ }
747
+ else if (check.kind === "duration") {
748
+ if (!durationRegex.test(input.data)) {
749
+ ctx = this._getOrReturnCtx(input, ctx);
750
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
751
+ validation: "duration",
752
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
753
+ message: check.message,
754
+ });
755
+ status.dirty();
756
+ }
757
+ }
758
+ else if (check.kind === "ip") {
759
+ if (!isValidIP(input.data, check.version)) {
760
+ ctx = this._getOrReturnCtx(input, ctx);
761
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
762
+ validation: "ip",
763
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
764
+ message: check.message,
765
+ });
766
+ status.dirty();
767
+ }
768
+ }
769
+ else if (check.kind === "jwt") {
770
+ if (!isValidJWT(input.data, check.alg)) {
771
+ ctx = this._getOrReturnCtx(input, ctx);
772
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
773
+ validation: "jwt",
774
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
775
+ message: check.message,
776
+ });
777
+ status.dirty();
778
+ }
779
+ }
780
+ else if (check.kind === "cidr") {
781
+ if (!isValidCidr(input.data, check.version)) {
782
+ ctx = this._getOrReturnCtx(input, ctx);
783
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
784
+ validation: "cidr",
785
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
786
+ message: check.message,
787
+ });
788
+ status.dirty();
789
+ }
790
+ }
791
+ else if (check.kind === "base64") {
792
+ if (!base64Regex.test(input.data)) {
793
+ ctx = this._getOrReturnCtx(input, ctx);
794
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
795
+ validation: "base64",
796
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
797
+ message: check.message,
798
+ });
799
+ status.dirty();
800
+ }
801
+ }
802
+ else if (check.kind === "base64url") {
803
+ if (!base64urlRegex.test(input.data)) {
804
+ ctx = this._getOrReturnCtx(input, ctx);
805
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
806
+ validation: "base64url",
807
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
808
+ message: check.message,
809
+ });
810
+ status.dirty();
811
+ }
812
+ }
813
+ else {
814
+ util_js_1.util.assertNever(check);
815
+ }
816
+ }
817
+ return { status: status.value, value: input.data };
818
+ }
819
+ _regex(regex, validation, message) {
820
+ return this.refinement((data) => regex.test(data), {
821
+ validation,
822
+ code: YatspError_js_1.YatspIssueCode.invalid_string,
823
+ ...errorUtil_js_1.errorUtil.errToObj(message),
824
+ });
825
+ }
826
+ _addCheck(check) {
827
+ return new YatspString({
828
+ ...this._def,
829
+ checks: [...this._def.checks, check],
830
+ });
831
+ }
832
+ email(message) {
833
+ return this._addCheck({ kind: "email", ...errorUtil_js_1.errorUtil.errToObj(message) });
834
+ }
835
+ url(message) {
836
+ return this._addCheck({ kind: "url", ...errorUtil_js_1.errorUtil.errToObj(message) });
837
+ }
838
+ emoji(message) {
839
+ return this._addCheck({ kind: "emoji", ...errorUtil_js_1.errorUtil.errToObj(message) });
840
+ }
841
+ uuid(message) {
842
+ return this._addCheck({ kind: "uuid", ...errorUtil_js_1.errorUtil.errToObj(message) });
843
+ }
844
+ nanoid(message) {
845
+ return this._addCheck({ kind: "nanoid", ...errorUtil_js_1.errorUtil.errToObj(message) });
846
+ }
847
+ cuid(message) {
848
+ return this._addCheck({ kind: "cuid", ...errorUtil_js_1.errorUtil.errToObj(message) });
849
+ }
850
+ cuid2(message) {
851
+ return this._addCheck({ kind: "cuid2", ...errorUtil_js_1.errorUtil.errToObj(message) });
852
+ }
853
+ ulid(message) {
854
+ return this._addCheck({ kind: "ulid", ...errorUtil_js_1.errorUtil.errToObj(message) });
855
+ }
856
+ base64(message) {
857
+ return this._addCheck({ kind: "base64", ...errorUtil_js_1.errorUtil.errToObj(message) });
858
+ }
859
+ base64url(message) {
860
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
861
+ return this._addCheck({
862
+ kind: "base64url",
863
+ ...errorUtil_js_1.errorUtil.errToObj(message),
864
+ });
865
+ }
866
+ jwt(options) {
867
+ return this._addCheck({ kind: "jwt", ...errorUtil_js_1.errorUtil.errToObj(options) });
868
+ }
869
+ ip(options) {
870
+ return this._addCheck({ kind: "ip", ...errorUtil_js_1.errorUtil.errToObj(options) });
871
+ }
872
+ cidr(options) {
873
+ return this._addCheck({ kind: "cidr", ...errorUtil_js_1.errorUtil.errToObj(options) });
874
+ }
875
+ datetime(options) {
876
+ if (typeof options === "string") {
877
+ return this._addCheck({
878
+ kind: "datetime",
879
+ precision: null,
880
+ offset: false,
881
+ local: false,
882
+ message: options,
883
+ });
884
+ }
885
+ return this._addCheck({
886
+ kind: "datetime",
887
+ precision: typeof options?.precision === "undefined" ? null : options?.precision,
888
+ offset: options?.offset ?? false,
889
+ local: options?.local ?? false,
890
+ ...errorUtil_js_1.errorUtil.errToObj(options?.message),
891
+ });
892
+ }
893
+ date(message) {
894
+ return this._addCheck({ kind: "date", message });
895
+ }
896
+ time(options) {
897
+ if (typeof options === "string") {
898
+ return this._addCheck({
899
+ kind: "time",
900
+ precision: null,
901
+ message: options,
902
+ });
903
+ }
904
+ return this._addCheck({
905
+ kind: "time",
906
+ precision: typeof options?.precision === "undefined" ? null : options?.precision,
907
+ ...errorUtil_js_1.errorUtil.errToObj(options?.message),
908
+ });
909
+ }
910
+ duration(message) {
911
+ return this._addCheck({ kind: "duration", ...errorUtil_js_1.errorUtil.errToObj(message) });
912
+ }
913
+ regex(regex, message) {
914
+ return this._addCheck({
915
+ kind: "regex",
916
+ regex: regex,
917
+ ...errorUtil_js_1.errorUtil.errToObj(message),
918
+ });
919
+ }
920
+ includes(value, options) {
921
+ return this._addCheck({
922
+ kind: "includes",
923
+ value: value,
924
+ position: options?.position,
925
+ ...errorUtil_js_1.errorUtil.errToObj(options?.message),
926
+ });
927
+ }
928
+ startsWith(value, message) {
929
+ return this._addCheck({
930
+ kind: "startsWith",
931
+ value: value,
932
+ ...errorUtil_js_1.errorUtil.errToObj(message),
933
+ });
934
+ }
935
+ endsWith(value, message) {
936
+ return this._addCheck({
937
+ kind: "endsWith",
938
+ value: value,
939
+ ...errorUtil_js_1.errorUtil.errToObj(message),
940
+ });
941
+ }
942
+ min(minLength, message) {
943
+ return this._addCheck({
944
+ kind: "min",
945
+ value: minLength,
946
+ ...errorUtil_js_1.errorUtil.errToObj(message),
947
+ });
948
+ }
949
+ max(maxLength, message) {
950
+ return this._addCheck({
951
+ kind: "max",
952
+ value: maxLength,
953
+ ...errorUtil_js_1.errorUtil.errToObj(message),
954
+ });
955
+ }
956
+ length(len, message) {
957
+ return this._addCheck({
958
+ kind: "length",
959
+ value: len,
960
+ ...errorUtil_js_1.errorUtil.errToObj(message),
961
+ });
962
+ }
963
+ /**
964
+ * Equivalent to `.min(1)`
965
+ */
966
+ nonempty(message) {
967
+ return this.min(1, errorUtil_js_1.errorUtil.errToObj(message));
968
+ }
969
+ trim() {
970
+ return new YatspString({
971
+ ...this._def,
972
+ checks: [...this._def.checks, { kind: "trim" }],
973
+ });
974
+ }
975
+ toLowerCase() {
976
+ return new YatspString({
977
+ ...this._def,
978
+ checks: [...this._def.checks, { kind: "toLowerCase" }],
979
+ });
980
+ }
981
+ toUpperCase() {
982
+ return new YatspString({
983
+ ...this._def,
984
+ checks: [...this._def.checks, { kind: "toUpperCase" }],
985
+ });
986
+ }
987
+ get isDatetime() {
988
+ return !!this._def.checks.find((ch) => ch.kind === "datetime");
989
+ }
990
+ get isDate() {
991
+ return !!this._def.checks.find((ch) => ch.kind === "date");
992
+ }
993
+ get isTime() {
994
+ return !!this._def.checks.find((ch) => ch.kind === "time");
995
+ }
996
+ get isDuration() {
997
+ return !!this._def.checks.find((ch) => ch.kind === "duration");
998
+ }
999
+ get isEmail() {
1000
+ return !!this._def.checks.find((ch) => ch.kind === "email");
1001
+ }
1002
+ get isURL() {
1003
+ return !!this._def.checks.find((ch) => ch.kind === "url");
1004
+ }
1005
+ get isEmoji() {
1006
+ return !!this._def.checks.find((ch) => ch.kind === "emoji");
1007
+ }
1008
+ get isUUID() {
1009
+ return !!this._def.checks.find((ch) => ch.kind === "uuid");
1010
+ }
1011
+ get isNANOID() {
1012
+ return !!this._def.checks.find((ch) => ch.kind === "nanoid");
1013
+ }
1014
+ get isCUID() {
1015
+ return !!this._def.checks.find((ch) => ch.kind === "cuid");
1016
+ }
1017
+ get isCUID2() {
1018
+ return !!this._def.checks.find((ch) => ch.kind === "cuid2");
1019
+ }
1020
+ get isULID() {
1021
+ return !!this._def.checks.find((ch) => ch.kind === "ulid");
1022
+ }
1023
+ get isIP() {
1024
+ return !!this._def.checks.find((ch) => ch.kind === "ip");
1025
+ }
1026
+ get isCIDR() {
1027
+ return !!this._def.checks.find((ch) => ch.kind === "cidr");
1028
+ }
1029
+ get isBase64() {
1030
+ return !!this._def.checks.find((ch) => ch.kind === "base64");
1031
+ }
1032
+ get isBase64url() {
1033
+ // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1034
+ return !!this._def.checks.find((ch) => ch.kind === "base64url");
1035
+ }
1036
+ get minLength() {
1037
+ let min = null;
1038
+ for (const ch of this._def.checks) {
1039
+ if (ch.kind === "min") {
1040
+ if (min === null || ch.value > min)
1041
+ min = ch.value;
1042
+ }
1043
+ }
1044
+ return min;
1045
+ }
1046
+ get maxLength() {
1047
+ let max = null;
1048
+ for (const ch of this._def.checks) {
1049
+ if (ch.kind === "max") {
1050
+ if (max === null || ch.value < max)
1051
+ max = ch.value;
1052
+ }
1053
+ }
1054
+ return max;
1055
+ }
1056
+ }
1057
+ exports.YatspString = YatspString;
1058
+ YatspString.create = (params) => {
1059
+ return new YatspString({
1060
+ checks: [],
1061
+ typeName: YatspFirstPartyTypeKind.YatspString,
1062
+ coerce: params?.coerce ?? false,
1063
+ ...processCreateParams(params),
1064
+ });
1065
+ };
1066
+ // https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
1067
+ function floatSafeRemainder(val, step) {
1068
+ const valDecCount = (val.toString().split(".")[1] || "").length;
1069
+ const stepDecCount = (step.toString().split(".")[1] || "").length;
1070
+ const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
1071
+ const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
1072
+ const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
1073
+ return (valInt % stepInt) / 10 ** decCount;
1074
+ }
1075
+ class YatspNumber extends YatspType {
1076
+ constructor() {
1077
+ super(...arguments);
1078
+ this.min = this.gte;
1079
+ this.max = this.lte;
1080
+ this.step = this.multipleOf;
1081
+ }
1082
+ _parse(input) {
1083
+ if (this._def.coerce) {
1084
+ input.data = Number(input.data);
1085
+ }
1086
+ const parsedType = this._getType(input);
1087
+ if (parsedType !== util_js_1.YatspParsedType.number) {
1088
+ const ctx = this._getOrReturnCtx(input);
1089
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1090
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1091
+ expected: util_js_1.YatspParsedType.number,
1092
+ received: ctx.parsedType,
1093
+ });
1094
+ return parseUtil_js_1.INVALID;
1095
+ }
1096
+ let ctx = undefined;
1097
+ const status = new parseUtil_js_1.ParseStatus();
1098
+ for (const check of this._def.checks) {
1099
+ if (check.kind === "int") {
1100
+ if (!util_js_1.util.isInteger(input.data)) {
1101
+ ctx = this._getOrReturnCtx(input, ctx);
1102
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1103
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1104
+ expected: "integer",
1105
+ received: "float",
1106
+ message: check.message,
1107
+ });
1108
+ status.dirty();
1109
+ }
1110
+ }
1111
+ else if (check.kind === "min") {
1112
+ const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
1113
+ if (tooSmall) {
1114
+ ctx = this._getOrReturnCtx(input, ctx);
1115
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1116
+ code: YatspError_js_1.YatspIssueCode.too_small,
1117
+ minimum: check.value,
1118
+ type: "number",
1119
+ inclusive: check.inclusive,
1120
+ exact: false,
1121
+ message: check.message,
1122
+ });
1123
+ status.dirty();
1124
+ }
1125
+ }
1126
+ else if (check.kind === "max") {
1127
+ const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
1128
+ if (tooBig) {
1129
+ ctx = this._getOrReturnCtx(input, ctx);
1130
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1131
+ code: YatspError_js_1.YatspIssueCode.too_big,
1132
+ maximum: check.value,
1133
+ type: "number",
1134
+ inclusive: check.inclusive,
1135
+ exact: false,
1136
+ message: check.message,
1137
+ });
1138
+ status.dirty();
1139
+ }
1140
+ }
1141
+ else if (check.kind === "multipleOf") {
1142
+ if (floatSafeRemainder(input.data, check.value) !== 0) {
1143
+ ctx = this._getOrReturnCtx(input, ctx);
1144
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1145
+ code: YatspError_js_1.YatspIssueCode.not_multiple_of,
1146
+ multipleOf: check.value,
1147
+ message: check.message,
1148
+ });
1149
+ status.dirty();
1150
+ }
1151
+ }
1152
+ else if (check.kind === "finite") {
1153
+ if (!Number.isFinite(input.data)) {
1154
+ ctx = this._getOrReturnCtx(input, ctx);
1155
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1156
+ code: YatspError_js_1.YatspIssueCode.not_finite,
1157
+ message: check.message,
1158
+ });
1159
+ status.dirty();
1160
+ }
1161
+ }
1162
+ else {
1163
+ util_js_1.util.assertNever(check);
1164
+ }
1165
+ }
1166
+ return { status: status.value, value: input.data };
1167
+ }
1168
+ gte(value, message) {
1169
+ return this.setLimit("min", value, true, errorUtil_js_1.errorUtil.toString(message));
1170
+ }
1171
+ gt(value, message) {
1172
+ return this.setLimit("min", value, false, errorUtil_js_1.errorUtil.toString(message));
1173
+ }
1174
+ lte(value, message) {
1175
+ return this.setLimit("max", value, true, errorUtil_js_1.errorUtil.toString(message));
1176
+ }
1177
+ lt(value, message) {
1178
+ return this.setLimit("max", value, false, errorUtil_js_1.errorUtil.toString(message));
1179
+ }
1180
+ setLimit(kind, value, inclusive, message) {
1181
+ return new YatspNumber({
1182
+ ...this._def,
1183
+ checks: [
1184
+ ...this._def.checks,
1185
+ {
1186
+ kind,
1187
+ value,
1188
+ inclusive,
1189
+ message: errorUtil_js_1.errorUtil.toString(message),
1190
+ },
1191
+ ],
1192
+ });
1193
+ }
1194
+ _addCheck(check) {
1195
+ return new YatspNumber({
1196
+ ...this._def,
1197
+ checks: [...this._def.checks, check],
1198
+ });
1199
+ }
1200
+ int(message) {
1201
+ return this._addCheck({
1202
+ kind: "int",
1203
+ message: errorUtil_js_1.errorUtil.toString(message),
1204
+ });
1205
+ }
1206
+ positive(message) {
1207
+ return this._addCheck({
1208
+ kind: "min",
1209
+ value: 0,
1210
+ inclusive: false,
1211
+ message: errorUtil_js_1.errorUtil.toString(message),
1212
+ });
1213
+ }
1214
+ negative(message) {
1215
+ return this._addCheck({
1216
+ kind: "max",
1217
+ value: 0,
1218
+ inclusive: false,
1219
+ message: errorUtil_js_1.errorUtil.toString(message),
1220
+ });
1221
+ }
1222
+ nonpositive(message) {
1223
+ return this._addCheck({
1224
+ kind: "max",
1225
+ value: 0,
1226
+ inclusive: true,
1227
+ message: errorUtil_js_1.errorUtil.toString(message),
1228
+ });
1229
+ }
1230
+ nonnegative(message) {
1231
+ return this._addCheck({
1232
+ kind: "min",
1233
+ value: 0,
1234
+ inclusive: true,
1235
+ message: errorUtil_js_1.errorUtil.toString(message),
1236
+ });
1237
+ }
1238
+ multipleOf(value, message) {
1239
+ return this._addCheck({
1240
+ kind: "multipleOf",
1241
+ value: value,
1242
+ message: errorUtil_js_1.errorUtil.toString(message),
1243
+ });
1244
+ }
1245
+ finite(message) {
1246
+ return this._addCheck({
1247
+ kind: "finite",
1248
+ message: errorUtil_js_1.errorUtil.toString(message),
1249
+ });
1250
+ }
1251
+ safe(message) {
1252
+ return this._addCheck({
1253
+ kind: "min",
1254
+ inclusive: true,
1255
+ value: Number.MIN_SAFE_INTEGER,
1256
+ message: errorUtil_js_1.errorUtil.toString(message),
1257
+ })._addCheck({
1258
+ kind: "max",
1259
+ inclusive: true,
1260
+ value: Number.MAX_SAFE_INTEGER,
1261
+ message: errorUtil_js_1.errorUtil.toString(message),
1262
+ });
1263
+ }
1264
+ get minValue() {
1265
+ let min = null;
1266
+ for (const ch of this._def.checks) {
1267
+ if (ch.kind === "min") {
1268
+ if (min === null || ch.value > min)
1269
+ min = ch.value;
1270
+ }
1271
+ }
1272
+ return min;
1273
+ }
1274
+ get maxValue() {
1275
+ let max = null;
1276
+ for (const ch of this._def.checks) {
1277
+ if (ch.kind === "max") {
1278
+ if (max === null || ch.value < max)
1279
+ max = ch.value;
1280
+ }
1281
+ }
1282
+ return max;
1283
+ }
1284
+ get isInt() {
1285
+ return !!this._def.checks.find((ch) => ch.kind === "int" || (ch.kind === "multipleOf" && util_js_1.util.isInteger(ch.value)));
1286
+ }
1287
+ get isFinite() {
1288
+ let max = null;
1289
+ let min = null;
1290
+ for (const ch of this._def.checks) {
1291
+ if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
1292
+ return true;
1293
+ }
1294
+ else if (ch.kind === "min") {
1295
+ if (min === null || ch.value > min)
1296
+ min = ch.value;
1297
+ }
1298
+ else if (ch.kind === "max") {
1299
+ if (max === null || ch.value < max)
1300
+ max = ch.value;
1301
+ }
1302
+ }
1303
+ return Number.isFinite(min) && Number.isFinite(max);
1304
+ }
1305
+ }
1306
+ exports.YatspNumber = YatspNumber;
1307
+ YatspNumber.create = (params) => {
1308
+ return new YatspNumber({
1309
+ checks: [],
1310
+ typeName: YatspFirstPartyTypeKind.YatspNumber,
1311
+ coerce: params?.coerce || false,
1312
+ ...processCreateParams(params),
1313
+ });
1314
+ };
1315
+ class YatspBigInt extends YatspType {
1316
+ constructor() {
1317
+ super(...arguments);
1318
+ this.min = this.gte;
1319
+ this.max = this.lte;
1320
+ }
1321
+ _parse(input) {
1322
+ if (this._def.coerce) {
1323
+ try {
1324
+ input.data = BigInt(input.data);
1325
+ }
1326
+ catch {
1327
+ return this._getInvalidInput(input);
1328
+ }
1329
+ }
1330
+ const parsedType = this._getType(input);
1331
+ if (parsedType !== util_js_1.YatspParsedType.bigint) {
1332
+ return this._getInvalidInput(input);
1333
+ }
1334
+ let ctx = undefined;
1335
+ const status = new parseUtil_js_1.ParseStatus();
1336
+ for (const check of this._def.checks) {
1337
+ if (check.kind === "min") {
1338
+ const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
1339
+ if (tooSmall) {
1340
+ ctx = this._getOrReturnCtx(input, ctx);
1341
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1342
+ code: YatspError_js_1.YatspIssueCode.too_small,
1343
+ type: "bigint",
1344
+ minimum: check.value,
1345
+ inclusive: check.inclusive,
1346
+ message: check.message,
1347
+ });
1348
+ status.dirty();
1349
+ }
1350
+ }
1351
+ else if (check.kind === "max") {
1352
+ const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
1353
+ if (tooBig) {
1354
+ ctx = this._getOrReturnCtx(input, ctx);
1355
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1356
+ code: YatspError_js_1.YatspIssueCode.too_big,
1357
+ type: "bigint",
1358
+ maximum: check.value,
1359
+ inclusive: check.inclusive,
1360
+ message: check.message,
1361
+ });
1362
+ status.dirty();
1363
+ }
1364
+ }
1365
+ else if (check.kind === "multipleOf") {
1366
+ if (input.data % check.value !== BigInt(0)) {
1367
+ ctx = this._getOrReturnCtx(input, ctx);
1368
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1369
+ code: YatspError_js_1.YatspIssueCode.not_multiple_of,
1370
+ multipleOf: check.value,
1371
+ message: check.message,
1372
+ });
1373
+ status.dirty();
1374
+ }
1375
+ }
1376
+ else {
1377
+ util_js_1.util.assertNever(check);
1378
+ }
1379
+ }
1380
+ return { status: status.value, value: input.data };
1381
+ }
1382
+ _getInvalidInput(input) {
1383
+ const ctx = this._getOrReturnCtx(input);
1384
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1385
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1386
+ expected: util_js_1.YatspParsedType.bigint,
1387
+ received: ctx.parsedType,
1388
+ });
1389
+ return parseUtil_js_1.INVALID;
1390
+ }
1391
+ gte(value, message) {
1392
+ return this.setLimit("min", value, true, errorUtil_js_1.errorUtil.toString(message));
1393
+ }
1394
+ gt(value, message) {
1395
+ return this.setLimit("min", value, false, errorUtil_js_1.errorUtil.toString(message));
1396
+ }
1397
+ lte(value, message) {
1398
+ return this.setLimit("max", value, true, errorUtil_js_1.errorUtil.toString(message));
1399
+ }
1400
+ lt(value, message) {
1401
+ return this.setLimit("max", value, false, errorUtil_js_1.errorUtil.toString(message));
1402
+ }
1403
+ setLimit(kind, value, inclusive, message) {
1404
+ return new YatspBigInt({
1405
+ ...this._def,
1406
+ checks: [
1407
+ ...this._def.checks,
1408
+ {
1409
+ kind,
1410
+ value,
1411
+ inclusive,
1412
+ message: errorUtil_js_1.errorUtil.toString(message),
1413
+ },
1414
+ ],
1415
+ });
1416
+ }
1417
+ _addCheck(check) {
1418
+ return new YatspBigInt({
1419
+ ...this._def,
1420
+ checks: [...this._def.checks, check],
1421
+ });
1422
+ }
1423
+ positive(message) {
1424
+ return this._addCheck({
1425
+ kind: "min",
1426
+ value: BigInt(0),
1427
+ inclusive: false,
1428
+ message: errorUtil_js_1.errorUtil.toString(message),
1429
+ });
1430
+ }
1431
+ negative(message) {
1432
+ return this._addCheck({
1433
+ kind: "max",
1434
+ value: BigInt(0),
1435
+ inclusive: false,
1436
+ message: errorUtil_js_1.errorUtil.toString(message),
1437
+ });
1438
+ }
1439
+ nonpositive(message) {
1440
+ return this._addCheck({
1441
+ kind: "max",
1442
+ value: BigInt(0),
1443
+ inclusive: true,
1444
+ message: errorUtil_js_1.errorUtil.toString(message),
1445
+ });
1446
+ }
1447
+ nonnegative(message) {
1448
+ return this._addCheck({
1449
+ kind: "min",
1450
+ value: BigInt(0),
1451
+ inclusive: true,
1452
+ message: errorUtil_js_1.errorUtil.toString(message),
1453
+ });
1454
+ }
1455
+ multipleOf(value, message) {
1456
+ return this._addCheck({
1457
+ kind: "multipleOf",
1458
+ value,
1459
+ message: errorUtil_js_1.errorUtil.toString(message),
1460
+ });
1461
+ }
1462
+ get minValue() {
1463
+ let min = null;
1464
+ for (const ch of this._def.checks) {
1465
+ if (ch.kind === "min") {
1466
+ if (min === null || ch.value > min)
1467
+ min = ch.value;
1468
+ }
1469
+ }
1470
+ return min;
1471
+ }
1472
+ get maxValue() {
1473
+ let max = null;
1474
+ for (const ch of this._def.checks) {
1475
+ if (ch.kind === "max") {
1476
+ if (max === null || ch.value < max)
1477
+ max = ch.value;
1478
+ }
1479
+ }
1480
+ return max;
1481
+ }
1482
+ }
1483
+ exports.YatspBigInt = YatspBigInt;
1484
+ YatspBigInt.create = (params) => {
1485
+ return new YatspBigInt({
1486
+ checks: [],
1487
+ typeName: YatspFirstPartyTypeKind.YatspBigInt,
1488
+ coerce: params?.coerce ?? false,
1489
+ ...processCreateParams(params),
1490
+ });
1491
+ };
1492
+ class YatspBoolean extends YatspType {
1493
+ _parse(input) {
1494
+ if (this._def.coerce) {
1495
+ input.data = Boolean(input.data);
1496
+ }
1497
+ const parsedType = this._getType(input);
1498
+ if (parsedType !== util_js_1.YatspParsedType.boolean) {
1499
+ const ctx = this._getOrReturnCtx(input);
1500
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1501
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1502
+ expected: util_js_1.YatspParsedType.boolean,
1503
+ received: ctx.parsedType,
1504
+ });
1505
+ return parseUtil_js_1.INVALID;
1506
+ }
1507
+ return (0, parseUtil_js_1.OK)(input.data);
1508
+ }
1509
+ }
1510
+ exports.YatspBoolean = YatspBoolean;
1511
+ YatspBoolean.create = (params) => {
1512
+ return new YatspBoolean({
1513
+ typeName: YatspFirstPartyTypeKind.YatspBoolean,
1514
+ coerce: params?.coerce || false,
1515
+ ...processCreateParams(params),
1516
+ });
1517
+ };
1518
+ class YatspDate extends YatspType {
1519
+ _parse(input) {
1520
+ if (this._def.coerce) {
1521
+ input.data = new Date(input.data);
1522
+ }
1523
+ const parsedType = this._getType(input);
1524
+ if (parsedType !== util_js_1.YatspParsedType.date) {
1525
+ const ctx = this._getOrReturnCtx(input);
1526
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1527
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1528
+ expected: util_js_1.YatspParsedType.date,
1529
+ received: ctx.parsedType,
1530
+ });
1531
+ return parseUtil_js_1.INVALID;
1532
+ }
1533
+ if (Number.isNaN(input.data.getTime())) {
1534
+ const ctx = this._getOrReturnCtx(input);
1535
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1536
+ code: YatspError_js_1.YatspIssueCode.invalid_date,
1537
+ });
1538
+ return parseUtil_js_1.INVALID;
1539
+ }
1540
+ const status = new parseUtil_js_1.ParseStatus();
1541
+ let ctx = undefined;
1542
+ for (const check of this._def.checks) {
1543
+ if (check.kind === "min") {
1544
+ if (input.data.getTime() < check.value) {
1545
+ ctx = this._getOrReturnCtx(input, ctx);
1546
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1547
+ code: YatspError_js_1.YatspIssueCode.too_small,
1548
+ message: check.message,
1549
+ inclusive: true,
1550
+ exact: false,
1551
+ minimum: check.value,
1552
+ type: "date",
1553
+ });
1554
+ status.dirty();
1555
+ }
1556
+ }
1557
+ else if (check.kind === "max") {
1558
+ if (input.data.getTime() > check.value) {
1559
+ ctx = this._getOrReturnCtx(input, ctx);
1560
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1561
+ code: YatspError_js_1.YatspIssueCode.too_big,
1562
+ message: check.message,
1563
+ inclusive: true,
1564
+ exact: false,
1565
+ maximum: check.value,
1566
+ type: "date",
1567
+ });
1568
+ status.dirty();
1569
+ }
1570
+ }
1571
+ else {
1572
+ util_js_1.util.assertNever(check);
1573
+ }
1574
+ }
1575
+ return {
1576
+ status: status.value,
1577
+ value: new Date(input.data.getTime()),
1578
+ };
1579
+ }
1580
+ _addCheck(check) {
1581
+ return new YatspDate({
1582
+ ...this._def,
1583
+ checks: [...this._def.checks, check],
1584
+ });
1585
+ }
1586
+ min(minDate, message) {
1587
+ return this._addCheck({
1588
+ kind: "min",
1589
+ value: minDate.getTime(),
1590
+ message: errorUtil_js_1.errorUtil.toString(message),
1591
+ });
1592
+ }
1593
+ max(maxDate, message) {
1594
+ return this._addCheck({
1595
+ kind: "max",
1596
+ value: maxDate.getTime(),
1597
+ message: errorUtil_js_1.errorUtil.toString(message),
1598
+ });
1599
+ }
1600
+ get minDate() {
1601
+ let min = null;
1602
+ for (const ch of this._def.checks) {
1603
+ if (ch.kind === "min") {
1604
+ if (min === null || ch.value > min)
1605
+ min = ch.value;
1606
+ }
1607
+ }
1608
+ return min != null ? new Date(min) : null;
1609
+ }
1610
+ get maxDate() {
1611
+ let max = null;
1612
+ for (const ch of this._def.checks) {
1613
+ if (ch.kind === "max") {
1614
+ if (max === null || ch.value < max)
1615
+ max = ch.value;
1616
+ }
1617
+ }
1618
+ return max != null ? new Date(max) : null;
1619
+ }
1620
+ }
1621
+ exports.YatspDate = YatspDate;
1622
+ YatspDate.create = (params) => {
1623
+ return new YatspDate({
1624
+ checks: [],
1625
+ coerce: params?.coerce || false,
1626
+ typeName: YatspFirstPartyTypeKind.YatspDate,
1627
+ ...processCreateParams(params),
1628
+ });
1629
+ };
1630
+ class YatspSymbol extends YatspType {
1631
+ _parse(input) {
1632
+ const parsedType = this._getType(input);
1633
+ if (parsedType !== util_js_1.YatspParsedType.symbol) {
1634
+ const ctx = this._getOrReturnCtx(input);
1635
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1636
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1637
+ expected: util_js_1.YatspParsedType.symbol,
1638
+ received: ctx.parsedType,
1639
+ });
1640
+ return parseUtil_js_1.INVALID;
1641
+ }
1642
+ return (0, parseUtil_js_1.OK)(input.data);
1643
+ }
1644
+ }
1645
+ exports.YatspSymbol = YatspSymbol;
1646
+ YatspSymbol.create = (params) => {
1647
+ return new YatspSymbol({
1648
+ typeName: YatspFirstPartyTypeKind.YatspSymbol,
1649
+ ...processCreateParams(params),
1650
+ });
1651
+ };
1652
+ class YatspUndefined extends YatspType {
1653
+ _parse(input) {
1654
+ const parsedType = this._getType(input);
1655
+ if (parsedType !== util_js_1.YatspParsedType.undefined) {
1656
+ const ctx = this._getOrReturnCtx(input);
1657
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1658
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1659
+ expected: util_js_1.YatspParsedType.undefined,
1660
+ received: ctx.parsedType,
1661
+ });
1662
+ return parseUtil_js_1.INVALID;
1663
+ }
1664
+ return (0, parseUtil_js_1.OK)(input.data);
1665
+ }
1666
+ }
1667
+ exports.YatspUndefined = YatspUndefined;
1668
+ YatspUndefined.create = (params) => {
1669
+ return new YatspUndefined({
1670
+ typeName: YatspFirstPartyTypeKind.YatspUndefined,
1671
+ ...processCreateParams(params),
1672
+ });
1673
+ };
1674
+ class YatspNull extends YatspType {
1675
+ _parse(input) {
1676
+ const parsedType = this._getType(input);
1677
+ if (parsedType !== util_js_1.YatspParsedType.null) {
1678
+ const ctx = this._getOrReturnCtx(input);
1679
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1680
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1681
+ expected: util_js_1.YatspParsedType.null,
1682
+ received: ctx.parsedType,
1683
+ });
1684
+ return parseUtil_js_1.INVALID;
1685
+ }
1686
+ return (0, parseUtil_js_1.OK)(input.data);
1687
+ }
1688
+ }
1689
+ exports.YatspNull = YatspNull;
1690
+ YatspNull.create = (params) => {
1691
+ return new YatspNull({
1692
+ typeName: YatspFirstPartyTypeKind.YatspNull,
1693
+ ...processCreateParams(params),
1694
+ });
1695
+ };
1696
+ class YatspAny extends YatspType {
1697
+ constructor() {
1698
+ super(...arguments);
1699
+ // to prevent instances of other classes from extending YatspAny. this causes issues with catchall in YatspObject.
1700
+ this._any = true;
1701
+ }
1702
+ _parse(input) {
1703
+ return (0, parseUtil_js_1.OK)(input.data);
1704
+ }
1705
+ }
1706
+ exports.YatspAny = YatspAny;
1707
+ YatspAny.create = (params) => {
1708
+ return new YatspAny({
1709
+ typeName: YatspFirstPartyTypeKind.YatspAny,
1710
+ ...processCreateParams(params),
1711
+ });
1712
+ };
1713
+ class YatspUnknown extends YatspType {
1714
+ constructor() {
1715
+ super(...arguments);
1716
+ // required
1717
+ this._unknown = true;
1718
+ }
1719
+ _parse(input) {
1720
+ return (0, parseUtil_js_1.OK)(input.data);
1721
+ }
1722
+ }
1723
+ exports.YatspUnknown = YatspUnknown;
1724
+ YatspUnknown.create = (params) => {
1725
+ return new YatspUnknown({
1726
+ typeName: YatspFirstPartyTypeKind.YatspUnknown,
1727
+ ...processCreateParams(params),
1728
+ });
1729
+ };
1730
+ class YatspNever extends YatspType {
1731
+ _parse(input) {
1732
+ const ctx = this._getOrReturnCtx(input);
1733
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1734
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1735
+ expected: util_js_1.YatspParsedType.never,
1736
+ received: ctx.parsedType,
1737
+ });
1738
+ return parseUtil_js_1.INVALID;
1739
+ }
1740
+ }
1741
+ exports.YatspNever = YatspNever;
1742
+ YatspNever.create = (params) => {
1743
+ return new YatspNever({
1744
+ typeName: YatspFirstPartyTypeKind.YatspNever,
1745
+ ...processCreateParams(params),
1746
+ });
1747
+ };
1748
+ class YatspVoid extends YatspType {
1749
+ _parse(input) {
1750
+ const parsedType = this._getType(input);
1751
+ if (parsedType !== util_js_1.YatspParsedType.undefined) {
1752
+ const ctx = this._getOrReturnCtx(input);
1753
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1754
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1755
+ expected: util_js_1.YatspParsedType.void,
1756
+ received: ctx.parsedType,
1757
+ });
1758
+ return parseUtil_js_1.INVALID;
1759
+ }
1760
+ return (0, parseUtil_js_1.OK)(input.data);
1761
+ }
1762
+ }
1763
+ exports.YatspVoid = YatspVoid;
1764
+ YatspVoid.create = (params) => {
1765
+ return new YatspVoid({
1766
+ typeName: YatspFirstPartyTypeKind.YatspVoid,
1767
+ ...processCreateParams(params),
1768
+ });
1769
+ };
1770
+ class YatspArray extends YatspType {
1771
+ _parse(input) {
1772
+ const { ctx, status } = this._processInputParams(input);
1773
+ const def = this._def;
1774
+ if (ctx.parsedType !== util_js_1.YatspParsedType.array) {
1775
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1776
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1777
+ expected: util_js_1.YatspParsedType.array,
1778
+ received: ctx.parsedType,
1779
+ });
1780
+ return parseUtil_js_1.INVALID;
1781
+ }
1782
+ if (def.exactLength !== null) {
1783
+ const tooBig = ctx.data.length > def.exactLength.value;
1784
+ const tooSmall = ctx.data.length < def.exactLength.value;
1785
+ if (tooBig || tooSmall) {
1786
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1787
+ code: tooBig ? YatspError_js_1.YatspIssueCode.too_big : YatspError_js_1.YatspIssueCode.too_small,
1788
+ minimum: (tooSmall ? def.exactLength.value : undefined),
1789
+ maximum: (tooBig ? def.exactLength.value : undefined),
1790
+ type: "array",
1791
+ inclusive: true,
1792
+ exact: true,
1793
+ message: def.exactLength.message,
1794
+ });
1795
+ status.dirty();
1796
+ }
1797
+ }
1798
+ if (def.minLength !== null) {
1799
+ if (ctx.data.length < def.minLength.value) {
1800
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1801
+ code: YatspError_js_1.YatspIssueCode.too_small,
1802
+ minimum: def.minLength.value,
1803
+ type: "array",
1804
+ inclusive: true,
1805
+ exact: false,
1806
+ message: def.minLength.message,
1807
+ });
1808
+ status.dirty();
1809
+ }
1810
+ }
1811
+ if (def.maxLength !== null) {
1812
+ if (ctx.data.length > def.maxLength.value) {
1813
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1814
+ code: YatspError_js_1.YatspIssueCode.too_big,
1815
+ maximum: def.maxLength.value,
1816
+ type: "array",
1817
+ inclusive: true,
1818
+ exact: false,
1819
+ message: def.maxLength.message,
1820
+ });
1821
+ status.dirty();
1822
+ }
1823
+ }
1824
+ if (ctx.common.async) {
1825
+ return Promise.all([...ctx.data].map((item, i) => {
1826
+ return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1827
+ })).then((result) => {
1828
+ return parseUtil_js_1.ParseStatus.mergeArray(status, result);
1829
+ });
1830
+ }
1831
+ const result = [...ctx.data].map((item, i) => {
1832
+ return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1833
+ });
1834
+ return parseUtil_js_1.ParseStatus.mergeArray(status, result);
1835
+ }
1836
+ get element() {
1837
+ return this._def.type;
1838
+ }
1839
+ min(minLength, message) {
1840
+ return new YatspArray({
1841
+ ...this._def,
1842
+ minLength: { value: minLength, message: errorUtil_js_1.errorUtil.toString(message) },
1843
+ });
1844
+ }
1845
+ max(maxLength, message) {
1846
+ return new YatspArray({
1847
+ ...this._def,
1848
+ maxLength: { value: maxLength, message: errorUtil_js_1.errorUtil.toString(message) },
1849
+ });
1850
+ }
1851
+ length(len, message) {
1852
+ return new YatspArray({
1853
+ ...this._def,
1854
+ exactLength: { value: len, message: errorUtil_js_1.errorUtil.toString(message) },
1855
+ });
1856
+ }
1857
+ nonempty(message) {
1858
+ return this.min(1, message);
1859
+ }
1860
+ }
1861
+ exports.YatspArray = YatspArray;
1862
+ YatspArray.create = (schema, params) => {
1863
+ return new YatspArray({
1864
+ type: schema,
1865
+ minLength: null,
1866
+ maxLength: null,
1867
+ exactLength: null,
1868
+ typeName: YatspFirstPartyTypeKind.YatspArray,
1869
+ ...processCreateParams(params),
1870
+ });
1871
+ };
1872
+ function deepPartialify(schema) {
1873
+ if (schema instanceof YatspObject) {
1874
+ const newShape = {};
1875
+ for (const key in schema.shape) {
1876
+ const fieldSchema = schema.shape[key];
1877
+ newShape[key] = YatspOptional.create(deepPartialify(fieldSchema));
1878
+ }
1879
+ return new YatspObject({
1880
+ ...schema._def,
1881
+ shape: () => newShape,
1882
+ });
1883
+ }
1884
+ else if (schema instanceof YatspArray) {
1885
+ return new YatspArray({
1886
+ ...schema._def,
1887
+ type: deepPartialify(schema.element),
1888
+ });
1889
+ }
1890
+ else if (schema instanceof YatspOptional) {
1891
+ return YatspOptional.create(deepPartialify(schema.unwrap()));
1892
+ }
1893
+ else if (schema instanceof YatspNullable) {
1894
+ return YatspNullable.create(deepPartialify(schema.unwrap()));
1895
+ }
1896
+ else if (schema instanceof YatspTuple) {
1897
+ return YatspTuple.create(schema.items.map((item) => deepPartialify(item)));
1898
+ }
1899
+ else {
1900
+ return schema;
1901
+ }
1902
+ }
1903
+ class YatspObject extends YatspType {
1904
+ constructor() {
1905
+ super(...arguments);
1906
+ this._cached = null;
1907
+ /**
1908
+ * @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
1909
+ * If you want to pass through unknown properties, use `.passthrough()` instead.
1910
+ */
1911
+ this.nonstrict = this.passthrough;
1912
+ // extend<
1913
+ // Augmentation extends YatspRawShape,
1914
+ // NewOutput extends util.flatten<{
1915
+ // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
1916
+ // ? Augmentation[k]["_output"]
1917
+ // : k extends keyof Output
1918
+ // ? Output[k]
1919
+ // : never;
1920
+ // }>,
1921
+ // NewInput extends util.flatten<{
1922
+ // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
1923
+ // ? Augmentation[k]["_input"]
1924
+ // : k extends keyof Input
1925
+ // ? Input[k]
1926
+ // : never;
1927
+ // }>
1928
+ // >(
1929
+ // augmentation: Augmentation
1930
+ // ): YatspObject<
1931
+ // extendShape<T, Augmentation>,
1932
+ // UnknownKeys,
1933
+ // Catchall,
1934
+ // NewOutput,
1935
+ // NewInput
1936
+ // > {
1937
+ // return new YatspObject({
1938
+ // ...this._def,
1939
+ // shape: () => ({
1940
+ // ...this._def.shape(),
1941
+ // ...augmentation,
1942
+ // }),
1943
+ // }) as any;
1944
+ // }
1945
+ /**
1946
+ * @deprecated Use `.extend` instead
1947
+ * */
1948
+ this.augment = this.extend;
1949
+ }
1950
+ _getCached() {
1951
+ if (this._cached !== null)
1952
+ return this._cached;
1953
+ const shape = this._def.shape();
1954
+ const keys = util_js_1.util.objectKeys(shape);
1955
+ this._cached = { shape, keys };
1956
+ return this._cached;
1957
+ }
1958
+ _parse(input) {
1959
+ const parsedType = this._getType(input);
1960
+ if (parsedType !== util_js_1.YatspParsedType.object) {
1961
+ const ctx = this._getOrReturnCtx(input);
1962
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
1963
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
1964
+ expected: util_js_1.YatspParsedType.object,
1965
+ received: ctx.parsedType,
1966
+ });
1967
+ return parseUtil_js_1.INVALID;
1968
+ }
1969
+ const { status, ctx } = this._processInputParams(input);
1970
+ const { shape, keys: shapeKeys } = this._getCached();
1971
+ const extraKeys = [];
1972
+ if (!(this._def.catchall instanceof YatspNever && this._def.unknownKeys === "strip")) {
1973
+ for (const key in ctx.data) {
1974
+ if (!shapeKeys.includes(key)) {
1975
+ extraKeys.push(key);
1976
+ }
1977
+ }
1978
+ }
1979
+ const pairs = [];
1980
+ for (const key of shapeKeys) {
1981
+ const keyValidator = shape[key];
1982
+ const value = ctx.data[key];
1983
+ pairs.push({
1984
+ key: { status: "valid", value: key },
1985
+ value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
1986
+ alwaysSet: key in ctx.data,
1987
+ });
1988
+ }
1989
+ if (this._def.catchall instanceof YatspNever) {
1990
+ const unknownKeys = this._def.unknownKeys;
1991
+ if (unknownKeys === "passthrough") {
1992
+ for (const key of extraKeys) {
1993
+ pairs.push({
1994
+ key: { status: "valid", value: key },
1995
+ value: { status: "valid", value: ctx.data[key] },
1996
+ });
1997
+ }
1998
+ }
1999
+ else if (unknownKeys === "strict") {
2000
+ if (extraKeys.length > 0) {
2001
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2002
+ code: YatspError_js_1.YatspIssueCode.unrecognized_keys,
2003
+ keys: extraKeys,
2004
+ });
2005
+ status.dirty();
2006
+ }
2007
+ }
2008
+ else if (unknownKeys === "strip") {
2009
+ }
2010
+ else {
2011
+ throw new Error(`Internal YatspObject error: invalid unknownKeys value.`);
2012
+ }
2013
+ }
2014
+ else {
2015
+ // run catchall validation
2016
+ const catchall = this._def.catchall;
2017
+ for (const key of extraKeys) {
2018
+ const value = ctx.data[key];
2019
+ pairs.push({
2020
+ key: { status: "valid", value: key },
2021
+ value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
2022
+ ),
2023
+ alwaysSet: key in ctx.data,
2024
+ });
2025
+ }
2026
+ }
2027
+ if (ctx.common.async) {
2028
+ return Promise.resolve()
2029
+ .then(async () => {
2030
+ const syncPairs = [];
2031
+ for (const pair of pairs) {
2032
+ const key = await pair.key;
2033
+ const value = await pair.value;
2034
+ syncPairs.push({
2035
+ key,
2036
+ value,
2037
+ alwaysSet: pair.alwaysSet,
2038
+ });
2039
+ }
2040
+ return syncPairs;
2041
+ })
2042
+ .then((syncPairs) => {
2043
+ return parseUtil_js_1.ParseStatus.mergeObjectSync(status, syncPairs);
2044
+ });
2045
+ }
2046
+ else {
2047
+ return parseUtil_js_1.ParseStatus.mergeObjectSync(status, pairs);
2048
+ }
2049
+ }
2050
+ get shape() {
2051
+ return this._def.shape();
2052
+ }
2053
+ strict(message) {
2054
+ errorUtil_js_1.errorUtil.errToObj;
2055
+ return new YatspObject({
2056
+ ...this._def,
2057
+ unknownKeys: "strict",
2058
+ ...(message !== undefined
2059
+ ? {
2060
+ errorMap: (issue, ctx) => {
2061
+ const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
2062
+ if (issue.code === "unrecognized_keys")
2063
+ return {
2064
+ message: errorUtil_js_1.errorUtil.errToObj(message).message ?? defaultError,
2065
+ };
2066
+ return {
2067
+ message: defaultError,
2068
+ };
2069
+ },
2070
+ }
2071
+ : {}),
2072
+ });
2073
+ }
2074
+ strip() {
2075
+ return new YatspObject({
2076
+ ...this._def,
2077
+ unknownKeys: "strip",
2078
+ });
2079
+ }
2080
+ passthrough() {
2081
+ return new YatspObject({
2082
+ ...this._def,
2083
+ unknownKeys: "passthrough",
2084
+ });
2085
+ }
2086
+ // const AugmentFactory =
2087
+ // <Def extends YatspObjectDef>(def: Def) =>
2088
+ // <Augmentation extends YatspRawShape>(
2089
+ // augmentation: Augmentation
2090
+ // ): YatspObject<
2091
+ // extendShape<ReturnType<Def["shape"]>, Augmentation>,
2092
+ // Def["unknownKeys"],
2093
+ // Def["catchall"]
2094
+ // > => {
2095
+ // return new YatspObject({
2096
+ // ...def,
2097
+ // shape: () => ({
2098
+ // ...def.shape(),
2099
+ // ...augmentation,
2100
+ // }),
2101
+ // }) as any;
2102
+ // };
2103
+ extend(augmentation) {
2104
+ return new YatspObject({
2105
+ ...this._def,
2106
+ shape: () => ({
2107
+ ...this._def.shape(),
2108
+ ...augmentation,
2109
+ }),
2110
+ });
2111
+ }
2112
+ /**
2113
+ * Prior to yatsp@1.0.12 there was a bug in the
2114
+ * inferred type of merged objects. Please
2115
+ * upgrade if you are experiencing issues.
2116
+ */
2117
+ merge(merging) {
2118
+ const merged = new YatspObject({
2119
+ unknownKeys: merging._def.unknownKeys,
2120
+ catchall: merging._def.catchall,
2121
+ shape: () => ({
2122
+ ...this._def.shape(),
2123
+ ...merging._def.shape(),
2124
+ }),
2125
+ typeName: YatspFirstPartyTypeKind.YatspObject,
2126
+ });
2127
+ return merged;
2128
+ }
2129
+ // merge<
2130
+ // Incoming extends AnyYatspObject,
2131
+ // Augmentation extends Incoming["shape"],
2132
+ // NewOutput extends {
2133
+ // [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
2134
+ // ? Augmentation[k]["_output"]
2135
+ // : k extends keyof Output
2136
+ // ? Output[k]
2137
+ // : never;
2138
+ // },
2139
+ // NewInput extends {
2140
+ // [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
2141
+ // ? Augmentation[k]["_input"]
2142
+ // : k extends keyof Input
2143
+ // ? Input[k]
2144
+ // : never;
2145
+ // }
2146
+ // >(
2147
+ // merging: Incoming
2148
+ // ): YatspObject<
2149
+ // extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
2150
+ // Incoming["_def"]["unknownKeys"],
2151
+ // Incoming["_def"]["catchall"],
2152
+ // NewOutput,
2153
+ // NewInput
2154
+ // > {
2155
+ // const merged: any = new YatspObject({
2156
+ // unknownKeys: merging._def.unknownKeys,
2157
+ // catchall: merging._def.catchall,
2158
+ // shape: () =>
2159
+ // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
2160
+ // typeName: YatspFirstPartyTypeKind.YatspObject,
2161
+ // }) as any;
2162
+ // return merged;
2163
+ // }
2164
+ setKey(key, schema) {
2165
+ return this.augment({ [key]: schema });
2166
+ }
2167
+ // merge<Incoming extends AnyYatspObject>(
2168
+ // merging: Incoming
2169
+ // ): //YatspObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
2170
+ // YatspObject<
2171
+ // extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
2172
+ // Incoming["_def"]["unknownKeys"],
2173
+ // Incoming["_def"]["catchall"]
2174
+ // > {
2175
+ // // const mergedShape = objectUtil.mergeShapes(
2176
+ // // this._def.shape(),
2177
+ // // merging._def.shape()
2178
+ // // );
2179
+ // const merged: any = new YatspObject({
2180
+ // unknownKeys: merging._def.unknownKeys,
2181
+ // catchall: merging._def.catchall,
2182
+ // shape: () =>
2183
+ // objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
2184
+ // typeName: YatspFirstPartyTypeKind.YatspObject,
2185
+ // }) as any;
2186
+ // return merged;
2187
+ // }
2188
+ catchall(index) {
2189
+ return new YatspObject({
2190
+ ...this._def,
2191
+ catchall: index,
2192
+ });
2193
+ }
2194
+ pick(mask) {
2195
+ const shape = {};
2196
+ for (const key of util_js_1.util.objectKeys(mask)) {
2197
+ if (mask[key] && this.shape[key]) {
2198
+ shape[key] = this.shape[key];
2199
+ }
2200
+ }
2201
+ return new YatspObject({
2202
+ ...this._def,
2203
+ shape: () => shape,
2204
+ });
2205
+ }
2206
+ omit(mask) {
2207
+ const shape = {};
2208
+ for (const key of util_js_1.util.objectKeys(this.shape)) {
2209
+ if (!mask[key]) {
2210
+ shape[key] = this.shape[key];
2211
+ }
2212
+ }
2213
+ return new YatspObject({
2214
+ ...this._def,
2215
+ shape: () => shape,
2216
+ });
2217
+ }
2218
+ /**
2219
+ * @deprecated
2220
+ */
2221
+ deepPartial() {
2222
+ return deepPartialify(this);
2223
+ }
2224
+ partial(mask) {
2225
+ const newShape = {};
2226
+ for (const key of util_js_1.util.objectKeys(this.shape)) {
2227
+ const fieldSchema = this.shape[key];
2228
+ if (mask && !mask[key]) {
2229
+ newShape[key] = fieldSchema;
2230
+ }
2231
+ else {
2232
+ newShape[key] = fieldSchema.optional();
2233
+ }
2234
+ }
2235
+ return new YatspObject({
2236
+ ...this._def,
2237
+ shape: () => newShape,
2238
+ });
2239
+ }
2240
+ required(mask) {
2241
+ const newShape = {};
2242
+ for (const key of util_js_1.util.objectKeys(this.shape)) {
2243
+ if (mask && !mask[key]) {
2244
+ newShape[key] = this.shape[key];
2245
+ }
2246
+ else {
2247
+ const fieldSchema = this.shape[key];
2248
+ let newField = fieldSchema;
2249
+ while (newField instanceof YatspOptional) {
2250
+ newField = newField._def.innerType;
2251
+ }
2252
+ newShape[key] = newField;
2253
+ }
2254
+ }
2255
+ return new YatspObject({
2256
+ ...this._def,
2257
+ shape: () => newShape,
2258
+ });
2259
+ }
2260
+ keyof() {
2261
+ return createYatspEnum(util_js_1.util.objectKeys(this.shape));
2262
+ }
2263
+ }
2264
+ exports.YatspObject = YatspObject;
2265
+ YatspObject.create = (shape, params) => {
2266
+ return new YatspObject({
2267
+ shape: () => shape,
2268
+ unknownKeys: "strip",
2269
+ catchall: YatspNever.create(),
2270
+ typeName: YatspFirstPartyTypeKind.YatspObject,
2271
+ ...processCreateParams(params),
2272
+ });
2273
+ };
2274
+ YatspObject.strictCreate = (shape, params) => {
2275
+ return new YatspObject({
2276
+ shape: () => shape,
2277
+ unknownKeys: "strict",
2278
+ catchall: YatspNever.create(),
2279
+ typeName: YatspFirstPartyTypeKind.YatspObject,
2280
+ ...processCreateParams(params),
2281
+ });
2282
+ };
2283
+ YatspObject.lazycreate = (shape, params) => {
2284
+ return new YatspObject({
2285
+ shape,
2286
+ unknownKeys: "strip",
2287
+ catchall: YatspNever.create(),
2288
+ typeName: YatspFirstPartyTypeKind.YatspObject,
2289
+ ...processCreateParams(params),
2290
+ });
2291
+ };
2292
+ class YatspUnion extends YatspType {
2293
+ _parse(input) {
2294
+ const { ctx } = this._processInputParams(input);
2295
+ const options = this._def.options;
2296
+ function handleResults(results) {
2297
+ // return first issue-free validation if it exists
2298
+ for (const result of results) {
2299
+ if (result.result.status === "valid") {
2300
+ return result.result;
2301
+ }
2302
+ }
2303
+ for (const result of results) {
2304
+ if (result.result.status === "dirty") {
2305
+ // add issues from dirty option
2306
+ ctx.common.issues.push(...result.ctx.common.issues);
2307
+ return result.result;
2308
+ }
2309
+ }
2310
+ // return invalid
2311
+ const unionErrors = results.map((result) => new YatspError_js_1.YatspError(result.ctx.common.issues));
2312
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2313
+ code: YatspError_js_1.YatspIssueCode.invalid_union,
2314
+ unionErrors,
2315
+ });
2316
+ return parseUtil_js_1.INVALID;
2317
+ }
2318
+ if (ctx.common.async) {
2319
+ return Promise.all(options.map(async (option) => {
2320
+ const childCtx = {
2321
+ ...ctx,
2322
+ common: {
2323
+ ...ctx.common,
2324
+ issues: [],
2325
+ },
2326
+ parent: null,
2327
+ };
2328
+ return {
2329
+ result: await option._parseAsync({
2330
+ data: ctx.data,
2331
+ path: ctx.path,
2332
+ parent: childCtx,
2333
+ }),
2334
+ ctx: childCtx,
2335
+ };
2336
+ })).then(handleResults);
2337
+ }
2338
+ else {
2339
+ let dirty = undefined;
2340
+ const issues = [];
2341
+ for (const option of options) {
2342
+ const childCtx = {
2343
+ ...ctx,
2344
+ common: {
2345
+ ...ctx.common,
2346
+ issues: [],
2347
+ },
2348
+ parent: null,
2349
+ };
2350
+ const result = option._parseSync({
2351
+ data: ctx.data,
2352
+ path: ctx.path,
2353
+ parent: childCtx,
2354
+ });
2355
+ if (result.status === "valid") {
2356
+ return result;
2357
+ }
2358
+ else if (result.status === "dirty" && !dirty) {
2359
+ dirty = { result, ctx: childCtx };
2360
+ }
2361
+ if (childCtx.common.issues.length) {
2362
+ issues.push(childCtx.common.issues);
2363
+ }
2364
+ }
2365
+ if (dirty) {
2366
+ ctx.common.issues.push(...dirty.ctx.common.issues);
2367
+ return dirty.result;
2368
+ }
2369
+ const unionErrors = issues.map((issues) => new YatspError_js_1.YatspError(issues));
2370
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2371
+ code: YatspError_js_1.YatspIssueCode.invalid_union,
2372
+ unionErrors,
2373
+ });
2374
+ return parseUtil_js_1.INVALID;
2375
+ }
2376
+ }
2377
+ get options() {
2378
+ return this._def.options;
2379
+ }
2380
+ }
2381
+ exports.YatspUnion = YatspUnion;
2382
+ YatspUnion.create = (types, params) => {
2383
+ return new YatspUnion({
2384
+ options: types,
2385
+ typeName: YatspFirstPartyTypeKind.YatspUnion,
2386
+ ...processCreateParams(params),
2387
+ });
2388
+ };
2389
+ /////////////////////////////////////////////////////
2390
+ /////////////////////////////////////////////////////
2391
+ ////////// //////////
2392
+ ////////// YatspDiscriminatedUnion //////////
2393
+ ////////// //////////
2394
+ /////////////////////////////////////////////////////
2395
+ /////////////////////////////////////////////////////
2396
+ const getDiscriminator = (type) => {
2397
+ if (type instanceof YatspLazy) {
2398
+ return getDiscriminator(type.schema);
2399
+ }
2400
+ else if (type instanceof YatspEffects) {
2401
+ return getDiscriminator(type.innerType());
2402
+ }
2403
+ else if (type instanceof YatspLiteral) {
2404
+ return [type.value];
2405
+ }
2406
+ else if (type instanceof YatspEnum) {
2407
+ return type.options;
2408
+ }
2409
+ else if (type instanceof YatspNativeEnum) {
2410
+ // eslint-disable-next-line ban/ban
2411
+ return util_js_1.util.objectValues(type.enum);
2412
+ }
2413
+ else if (type instanceof YatspDefault) {
2414
+ return getDiscriminator(type._def.innerType);
2415
+ }
2416
+ else if (type instanceof YatspUndefined) {
2417
+ return [undefined];
2418
+ }
2419
+ else if (type instanceof YatspNull) {
2420
+ return [null];
2421
+ }
2422
+ else if (type instanceof YatspOptional) {
2423
+ return [undefined, ...getDiscriminator(type.unwrap())];
2424
+ }
2425
+ else if (type instanceof YatspNullable) {
2426
+ return [null, ...getDiscriminator(type.unwrap())];
2427
+ }
2428
+ else if (type instanceof YatspBranded) {
2429
+ return getDiscriminator(type.unwrap());
2430
+ }
2431
+ else if (type instanceof YatspReadonly) {
2432
+ return getDiscriminator(type.unwrap());
2433
+ }
2434
+ else if (type instanceof YatspCatch) {
2435
+ return getDiscriminator(type._def.innerType);
2436
+ }
2437
+ else {
2438
+ return [];
2439
+ }
2440
+ };
2441
+ class YatspDiscriminatedUnion extends YatspType {
2442
+ _parse(input) {
2443
+ const { ctx } = this._processInputParams(input);
2444
+ if (ctx.parsedType !== util_js_1.YatspParsedType.object) {
2445
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2446
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2447
+ expected: util_js_1.YatspParsedType.object,
2448
+ received: ctx.parsedType,
2449
+ });
2450
+ return parseUtil_js_1.INVALID;
2451
+ }
2452
+ const discriminator = this.discriminator;
2453
+ const discriminatorValue = ctx.data[discriminator];
2454
+ const option = this.optionsMap.get(discriminatorValue);
2455
+ if (!option) {
2456
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2457
+ code: YatspError_js_1.YatspIssueCode.invalid_union_discriminator,
2458
+ options: Array.from(this.optionsMap.keys()),
2459
+ path: [discriminator],
2460
+ });
2461
+ return parseUtil_js_1.INVALID;
2462
+ }
2463
+ if (ctx.common.async) {
2464
+ return option._parseAsync({
2465
+ data: ctx.data,
2466
+ path: ctx.path,
2467
+ parent: ctx,
2468
+ });
2469
+ }
2470
+ else {
2471
+ return option._parseSync({
2472
+ data: ctx.data,
2473
+ path: ctx.path,
2474
+ parent: ctx,
2475
+ });
2476
+ }
2477
+ }
2478
+ get discriminator() {
2479
+ return this._def.discriminator;
2480
+ }
2481
+ get options() {
2482
+ return this._def.options;
2483
+ }
2484
+ get optionsMap() {
2485
+ return this._def.optionsMap;
2486
+ }
2487
+ /**
2488
+ * The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
2489
+ * However, it only allows a union of objects, all of which need to share a discriminator property. This property must
2490
+ * have a different value for each object in the union.
2491
+ * @param discriminator the name of the discriminator property
2492
+ * @param types an array of object schemas
2493
+ * @param params
2494
+ */
2495
+ static create(discriminator, options, params) {
2496
+ // Get all the valid discriminator values
2497
+ const optionsMap = new Map();
2498
+ // try {
2499
+ for (const type of options) {
2500
+ const discriminatorValues = getDiscriminator(type.shape[discriminator]);
2501
+ if (!discriminatorValues.length) {
2502
+ throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
2503
+ }
2504
+ for (const value of discriminatorValues) {
2505
+ if (optionsMap.has(value)) {
2506
+ throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
2507
+ }
2508
+ optionsMap.set(value, type);
2509
+ }
2510
+ }
2511
+ return new YatspDiscriminatedUnion({
2512
+ typeName: YatspFirstPartyTypeKind.YatspDiscriminatedUnion,
2513
+ discriminator,
2514
+ options,
2515
+ optionsMap,
2516
+ ...processCreateParams(params),
2517
+ });
2518
+ }
2519
+ }
2520
+ exports.YatspDiscriminatedUnion = YatspDiscriminatedUnion;
2521
+ function mergeValues(a, b) {
2522
+ const aType = (0, util_js_1.getParsedType)(a);
2523
+ const bType = (0, util_js_1.getParsedType)(b);
2524
+ if (a === b) {
2525
+ return { valid: true, data: a };
2526
+ }
2527
+ else if (aType === util_js_1.YatspParsedType.object && bType === util_js_1.YatspParsedType.object) {
2528
+ const bKeys = util_js_1.util.objectKeys(b);
2529
+ const sharedKeys = util_js_1.util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
2530
+ const newObj = { ...a, ...b };
2531
+ if (Object.prototype.hasOwnProperty.call(newObj, "__proto__"))
2532
+ delete newObj.__proto__;
2533
+ for (const key of sharedKeys) {
2534
+ if (key === "__proto__")
2535
+ continue;
2536
+ const sharedValue = mergeValues(a[key], b[key]);
2537
+ if (!sharedValue.valid) {
2538
+ return { valid: false };
2539
+ }
2540
+ newObj[key] = sharedValue.data;
2541
+ }
2542
+ return { valid: true, data: newObj };
2543
+ }
2544
+ else if (aType === util_js_1.YatspParsedType.array && bType === util_js_1.YatspParsedType.array) {
2545
+ if (a.length !== b.length) {
2546
+ return { valid: false };
2547
+ }
2548
+ const newArray = [];
2549
+ for (let index = 0; index < a.length; index++) {
2550
+ const itemA = a[index];
2551
+ const itemB = b[index];
2552
+ const sharedValue = mergeValues(itemA, itemB);
2553
+ if (!sharedValue.valid) {
2554
+ return { valid: false };
2555
+ }
2556
+ newArray.push(sharedValue.data);
2557
+ }
2558
+ return { valid: true, data: newArray };
2559
+ }
2560
+ else if (aType === util_js_1.YatspParsedType.date && bType === util_js_1.YatspParsedType.date && +a === +b) {
2561
+ return { valid: true, data: a };
2562
+ }
2563
+ else {
2564
+ return { valid: false };
2565
+ }
2566
+ }
2567
+ class YatspIntersection extends YatspType {
2568
+ _parse(input) {
2569
+ const { status, ctx } = this._processInputParams(input);
2570
+ const handleParsed = (parsedLeft, parsedRight) => {
2571
+ if ((0, parseUtil_js_1.isAborted)(parsedLeft) || (0, parseUtil_js_1.isAborted)(parsedRight)) {
2572
+ return parseUtil_js_1.INVALID;
2573
+ }
2574
+ const merged = mergeValues(parsedLeft.value, parsedRight.value);
2575
+ if (!merged.valid) {
2576
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2577
+ code: YatspError_js_1.YatspIssueCode.invalid_intersection_types,
2578
+ });
2579
+ return parseUtil_js_1.INVALID;
2580
+ }
2581
+ if ((0, parseUtil_js_1.isDirty)(parsedLeft) || (0, parseUtil_js_1.isDirty)(parsedRight)) {
2582
+ status.dirty();
2583
+ }
2584
+ return { status: status.value, value: merged.data };
2585
+ };
2586
+ if (ctx.common.async) {
2587
+ return Promise.all([
2588
+ this._def.left._parseAsync({
2589
+ data: ctx.data,
2590
+ path: ctx.path,
2591
+ parent: ctx,
2592
+ }),
2593
+ this._def.right._parseAsync({
2594
+ data: ctx.data,
2595
+ path: ctx.path,
2596
+ parent: ctx,
2597
+ }),
2598
+ ]).then(([left, right]) => handleParsed(left, right));
2599
+ }
2600
+ else {
2601
+ return handleParsed(this._def.left._parseSync({
2602
+ data: ctx.data,
2603
+ path: ctx.path,
2604
+ parent: ctx,
2605
+ }), this._def.right._parseSync({
2606
+ data: ctx.data,
2607
+ path: ctx.path,
2608
+ parent: ctx,
2609
+ }));
2610
+ }
2611
+ }
2612
+ }
2613
+ exports.YatspIntersection = YatspIntersection;
2614
+ YatspIntersection.create = (left, right, params) => {
2615
+ return new YatspIntersection({
2616
+ left: left,
2617
+ right: right,
2618
+ typeName: YatspFirstPartyTypeKind.YatspIntersection,
2619
+ ...processCreateParams(params),
2620
+ });
2621
+ };
2622
+ // type YatspTupleItems = [YatspTypeAny, ...YatspTypeAny[]];
2623
+ class YatspTuple extends YatspType {
2624
+ _parse(input) {
2625
+ const { status, ctx } = this._processInputParams(input);
2626
+ if (ctx.parsedType !== util_js_1.YatspParsedType.array) {
2627
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2628
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2629
+ expected: util_js_1.YatspParsedType.array,
2630
+ received: ctx.parsedType,
2631
+ });
2632
+ return parseUtil_js_1.INVALID;
2633
+ }
2634
+ if (ctx.data.length < this._def.items.length) {
2635
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2636
+ code: YatspError_js_1.YatspIssueCode.too_small,
2637
+ minimum: this._def.items.length,
2638
+ inclusive: true,
2639
+ exact: false,
2640
+ type: "array",
2641
+ });
2642
+ return parseUtil_js_1.INVALID;
2643
+ }
2644
+ const rest = this._def.rest;
2645
+ if (!rest && ctx.data.length > this._def.items.length) {
2646
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2647
+ code: YatspError_js_1.YatspIssueCode.too_big,
2648
+ maximum: this._def.items.length,
2649
+ inclusive: true,
2650
+ exact: false,
2651
+ type: "array",
2652
+ });
2653
+ status.dirty();
2654
+ }
2655
+ const items = [...ctx.data]
2656
+ .map((item, itemIndex) => {
2657
+ const schema = this._def.items[itemIndex] || this._def.rest;
2658
+ if (!schema)
2659
+ return null;
2660
+ return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
2661
+ })
2662
+ .filter((x) => !!x); // filter nulls
2663
+ if (ctx.common.async) {
2664
+ return Promise.all(items).then((results) => {
2665
+ return parseUtil_js_1.ParseStatus.mergeArray(status, results);
2666
+ });
2667
+ }
2668
+ else {
2669
+ return parseUtil_js_1.ParseStatus.mergeArray(status, items);
2670
+ }
2671
+ }
2672
+ get items() {
2673
+ return this._def.items;
2674
+ }
2675
+ rest(rest) {
2676
+ return new YatspTuple({
2677
+ ...this._def,
2678
+ rest,
2679
+ });
2680
+ }
2681
+ }
2682
+ exports.YatspTuple = YatspTuple;
2683
+ YatspTuple.create = (schemas, params) => {
2684
+ if (!Array.isArray(schemas)) {
2685
+ throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
2686
+ }
2687
+ return new YatspTuple({
2688
+ items: schemas,
2689
+ typeName: YatspFirstPartyTypeKind.YatspTuple,
2690
+ rest: null,
2691
+ ...processCreateParams(params),
2692
+ });
2693
+ };
2694
+ class YatspRecord extends YatspType {
2695
+ get keySchema() {
2696
+ return this._def.keyType;
2697
+ }
2698
+ get valueSchema() {
2699
+ return this._def.valueType;
2700
+ }
2701
+ _parse(input) {
2702
+ const { status, ctx } = this._processInputParams(input);
2703
+ if (ctx.parsedType !== util_js_1.YatspParsedType.object) {
2704
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2705
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2706
+ expected: util_js_1.YatspParsedType.object,
2707
+ received: ctx.parsedType,
2708
+ });
2709
+ return parseUtil_js_1.INVALID;
2710
+ }
2711
+ const pairs = [];
2712
+ const keyType = this._def.keyType;
2713
+ const valueType = this._def.valueType;
2714
+ for (const key in ctx.data) {
2715
+ pairs.push({
2716
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2717
+ value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
2718
+ alwaysSet: key in ctx.data,
2719
+ });
2720
+ }
2721
+ if (ctx.common.async) {
2722
+ return parseUtil_js_1.ParseStatus.mergeObjectAsync(status, pairs);
2723
+ }
2724
+ else {
2725
+ return parseUtil_js_1.ParseStatus.mergeObjectSync(status, pairs);
2726
+ }
2727
+ }
2728
+ get element() {
2729
+ return this._def.valueType;
2730
+ }
2731
+ static create(first, second, third) {
2732
+ if (second instanceof YatspType) {
2733
+ return new YatspRecord({
2734
+ keyType: first,
2735
+ valueType: second,
2736
+ typeName: YatspFirstPartyTypeKind.YatspRecord,
2737
+ ...processCreateParams(third),
2738
+ });
2739
+ }
2740
+ return new YatspRecord({
2741
+ keyType: YatspString.create(),
2742
+ valueType: first,
2743
+ typeName: YatspFirstPartyTypeKind.YatspRecord,
2744
+ ...processCreateParams(second),
2745
+ });
2746
+ }
2747
+ }
2748
+ exports.YatspRecord = YatspRecord;
2749
+ class YatspMap extends YatspType {
2750
+ get keySchema() {
2751
+ return this._def.keyType;
2752
+ }
2753
+ get valueSchema() {
2754
+ return this._def.valueType;
2755
+ }
2756
+ _parse(input) {
2757
+ const { status, ctx } = this._processInputParams(input);
2758
+ if (ctx.parsedType !== util_js_1.YatspParsedType.map) {
2759
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2760
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2761
+ expected: util_js_1.YatspParsedType.map,
2762
+ received: ctx.parsedType,
2763
+ });
2764
+ return parseUtil_js_1.INVALID;
2765
+ }
2766
+ const keyType = this._def.keyType;
2767
+ const valueType = this._def.valueType;
2768
+ const pairs = [...ctx.data.entries()].map(([key, value], index) => {
2769
+ return {
2770
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
2771
+ value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
2772
+ };
2773
+ });
2774
+ if (ctx.common.async) {
2775
+ const finalMap = new Map();
2776
+ return Promise.resolve().then(async () => {
2777
+ for (const pair of pairs) {
2778
+ const key = await pair.key;
2779
+ const value = await pair.value;
2780
+ if (key.status === "aborted" || value.status === "aborted") {
2781
+ return parseUtil_js_1.INVALID;
2782
+ }
2783
+ if (key.status === "dirty" || value.status === "dirty") {
2784
+ status.dirty();
2785
+ }
2786
+ finalMap.set(key.value, value.value);
2787
+ }
2788
+ return { status: status.value, value: finalMap };
2789
+ });
2790
+ }
2791
+ else {
2792
+ const finalMap = new Map();
2793
+ for (const pair of pairs) {
2794
+ const key = pair.key;
2795
+ const value = pair.value;
2796
+ if (key.status === "aborted" || value.status === "aborted") {
2797
+ return parseUtil_js_1.INVALID;
2798
+ }
2799
+ if (key.status === "dirty" || value.status === "dirty") {
2800
+ status.dirty();
2801
+ }
2802
+ finalMap.set(key.value, value.value);
2803
+ }
2804
+ return { status: status.value, value: finalMap };
2805
+ }
2806
+ }
2807
+ }
2808
+ exports.YatspMap = YatspMap;
2809
+ YatspMap.create = (keyType, valueType, params) => {
2810
+ return new YatspMap({
2811
+ valueType,
2812
+ keyType,
2813
+ typeName: YatspFirstPartyTypeKind.YatspMap,
2814
+ ...processCreateParams(params),
2815
+ });
2816
+ };
2817
+ class YatspSet extends YatspType {
2818
+ _parse(input) {
2819
+ const { status, ctx } = this._processInputParams(input);
2820
+ if (ctx.parsedType !== util_js_1.YatspParsedType.set) {
2821
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2822
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2823
+ expected: util_js_1.YatspParsedType.set,
2824
+ received: ctx.parsedType,
2825
+ });
2826
+ return parseUtil_js_1.INVALID;
2827
+ }
2828
+ const def = this._def;
2829
+ if (def.minSize !== null) {
2830
+ if (ctx.data.size < def.minSize.value) {
2831
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2832
+ code: YatspError_js_1.YatspIssueCode.too_small,
2833
+ minimum: def.minSize.value,
2834
+ type: "set",
2835
+ inclusive: true,
2836
+ exact: false,
2837
+ message: def.minSize.message,
2838
+ });
2839
+ status.dirty();
2840
+ }
2841
+ }
2842
+ if (def.maxSize !== null) {
2843
+ if (ctx.data.size > def.maxSize.value) {
2844
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2845
+ code: YatspError_js_1.YatspIssueCode.too_big,
2846
+ maximum: def.maxSize.value,
2847
+ type: "set",
2848
+ inclusive: true,
2849
+ exact: false,
2850
+ message: def.maxSize.message,
2851
+ });
2852
+ status.dirty();
2853
+ }
2854
+ }
2855
+ const valueType = this._def.valueType;
2856
+ function finalizeSet(elements) {
2857
+ const parsedSet = new Set();
2858
+ for (const element of elements) {
2859
+ if (element.status === "aborted")
2860
+ return parseUtil_js_1.INVALID;
2861
+ if (element.status === "dirty")
2862
+ status.dirty();
2863
+ parsedSet.add(element.value);
2864
+ }
2865
+ return { status: status.value, value: parsedSet };
2866
+ }
2867
+ const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
2868
+ if (ctx.common.async) {
2869
+ return Promise.all(elements).then((elements) => finalizeSet(elements));
2870
+ }
2871
+ else {
2872
+ return finalizeSet(elements);
2873
+ }
2874
+ }
2875
+ min(minSize, message) {
2876
+ return new YatspSet({
2877
+ ...this._def,
2878
+ minSize: { value: minSize, message: errorUtil_js_1.errorUtil.toString(message) },
2879
+ });
2880
+ }
2881
+ max(maxSize, message) {
2882
+ return new YatspSet({
2883
+ ...this._def,
2884
+ maxSize: { value: maxSize, message: errorUtil_js_1.errorUtil.toString(message) },
2885
+ });
2886
+ }
2887
+ size(size, message) {
2888
+ return this.min(size, message).max(size, message);
2889
+ }
2890
+ nonempty(message) {
2891
+ return this.min(1, message);
2892
+ }
2893
+ }
2894
+ exports.YatspSet = YatspSet;
2895
+ YatspSet.create = (valueType, params) => {
2896
+ return new YatspSet({
2897
+ valueType,
2898
+ minSize: null,
2899
+ maxSize: null,
2900
+ typeName: YatspFirstPartyTypeKind.YatspSet,
2901
+ ...processCreateParams(params),
2902
+ });
2903
+ };
2904
+ class YatspFunction extends YatspType {
2905
+ constructor() {
2906
+ super(...arguments);
2907
+ this.validate = this.implement;
2908
+ }
2909
+ _parse(input) {
2910
+ const { ctx } = this._processInputParams(input);
2911
+ if (ctx.parsedType !== util_js_1.YatspParsedType.function) {
2912
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
2913
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
2914
+ expected: util_js_1.YatspParsedType.function,
2915
+ received: ctx.parsedType,
2916
+ });
2917
+ return parseUtil_js_1.INVALID;
2918
+ }
2919
+ function makeArgsIssue(args, error) {
2920
+ return (0, parseUtil_js_1.makeIssue)({
2921
+ data: args,
2922
+ path: ctx.path,
2923
+ errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, (0, errors_js_1.getErrorMap)(), errors_js_1.defaultErrorMap].filter((x) => !!x),
2924
+ issueData: {
2925
+ code: YatspError_js_1.YatspIssueCode.invalid_arguments,
2926
+ argumentsError: error,
2927
+ },
2928
+ });
2929
+ }
2930
+ function makeReturnsIssue(returns, error) {
2931
+ return (0, parseUtil_js_1.makeIssue)({
2932
+ data: returns,
2933
+ path: ctx.path,
2934
+ errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, (0, errors_js_1.getErrorMap)(), errors_js_1.defaultErrorMap].filter((x) => !!x),
2935
+ issueData: {
2936
+ code: YatspError_js_1.YatspIssueCode.invalid_return_type,
2937
+ returnTypeError: error,
2938
+ },
2939
+ });
2940
+ }
2941
+ const params = { errorMap: ctx.common.contextualErrorMap };
2942
+ const fn = ctx.data;
2943
+ if (this._def.returns instanceof YatspPromise) {
2944
+ // Would love a way to avoid disabling this rule, but we need an alias (using an arrow function was what caused 2651).
2945
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
2946
+ const me = this;
2947
+ return (0, parseUtil_js_1.OK)(async function (...args) {
2948
+ const error = new YatspError_js_1.YatspError([]);
2949
+ const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
2950
+ error.addIssue(makeArgsIssue(args, e));
2951
+ throw error;
2952
+ });
2953
+ const result = await Reflect.apply(fn, this, parsedArgs);
2954
+ const parsedReturns = await me._def.returns._def.type
2955
+ .parseAsync(result, params)
2956
+ .catch((e) => {
2957
+ error.addIssue(makeReturnsIssue(result, e));
2958
+ throw error;
2959
+ });
2960
+ return parsedReturns;
2961
+ });
2962
+ }
2963
+ else {
2964
+ // Would love a way to avoid disabling this rule, but we need an alias (using an arrow function was what caused 2651).
2965
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
2966
+ const me = this;
2967
+ return (0, parseUtil_js_1.OK)(function (...args) {
2968
+ const parsedArgs = me._def.args.safeParse(args, params);
2969
+ if (!parsedArgs.success) {
2970
+ throw new YatspError_js_1.YatspError([makeArgsIssue(args, parsedArgs.error)]);
2971
+ }
2972
+ const result = Reflect.apply(fn, this, parsedArgs.data);
2973
+ const parsedReturns = me._def.returns.safeParse(result, params);
2974
+ if (!parsedReturns.success) {
2975
+ throw new YatspError_js_1.YatspError([makeReturnsIssue(result, parsedReturns.error)]);
2976
+ }
2977
+ return parsedReturns.data;
2978
+ });
2979
+ }
2980
+ }
2981
+ parameters() {
2982
+ return this._def.args;
2983
+ }
2984
+ returnType() {
2985
+ return this._def.returns;
2986
+ }
2987
+ args(...items) {
2988
+ return new YatspFunction({
2989
+ ...this._def,
2990
+ args: YatspTuple.create(items).rest(YatspUnknown.create()),
2991
+ });
2992
+ }
2993
+ returns(returnType) {
2994
+ return new YatspFunction({
2995
+ ...this._def,
2996
+ returns: returnType,
2997
+ });
2998
+ }
2999
+ implement(func) {
3000
+ const validatedFunc = this.parse(func);
3001
+ return validatedFunc;
3002
+ }
3003
+ strictImplement(func) {
3004
+ const validatedFunc = this.parse(func);
3005
+ return validatedFunc;
3006
+ }
3007
+ static create(args, returns, params) {
3008
+ return new YatspFunction({
3009
+ args: (args ? args : YatspTuple.create([]).rest(YatspUnknown.create())),
3010
+ returns: returns || YatspUnknown.create(),
3011
+ typeName: YatspFirstPartyTypeKind.YatspFunction,
3012
+ ...processCreateParams(params),
3013
+ });
3014
+ }
3015
+ }
3016
+ exports.YatspFunction = YatspFunction;
3017
+ class YatspLazy extends YatspType {
3018
+ get schema() {
3019
+ return this._def.getter();
3020
+ }
3021
+ _parse(input) {
3022
+ const { ctx } = this._processInputParams(input);
3023
+ const lazySchema = this._def.getter();
3024
+ return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
3025
+ }
3026
+ }
3027
+ exports.YatspLazy = YatspLazy;
3028
+ YatspLazy.create = (getter, params) => {
3029
+ return new YatspLazy({
3030
+ getter: getter,
3031
+ typeName: YatspFirstPartyTypeKind.YatspLazy,
3032
+ ...processCreateParams(params),
3033
+ });
3034
+ };
3035
+ class YatspLiteral extends YatspType {
3036
+ _parse(input) {
3037
+ if (input.data !== this._def.value) {
3038
+ const ctx = this._getOrReturnCtx(input);
3039
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3040
+ received: ctx.data,
3041
+ code: YatspError_js_1.YatspIssueCode.invalid_literal,
3042
+ expected: this._def.value,
3043
+ });
3044
+ return parseUtil_js_1.INVALID;
3045
+ }
3046
+ return { status: "valid", value: input.data };
3047
+ }
3048
+ get value() {
3049
+ return this._def.value;
3050
+ }
3051
+ }
3052
+ exports.YatspLiteral = YatspLiteral;
3053
+ YatspLiteral.create = (value, params) => {
3054
+ return new YatspLiteral({
3055
+ value: value,
3056
+ typeName: YatspFirstPartyTypeKind.YatspLiteral,
3057
+ ...processCreateParams(params),
3058
+ });
3059
+ };
3060
+ function createYatspEnum(values, params) {
3061
+ return new YatspEnum({
3062
+ values,
3063
+ typeName: YatspFirstPartyTypeKind.YatspEnum,
3064
+ ...processCreateParams(params),
3065
+ });
3066
+ }
3067
+ class YatspEnum extends YatspType {
3068
+ _parse(input) {
3069
+ if (typeof input.data !== "string") {
3070
+ const ctx = this._getOrReturnCtx(input);
3071
+ const expectedValues = this._def.values;
3072
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3073
+ expected: util_js_1.util.joinValues(expectedValues),
3074
+ received: ctx.parsedType,
3075
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
3076
+ });
3077
+ return parseUtil_js_1.INVALID;
3078
+ }
3079
+ if (!this._cache) {
3080
+ this._cache = new Set(this._def.values);
3081
+ }
3082
+ if (!this._cache.has(input.data)) {
3083
+ const ctx = this._getOrReturnCtx(input);
3084
+ const expectedValues = this._def.values;
3085
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3086
+ received: ctx.data,
3087
+ code: YatspError_js_1.YatspIssueCode.invalid_enum_value,
3088
+ options: expectedValues,
3089
+ });
3090
+ return parseUtil_js_1.INVALID;
3091
+ }
3092
+ return (0, parseUtil_js_1.OK)(input.data);
3093
+ }
3094
+ get options() {
3095
+ return this._def.values;
3096
+ }
3097
+ get enum() {
3098
+ const enumValues = {};
3099
+ for (const val of this._def.values) {
3100
+ enumValues[val] = val;
3101
+ }
3102
+ return enumValues;
3103
+ }
3104
+ get Values() {
3105
+ const enumValues = {};
3106
+ for (const val of this._def.values) {
3107
+ enumValues[val] = val;
3108
+ }
3109
+ return enumValues;
3110
+ }
3111
+ get Enum() {
3112
+ const enumValues = {};
3113
+ for (const val of this._def.values) {
3114
+ enumValues[val] = val;
3115
+ }
3116
+ return enumValues;
3117
+ }
3118
+ extract(values, newDef = this._def) {
3119
+ return YatspEnum.create(values, {
3120
+ ...this._def,
3121
+ ...newDef,
3122
+ });
3123
+ }
3124
+ exclude(values, newDef = this._def) {
3125
+ return YatspEnum.create(this.options.filter((opt) => !values.includes(opt)), {
3126
+ ...this._def,
3127
+ ...newDef,
3128
+ });
3129
+ }
3130
+ }
3131
+ exports.YatspEnum = YatspEnum;
3132
+ YatspEnum.create = createYatspEnum;
3133
+ class YatspNativeEnum extends YatspType {
3134
+ _parse(input) {
3135
+ const nativeEnumValues = util_js_1.util.getValidEnumValues(this._def.values);
3136
+ const ctx = this._getOrReturnCtx(input);
3137
+ if (ctx.parsedType !== util_js_1.YatspParsedType.string && ctx.parsedType !== util_js_1.YatspParsedType.number) {
3138
+ const expectedValues = util_js_1.util.objectValues(nativeEnumValues);
3139
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3140
+ expected: util_js_1.util.joinValues(expectedValues),
3141
+ received: ctx.parsedType,
3142
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
3143
+ });
3144
+ return parseUtil_js_1.INVALID;
3145
+ }
3146
+ if (!this._cache) {
3147
+ this._cache = new Set(util_js_1.util.getValidEnumValues(this._def.values));
3148
+ }
3149
+ if (!this._cache.has(input.data)) {
3150
+ const expectedValues = util_js_1.util.objectValues(nativeEnumValues);
3151
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3152
+ received: ctx.data,
3153
+ code: YatspError_js_1.YatspIssueCode.invalid_enum_value,
3154
+ options: expectedValues,
3155
+ });
3156
+ return parseUtil_js_1.INVALID;
3157
+ }
3158
+ return (0, parseUtil_js_1.OK)(input.data);
3159
+ }
3160
+ get enum() {
3161
+ return this._def.values;
3162
+ }
3163
+ }
3164
+ exports.YatspNativeEnum = YatspNativeEnum;
3165
+ YatspNativeEnum.create = (values, params) => {
3166
+ return new YatspNativeEnum({
3167
+ values: values,
3168
+ typeName: YatspFirstPartyTypeKind.YatspNativeEnum,
3169
+ ...processCreateParams(params),
3170
+ });
3171
+ };
3172
+ class YatspPromise extends YatspType {
3173
+ unwrap() {
3174
+ return this._def.type;
3175
+ }
3176
+ _parse(input) {
3177
+ const { ctx } = this._processInputParams(input);
3178
+ if (ctx.parsedType !== util_js_1.YatspParsedType.promise && ctx.common.async === false) {
3179
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3180
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
3181
+ expected: util_js_1.YatspParsedType.promise,
3182
+ received: ctx.parsedType,
3183
+ });
3184
+ return parseUtil_js_1.INVALID;
3185
+ }
3186
+ const promisified = ctx.parsedType === util_js_1.YatspParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
3187
+ return (0, parseUtil_js_1.OK)(promisified.then((data) => {
3188
+ return this._def.type.parseAsync(data, {
3189
+ path: ctx.path,
3190
+ errorMap: ctx.common.contextualErrorMap,
3191
+ });
3192
+ }));
3193
+ }
3194
+ }
3195
+ exports.YatspPromise = YatspPromise;
3196
+ YatspPromise.create = (schema, params) => {
3197
+ return new YatspPromise({
3198
+ type: schema,
3199
+ typeName: YatspFirstPartyTypeKind.YatspPromise,
3200
+ ...processCreateParams(params),
3201
+ });
3202
+ };
3203
+ class YatspEffects extends YatspType {
3204
+ innerType() {
3205
+ return this._def.schema;
3206
+ }
3207
+ sourceType() {
3208
+ return this._def.schema._def.typeName === YatspFirstPartyTypeKind.YatspEffects
3209
+ ? this._def.schema.sourceType()
3210
+ : this._def.schema;
3211
+ }
3212
+ _parse(input) {
3213
+ const { status, ctx } = this._processInputParams(input);
3214
+ const effect = this._def.effect || null;
3215
+ const checkCtx = {
3216
+ addIssue: (arg) => {
3217
+ (0, parseUtil_js_1.addIssueToContext)(ctx, arg);
3218
+ if (arg.fatal) {
3219
+ status.abort();
3220
+ }
3221
+ else {
3222
+ status.dirty();
3223
+ }
3224
+ },
3225
+ get path() {
3226
+ return ctx.path;
3227
+ },
3228
+ };
3229
+ checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
3230
+ if (effect.type === "preprocess") {
3231
+ const processed = effect.transform(ctx.data, checkCtx);
3232
+ if (ctx.common.async) {
3233
+ return Promise.resolve(processed).then(async (processed) => {
3234
+ if (status.value === "aborted")
3235
+ return parseUtil_js_1.INVALID;
3236
+ const result = await this._def.schema._parseAsync({
3237
+ data: processed,
3238
+ path: ctx.path,
3239
+ parent: ctx,
3240
+ });
3241
+ if (result.status === "aborted")
3242
+ return parseUtil_js_1.INVALID;
3243
+ if (result.status === "dirty")
3244
+ return (0, parseUtil_js_1.DIRTY)(result.value);
3245
+ if (status.value === "dirty")
3246
+ return (0, parseUtil_js_1.DIRTY)(result.value);
3247
+ return result;
3248
+ });
3249
+ }
3250
+ else {
3251
+ if (status.value === "aborted")
3252
+ return parseUtil_js_1.INVALID;
3253
+ const result = this._def.schema._parseSync({
3254
+ data: processed,
3255
+ path: ctx.path,
3256
+ parent: ctx,
3257
+ });
3258
+ if (result.status === "aborted")
3259
+ return parseUtil_js_1.INVALID;
3260
+ if (result.status === "dirty")
3261
+ return (0, parseUtil_js_1.DIRTY)(result.value);
3262
+ if (status.value === "dirty")
3263
+ return (0, parseUtil_js_1.DIRTY)(result.value);
3264
+ return result;
3265
+ }
3266
+ }
3267
+ if (effect.type === "refinement") {
3268
+ const executeRefinement = (acc) => {
3269
+ const result = effect.refinement(acc, checkCtx);
3270
+ if (ctx.common.async) {
3271
+ return Promise.resolve(result);
3272
+ }
3273
+ if (result instanceof Promise) {
3274
+ throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
3275
+ }
3276
+ return acc;
3277
+ };
3278
+ if (ctx.common.async === false) {
3279
+ const inner = this._def.schema._parseSync({
3280
+ data: ctx.data,
3281
+ path: ctx.path,
3282
+ parent: ctx,
3283
+ });
3284
+ if (inner.status === "aborted")
3285
+ return parseUtil_js_1.INVALID;
3286
+ if (inner.status === "dirty")
3287
+ status.dirty();
3288
+ // return value is ignored
3289
+ executeRefinement(inner.value);
3290
+ return { status: status.value, value: inner.value };
3291
+ }
3292
+ else {
3293
+ return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
3294
+ if (inner.status === "aborted")
3295
+ return parseUtil_js_1.INVALID;
3296
+ if (inner.status === "dirty")
3297
+ status.dirty();
3298
+ return executeRefinement(inner.value).then(() => {
3299
+ return { status: status.value, value: inner.value };
3300
+ });
3301
+ });
3302
+ }
3303
+ }
3304
+ if (effect.type === "transform") {
3305
+ if (ctx.common.async === false) {
3306
+ const base = this._def.schema._parseSync({
3307
+ data: ctx.data,
3308
+ path: ctx.path,
3309
+ parent: ctx,
3310
+ });
3311
+ if (!(0, parseUtil_js_1.isValid)(base))
3312
+ return parseUtil_js_1.INVALID;
3313
+ const result = effect.transform(base.value, checkCtx);
3314
+ if (result instanceof Promise) {
3315
+ throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
3316
+ }
3317
+ return { status: status.value, value: result };
3318
+ }
3319
+ else {
3320
+ return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
3321
+ if (!(0, parseUtil_js_1.isValid)(base))
3322
+ return parseUtil_js_1.INVALID;
3323
+ return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
3324
+ status: status.value,
3325
+ value: result,
3326
+ }));
3327
+ });
3328
+ }
3329
+ }
3330
+ util_js_1.util.assertNever(effect);
3331
+ }
3332
+ }
3333
+ exports.YatspEffects = YatspEffects;
3334
+ exports.YatspTransformer = YatspEffects;
3335
+ YatspEffects.create = (schema, effect, params) => {
3336
+ return new YatspEffects({
3337
+ schema,
3338
+ typeName: YatspFirstPartyTypeKind.YatspEffects,
3339
+ effect,
3340
+ ...processCreateParams(params),
3341
+ });
3342
+ };
3343
+ YatspEffects.createWithPreprocess = (preprocess, schema, params) => {
3344
+ return new YatspEffects({
3345
+ schema,
3346
+ effect: { type: "preprocess", transform: preprocess },
3347
+ typeName: YatspFirstPartyTypeKind.YatspEffects,
3348
+ ...processCreateParams(params),
3349
+ });
3350
+ };
3351
+ class YatspOptional extends YatspType {
3352
+ _parse(input) {
3353
+ const parsedType = this._getType(input);
3354
+ if (parsedType === util_js_1.YatspParsedType.undefined) {
3355
+ return (0, parseUtil_js_1.OK)(undefined);
3356
+ }
3357
+ return this._def.innerType._parse(input);
3358
+ }
3359
+ unwrap() {
3360
+ return this._def.innerType;
3361
+ }
3362
+ }
3363
+ exports.YatspOptional = YatspOptional;
3364
+ YatspOptional.create = (type, params) => {
3365
+ return new YatspOptional({
3366
+ innerType: type,
3367
+ typeName: YatspFirstPartyTypeKind.YatspOptional,
3368
+ ...processCreateParams(params),
3369
+ });
3370
+ };
3371
+ class YatspNullable extends YatspType {
3372
+ _parse(input) {
3373
+ const parsedType = this._getType(input);
3374
+ if (parsedType === util_js_1.YatspParsedType.null) {
3375
+ return (0, parseUtil_js_1.OK)(null);
3376
+ }
3377
+ return this._def.innerType._parse(input);
3378
+ }
3379
+ unwrap() {
3380
+ return this._def.innerType;
3381
+ }
3382
+ }
3383
+ exports.YatspNullable = YatspNullable;
3384
+ YatspNullable.create = (type, params) => {
3385
+ return new YatspNullable({
3386
+ innerType: type,
3387
+ typeName: YatspFirstPartyTypeKind.YatspNullable,
3388
+ ...processCreateParams(params),
3389
+ });
3390
+ };
3391
+ class YatspDefault extends YatspType {
3392
+ _parse(input) {
3393
+ const { ctx } = this._processInputParams(input);
3394
+ let data = ctx.data;
3395
+ if (ctx.parsedType === util_js_1.YatspParsedType.undefined) {
3396
+ data = this._def.defaultValue();
3397
+ }
3398
+ return this._def.innerType._parse({
3399
+ data,
3400
+ path: ctx.path,
3401
+ parent: ctx,
3402
+ });
3403
+ }
3404
+ removeDefault() {
3405
+ return this._def.innerType;
3406
+ }
3407
+ }
3408
+ exports.YatspDefault = YatspDefault;
3409
+ YatspDefault.create = (type, params) => {
3410
+ return new YatspDefault({
3411
+ innerType: type,
3412
+ typeName: YatspFirstPartyTypeKind.YatspDefault,
3413
+ defaultValue: typeof params.default === "function" ? params.default : () => params.default,
3414
+ ...processCreateParams(params),
3415
+ });
3416
+ };
3417
+ class YatspCatch extends YatspType {
3418
+ _parse(input) {
3419
+ const { ctx } = this._processInputParams(input);
3420
+ // newCtx is used to not collect issues from inner types in ctx
3421
+ const newCtx = {
3422
+ ...ctx,
3423
+ common: {
3424
+ ...ctx.common,
3425
+ issues: [],
3426
+ },
3427
+ };
3428
+ const result = this._def.innerType._parse({
3429
+ data: newCtx.data,
3430
+ path: newCtx.path,
3431
+ parent: {
3432
+ ...newCtx,
3433
+ },
3434
+ });
3435
+ if ((0, parseUtil_js_1.isAsync)(result)) {
3436
+ return result.then((result) => {
3437
+ return {
3438
+ status: "valid",
3439
+ value: result.status === "valid"
3440
+ ? result.value
3441
+ : this._def.catchValue({
3442
+ get error() {
3443
+ return new YatspError_js_1.YatspError(newCtx.common.issues);
3444
+ },
3445
+ input: newCtx.data,
3446
+ }),
3447
+ };
3448
+ });
3449
+ }
3450
+ else {
3451
+ return {
3452
+ status: "valid",
3453
+ value: result.status === "valid"
3454
+ ? result.value
3455
+ : this._def.catchValue({
3456
+ get error() {
3457
+ return new YatspError_js_1.YatspError(newCtx.common.issues);
3458
+ },
3459
+ input: newCtx.data,
3460
+ }),
3461
+ };
3462
+ }
3463
+ }
3464
+ removeCatch() {
3465
+ return this._def.innerType;
3466
+ }
3467
+ }
3468
+ exports.YatspCatch = YatspCatch;
3469
+ YatspCatch.create = (type, params) => {
3470
+ return new YatspCatch({
3471
+ innerType: type,
3472
+ typeName: YatspFirstPartyTypeKind.YatspCatch,
3473
+ catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
3474
+ ...processCreateParams(params),
3475
+ });
3476
+ };
3477
+ class YatspNaN extends YatspType {
3478
+ _parse(input) {
3479
+ const parsedType = this._getType(input);
3480
+ if (parsedType !== util_js_1.YatspParsedType.nan) {
3481
+ const ctx = this._getOrReturnCtx(input);
3482
+ (0, parseUtil_js_1.addIssueToContext)(ctx, {
3483
+ code: YatspError_js_1.YatspIssueCode.invalid_type,
3484
+ expected: util_js_1.YatspParsedType.nan,
3485
+ received: ctx.parsedType,
3486
+ });
3487
+ return parseUtil_js_1.INVALID;
3488
+ }
3489
+ return { status: "valid", value: input.data };
3490
+ }
3491
+ }
3492
+ exports.YatspNaN = YatspNaN;
3493
+ YatspNaN.create = (params) => {
3494
+ return new YatspNaN({
3495
+ typeName: YatspFirstPartyTypeKind.YatspNaN,
3496
+ ...processCreateParams(params),
3497
+ });
3498
+ };
3499
+ exports.BRAND = Symbol("yatsp_brand");
3500
+ class YatspBranded extends YatspType {
3501
+ _parse(input) {
3502
+ const { ctx } = this._processInputParams(input);
3503
+ const data = ctx.data;
3504
+ return this._def.type._parse({
3505
+ data,
3506
+ path: ctx.path,
3507
+ parent: ctx,
3508
+ });
3509
+ }
3510
+ unwrap() {
3511
+ return this._def.type;
3512
+ }
3513
+ }
3514
+ exports.YatspBranded = YatspBranded;
3515
+ class YatspPipeline extends YatspType {
3516
+ _parse(input) {
3517
+ const { status, ctx } = this._processInputParams(input);
3518
+ if (ctx.common.async) {
3519
+ const handleAsync = async () => {
3520
+ const inResult = await this._def.in._parseAsync({
3521
+ data: ctx.data,
3522
+ path: ctx.path,
3523
+ parent: ctx,
3524
+ });
3525
+ if (inResult.status === "aborted")
3526
+ return parseUtil_js_1.INVALID;
3527
+ if (inResult.status === "dirty") {
3528
+ status.dirty();
3529
+ return (0, parseUtil_js_1.DIRTY)(inResult.value);
3530
+ }
3531
+ else {
3532
+ return this._def.out._parseAsync({
3533
+ data: inResult.value,
3534
+ path: ctx.path,
3535
+ parent: ctx,
3536
+ });
3537
+ }
3538
+ };
3539
+ return handleAsync();
3540
+ }
3541
+ else {
3542
+ const inResult = this._def.in._parseSync({
3543
+ data: ctx.data,
3544
+ path: ctx.path,
3545
+ parent: ctx,
3546
+ });
3547
+ if (inResult.status === "aborted")
3548
+ return parseUtil_js_1.INVALID;
3549
+ if (inResult.status === "dirty") {
3550
+ status.dirty();
3551
+ return {
3552
+ status: "dirty",
3553
+ value: inResult.value,
3554
+ };
3555
+ }
3556
+ else {
3557
+ return this._def.out._parseSync({
3558
+ data: inResult.value,
3559
+ path: ctx.path,
3560
+ parent: ctx,
3561
+ });
3562
+ }
3563
+ }
3564
+ }
3565
+ static create(a, b) {
3566
+ return new YatspPipeline({
3567
+ in: a,
3568
+ out: b,
3569
+ typeName: YatspFirstPartyTypeKind.YatspPipeline,
3570
+ });
3571
+ }
3572
+ }
3573
+ exports.YatspPipeline = YatspPipeline;
3574
+ class YatspReadonly extends YatspType {
3575
+ _parse(input) {
3576
+ const result = this._def.innerType._parse(input);
3577
+ const freeze = (data) => {
3578
+ if ((0, parseUtil_js_1.isValid)(data)) {
3579
+ data.value = Object.freeze(data.value);
3580
+ }
3581
+ return data;
3582
+ };
3583
+ return (0, parseUtil_js_1.isAsync)(result) ? result.then((data) => freeze(data)) : freeze(result);
3584
+ }
3585
+ unwrap() {
3586
+ return this._def.innerType;
3587
+ }
3588
+ }
3589
+ exports.YatspReadonly = YatspReadonly;
3590
+ YatspReadonly.create = (type, params) => {
3591
+ return new YatspReadonly({
3592
+ innerType: type,
3593
+ typeName: YatspFirstPartyTypeKind.YatspReadonly,
3594
+ ...processCreateParams(params),
3595
+ });
3596
+ };
3597
+ ////////////////////////////////////////
3598
+ ////////////////////////////////////////
3599
+ ////////// //////////
3600
+ ////////// z.custom //////////
3601
+ ////////// //////////
3602
+ ////////////////////////////////////////
3603
+ ////////////////////////////////////////
3604
+ function cleanParams(params, data) {
3605
+ const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
3606
+ const p2 = typeof p === "string" ? { message: p } : p;
3607
+ return p2;
3608
+ }
3609
+ function custom(check, _params = {},
3610
+ /**
3611
+ * @deprecated
3612
+ *
3613
+ * Pass `fatal` into the params object instead:
3614
+ *
3615
+ * ```ts
3616
+ * z.string().custom((val) => val.length > 5, { fatal: false })
3617
+ * ```
3618
+ *
3619
+ */
3620
+ fatal) {
3621
+ if (check)
3622
+ return YatspAny.create().superRefine((data, ctx) => {
3623
+ const r = check(data);
3624
+ if (r instanceof Promise) {
3625
+ return r.then((r) => {
3626
+ if (!r) {
3627
+ const params = cleanParams(_params, data);
3628
+ const _fatal = params.fatal ?? fatal ?? true;
3629
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
3630
+ }
3631
+ });
3632
+ }
3633
+ if (!r) {
3634
+ const params = cleanParams(_params, data);
3635
+ const _fatal = params.fatal ?? fatal ?? true;
3636
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
3637
+ }
3638
+ return;
3639
+ });
3640
+ return YatspAny.create();
3641
+ }
3642
+ exports.late = {
3643
+ object: YatspObject.lazycreate,
3644
+ };
3645
+ var YatspFirstPartyTypeKind;
3646
+ (function (YatspFirstPartyTypeKind) {
3647
+ YatspFirstPartyTypeKind["YatspString"] = "YatspString";
3648
+ YatspFirstPartyTypeKind["YatspNumber"] = "YatspNumber";
3649
+ YatspFirstPartyTypeKind["YatspNaN"] = "YatspNaN";
3650
+ YatspFirstPartyTypeKind["YatspBigInt"] = "YatspBigInt";
3651
+ YatspFirstPartyTypeKind["YatspBoolean"] = "YatspBoolean";
3652
+ YatspFirstPartyTypeKind["YatspDate"] = "YatspDate";
3653
+ YatspFirstPartyTypeKind["YatspSymbol"] = "YatspSymbol";
3654
+ YatspFirstPartyTypeKind["YatspUndefined"] = "YatspUndefined";
3655
+ YatspFirstPartyTypeKind["YatspNull"] = "YatspNull";
3656
+ YatspFirstPartyTypeKind["YatspAny"] = "YatspAny";
3657
+ YatspFirstPartyTypeKind["YatspUnknown"] = "YatspUnknown";
3658
+ YatspFirstPartyTypeKind["YatspNever"] = "YatspNever";
3659
+ YatspFirstPartyTypeKind["YatspVoid"] = "YatspVoid";
3660
+ YatspFirstPartyTypeKind["YatspArray"] = "YatspArray";
3661
+ YatspFirstPartyTypeKind["YatspObject"] = "YatspObject";
3662
+ YatspFirstPartyTypeKind["YatspUnion"] = "YatspUnion";
3663
+ YatspFirstPartyTypeKind["YatspDiscriminatedUnion"] = "YatspDiscriminatedUnion";
3664
+ YatspFirstPartyTypeKind["YatspIntersection"] = "YatspIntersection";
3665
+ YatspFirstPartyTypeKind["YatspTuple"] = "YatspTuple";
3666
+ YatspFirstPartyTypeKind["YatspRecord"] = "YatspRecord";
3667
+ YatspFirstPartyTypeKind["YatspMap"] = "YatspMap";
3668
+ YatspFirstPartyTypeKind["YatspSet"] = "YatspSet";
3669
+ YatspFirstPartyTypeKind["YatspFunction"] = "YatspFunction";
3670
+ YatspFirstPartyTypeKind["YatspLazy"] = "YatspLazy";
3671
+ YatspFirstPartyTypeKind["YatspLiteral"] = "YatspLiteral";
3672
+ YatspFirstPartyTypeKind["YatspEnum"] = "YatspEnum";
3673
+ YatspFirstPartyTypeKind["YatspEffects"] = "YatspEffects";
3674
+ YatspFirstPartyTypeKind["YatspNativeEnum"] = "YatspNativeEnum";
3675
+ YatspFirstPartyTypeKind["YatspOptional"] = "YatspOptional";
3676
+ YatspFirstPartyTypeKind["YatspNullable"] = "YatspNullable";
3677
+ YatspFirstPartyTypeKind["YatspDefault"] = "YatspDefault";
3678
+ YatspFirstPartyTypeKind["YatspCatch"] = "YatspCatch";
3679
+ YatspFirstPartyTypeKind["YatspPromise"] = "YatspPromise";
3680
+ YatspFirstPartyTypeKind["YatspBranded"] = "YatspBranded";
3681
+ YatspFirstPartyTypeKind["YatspPipeline"] = "YatspPipeline";
3682
+ YatspFirstPartyTypeKind["YatspReadonly"] = "YatspReadonly";
3683
+ })(YatspFirstPartyTypeKind || (exports.YatspFirstPartyTypeKind = YatspFirstPartyTypeKind = {}));
3684
+ // requires TS 4.4+
3685
+ class Class {
3686
+ constructor(..._) { }
3687
+ }
3688
+ const instanceOfType = (
3689
+ // const instanceOfType = <T extends new (...args: any[]) => any>(
3690
+ cls, params = {
3691
+ message: `Input not instance of ${cls.name}`,
3692
+ }) => custom((data) => data instanceof cls, params);
3693
+ exports.instanceof = instanceOfType;
3694
+ const stringType = YatspString.create;
3695
+ exports.string = stringType;
3696
+ const numberType = YatspNumber.create;
3697
+ exports.number = numberType;
3698
+ const nanType = YatspNaN.create;
3699
+ exports.nan = nanType;
3700
+ const bigIntType = YatspBigInt.create;
3701
+ exports.bigint = bigIntType;
3702
+ const booleanType = YatspBoolean.create;
3703
+ exports.boolean = booleanType;
3704
+ const dateType = YatspDate.create;
3705
+ exports.date = dateType;
3706
+ const symbolType = YatspSymbol.create;
3707
+ exports.symbol = symbolType;
3708
+ const undefinedType = YatspUndefined.create;
3709
+ exports.undefined = undefinedType;
3710
+ const nullType = YatspNull.create;
3711
+ exports.null = nullType;
3712
+ const anyType = YatspAny.create;
3713
+ exports.any = anyType;
3714
+ const unknownType = YatspUnknown.create;
3715
+ exports.unknown = unknownType;
3716
+ const neverType = YatspNever.create;
3717
+ exports.never = neverType;
3718
+ const voidType = YatspVoid.create;
3719
+ exports.void = voidType;
3720
+ const arrayType = YatspArray.create;
3721
+ exports.array = arrayType;
3722
+ const objectType = YatspObject.create;
3723
+ exports.object = objectType;
3724
+ const strictObjectType = YatspObject.strictCreate;
3725
+ exports.strictObject = strictObjectType;
3726
+ const unionType = YatspUnion.create;
3727
+ exports.union = unionType;
3728
+ const discriminatedUnionType = YatspDiscriminatedUnion.create;
3729
+ exports.discriminatedUnion = discriminatedUnionType;
3730
+ const intersectionType = YatspIntersection.create;
3731
+ exports.intersection = intersectionType;
3732
+ const tupleType = YatspTuple.create;
3733
+ exports.tuple = tupleType;
3734
+ const recordType = YatspRecord.create;
3735
+ exports.record = recordType;
3736
+ const mapType = YatspMap.create;
3737
+ exports.map = mapType;
3738
+ const setType = YatspSet.create;
3739
+ exports.set = setType;
3740
+ const functionType = YatspFunction.create;
3741
+ exports.function = functionType;
3742
+ const lazyType = YatspLazy.create;
3743
+ exports.lazy = lazyType;
3744
+ const literalType = YatspLiteral.create;
3745
+ exports.literal = literalType;
3746
+ const enumType = YatspEnum.create;
3747
+ exports.enum = enumType;
3748
+ const nativeEnumType = YatspNativeEnum.create;
3749
+ exports.nativeEnum = nativeEnumType;
3750
+ const promiseType = YatspPromise.create;
3751
+ exports.promise = promiseType;
3752
+ const effectsType = YatspEffects.create;
3753
+ exports.effect = effectsType;
3754
+ exports.transformer = effectsType;
3755
+ const optionalType = YatspOptional.create;
3756
+ exports.optional = optionalType;
3757
+ const nullableType = YatspNullable.create;
3758
+ exports.nullable = nullableType;
3759
+ const preprocessType = YatspEffects.createWithPreprocess;
3760
+ exports.preprocess = preprocessType;
3761
+ const pipelineType = YatspPipeline.create;
3762
+ exports.pipeline = pipelineType;
3763
+ const ostring = () => stringType().optional();
3764
+ exports.ostring = ostring;
3765
+ const onumber = () => numberType().optional();
3766
+ exports.onumber = onumber;
3767
+ const oboolean = () => booleanType().optional();
3768
+ exports.oboolean = oboolean;
3769
+ exports.coerce = {
3770
+ string: ((arg) => YatspString.create({ ...arg, coerce: true })),
3771
+ number: ((arg) => YatspNumber.create({ ...arg, coerce: true })),
3772
+ boolean: ((arg) => YatspBoolean.create({
3773
+ ...arg,
3774
+ coerce: true,
3775
+ })),
3776
+ bigint: ((arg) => YatspBigInt.create({ ...arg, coerce: true })),
3777
+ date: ((arg) => YatspDate.create({ ...arg, coerce: true })),
3778
+ };
3779
+ exports.NEVER = parseUtil_js_1.INVALID;
3780
+
3781
+ // seal-cjs-exports
3782
+ (function () {
3783
+ var keys = Object.getOwnPropertyNames(exports);
3784
+ for (var i = 0; i < keys.length; i++) {
3785
+ var desc = Object.getOwnPropertyDescriptor(exports, keys[i]);
3786
+ if (!desc || !desc.get || !desc.configurable) continue;
3787
+ var value;
3788
+ try {
3789
+ value = desc.get();
3790
+ } catch (e) {
3791
+ continue;
3792
+ }
3793
+ // a circular require may not have settled this one yet, so leave it live
3794
+ if (value === undefined) continue;
3795
+ Object.defineProperty(exports, keys[i], { value: value, writable: false, enumerable: desc.enumerable, configurable: false });
3796
+ }
3797
+ Object.freeze(exports);
3798
+ })();