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

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 (278) hide show
  1. package/package.json +20 -20
  2. package/src/index.ts +4 -0
  3. package/src/v3/ZodError.ts +330 -0
  4. package/src/v3/benchmarks/datetime.ts +58 -0
  5. package/src/v3/benchmarks/discriminatedUnion.ts +80 -0
  6. package/src/v3/benchmarks/index.ts +59 -0
  7. package/src/v3/benchmarks/ipv4.ts +57 -0
  8. package/src/v3/benchmarks/object.ts +69 -0
  9. package/src/v3/benchmarks/primitives.ts +162 -0
  10. package/src/v3/benchmarks/realworld.ts +63 -0
  11. package/src/v3/benchmarks/string.ts +55 -0
  12. package/src/v3/benchmarks/union.ts +80 -0
  13. package/src/v3/errors.ts +13 -0
  14. package/src/v3/external.ts +6 -0
  15. package/src/v3/helpers/enumUtil.ts +17 -0
  16. package/src/v3/helpers/errorUtil.ts +8 -0
  17. package/src/v3/helpers/parseUtil.ts +176 -0
  18. package/src/v3/helpers/partialUtil.ts +34 -0
  19. package/src/v3/helpers/typeAliases.ts +2 -0
  20. package/src/v3/helpers/util.ts +224 -0
  21. package/src/v3/index.ts +4 -0
  22. package/src/v3/locales/en.ts +124 -0
  23. package/src/v3/standard-schema.ts +113 -0
  24. package/src/v3/tests/Mocker.ts +54 -0
  25. package/src/v3/tests/all-errors.test.ts +157 -0
  26. package/src/v3/tests/anyunknown.test.ts +28 -0
  27. package/src/v3/tests/array.test.ts +71 -0
  28. package/src/v3/tests/async-parsing.test.ts +388 -0
  29. package/src/v3/tests/async-refinements.test.ts +46 -0
  30. package/src/v3/tests/base.test.ts +29 -0
  31. package/src/v3/tests/bigint.test.ts +55 -0
  32. package/src/v3/tests/branded.test.ts +53 -0
  33. package/src/v3/tests/catch.test.ts +220 -0
  34. package/src/v3/tests/coerce.test.ts +133 -0
  35. package/src/v3/tests/complex.test.ts +56 -0
  36. package/src/v3/tests/custom.test.ts +31 -0
  37. package/src/v3/tests/date.test.ts +32 -0
  38. package/src/v3/tests/deepmasking.test.ts +186 -0
  39. package/src/v3/tests/default.test.ts +112 -0
  40. package/src/v3/tests/description.test.ts +33 -0
  41. package/src/v3/tests/discriminated-unions.test.ts +315 -0
  42. package/src/v3/tests/enum.test.ts +80 -0
  43. package/src/v3/tests/error.test.ts +551 -0
  44. package/src/v3/tests/firstparty.test.ts +87 -0
  45. package/src/v3/tests/firstpartyschematypes.test.ts +21 -0
  46. package/src/v3/tests/function.test.ts +257 -0
  47. package/src/v3/tests/generics.test.ts +48 -0
  48. package/src/v3/tests/instanceof.test.ts +37 -0
  49. package/src/v3/tests/intersection.test.ts +110 -0
  50. package/src/v3/tests/language-server.source.ts +76 -0
  51. package/src/v3/tests/language-server.test.ts +207 -0
  52. package/src/v3/tests/literal.test.ts +36 -0
  53. package/src/v3/tests/map.test.ts +110 -0
  54. package/src/v3/tests/masking.test.ts +4 -0
  55. package/src/v3/tests/mocker.test.ts +19 -0
  56. package/src/v3/tests/nan.test.ts +21 -0
  57. package/src/v3/tests/nativeEnum.test.ts +87 -0
  58. package/src/v3/tests/nullable.test.ts +42 -0
  59. package/src/v3/tests/number.test.ts +176 -0
  60. package/src/v3/tests/object-augmentation.test.ts +29 -0
  61. package/src/v3/tests/object-in-es5-env.test.ts +29 -0
  62. package/src/v3/tests/object.test.ts +434 -0
  63. package/src/v3/tests/optional.test.ts +42 -0
  64. package/src/v3/tests/parseUtil.test.ts +23 -0
  65. package/src/v3/tests/parser.test.ts +41 -0
  66. package/src/v3/tests/partials.test.ts +243 -0
  67. package/src/v3/tests/pickomit.test.ts +111 -0
  68. package/src/v3/tests/pipeline.test.ts +29 -0
  69. package/src/v3/tests/preprocess.test.ts +186 -0
  70. package/src/v3/tests/primitive.test.ts +440 -0
  71. package/src/v3/tests/promise.test.ts +90 -0
  72. package/src/v3/tests/readonly.test.ts +194 -0
  73. package/src/v3/tests/record.test.ts +171 -0
  74. package/src/v3/tests/recursive.test.ts +197 -0
  75. package/src/v3/tests/refine.test.ts +313 -0
  76. package/src/v3/tests/safeparse.test.ts +27 -0
  77. package/src/v3/tests/set.test.ts +142 -0
  78. package/src/v3/tests/standard-schema.test.ts +83 -0
  79. package/src/v3/tests/string.test.ts +916 -0
  80. package/src/v3/tests/transformer.test.ts +233 -0
  81. package/src/v3/tests/tuple.test.ts +90 -0
  82. package/src/v3/tests/unions.test.ts +57 -0
  83. package/src/v3/tests/validations.test.ts +133 -0
  84. package/src/v3/tests/void.test.ts +15 -0
  85. package/src/v3/types.ts +5136 -0
  86. package/src/v4/classic/checks.ts +30 -0
  87. package/src/v4/classic/coerce.ts +27 -0
  88. package/src/v4/classic/compat.ts +66 -0
  89. package/src/v4/classic/errors.ts +75 -0
  90. package/src/v4/classic/external.ts +50 -0
  91. package/src/v4/classic/index.ts +5 -0
  92. package/src/v4/classic/iso.ts +90 -0
  93. package/src/v4/classic/parse.ts +33 -0
  94. package/src/v4/classic/schemas.ts +2055 -0
  95. package/src/v4/classic/tests/anyunknown.test.ts +26 -0
  96. package/src/v4/classic/tests/array.test.ts +264 -0
  97. package/src/v4/classic/tests/assignability.test.ts +210 -0
  98. package/src/v4/classic/tests/async-parsing.test.ts +381 -0
  99. package/src/v4/classic/tests/async-refinements.test.ts +68 -0
  100. package/src/v4/classic/tests/base.test.ts +7 -0
  101. package/src/v4/classic/tests/bigint.test.ts +54 -0
  102. package/src/v4/classic/tests/brand.test.ts +65 -0
  103. package/src/v4/classic/tests/catch.test.ts +252 -0
  104. package/src/v4/classic/tests/coalesce.test.ts +20 -0
  105. package/src/v4/classic/tests/coerce.test.ts +160 -0
  106. package/src/v4/classic/tests/continuability.test.ts +352 -0
  107. package/src/v4/classic/tests/custom.test.ts +40 -0
  108. package/src/v4/classic/tests/date.test.ts +31 -0
  109. package/src/v4/classic/tests/datetime.test.ts +296 -0
  110. package/src/v4/classic/tests/default.test.ts +313 -0
  111. package/src/v4/classic/tests/description.test.ts +32 -0
  112. package/src/v4/classic/tests/discriminated-unions.test.ts +592 -0
  113. package/src/v4/classic/tests/enum.test.ts +285 -0
  114. package/src/v4/classic/tests/error-utils.test.ts +527 -0
  115. package/src/v4/classic/tests/error.test.ts +711 -0
  116. package/src/v4/classic/tests/file.test.ts +91 -0
  117. package/src/v4/classic/tests/firstparty.test.ts +175 -0
  118. package/src/v4/classic/tests/function.test.ts +268 -0
  119. package/src/v4/classic/tests/generics.test.ts +72 -0
  120. package/src/v4/classic/tests/index.test.ts +829 -0
  121. package/src/v4/classic/tests/instanceof.test.ts +34 -0
  122. package/src/v4/classic/tests/intersection.test.ts +171 -0
  123. package/src/v4/classic/tests/json.test.ts +108 -0
  124. package/src/v4/classic/tests/lazy.test.ts +227 -0
  125. package/src/v4/classic/tests/literal.test.ts +92 -0
  126. package/src/v4/classic/tests/map.test.ts +196 -0
  127. package/src/v4/classic/tests/nan.test.ts +21 -0
  128. package/src/v4/classic/tests/nested-refine.test.ts +168 -0
  129. package/src/v4/classic/tests/nonoptional.test.ts +86 -0
  130. package/src/v4/classic/tests/nullable.test.ts +22 -0
  131. package/src/v4/classic/tests/number.test.ts +247 -0
  132. package/src/v4/classic/tests/object.test.ts +553 -0
  133. package/src/v4/classic/tests/optional.test.ts +103 -0
  134. package/src/v4/classic/tests/partial.test.ts +147 -0
  135. package/src/v4/classic/tests/pickomit.test.ts +127 -0
  136. package/src/v4/classic/tests/pipe.test.ts +81 -0
  137. package/src/v4/classic/tests/prefault.test.ts +37 -0
  138. package/src/v4/classic/tests/preprocess.test.ts +298 -0
  139. package/src/v4/classic/tests/primitive.test.ts +175 -0
  140. package/src/v4/classic/tests/promise.test.ts +81 -0
  141. package/src/v4/classic/tests/prototypes.test.ts +23 -0
  142. package/src/v4/classic/tests/readonly.test.ts +252 -0
  143. package/src/v4/classic/tests/record.test.ts +332 -0
  144. package/src/v4/classic/tests/recursive-types.test.ts +325 -0
  145. package/src/v4/classic/tests/refine.test.ts +423 -0
  146. package/src/v4/classic/tests/registries.test.ts +195 -0
  147. package/src/v4/classic/tests/set.test.ts +179 -0
  148. package/src/v4/classic/tests/standard-schema.test.ts +57 -0
  149. package/src/v4/classic/tests/string-formats.test.ts +109 -0
  150. package/src/v4/classic/tests/string.test.ts +881 -0
  151. package/src/v4/classic/tests/stringbool.test.ts +66 -0
  152. package/src/v4/classic/tests/template-literal.test.ts +758 -0
  153. package/src/v4/classic/tests/to-json-schema.test.ts +2182 -0
  154. package/src/v4/classic/tests/transform.test.ts +250 -0
  155. package/src/v4/classic/tests/tuple.test.ts +163 -0
  156. package/src/v4/classic/tests/union.test.ts +94 -0
  157. package/src/v4/classic/tests/validations.test.ts +283 -0
  158. package/src/v4/classic/tests/void.test.ts +12 -0
  159. package/src/v4/core/api.ts +1592 -0
  160. package/src/v4/core/checks.ts +1285 -0
  161. package/src/v4/core/config.ts +15 -0
  162. package/src/v4/core/core.ts +134 -0
  163. package/src/v4/core/doc.ts +44 -0
  164. package/src/v4/core/errors.ts +420 -0
  165. package/src/v4/core/function.ts +176 -0
  166. package/src/v4/core/index.ts +15 -0
  167. package/src/v4/core/json-schema.ts +143 -0
  168. package/src/v4/core/parse.ts +94 -0
  169. package/src/v4/core/regexes.ts +135 -0
  170. package/src/v4/core/registries.ts +86 -0
  171. package/src/v4/core/schemas.ts +3781 -0
  172. package/src/v4/core/standard-schema.ts +64 -0
  173. package/src/v4/core/tests/index.test.ts +46 -0
  174. package/src/v4/core/tests/locales/be.test.ts +124 -0
  175. package/src/v4/core/tests/locales/en.test.ts +22 -0
  176. package/src/v4/core/tests/locales/ru.test.ts +128 -0
  177. package/src/v4/core/tests/locales/tr.test.ts +69 -0
  178. package/src/v4/core/to-json-schema.ts +944 -0
  179. package/src/v4/core/util.ts +775 -0
  180. package/src/v4/core/versions.ts +5 -0
  181. package/src/v4/core/zsf.ts +323 -0
  182. package/src/v4/index.ts +4 -0
  183. package/src/v4/locales/ar.ts +125 -0
  184. package/src/v4/locales/az.ts +121 -0
  185. package/src/v4/locales/be.ts +184 -0
  186. package/src/v4/locales/ca.ts +127 -0
  187. package/src/v4/locales/cs.ts +142 -0
  188. package/src/v4/locales/de.ts +124 -0
  189. package/src/v4/locales/en.ts +127 -0
  190. package/src/v4/locales/eo.ts +125 -0
  191. package/src/v4/locales/es.ts +125 -0
  192. package/src/v4/locales/fa.ts +134 -0
  193. package/src/v4/locales/fi.ts +131 -0
  194. package/src/v4/locales/fr-CA.ts +126 -0
  195. package/src/v4/locales/fr.ts +124 -0
  196. package/src/v4/locales/he.ts +125 -0
  197. package/src/v4/locales/hu.ts +126 -0
  198. package/src/v4/locales/id.ts +125 -0
  199. package/src/v4/locales/index.ts +39 -0
  200. package/src/v4/locales/it.ts +125 -0
  201. package/src/v4/locales/ja.ts +122 -0
  202. package/src/v4/locales/kh.ts +126 -0
  203. package/src/v4/locales/ko.ts +131 -0
  204. package/src/v4/locales/mk.ts +127 -0
  205. package/src/v4/locales/ms.ts +124 -0
  206. package/src/v4/locales/nl.ts +126 -0
  207. package/src/v4/locales/no.ts +124 -0
  208. package/src/v4/locales/ota.ts +125 -0
  209. package/src/v4/locales/pl.ts +126 -0
  210. package/src/v4/locales/ps.ts +133 -0
  211. package/src/v4/locales/pt.ts +123 -0
  212. package/src/v4/locales/ru.ts +184 -0
  213. package/src/v4/locales/sl.ts +126 -0
  214. package/src/v4/locales/sv.ts +127 -0
  215. package/src/v4/locales/ta.ts +125 -0
  216. package/src/v4/locales/th.ts +126 -0
  217. package/src/v4/locales/tr.ts +121 -0
  218. package/src/v4/locales/ua.ts +126 -0
  219. package/src/v4/locales/ur.ts +126 -0
  220. package/src/v4/locales/vi.ts +125 -0
  221. package/src/v4/locales/zh-CN.ts +123 -0
  222. package/src/v4/locales/zh-TW.ts +125 -0
  223. package/src/v4/mini/checks.ts +32 -0
  224. package/src/v4/mini/coerce.ts +22 -0
  225. package/src/v4/mini/external.ts +40 -0
  226. package/src/v4/mini/index.ts +3 -0
  227. package/src/v4/mini/iso.ts +62 -0
  228. package/src/v4/mini/parse.ts +1 -0
  229. package/src/v4/mini/schemas.ts +1579 -0
  230. package/src/v4/mini/tests/assignability.test.ts +129 -0
  231. package/src/v4/mini/tests/brand.test.ts +51 -0
  232. package/src/v4/mini/tests/checks.test.ts +144 -0
  233. package/src/v4/mini/tests/computed.test.ts +36 -0
  234. package/src/v4/mini/tests/error.test.ts +22 -0
  235. package/src/v4/mini/tests/functions.test.ts +43 -0
  236. package/src/v4/mini/tests/index.test.ts +871 -0
  237. package/src/v4/mini/tests/number.test.ts +95 -0
  238. package/src/v4/mini/tests/object.test.ts +185 -0
  239. package/src/v4/mini/tests/prototypes.test.ts +43 -0
  240. package/src/v4/mini/tests/recursive-types.test.ts +275 -0
  241. package/src/v4/mini/tests/string.test.ts +293 -0
  242. package/src/v4-mini/index.ts +1 -0
  243. package/v4/classic/compat.cjs +1 -7
  244. package/v4/classic/compat.d.cts +0 -2
  245. package/v4/classic/compat.d.ts +0 -2
  246. package/v4/classic/compat.js +0 -6
  247. package/v4/classic/external.cjs +2 -1
  248. package/v4/classic/external.d.cts +1 -1
  249. package/v4/classic/external.d.ts +1 -1
  250. package/v4/classic/external.js +1 -1
  251. package/v4/classic/schemas.d.cts +4 -4
  252. package/v4/classic/schemas.d.ts +4 -4
  253. package/v4/core/api.cjs +11 -10
  254. package/v4/core/api.js +11 -10
  255. package/v4/core/checks.cjs +6 -4
  256. package/v4/core/checks.js +6 -4
  257. package/v4/core/core.cjs +5 -1
  258. package/v4/core/core.d.cts +2 -0
  259. package/v4/core/core.d.ts +2 -0
  260. package/v4/core/core.js +4 -0
  261. package/v4/core/schemas.cjs +12 -13
  262. package/v4/core/schemas.js +12 -13
  263. package/v4/core/util.cjs +3 -0
  264. package/v4/core/util.d.cts +1 -1
  265. package/v4/core/util.d.ts +1 -1
  266. package/v4/core/util.js +3 -0
  267. package/v4/locales/eo.cjs +144 -0
  268. package/v4/locales/eo.d.cts +5 -0
  269. package/v4/locales/eo.d.ts +5 -0
  270. package/v4/locales/eo.js +116 -0
  271. package/v4/locales/index.cjs +3 -1
  272. package/v4/locales/index.d.cts +1 -0
  273. package/v4/locales/index.d.ts +1 -0
  274. package/v4/locales/index.js +1 -0
  275. package/v4/mini/external.cjs +2 -1
  276. package/v4/mini/external.d.cts +1 -1
  277. package/v4/mini/external.d.ts +1 -1
  278. package/v4/mini/external.js +1 -1
