zod 4.2.0-canary.20251106T214835 → 4.2.0-canary.20251106T231624

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "4.2.0-canary.20251106T214835",
3
+ "version": "4.2.0-canary.20251106T231624",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -145,6 +145,24 @@ test("tuple with optional elements followed by required", () => {
145
145
  }
146
146
  });
147
147
 
148
+ test("tuple with all optional elements", () => {
149
+ const allOptionalTuple = z.tuple([z.string().optional(), z.number().optional(), z.boolean().optional()]);
150
+ expectTypeOf<typeof allOptionalTuple._output>().toEqualTypeOf<[string?, number?, boolean?]>();
151
+
152
+ // Empty array should be valid (all items optional)
153
+ expect(allOptionalTuple.parse([])).toEqual([]);
154
+
155
+ // Partial arrays should be valid
156
+ expect(allOptionalTuple.parse(["hello"])).toEqual(["hello"]);
157
+ expect(allOptionalTuple.parse(["hello", 42])).toEqual(["hello", 42]);
158
+
159
+ // Full array should be valid
160
+ expect(allOptionalTuple.parse(["hello", 42, true])).toEqual(["hello", 42, true]);
161
+
162
+ // Array that's too long should fail
163
+ expect(() => allOptionalTuple.parse(["hello", 42, true, "extra"])).toThrow();
164
+ });
165
+
148
166
  test("tuple with rest schema", () => {
149
167
  const myTuple = z.tuple([z.string(), z.number()]).rest(z.boolean());
150
168
  expect(myTuple.parse(["asdf", 1234, true, false, true])).toEqual(["asdf", 1234, true, false, true]);
@@ -2411,7 +2411,6 @@ export interface $ZodTuple<
2411
2411
  export const $ZodTuple: core.$constructor<$ZodTuple> = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def) => {
2412
2412
  $ZodType.init(inst, def);
2413
2413
  const items = def.items;
2414
- const optStart = items.length - [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
2415
2414
 
2416
2415
  inst._zod.parse = (payload, ctx) => {
2417
2416
  const input = payload.value;
@@ -2428,6 +2427,9 @@ export const $ZodTuple: core.$constructor<$ZodTuple> = /*@__PURE__*/ core.$const
2428
2427
  payload.value = [];
2429
2428
  const proms: Promise<any>[] = [];
2430
2429
 
2430
+ const reversedIndex = [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
2431
+ const optStart = reversedIndex === -1 ? 0 : items.length - reversedIndex;
2432
+
2431
2433
  if (!def.rest) {
2432
2434
  const tooBig = input.length > items.length;
2433
2435
  const tooSmall = input.length < optStart - 1;
@@ -4164,10 +4166,10 @@ export const $ZodLazy: core.$constructor<$ZodLazy> = /*@__PURE__*/ core.$constru
4164
4166
  // return () => _innerType;
4165
4167
  // });
4166
4168
  util.defineLazy(inst._zod, "innerType", () => def.getter() as $ZodType);
4167
- util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType._zod.pattern);
4168
- util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType._zod.propValues);
4169
- util.defineLazy(inst._zod, "optin", () => inst._zod.innerType._zod.optin ?? undefined);
4170
- util.defineLazy(inst._zod, "optout", () => inst._zod.innerType._zod.optout ?? undefined);
4169
+ util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod.pattern);
4170
+ util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod.propValues);
4171
+ util.defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod.optin ?? undefined);
4172
+ util.defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod.optout ?? undefined);
4171
4173
  inst._zod.parse = (payload, ctx) => {
4172
4174
  const inner = inst._zod.innerType;
4173
4175
  return inner._zod.run(payload, ctx);
@@ -1146,7 +1146,6 @@ function handleIntersectionResults(result, left, right) {
1146
1146
  exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
1147
1147
  exports.$ZodType.init(inst, def);
1148
1148
  const items = def.items;
1149
- const optStart = items.length - [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
1150
1149
  inst._zod.parse = (payload, ctx) => {
1151
1150
  const input = payload.value;
1152
1151
  if (!Array.isArray(input)) {
@@ -1160,6 +1159,8 @@ exports.$ZodTuple = core.$constructor("$ZodTuple", (inst, def) => {
1160
1159
  }
1161
1160
  payload.value = [];
1162
1161
  const proms = [];
1162
+ const reversedIndex = [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
1163
+ const optStart = reversedIndex === -1 ? 0 : items.length - reversedIndex;
1163
1164
  if (!def.rest) {
1164
1165
  const tooBig = input.length > items.length;
1165
1166
  const tooSmall = input.length < optStart - 1;
@@ -1935,10 +1936,10 @@ exports.$ZodLazy = core.$constructor("$ZodLazy", (inst, def) => {
1935
1936
  // return () => _innerType;
1936
1937
  // });
1937
1938
  util.defineLazy(inst._zod, "innerType", () => def.getter());
1938
- util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType._zod.pattern);
1939
- util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType._zod.propValues);
1940
- util.defineLazy(inst._zod, "optin", () => inst._zod.innerType._zod.optin ?? undefined);
1941
- util.defineLazy(inst._zod, "optout", () => inst._zod.innerType._zod.optout ?? undefined);
1939
+ util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod.pattern);
1940
+ util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod.propValues);
1941
+ util.defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod.optin ?? undefined);
1942
+ util.defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod.optout ?? undefined);
1942
1943
  inst._zod.parse = (payload, ctx) => {
1943
1944
  const inner = inst._zod.innerType;
1944
1945
  return inner._zod.run(payload, ctx);
@@ -1115,7 +1115,6 @@ function handleIntersectionResults(result, left, right) {
1115
1115
  export const $ZodTuple = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def) => {
1116
1116
  $ZodType.init(inst, def);
1117
1117
  const items = def.items;
1118
- const optStart = items.length - [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
1119
1118
  inst._zod.parse = (payload, ctx) => {
1120
1119
  const input = payload.value;
1121
1120
  if (!Array.isArray(input)) {
@@ -1129,6 +1128,8 @@ export const $ZodTuple = /*@__PURE__*/ core.$constructor("$ZodTuple", (inst, def
1129
1128
  }
1130
1129
  payload.value = [];
1131
1130
  const proms = [];
1131
+ const reversedIndex = [...items].reverse().findIndex((item) => item._zod.optin !== "optional");
1132
+ const optStart = reversedIndex === -1 ? 0 : items.length - reversedIndex;
1132
1133
  if (!def.rest) {
1133
1134
  const tooBig = input.length > items.length;
1134
1135
  const tooSmall = input.length < optStart - 1;
@@ -1904,10 +1905,10 @@ export const $ZodLazy = /*@__PURE__*/ core.$constructor("$ZodLazy", (inst, def)
1904
1905
  // return () => _innerType;
1905
1906
  // });
1906
1907
  util.defineLazy(inst._zod, "innerType", () => def.getter());
1907
- util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType._zod.pattern);
1908
- util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType._zod.propValues);
1909
- util.defineLazy(inst._zod, "optin", () => inst._zod.innerType._zod.optin ?? undefined);
1910
- util.defineLazy(inst._zod, "optout", () => inst._zod.innerType._zod.optout ?? undefined);
1908
+ util.defineLazy(inst._zod, "pattern", () => inst._zod.innerType?._zod.pattern);
1909
+ util.defineLazy(inst._zod, "propValues", () => inst._zod.innerType?._zod.propValues);
1910
+ util.defineLazy(inst._zod, "optin", () => inst._zod.innerType?._zod.optin ?? undefined);
1911
+ util.defineLazy(inst._zod, "optout", () => inst._zod.innerType?._zod.optout ?? undefined);
1911
1912
  inst._zod.parse = (payload, ctx) => {
1912
1913
  const inner = inst._zod.innerType;
1913
1914
  return inner._zod.run(payload, ctx);