@@ -0,0 +1,553 @@
1
+ import { expect, expectTypeOf, test } from "vitest";
2
+ import * as z from "zod/v4";
3
+ import * as core from "zod/v4/core";
4
+
5
+ const Test = z.object({
6
+ f1: z.number(),
7
+ f2: z.string().optional(),
8
+ f3: z.string().nullable(),
9
+ f4: z.array(z.object({ t: z.union([z.string(), z.boolean()]) })),
10
+ });
11
+
12
+ test("object type inference", () => {
13
+ type TestType = {
14
+ f1: number;
15
+ f2?: string | undefined;
16
+ f3: string | null;
17
+ f4: { t: string | boolean }[];
18
+ };
19
+
20
+ expectTypeOf<z.TypeOf<typeof Test>>().toEqualTypeOf<TestType>();
21
+ });
22
+
23
+ test("unknown throw", () => {
24
+ const asdf: unknown = 35;
25
+ expect(() => Test.parse(asdf)).toThrow();
26
+ });
27
+
28
+ test("shape() should return schema of particular key", () => {
29
+ const f1Schema = Test.shape.f1;
30
+ const f2Schema = Test.shape.f2;
31
+ const f3Schema = Test.shape.f3;
32
+ const f4Schema = Test.shape.f4;
33
+
34
+ expect(f1Schema).toBeInstanceOf(z.ZodNumber);
35
+ expect(f2Schema).toBeInstanceOf(z.ZodOptional);
36
+ expect(f3Schema).toBeInstanceOf(z.ZodNullable);
37
+ expect(f4Schema).toBeInstanceOf(z.ZodArray);
38
+ });
39
+
40
+ test("correct parsing", () => {
41
+ Test.parse({
42
+ f1: 12,
43
+ f2: "string",
44
+ f3: "string",
45
+ f4: [
46
+ {
47
+ t: "string",
48
+ },
49
+ ],
50
+ });
51
+
52
+ Test.parse({
53
+ f1: 12,
54
+ f3: null,
55
+ f4: [
56
+ {
57
+ t: false,
58
+ },
59
+ ],
60
+ });
61
+ });
62
+
63
+ test("nonstrict by default", () => {
64
+ z.object({ points: z.number() }).parse({
65
+ points: 2314,
66
+ unknown: "asdf",
67
+ });
68
+ });
69
+
70
+ test("parse optional keys ", () => {
71
+ const schema = z.object({
72
+ a: z.string().optional(),
73
+ });
74
+ expect(schema.parse({ a: "asdf" })).toEqual({ a: "asdf" });
75
+ });
76
+
77
+ test("empty object", () => {
78
+ const schema = z.object({});
79
+ expect(schema.parse({})).toEqual({});
80
+ expect(schema.parse({ name: "asdf" })).toEqual({});
81
+ expect(schema.safeParse(null).success).toEqual(false);
82
+ expect(schema.safeParse("asdf").success).toEqual(false);
83
+ expectTypeOf<z.output<typeof schema>>().toEqualTypeOf<Record<string, never>>();
84
+ });
85
+
86
+ const data = {
87
+ points: 2314,
88
+ unknown: "asdf",
89
+ };
90
+
91
+ test("strip by default", () => {
92
+ const val = z.object({ points: z.number() }).parse(data);
93
+ expect(val).toEqual({ points: 2314 });
94
+ });
95
+
96
+ test("unknownkeys override", () => {
97
+ const val = z.object({ points: z.number() }).strict().passthrough().strip().passthrough().parse(data);
98
+
99
+ expect(val).toEqual(data);
100
+ });
101
+
102
+ test("passthrough unknown", () => {
103
+ const val = z.object({ points: z.number() }).passthrough().parse(data);
104
+
105
+ expect(val).toEqual(data);
106
+ });
107
+
108
+ test("strip unknown", () => {
109
+ const val = z.object({ points: z.number() }).strip().parse(data);
110
+
111
+ expect(val).toEqual({ points: 2314 });
112
+ });
113
+
114
+ test("strict", () => {
115
+ const val = z.object({ points: z.number() }).strict().safeParse(data);
116
+
117
+ expect(val.success).toEqual(false);
118
+ });
119
+
120
+ test("catchall inference", () => {
121
+ const o1 = z
122
+ .object({
123
+ first: z.string(),
124
+ })
125
+ .catchall(z.number());
126
+
127
+ const d1 = o1.parse({ first: "asdf", num: 1243 });
128
+ // expectTypeOf<(typeof d1)["asdf"]>().toEqualTypeOf<number>();
129
+ expectTypeOf<(typeof d1)["first"]>().toEqualTypeOf<string>();
130
+ });
131
+
132
+ test("catchall overrides strict", () => {
133
+ const o1 = z.object({ first: z.string().optional() }).strict().catchall(z.number());
134
+
135
+ // should run fine
136
+ // setting a catchall overrides the unknownKeys behavior
137
+ o1.parse({
138
+ asdf: 1234,
139
+ });
140
+
141
+ // should only run catchall validation
142
+ // against unknown keys
143
+ o1.parse({
144
+ first: "asdf",
145
+ asdf: 1234,
146
+ });
147
+ });
148
+
149
+ test("catchall overrides strict", () => {
150
+ const o1 = z
151
+ .object({
152
+ first: z.string(),
153
+ })
154
+ .strict()
155
+ .catchall(z.number());
156
+
157
+ // should run fine
158
+ // setting a catchall overrides the unknownKeys behavior
159
+ o1.parse({
160
+ first: "asdf",
161
+ asdf: 1234,
162
+ });
163
+ });
164
+
165
+ test("optional keys are unset", async () => {
166
+ const SNamedEntity = z.object({
167
+ id: z.string(),
168
+ set: z.string().optional(),
169
+ unset: z.string().optional(),
170
+ });
171
+ const result = await SNamedEntity.parse({
172
+ id: "asdf",
173
+ set: undefined,
174
+ });
175
+ expect(Object.keys(result)).toEqual(["id", "set"]);
176
+ });
177
+
178
+ test("catchall parsing", async () => {
179
+ const result = z.object({ name: z.string() }).catchall(z.number()).parse({ name: "Foo", validExtraKey: 61 });
180
+
181
+ expect(result).toEqual({ name: "Foo", validExtraKey: 61 });
182
+
183
+ const result2 = z
184
+ .object({ name: z.string() })
185
+ .catchall(z.number())
186
+ .safeParse({ name: "Foo", validExtraKey: 61, invalid: "asdf" });
187
+
188
+ expect(result2.success).toEqual(false);
189
+ });
190
+
191
+ test("nonexistent keys", async () => {
192
+ const Schema = z.union([z.object({ a: z.string() }), z.object({ b: z.number() })]);
193
+ const obj = { a: "A" };
194
+ const result = await Schema.spa(obj); // Works with 1.11.10, breaks with 2.0.0-beta.21
195
+ expect(result.success).toBe(true);
196
+ });
197
+
198
+ test("test async union", async () => {
199
+ const Schema2 = z.union([
200
+ z.object({
201
+ ty: z.string(),
202
+ }),
203
+ z.object({
204
+ ty: z.number(),
205
+ }),
206
+ ]);
207
+
208
+ const obj = { ty: "A" };
209
+ const result = await Schema2.spa(obj); // Works with 1.11.10, breaks with 2.0.0-beta.21
210
+ expect(result.success).toEqual(true);
211
+ });
212
+
213
+ test("test inferred merged type", async () => {
214
+ const asdf = z.object({ a: z.string() }).merge(z.object({ a: z.number() }));
215
+ type asdf = z.infer<typeof asdf>;
216
+
217
+ expectTypeOf<asdf>().toEqualTypeOf<{ a: number }>();
218
+ });
219
+
220
+ test("inferred merged object type with optional properties", async () => {
221
+ const Merged = z
222
+ .object({ a: z.string(), b: z.string().optional() })
223
+ .merge(z.object({ a: z.string().optional(), b: z.string() }));
224
+ type Merged = z.infer<typeof Merged>;
225
+ expectTypeOf<Merged>().toEqualTypeOf<{ a?: string; b: string }>();
226
+ expectTypeOf<Merged>().toEqualTypeOf<{ a?: string; b: string }>();
227
+ });
228
+
229
+ test("inferred unioned object type with optional properties", async () => {
230
+ const Unioned = z.union([
231
+ z.object({ a: z.string(), b: z.string().optional() }),
232
+ z.object({ a: z.string().optional(), b: z.string() }),
233
+ ]);
234
+ type Unioned = z.infer<typeof Unioned>;
235
+ expectTypeOf<Unioned>().toEqualTypeOf<{ a: string; b?: string } | { a?: string; b: string }>();
236
+ });
237
+
238
+ test("inferred enum type", async () => {
239
+ const Enum = z.object({ a: z.string(), b: z.string().optional() }).keyof();
240
+
241
+ expect(Enum.enum).toEqual({
242
+ a: "a",
243
+ b: "b",
244
+ });
245
+
246
+ expect(Enum._zod.def.entries).toEqual({
247
+ a: "a",
248
+ b: "b",
249
+ });
250
+ type Enum = z.infer<typeof Enum>;
251
+ expectTypeOf<Enum>().toEqualTypeOf<"a" | "b">();
252
+ });
253
+
254
+ test("inferred partial object type with optional properties", async () => {
255
+ const Partial = z.object({ a: z.string(), b: z.string().optional() }).partial();
256
+ type Partial = z.infer<typeof Partial>;
257
+ expectTypeOf<Partial>().toEqualTypeOf<{ a?: string; b?: string }>();
258
+ });
259
+
260
+ test("inferred picked object type with optional properties", async () => {
261
+ const Picked = z.object({ a: z.string(), b: z.string().optional() }).pick({ b: true });
262
+ type Picked = z.infer<typeof Picked>;
263
+ expectTypeOf<Picked>().toEqualTypeOf<{ b?: string }>();
264
+ });
265
+
266
+ test("inferred type for unknown/any keys", () => {
267
+ const myType = z.object({
268
+ anyOptional: z.any().optional(),
269
+ anyRequired: z.any(),
270
+ unknownOptional: z.unknown().optional(),
271
+ unknownRequired: z.unknown(),
272
+ });
273
+ type myType = z.infer<typeof myType>;
274
+ expectTypeOf<myType>().toEqualTypeOf<{
275
+ anyOptional?: any;
276
+ anyRequired: any;
277
+ unknownOptional?: unknown;
278
+ unknownRequired: unknown;
279
+ }>();
280
+ });
281
+
282
+ test("strictObject", async () => {
283
+ const strictObj = z.strictObject({
284
+ name: z.string(),
285
+ });
286
+
287
+ const syncResult = strictObj.safeParse({ name: "asdf", unexpected: 13 });
288
+ expect(syncResult.success).toEqual(false);
289
+
290
+ const asyncResult = await strictObj.spa({ name: "asdf", unexpected: 13 });
291
+ expect(asyncResult.success).toEqual(false);
292
+ });
293
+
294
+ test("object with refine", async () => {
295
+ const schema = z
296
+ .object({
297
+ a: z.string().default("foo"),
298
+ b: z.number(),
299
+ })
300
+ .refine(() => true);
301
+ expect(schema.parse({ b: 5 })).toEqual({ b: 5, a: "foo" });
302
+ const result = await schema.parseAsync({ b: 5 });
303
+ expect(result).toEqual({ b: 5, a: "foo" });
304
+ });
305
+
306
+ test("intersection of object with date", async () => {
307
+ const schema = z.object({
308
+ a: z.date(),
309
+ });
310
+ expect(z.intersection(schema, schema).parse({ a: new Date(1637353595983) })).toEqual({
311
+ a: new Date(1637353595983),
312
+ });
313
+ const result = await schema.parseAsync({ a: new Date(1637353595983) });
314
+ expect(result).toEqual({ a: new Date(1637353595983) });
315
+ });
316
+
317
+ test("intersection of object with refine with date", async () => {
318
+ const schema = z
319
+ .object({
320
+ a: z.date(),
321
+ })
322
+ .refine(() => true);
323
+ expect(z.intersection(schema, schema).parse({ a: new Date(1637353595983) })).toEqual({
324
+ a: new Date(1637353595983),
325
+ });
326
+ const result = await schema.parseAsync({ a: new Date(1637353595983) });
327
+ expect(result).toEqual({ a: new Date(1637353595983) });
328
+ });
329
+
330
+ test("constructor key", () => {
331
+ const person = z
332
+ .object({
333
+ name: z.string(),
334
+ })
335
+ .strict();
336
+
337
+ expect(() =>
338
+ person.parse({
339
+ name: "bob dylan",
340
+ constructor: 61,
341
+ })
342
+ ).toThrow();
343
+ });
344
+
345
+ test("constructor key", () => {
346
+ const Example = z.object({
347
+ prop: z.string(),
348
+ opt: z.number().optional(),
349
+ arr: z.string().array(),
350
+ });
351
+
352
+ type Example = z.infer<typeof Example>;
353
+ expectTypeOf<keyof Example>().toEqualTypeOf<"prop" | "opt" | "arr">();
354
+ });
355
+
356
+ test("catchall", () => {
357
+ const a = z.object({});
358
+ expect(a._zod.def.catchall).toBeUndefined();
359
+
360
+ const b = z.strictObject({});
361
+ expect(b._zod.def.catchall).toBeInstanceOf(core.$ZodNever);
362
+
363
+ const c = z.looseObject({});
364
+ expect(c._zod.def.catchall).toBeInstanceOf(core.$ZodUnknown);
365
+
366
+ const d = z.object({}).catchall(z.number());
367
+ expect(d._zod.def.catchall).toBeInstanceOf(core.$ZodNumber);
368
+ });
369
+
370
+ test("unknownkeys merging", () => {
371
+ // This one is "strict"
372
+ const a = z.looseObject({
373
+ a: z.string(),
374
+ });
375
+
376
+ const b = z.strictObject({ b: z.string() });
377
+
378
+ // incoming object overrides
379
+ const c = a.merge(b);
380
+ expect(c._zod.def.catchall).toBeInstanceOf(core.$ZodNever);
381
+ });
382
+
383
+ const personToExtend = z.object({
384
+ firstName: z.string(),
385
+ lastName: z.string(),
386
+ });
387
+
388
+ test("extend() should return schema with new key", () => {
389
+ const PersonWithNickname = personToExtend.extend({ nickName: z.string() });
390
+ type PersonWithNickname = z.infer<typeof PersonWithNickname>;
391
+
392
+ const expected = { firstName: "f", nickName: "n", lastName: "l" };
393
+ const actual = PersonWithNickname.parse(expected);
394
+
395
+ expect(actual).toEqual(expected);
396
+ expectTypeOf<keyof PersonWithNickname>().toEqualTypeOf<"firstName" | "lastName" | "nickName">();
397
+ expectTypeOf<PersonWithNickname>().toEqualTypeOf<{ firstName: string; lastName: string; nickName: string }>();
398
+ });
399
+
400
+ test("extend() should have power to override existing key", () => {
401
+ const PersonWithNumberAsLastName = personToExtend.extend({
402
+ lastName: z.number(),
403
+ });
404
+ type PersonWithNumberAsLastName = z.infer<typeof PersonWithNumberAsLastName>;
405
+
406
+ const expected = { firstName: "f", lastName: 42 };
407
+ const actual = PersonWithNumberAsLastName.parse(expected);
408
+
409
+ expect(actual).toEqual(expected);
410
+ expectTypeOf<PersonWithNumberAsLastName>().toEqualTypeOf<{ firstName: string; lastName: number }>();
411
+ });
412
+
413
+ test("passthrough index signature", () => {
414
+ const a = z.object({ a: z.string() });
415
+ type a = z.infer<typeof a>;
416
+ expectTypeOf<a>().toEqualTypeOf<{ a: string }>();
417
+ const b = a.passthrough();
418
+ type b = z.infer<typeof b>;
419
+ expectTypeOf<b>().toEqualTypeOf<{ a: string; [k: string]: unknown }>();
420
+ });
421
+
422
+ // test("xor", () => {
423
+ // type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
424
+ // type XOR<T, U> = T extends object ? (U extends object ? (Without<T, U> & U) | (Without<U, T> & T) : U) : T;
425
+
426
+ // type A = { name: string; a: number };
427
+ // type B = { name: string; b: number };
428
+ // type C = XOR<A, B>;
429
+ // type Outer = { data: C };
430
+ // const Outer = z.object({
431
+ // data: z.union([z.object({ name: z.string(), a: z.number() }), z.object({ name: z.string(), b: z.number() })]),
432
+ // }) satisfies z.ZodType<Outer, any>;
433
+ // });
434
+
435
+ test("assignability", () => {
436
+ z.object({ a: z.string() }) satisfies z.ZodObject<{ a: z.ZodString }>;
437
+ z.object({ a: z.string() }).catchall(z.number()) satisfies z.ZodObject<{ a: z.ZodString }>;
438
+ z.object({ a: z.string() }).strict() satisfies z.ZodObject;
439
+ z.object({}) satisfies z.ZodObject;
440
+
441
+ z.looseObject({ name: z.string() }) satisfies z.ZodObject<
442
+ {
443
+ name: z.ZodString;
444
+ },
445
+ z.core.$loose
446
+ >;
447
+ z.looseObject({ name: z.string() }) satisfies z.ZodObject<{
448
+ name: z.ZodString;
449
+ }>;
450
+ z.strictObject({ name: z.string() }) satisfies z.ZodObject<
451
+ {
452
+ name: z.ZodString;
453
+ },
454
+ z.core.$loose
455
+ >;
456
+ z.strictObject({ name: z.string() }) satisfies z.ZodObject<
457
+ {
458
+ name: z.ZodString;
459
+ },
460
+ z.core.$strict
461
+ >;
462
+ z.object({ name: z.string() }) satisfies z.ZodObject<{
463
+ name: z.ZodString;
464
+ }>;
465
+ z.object({
466
+ a: z.string(),
467
+ b: z.number(),
468
+ c: z.boolean(),
469
+ }) satisfies z.core.$ZodObject;
470
+ });
471
+
472
+ test("null prototype", () => {
473
+ const schema = z.object({ a: z.string() });
474
+ const obj = Object.create(null);
475
+ obj.a = "foo";
476
+ expect(schema.parse(obj)).toEqual({ a: "foo" });
477
+ });
478
+
479
+ test("empty objects", () => {
480
+ const A = z.looseObject({});
481
+ type Ain = z.input<typeof A>;
482
+ expectTypeOf<Ain>().toEqualTypeOf<Record<string, unknown>>();
483
+ type Aout = z.output<typeof A>;
484
+ expectTypeOf<Aout>().toEqualTypeOf<Record<string, unknown>>();
485
+
486
+ const B = z.object({});
487
+ type Bout = z.output<typeof B>;
488
+ expectTypeOf<Bout>().toEqualTypeOf<Record<string, never>>();
489
+ type Bin = z.input<typeof B>;
490
+ expectTypeOf<Bin>().toEqualTypeOf<Record<string, never>>();
491
+
492
+ const C = z.strictObject({});
493
+ type Cout = z.output<typeof C>;
494
+ expectTypeOf<Cout>().toEqualTypeOf<Record<string, never>>();
495
+ type Cin = z.input<typeof C>;
496
+ expectTypeOf<Cin>().toEqualTypeOf<Record<string, never>>();
497
+ });
498
+
499
+ test("preserve key order", () => {
500
+ const schema = z.object({
501
+ a: z.string().optional(),
502
+ b: z.string(),
503
+ });
504
+ const r1 = schema.safeParse({ a: "asdf", b: "qwer" });
505
+ const r2 = schema.safeParse({ a: "asdf", b: "qwer" }, { jitless: true });
506
+
507
+ expect(Object.keys(r1.data!)).toMatchInlineSnapshot(`
508
+ [
509
+ "a",
510
+ "b",
511
+ ]
512
+ `);
513
+ expect(Object.keys(r1.data!)).toEqual(Object.keys(r2.data!));
514
+ });
515
+
516
+ test("empty shape", () => {
517
+ const a = z.object({});
518
+
519
+ a.parse({});
520
+ a.parse({}, { jitless: true });
521
+ a.parse(Object.create(null));
522
+ a.parse(Object.create(null), { jitless: true });
523
+
524
+ expect(() => a.parse([])).toThrow();
525
+ expect(() => a.parse([], { jitless: true })).toThrow();
526
+ });
527
+
528
+ test("zodtype assignability", () => {
529
+ // Does not error
530
+ z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello?: string | undefined }>;
531
+ z.object({ hello: z.string() }) satisfies z.ZodType<{ hello?: string | undefined }>;
532
+ // @ts-expect-error
533
+ z.object({}) satisfies z.ZodType<{ hello: string | undefined }>;
534
+ // @ts-expect-error
535
+ z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello: string | undefined }>;
536
+ // @ts-expect-error
537
+ z.object({ hello: z.string().optional() }) satisfies z.ZodType<{ hello: string }>;
538
+ // @ts-expect-error
539
+ z.object({ hello: z.number() }) satisfies z.ZodType<{ hello?: string | undefined }>;
540
+ });
541
+
542
+ test("index signature in shape", () => {
543
+ function makeZodObj<const T extends string>(key: T) {
544
+ return z.looseObject({
545
+ [key]: z.string(),
546
+ });
547
+ }
548
+
549
+ const schema = makeZodObj("foo");
550
+ type schema = z.infer<typeof schema>;
551
+
552
+ expectTypeOf<schema>().toEqualTypeOf<Record<string, unknown>>();
553
+ });
@@ -0,0 +1,103 @@
1
+ // @ts-ignore TS6133
2
+ import { expect, expectTypeOf, test } from "vitest";
3
+
4
+ import * as z from "zod/v4";
5
+
6
+ test(".optional()", () => {
7
+ const schema = z.string().optional();
8
+ expect(schema.parse("adsf")).toEqual("adsf");
9
+ expect(schema.parse(undefined)).toEqual(undefined);
10
+ expect(schema.safeParse(null).success).toEqual(false);
11
+
12
+ expectTypeOf<typeof schema._output>().toEqualTypeOf<string | undefined>();
13
+ });
14
+
15
+ test("unwrap", () => {
16
+ const unwrapped = z.string().optional().unwrap();
17
+ expect(unwrapped).toBeInstanceOf(z.ZodString);
18
+ });
19
+
20
+ test("optionality", () => {
21
+ const a = z.string();
22
+ expect(a._zod.optin).toEqual(undefined);
23
+ expect(a._zod.optout).toEqual(undefined);
24
+
25
+ const b = z.string().optional();
26
+ expect(b._zod.optin).toEqual("optional");
27
+ expect(b._zod.optout).toEqual("optional");
28
+
29
+ const c = z.string().default("asdf");
30
+ expect(c._zod.optin).toEqual("optional");
31
+ expect(c._zod.optout).toEqual(undefined);
32
+
33
+ const d = z.string().optional().nullable();
34
+ expect(d._zod.optin).toEqual("optional");
35
+ expect(d._zod.optout).toEqual("optional");
36
+
37
+ const e = z.string().default("asdf").nullable();
38
+ expect(e._zod.optin).toEqual("optional");
39
+ expect(e._zod.optout).toEqual(undefined);
40
+ });
41
+
42
+ test("pipe optionality", () => {
43
+ z.string().optional()._zod.optin;
44
+ const a = z.string().optional().pipe(z.string());
45
+ expect(a._zod.optin).toEqual("optional");
46
+ expect(a._zod.optout).toEqual(undefined);
47
+ expectTypeOf<typeof a._zod.optin>().toEqualTypeOf<"optional">();
48
+ expectTypeOf<typeof a._zod.optout>().toEqualTypeOf<"optional" | undefined>();
49
+
50
+ const b = z
51
+ .string()
52
+ .transform((val) => (Math.random() ? val : undefined))
53
+ .pipe(z.string().optional());
54
+ expect(b._zod.optin).toEqual(undefined);
55
+ expect(b._zod.optout).toEqual("optional");
56
+ expectTypeOf<typeof b._zod.optin>().toEqualTypeOf<"optional" | undefined>();
57
+ expectTypeOf<typeof b._zod.optout>().toEqualTypeOf<"optional">();
58
+
59
+ const c = z.string().default("asdf").pipe(z.string());
60
+ expect(c._zod.optin).toEqual("optional");
61
+ expect(c._zod.optout).toEqual(undefined);
62
+
63
+ const d = z
64
+ .string()
65
+ .transform((val) => (Math.random() ? val : undefined))
66
+ .pipe(z.string().default("asdf"));
67
+ expect(d._zod.optin).toEqual(undefined);
68
+ expect(d._zod.optout).toEqual(undefined);
69
+ });
70
+
71
+ test("pipe optionality inside objects", () => {
72
+ const schema = z.object({
73
+ a: z.string().optional(),
74
+ b: z.string().optional().pipe(z.string()),
75
+ c: z.string().default("asdf").pipe(z.string()),
76
+ d: z
77
+ .string()
78
+ .transform((val) => (Math.random() ? val : undefined))
79
+ .pipe(z.string().optional()),
80
+ e: z
81
+ .string()
82
+ .transform((val) => (Math.random() ? val : undefined))
83
+ .pipe(z.string().default("asdf")),
84
+ });
85
+
86
+ type SchemaIn = z.input<typeof schema>;
87
+ expectTypeOf<SchemaIn>().toEqualTypeOf<{
88
+ a?: string | undefined;
89
+ b?: string | undefined;
90
+ c?: string | undefined;
91
+ d: string;
92
+ e: string;
93
+ }>();
94
+
95
+ type SchemaOut = z.output<typeof schema>;
96
+ expectTypeOf<SchemaOut>().toEqualTypeOf<{
97
+ a?: string | undefined;
98
+ b: string;
99
+ c: string;
100
+ d?: string | undefined;
101
+ e: string;
102
+ }>();
103
+ });