zod 4.4.1 → 4.4.3

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/README.md CHANGED
@@ -31,23 +31,6 @@
31
31
  <br/>
32
32
  <br/>
33
33
 
34
- <h2 align="center">Featured sponsor: Jazz</h2>
35
-
36
- <div align="center">
37
- <a href="https://jazz.tools/?utm_source=zod">
38
- <picture width="85%" >
39
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/garden-co/jazz/938f6767e46cdfded60e50d99bf3b533f4809c68/homepage/homepage/public/Zod%20sponsor%20message.png">
40
- <img alt="jazz logo" src="https://raw.githubusercontent.com/garden-co/jazz/938f6767e46cdfded60e50d99bf3b533f4809c68/homepage/homepage/public/Zod%20sponsor%20message.png" width="85%">
41
- </picture>
42
- </a>
43
- <br/>
44
- <p><sub>Learn more about <a target="_blank" rel="noopener noreferrer" href="mailto:sponsorship@colinhacks.com">featured sponsorships</a></sub></p>
45
- </div>
46
-
47
- <br/>
48
- <br/>
49
- <br/>
50
-
51
34
  ### [Read the docs →](https://zod.dev/api)
52
35
 
53
36
  <br/>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "4.4.1",
3
+ "version": "4.4.3",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -1617,7 +1617,7 @@ export const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion> = /
1617
1617
  );
1618
1618
 
1619
1619
  export function discriminatedUnion<
1620
- Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]],
1620
+ Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]],
1621
1621
  Disc extends string,
1622
1622
  >(
1623
1623
  discriminator: Disc,
@@ -2034,10 +2034,12 @@ export const ZodTransform: core.$constructor<ZodTransform> = /*@__PURE__*/ core.
2034
2034
  if (output instanceof Promise) {
2035
2035
  return output.then((output) => {
2036
2036
  payload.value = output;
2037
+ payload.fallback = true;
2037
2038
  return payload;
2038
2039
  });
2039
2040
  }
2040
2041
  payload.value = output;
2042
+ payload.fallback = true;
2041
2043
  return payload;
2042
2044
  };
2043
2045
  }
@@ -2361,6 +2363,22 @@ export function invertCodec<A extends core.SomeType, B extends core.SomeType>(co
2361
2363
  }) as any;
2362
2364
  }
2363
2365
 
2366
+ // ZodPreprocess
2367
+ export interface ZodPreprocess<B extends core.SomeType = core.$ZodType>
2368
+ extends ZodPipe<core.$ZodTransform, B>,
2369
+ core.$ZodPreprocess<B> {
2370
+ "~standard": ZodStandardSchemaWithJSON<this>;
2371
+ _zod: core.$ZodPreprocessInternals<B>;
2372
+ def: core.$ZodPreprocessDef<B>;
2373
+ }
2374
+ export const ZodPreprocess: core.$constructor<ZodPreprocess> = /*@__PURE__*/ core.$constructor(
2375
+ "ZodPreprocess",
2376
+ (inst, def) => {
2377
+ ZodPipe.init(inst, def);
2378
+ core.$ZodPreprocess.init(inst, def);
2379
+ }
2380
+ );
2381
+
2364
2382
  // ZodReadonly
2365
2383
  export interface ZodReadonly<T extends core.SomeType = core.$ZodType>
2366
2384
  extends _ZodType<core.$ZodReadonlyInternals<T>>,
@@ -2645,6 +2663,10 @@ export function json(params?: string | core.$ZodCustomParams): ZodJSONSchema {
2645
2663
  export function preprocess<A, U extends core.SomeType, B = unknown>(
2646
2664
  fn: (arg: B, ctx: core.$RefinementCtx) => A,
2647
2665
  schema: U
2648
- ): ZodPipe<ZodTransform<A, B>, U> {
2649
- return pipe(transform(fn as any), schema as any) as any;
2666
+ ): ZodPreprocess<U> {
2667
+ return new ZodPreprocess({
2668
+ type: "pipe",
2669
+ in: transform(fn as any) as any as core.$ZodTransform,
2670
+ out: schema as any as core.$ZodType,
2671
+ }) as any;
2650
2672
  }
@@ -143,6 +143,12 @@ test("assignability", () => {
143
143
  z.unknown().pipe(z.number()) satisfies z.core.$ZodPipe;
144
144
  z.unknown().pipe(z.number()) satisfies z.ZodPipe;
145
145
 
146
+ // $ZodPreprocess
147
+ z.preprocess((v) => v, z.number()) satisfies z.core.$ZodPreprocess;
148
+ z.preprocess((v) => v, z.number()) satisfies z.ZodPreprocess;
149
+ z.preprocess((v) => v, z.number()) satisfies z.core.$ZodPipe<z.core.$ZodTransform, z.ZodNumber>;
150
+ z.preprocess((v) => v, z.number()) satisfies z.ZodPipe<z.core.$ZodTransform, z.ZodNumber>;
151
+
146
152
  // $ZodSuccess
147
153
  z.success(z.string()) satisfies z.core.$ZodSuccess;
148
154
  z.success(z.string()) satisfies z.ZodSuccess;
@@ -128,8 +128,9 @@ test("native enum", () => {
128
128
  fruit: z.nativeEnum(Fruits).catch(Fruits.apple),
129
129
  });
130
130
 
131
- expect(schema.safeParse({}).success).toEqual(false);
132
- expect(schema.safeParse({}, { jitless: true }).success).toEqual(false);
131
+ // Absent keys flow through to the catch handler.
132
+ expect(schema.parse({})).toEqual({ fruit: Fruits.apple });
133
+ expect(schema.parse({}, { jitless: true })).toEqual({ fruit: Fruits.apple });
133
134
  expect(schema.parse({ fruit: 15 })).toEqual({ fruit: Fruits.apple });
134
135
  });
135
136
 
@@ -138,8 +139,8 @@ test("enum", () => {
138
139
  fruit: z.enum(["apple", "orange"]).catch("apple"),
139
140
  });
140
141
 
141
- expect(schema.safeParse({}).success).toEqual(false);
142
- expect(schema.safeParse({}, { jitless: true }).success).toEqual(false);
142
+ expect(schema.parse({})).toEqual({ fruit: "apple" });
143
+ expect(schema.parse({}, { jitless: true })).toEqual({ fruit: "apple" });
143
144
  expect(schema.parse({ fruit: true })).toEqual({ fruit: "apple" });
144
145
  expect(schema.parse({ fruit: 15 })).toEqual({ fruit: "apple" });
145
146
  });
@@ -276,3 +277,50 @@ test("direction-aware catch", () => {
276
277
  // But valid values should still work in reverse
277
278
  expect(z.encode(schema, "world")).toBe("world");
278
279
  });
280
+
281
+ test("optional clobbers catch through pipe boundaries", () => {
282
+ expect(
283
+ z
284
+ .string()
285
+ .catch("X")
286
+ .transform((s) => s + "!")
287
+ .optional()
288
+ .parse(undefined)
289
+ ).toBeUndefined();
290
+ expect(z.string().catch("X").pipe(z.string()).optional().parse(undefined)).toBeUndefined();
291
+ expect(
292
+ z
293
+ .string()
294
+ .catch("X")
295
+ .transform((s) => s + "!")
296
+ .transform((s) => s.toLowerCase())
297
+ .optional()
298
+ .parse(undefined)
299
+ ).toBeUndefined();
300
+ expect(
301
+ z
302
+ .object({
303
+ a: z
304
+ .string()
305
+ .catch("X")
306
+ .transform((s) => s + "!")
307
+ .optional(),
308
+ })
309
+ .parse({})
310
+ ).toEqual({});
311
+
312
+ expect(
313
+ z
314
+ .string()
315
+ .catch("X")
316
+ .transform((s) => s + "!")
317
+ .parse("hi")
318
+ ).toBe("hi!");
319
+ expect(
320
+ z
321
+ .string()
322
+ .catch("X")
323
+ .transform((s) => s + "!")
324
+ .parse(123)
325
+ ).toBe("X!");
326
+ });
@@ -264,6 +264,9 @@ test("valid discriminator value, invalid data", () => {
264
264
  });
265
265
 
266
266
  test("wrong schema - missing discriminator", () => {
267
+ // @ts-expect-error missing discriminator property
268
+ z.discriminatedUnion("type", [z.object({ value: z.string() })]);
269
+
267
270
  try {
268
271
  z.discriminatedUnion("type", [
269
272
  z.object({ type: z.literal("a"), a: z.string() }),
@@ -156,30 +156,27 @@ test("catch/prefault/default", () => {
156
156
  f: z.string().prefault("prefault value"),
157
157
  });
158
158
 
159
- expect(mySchema.safeParse({}).error!.issues).toMatchInlineSnapshot(`
160
- [
161
- {
162
- "code": "invalid_type",
163
- "expected": "nonoptional",
164
- "message": "Invalid input: expected nonoptional, received undefined",
165
- "path": [
166
- "d",
167
- ],
168
- },
169
- ]
159
+ // Catch (d) and default/prefault (b, c, e, f) handle absent keys gracefully.
160
+ // `a: catch().optional()` short-circuits to undefined when the original
161
+ // input was undefined, so the property is omitted from the output. All
162
+ // other catch/default/prefault keys produce their fallback values.
163
+ expect(mySchema.parse({})).toMatchInlineSnapshot(`
164
+ {
165
+ "b": "default value",
166
+ "c": "prefault value",
167
+ "d": "catch value",
168
+ "e": "default value",
169
+ "f": "prefault value",
170
+ }
170
171
  `);
171
-
172
- expect(mySchema.safeParse({}, { jitless: true }).error!.issues).toMatchInlineSnapshot(`
173
- [
174
- {
175
- "code": "invalid_type",
176
- "expected": "nonoptional",
177
- "message": "Invalid input: expected nonoptional, received undefined",
178
- "path": [
179
- "d",
180
- ],
181
- },
182
- ]
172
+ expect(mySchema.parse({}, { jitless: true })).toMatchInlineSnapshot(`
173
+ {
174
+ "b": "default value",
175
+ "c": "prefault value",
176
+ "d": "catch value",
177
+ "e": "default value",
178
+ "f": "prefault value",
179
+ }
183
180
  `);
184
181
 
185
182
  expect(mySchema.parse({ d: undefined })).toMatchInlineSnapshot(`
@@ -0,0 +1,26 @@
1
+ import { expectTypeOf, test } from "vitest";
2
+ import * as z from "zod/v4";
3
+
4
+ test("ZodPreprocess<B> assignable to ZodPipe<$ZodTransform, B>", () => {
5
+ const pre = z.preprocess((v) => v, z.string().optional());
6
+ const _asPipe: z.ZodPipe<z.core.$ZodTransform, z.ZodOptional<z.ZodString>> = pre;
7
+ const _asCorePipe: z.core.$ZodPipe<z.core.$ZodTransform, z.ZodOptional<z.ZodString>> = pre;
8
+ expectTypeOf(_asPipe).toMatchTypeOf<z.ZodPipe>();
9
+ expectTypeOf(_asCorePipe).toMatchTypeOf<z.core.$ZodPipe>();
10
+ });
11
+
12
+ test("ZodPreprocess optin/optout defer to B", () => {
13
+ const optionalInside = z.preprocess((v) => v, z.string().optional());
14
+ expectTypeOf<(typeof optionalInside)["_zod"]["optin"]>().toEqualTypeOf<"optional">();
15
+ expectTypeOf<(typeof optionalInside)["_zod"]["optout"]>().toEqualTypeOf<"optional">();
16
+
17
+ const required = z.preprocess((v) => v, z.string());
18
+ expectTypeOf<(typeof required)["_zod"]["optin"]>().toEqualTypeOf<"optional" | undefined>();
19
+ expectTypeOf<(typeof required)["_zod"]["optout"]>().toEqualTypeOf<"optional" | undefined>();
20
+ });
21
+
22
+ test("ZodPreprocess input/output inference", () => {
23
+ const pre = z.preprocess((v) => v, z.number().optional());
24
+ expectTypeOf<z.output<typeof pre>>().toEqualTypeOf<number | undefined>();
25
+ expectTypeOf<z.input<typeof pre>>().toEqualTypeOf<unknown>();
26
+ });
@@ -280,3 +280,72 @@ test("perform transform with non-fatal issues", () => {
280
280
  ]]
281
281
  `);
282
282
  });
283
+
284
+ test("preprocess accepts absent object keys (4.3 parity)", () => {
285
+ const schema = z.object({ a: z.preprocess((v) => v ?? "X", z.string()) });
286
+ expect(schema.parse({})).toEqual({ a: "X" });
287
+ expect(schema.parse({ a: "hi" })).toEqual({ a: "hi" });
288
+ expect(schema.parse({ a: undefined })).toEqual({ a: "X" });
289
+
290
+ // Outer optional clobbers preprocess output on undefined input
291
+ expect(
292
+ z
293
+ .preprocess((v) => v ?? "X", z.string())
294
+ .optional()
295
+ .parse(undefined)
296
+ ).toBeUndefined();
297
+ expect(
298
+ z
299
+ .preprocess((v) => v ?? "X", z.string())
300
+ .optional()
301
+ .parse("hi")
302
+ ).toBe("hi");
303
+ expect(z.object({ a: z.preprocess((v) => v ?? "X", z.string()).optional() }).parse({})).toEqual({});
304
+
305
+ // Top-level direct call unchanged
306
+ expect(z.preprocess((v) => v ?? "X", z.string()).parse(undefined)).toBe("X");
307
+ });
308
+
309
+ // https://github.com/colinhacks/zod/issues/5917
310
+ test("optional propagates through preprocess inside object", () => {
311
+ const outer = z.object({ x: z.preprocess((v) => v, z.number()).optional() });
312
+ const inner = z.object({ x: z.preprocess((v) => v, z.number().optional()) });
313
+
314
+ expect(outer.safeParse({}).success).toBe(true);
315
+ expect(inner.safeParse({}).success).toBe(true);
316
+
317
+ expect(outer.safeParse({ x: 1 })).toEqual({ success: true, data: { x: 1 } });
318
+ expect(inner.safeParse({ x: 1 })).toEqual({ success: true, data: { x: 1 } });
319
+
320
+ expect(inner._zod.def.shape.x._zod.optin).toBe("optional");
321
+ expect(inner._zod.def.shape.x._zod.optout).toBe("optional");
322
+ });
323
+
324
+ test("preprocess is a structural subtype of ZodPipe", () => {
325
+ const schema = z.preprocess((v) => v, z.string());
326
+ expect(schema).toBeInstanceOf(z.ZodPipe);
327
+ expect(schema).toBeInstanceOf(z.ZodPreprocess);
328
+ expect(schema._zod.def.type).toBe("pipe");
329
+ });
330
+
331
+ test("preprocess does not propagate values/propValues from inner schema", () => {
332
+ const inner = z.preprocess((v) => v, z.literal("test"));
333
+ expect(inner._zod.values).toBeUndefined();
334
+ expect(inner._zod.propValues).toBeUndefined();
335
+ });
336
+
337
+ test("preprocess as discriminator throws at construction (no propValues to inherit)", () => {
338
+ const schema = z.discriminatedUnion("kind", [
339
+ z.object({ kind: z.preprocess((v: any) => String(v).toUpperCase(), z.literal("A")), a: z.string() }),
340
+ z.object({ kind: z.preprocess((v: any) => String(v).toUpperCase(), z.literal("B")), b: z.number() }),
341
+ ]);
342
+ expect(() => schema.parse({ kind: "a", a: "x" })).toThrow(/Invalid discriminated union option/);
343
+ });
344
+
345
+ test("preprocess as record key does not restrict accepted keys", () => {
346
+ const schema = z.record(
347
+ z.preprocess((v: any) => String(v).toLowerCase(), z.enum(["a", "b"])),
348
+ z.string()
349
+ );
350
+ expect(schema.safeParse({ A: "x", B: "y" })).toEqual({ success: true, data: { a: "x", b: "y" } });
351
+ });
@@ -2731,7 +2731,6 @@ test("input type", () => {
2731
2731
  "required": [
2732
2732
  "a",
2733
2733
  "d",
2734
- "f",
2735
2734
  "g",
2736
2735
  ],
2737
2736
  "type": "object",
@@ -2896,6 +2895,28 @@ test("use output type for preprocess", () => {
2896
2895
  `);
2897
2896
  });
2898
2897
 
2898
+ test("strip output-side examples from input JSON schema for codec", () => {
2899
+ const codec = z
2900
+ .codec(z.string(), z.number(), { decode: (s) => Number(s), encode: (n) => String(n) })
2901
+ .meta({ examples: [42] });
2902
+
2903
+ expect(z.toJSONSchema(codec, { io: "input" })).toMatchInlineSnapshot(`
2904
+ {
2905
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2906
+ "type": "string",
2907
+ }
2908
+ `);
2909
+ expect(z.toJSONSchema(codec, { io: "output" })).toMatchInlineSnapshot(`
2910
+ {
2911
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
2912
+ "examples": [
2913
+ 42,
2914
+ ],
2915
+ "type": "number",
2916
+ }
2917
+ `);
2918
+ });
2919
+
2899
2920
  // test("isTransforming", () => {
2900
2921
  // const tx = z.core.isTransforming;
2901
2922
  // expect(tx(z.string())).toEqual(false);
@@ -1207,17 +1207,19 @@ export function _xor<const T extends readonly schemas.$ZodObject[]>(
1207
1207
  }
1208
1208
 
1209
1209
  // ZodDiscriminatedUnion
1210
- export interface $ZodTypeDiscriminableInternals extends schemas.$ZodTypeInternals {
1210
+ export interface $ZodTypeDiscriminableInternals<Disc extends string = string>
1211
+ extends schemas.$ZodTypeInternals<unknown, { [K in Disc]?: unknown }> {
1211
1212
  propValues: util.PropValues;
1212
1213
  }
1213
1214
 
1214
- export interface $ZodTypeDiscriminable extends schemas.$ZodType {
1215
- _zod: $ZodTypeDiscriminableInternals;
1215
+ export interface $ZodTypeDiscriminable<Disc extends string = string> extends schemas.$ZodType {
1216
+ _zod: $ZodTypeDiscriminableInternals<Disc>;
1216
1217
  }
1218
+
1217
1219
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
1218
1220
  // @__NO_SIDE_EFFECTS__
1219
1221
  export function _discriminatedUnion<
1220
- Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]],
1222
+ Types extends [$ZodTypeDiscriminable<Disc>, ...$ZodTypeDiscriminable<Disc>[]],
1221
1223
  Disc extends string,
1222
1224
  >(
1223
1225
  Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>,
@@ -525,7 +525,8 @@ export const catchProcessor: Processor<schemas.$ZodCatch> = (schema, ctx, json,
525
525
 
526
526
  export const pipeProcessor: Processor<schemas.$ZodPipe> = (schema, ctx, _json, params) => {
527
527
  const def = schema._zod.def as schemas.$ZodPipeDef;
528
- const innerType = ctx.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
528
+ const inIsTransform = def.in._zod.traits.has("$ZodTransform");
529
+ const innerType = ctx.io === "input" ? (inIsTransform ? def.out : def.in) : def.out;
529
530
  process(innerType, ctx as any, params);
530
531
  const seen = ctx.seen.get(schema)!;
531
532
  seen.ref = innerType;
@@ -36,6 +36,11 @@ export interface ParsePayload<T = unknown> {
36
36
  issues: errors.$ZodRawIssue[];
37
37
  /** A way to mark a whole payload as aborted. Used in codecs/pipes. */
38
38
  aborted?: boolean;
39
+ /** @internal Marks a value as a fallback that an outer wrapper (e.g.
40
+ * $ZodOptional) may override with its own interpretation when input was
41
+ * undefined. Set by $ZodCatch when catchValue substitutes and by every
42
+ * $ZodTransform invocation. */
43
+ fallback?: boolean | undefined;
39
44
  }
40
45
 
41
46
  export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
@@ -3417,6 +3422,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
3417
3422
  "$ZodTransform",
3418
3423
  (inst, def) => {
3419
3424
  $ZodType.init(inst, def);
3425
+ inst._zod.optin = "optional";
3420
3426
  inst._zod.parse = (payload, ctx) => {
3421
3427
  if (ctx.direction === "backward") {
3422
3428
  throw new core.$ZodEncodeError(inst.constructor.name);
@@ -3427,6 +3433,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
3427
3433
  const output = _out instanceof Promise ? _out : Promise.resolve(_out);
3428
3434
  return output.then((output) => {
3429
3435
  payload.value = output;
3436
+ payload.fallback = true;
3430
3437
  return payload;
3431
3438
  });
3432
3439
  }
@@ -3436,6 +3443,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
3436
3443
  }
3437
3444
 
3438
3445
  payload.value = _out;
3446
+ payload.fallback = true;
3439
3447
  return payload;
3440
3448
  };
3441
3449
  }
@@ -3468,7 +3476,7 @@ export interface $ZodOptional<T extends SomeType = $ZodType> extends $ZodType {
3468
3476
  }
3469
3477
 
3470
3478
  function handleOptionalResult(result: ParsePayload, input: unknown) {
3471
- if (result.issues.length && input === undefined) {
3479
+ if (input === undefined && (result.issues.length || result.fallback)) {
3472
3480
  return { issues: [], value: undefined };
3473
3481
  }
3474
3482
  return result;
@@ -3491,9 +3499,10 @@ export const $ZodOptional: core.$constructor<$ZodOptional> = /*@__PURE__*/ core.
3491
3499
 
3492
3500
  inst._zod.parse = (payload, ctx) => {
3493
3501
  if (def.innerType._zod.optin === "optional") {
3502
+ const input = payload.value;
3494
3503
  const result = def.innerType._zod.run(payload, ctx);
3495
- if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, payload.value));
3496
- return handleOptionalResult(result, payload.value);
3504
+ if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, input));
3505
+ return handleOptionalResult(result, input);
3497
3506
  }
3498
3507
  if (payload.value === undefined) {
3499
3508
  return payload;
@@ -3886,7 +3895,7 @@ export interface $ZodCatch<T extends SomeType = $ZodType> extends $ZodType {
3886
3895
 
3887
3896
  export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
3888
3897
  $ZodType.init(inst, def);
3889
- util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
3898
+ inst._zod.optin = "optional";
3890
3899
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
3891
3900
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
3892
3901
 
@@ -3909,6 +3918,7 @@ export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$const
3909
3918
  input: payload.value,
3910
3919
  });
3911
3920
  payload.issues = [];
3921
+ payload.fallback = true;
3912
3922
  }
3913
3923
 
3914
3924
  return payload;
@@ -3926,6 +3936,7 @@ export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$const
3926
3936
  });
3927
3937
 
3928
3938
  payload.issues = [];
3939
+ payload.fallback = true;
3929
3940
  }
3930
3941
 
3931
3942
  return payload;
@@ -4030,7 +4041,7 @@ function handlePipeResult(left: ParsePayload, next: $ZodType, ctx: ParseContextI
4030
4041
  left.aborted = true;
4031
4042
  return left;
4032
4043
  }
4033
- return next._zod.run({ value: left.value, issues: left.issues }, ctx);
4044
+ return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
4034
4045
  }
4035
4046
 
4036
4047
  ////////////////////////////////////////////
@@ -4118,6 +4129,35 @@ function handleCodecTxResult(left: ParsePayload, value: any, nextSchema: SomeTyp
4118
4129
  return nextSchema._zod.run({ value, issues: left.issues }, ctx);
4119
4130
  }
4120
4131
 
4132
+ /////////////////////////////////////////////////
4133
+ /////////////////////////////////////////////////
4134
+ ////////// //////////
4135
+ ////////// $ZodPreprocess //////////
4136
+ ////////// //////////
4137
+ /////////////////////////////////////////////////
4138
+ /////////////////////////////////////////////////
4139
+ export interface $ZodPreprocessDef<B extends SomeType = $ZodType> extends $ZodPipeDef<$ZodTransform, B> {
4140
+ in: $ZodTransform;
4141
+ out: B;
4142
+ }
4143
+
4144
+ export interface $ZodPreprocessInternals<B extends SomeType = $ZodType> extends $ZodPipeInternals<$ZodTransform, B> {
4145
+ def: $ZodPreprocessDef<B>;
4146
+ optin: B["_zod"]["optin"];
4147
+ optout: B["_zod"]["optout"];
4148
+ }
4149
+
4150
+ export interface $ZodPreprocess<B extends SomeType = $ZodType> extends $ZodPipe<$ZodTransform, B> {
4151
+ _zod: $ZodPreprocessInternals<B>;
4152
+ }
4153
+
4154
+ export const $ZodPreprocess: core.$constructor<$ZodPreprocess> = /*@__PURE__*/ core.$constructor(
4155
+ "$ZodPreprocess",
4156
+ (inst, def) => {
4157
+ $ZodPipe.init(inst, def);
4158
+ }
4159
+ );
4160
+
4121
4161
  ////////////////////////////////////////////
4122
4162
  ////////////////////////////////////////////
4123
4163
  ////////// //////////
@@ -561,6 +561,7 @@ function isTransforming(
561
561
  return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
562
562
  }
563
563
  if (def.type === "pipe") {
564
+ if (_schema._zod.traits.has("$ZodCodec")) return true;
564
565
  return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
565
566
  }
566
567
 
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 4,
4
- patch: 1 as number,
4
+ patch: 3 as number,
5
5
  } as const;
@@ -1083,7 +1083,7 @@ export const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUn
1083
1083
 
1084
1084
  // @__NO_SIDE_EFFECTS__
1085
1085
  export function discriminatedUnion<
1086
- Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]],
1086
+ Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]],
1087
1087
  Disc extends string,
1088
1088
  >(
1089
1089
  discriminator: Disc,
@@ -247,6 +247,11 @@ test("z.union([]) / z.xor([]) / z.discriminatedUnion(_, []) construct and reject
247
247
  }
248
248
  });
249
249
 
250
+ test("z.discriminatedUnion rejects object options missing the discriminator at type level", () => {
251
+ // @ts-expect-error missing discriminator property
252
+ z.discriminatedUnion("type", [z.object({ value: z.string() })]);
253
+ });
254
+
250
255
  test("z.intersection", () => {
251
256
  const a = z.intersection(z.object({ a: z.string() }), z.object({ b: z.number() }));
252
257
  expect(z.parse(a, { a: "hello", b: 123 })).toEqual({ a: "hello", b: 123 });
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.ZodLiteral = exports.ZodEnum = exports.ZodSet = exports.ZodMap = exports.ZodRecord = exports.ZodTuple = exports.ZodIntersection = exports.ZodDiscriminatedUnion = exports.ZodXor = exports.ZodUnion = exports.ZodObject = exports.ZodArray = exports.ZodDate = exports.ZodVoid = exports.ZodNever = exports.ZodUnknown = exports.ZodAny = exports.ZodNull = exports.ZodUndefined = exports.ZodSymbol = exports.ZodBigIntFormat = exports.ZodBigInt = exports.ZodBoolean = exports.ZodNumberFormat = exports.ZodNumber = exports.ZodCustomStringFormat = exports.ZodJWT = exports.ZodE164 = exports.ZodBase64URL = exports.ZodBase64 = exports.ZodCIDRv6 = exports.ZodCIDRv4 = exports.ZodIPv6 = exports.ZodMAC = exports.ZodIPv4 = exports.ZodKSUID = exports.ZodXID = exports.ZodULID = exports.ZodCUID2 = exports.ZodCUID = exports.ZodNanoID = exports.ZodEmoji = exports.ZodURL = exports.ZodUUID = exports.ZodGUID = exports.ZodEmail = exports.ZodStringFormat = exports.ZodString = exports._ZodString = exports.ZodType = void 0;
27
- exports.stringbool = exports.meta = exports.describe = exports.ZodCustom = exports.ZodFunction = exports.ZodPromise = exports.ZodLazy = exports.ZodTemplateLiteral = exports.ZodReadonly = exports.ZodCodec = exports.ZodPipe = exports.ZodNaN = exports.ZodCatch = exports.ZodSuccess = exports.ZodNonOptional = exports.ZodPrefault = exports.ZodDefault = exports.ZodNullable = exports.ZodExactOptional = exports.ZodOptional = exports.ZodTransform = exports.ZodFile = void 0;
27
+ exports.stringbool = exports.meta = exports.describe = exports.ZodCustom = exports.ZodFunction = exports.ZodPromise = exports.ZodLazy = exports.ZodTemplateLiteral = exports.ZodReadonly = exports.ZodPreprocess = exports.ZodCodec = exports.ZodPipe = exports.ZodNaN = exports.ZodCatch = exports.ZodSuccess = exports.ZodNonOptional = exports.ZodPrefault = exports.ZodDefault = exports.ZodNullable = exports.ZodExactOptional = exports.ZodOptional = exports.ZodTransform = exports.ZodFile = void 0;
28
28
  exports.string = string;
29
29
  exports.email = email;
30
30
  exports.guid = guid;
@@ -1203,10 +1203,12 @@ exports.ZodTransform = core.$constructor("ZodTransform", (inst, def) => {
1203
1203
  if (output instanceof Promise) {
1204
1204
  return output.then((output) => {
1205
1205
  payload.value = output;
1206
+ payload.fallback = true;
1206
1207
  return payload;
1207
1208
  });
1208
1209
  }
1209
1210
  payload.value = output;
1211
+ payload.fallback = true;
1210
1212
  return payload;
1211
1213
  };
1212
1214
  });
@@ -1372,6 +1374,10 @@ function invertCodec(codec) {
1372
1374
  reverseTransform: def.transform,
1373
1375
  });
1374
1376
  }
1377
+ exports.ZodPreprocess = core.$constructor("ZodPreprocess", (inst, def) => {
1378
+ exports.ZodPipe.init(inst, def);
1379
+ core.$ZodPreprocess.init(inst, def);
1380
+ });
1375
1381
  exports.ZodReadonly = core.$constructor("ZodReadonly", (inst, def) => {
1376
1382
  core.$ZodReadonly.init(inst, def);
1377
1383
  exports.ZodType.init(inst, def);
@@ -1497,5 +1503,9 @@ function json(params) {
1497
1503
  }
1498
1504
  // preprocess
1499
1505
  function preprocess(fn, schema) {
1500
- return pipe(transform(fn), schema);
1506
+ return new exports.ZodPreprocess({
1507
+ type: "pipe",
1508
+ in: transform(fn),
1509
+ out: schema,
1510
+ });
1501
1511
  }
@@ -511,7 +511,7 @@ export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[]
511
511
  def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
512
512
  }
513
513
  export declare const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion>;
514
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
514
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
515
515
  export interface ZodIntersection<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodIntersectionInternals<A, B>>, core.$ZodIntersection<A, B> {
516
516
  "~standard": ZodStandardSchemaWithJSON<this>;
517
517
  }
@@ -674,6 +674,12 @@ export declare function codec<const A extends core.SomeType, B extends core.Some
674
674
  encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.util.MaybeAsync<core.output<A>>;
675
675
  }): ZodCodec<A, B>;
676
676
  export declare function invertCodec<A extends core.SomeType, B extends core.SomeType>(codec: ZodCodec<A, B>): ZodCodec<B, A>;
677
+ export interface ZodPreprocess<B extends core.SomeType = core.$ZodType> extends ZodPipe<core.$ZodTransform, B>, core.$ZodPreprocess<B> {
678
+ "~standard": ZodStandardSchemaWithJSON<this>;
679
+ _zod: core.$ZodPreprocessInternals<B>;
680
+ def: core.$ZodPreprocessDef<B>;
681
+ }
682
+ export declare const ZodPreprocess: core.$constructor<ZodPreprocess>;
677
683
  export interface ZodReadonly<T extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodReadonlyInternals<T>>, core.$ZodReadonly<T> {
678
684
  "~standard": ZodStandardSchemaWithJSON<this>;
679
685
  unwrap(): T;
@@ -758,4 +764,4 @@ export interface ZodJSONSchema extends _ZodJSONSchema {
758
764
  _zod: ZodJSONSchemaInternals;
759
765
  }
760
766
  export declare function json(params?: string | core.$ZodCustomParams): ZodJSONSchema;
761
- export declare function preprocess<A, U extends core.SomeType, B = unknown>(fn: (arg: B, ctx: core.$RefinementCtx) => A, schema: U): ZodPipe<ZodTransform<A, B>, U>;
767
+ export declare function preprocess<A, U extends core.SomeType, B = unknown>(fn: (arg: B, ctx: core.$RefinementCtx) => A, schema: U): ZodPreprocess<U>;
@@ -511,7 +511,7 @@ export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[]
511
511
  def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
512
512
  }
513
513
  export declare const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion>;
514
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
514
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
515
515
  export interface ZodIntersection<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodIntersectionInternals<A, B>>, core.$ZodIntersection<A, B> {
516
516
  "~standard": ZodStandardSchemaWithJSON<this>;
517
517
  }
@@ -674,6 +674,12 @@ export declare function codec<const A extends core.SomeType, B extends core.Some
674
674
  encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.util.MaybeAsync<core.output<A>>;
675
675
  }): ZodCodec<A, B>;
676
676
  export declare function invertCodec<A extends core.SomeType, B extends core.SomeType>(codec: ZodCodec<A, B>): ZodCodec<B, A>;
677
+ export interface ZodPreprocess<B extends core.SomeType = core.$ZodType> extends ZodPipe<core.$ZodTransform, B>, core.$ZodPreprocess<B> {
678
+ "~standard": ZodStandardSchemaWithJSON<this>;
679
+ _zod: core.$ZodPreprocessInternals<B>;
680
+ def: core.$ZodPreprocessDef<B>;
681
+ }
682
+ export declare const ZodPreprocess: core.$constructor<ZodPreprocess>;
677
683
  export interface ZodReadonly<T extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodReadonlyInternals<T>>, core.$ZodReadonly<T> {
678
684
  "~standard": ZodStandardSchemaWithJSON<this>;
679
685
  unwrap(): T;
@@ -758,4 +764,4 @@ export interface ZodJSONSchema extends _ZodJSONSchema {
758
764
  _zod: ZodJSONSchemaInternals;
759
765
  }
760
766
  export declare function json(params?: string | core.$ZodCustomParams): ZodJSONSchema;
761
- export declare function preprocess<A, U extends core.SomeType, B = unknown>(fn: (arg: B, ctx: core.$RefinementCtx) => A, schema: U): ZodPipe<ZodTransform<A, B>, U>;
767
+ export declare function preprocess<A, U extends core.SomeType, B = unknown>(fn: (arg: B, ctx: core.$RefinementCtx) => A, schema: U): ZodPreprocess<U>;
@@ -1085,10 +1085,12 @@ export const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (ins
1085
1085
  if (output instanceof Promise) {
1086
1086
  return output.then((output) => {
1087
1087
  payload.value = output;
1088
+ payload.fallback = true;
1088
1089
  return payload;
1089
1090
  });
1090
1091
  }
1091
1092
  payload.value = output;
1093
+ payload.fallback = true;
1092
1094
  return payload;
1093
1095
  };
1094
1096
  });
@@ -1255,6 +1257,10 @@ export function invertCodec(codec) {
1255
1257
  reverseTransform: def.transform,
1256
1258
  });
1257
1259
  }
1260
+ export const ZodPreprocess = /*@__PURE__*/ core.$constructor("ZodPreprocess", (inst, def) => {
1261
+ ZodPipe.init(inst, def);
1262
+ core.$ZodPreprocess.init(inst, def);
1263
+ });
1258
1264
  export const ZodReadonly = /*@__PURE__*/ core.$constructor("ZodReadonly", (inst, def) => {
1259
1265
  core.$ZodReadonly.init(inst, def);
1260
1266
  ZodType.init(inst, def);
@@ -1381,5 +1387,9 @@ export function json(params) {
1381
1387
  }
1382
1388
  // preprocess
1383
1389
  export function preprocess(fn, schema) {
1384
- return pipe(transform(fn), schema);
1390
+ return new ZodPreprocess({
1391
+ type: "pipe",
1392
+ in: transform(fn),
1393
+ out: schema,
1394
+ });
1385
1395
  }
package/v4/core/api.d.cts CHANGED
@@ -225,14 +225,16 @@ export type $ZodUnionParams = TypeParams<schemas.$ZodUnion, "options">;
225
225
  export declare function _union<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodUnion>, options: T, params?: string | $ZodUnionParams): schemas.$ZodUnion<T>;
226
226
  export type $ZodXorParams = TypeParams<schemas.$ZodXor, "options">;
227
227
  export declare function _xor<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodXor>, options: T, params?: string | $ZodXorParams): schemas.$ZodXor<T>;
228
- export interface $ZodTypeDiscriminableInternals extends schemas.$ZodTypeInternals {
228
+ export interface $ZodTypeDiscriminableInternals<Disc extends string = string> extends schemas.$ZodTypeInternals<unknown, {
229
+ [K in Disc]?: unknown;
230
+ }> {
229
231
  propValues: util.PropValues;
230
232
  }
231
- export interface $ZodTypeDiscriminable extends schemas.$ZodType {
232
- _zod: $ZodTypeDiscriminableInternals;
233
+ export interface $ZodTypeDiscriminable<Disc extends string = string> extends schemas.$ZodType {
234
+ _zod: $ZodTypeDiscriminableInternals<Disc>;
233
235
  }
234
236
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
235
- export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
237
+ export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable<Disc>, ...$ZodTypeDiscriminable<Disc>[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
236
238
  export type $ZodIntersectionParams = TypeParams<schemas.$ZodIntersection, "left" | "right">;
237
239
  export declare function _intersection<T extends schemas.$ZodObject, U extends schemas.$ZodObject>(Class: util.SchemaClass<schemas.$ZodIntersection>, left: T, right: U): schemas.$ZodIntersection<T, U>;
238
240
  export type $ZodTupleParams = TypeParams<schemas.$ZodTuple, "items" | "rest">;
package/v4/core/api.d.ts CHANGED
@@ -225,14 +225,16 @@ export type $ZodUnionParams = TypeParams<schemas.$ZodUnion, "options">;
225
225
  export declare function _union<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodUnion>, options: T, params?: string | $ZodUnionParams): schemas.$ZodUnion<T>;
226
226
  export type $ZodXorParams = TypeParams<schemas.$ZodXor, "options">;
227
227
  export declare function _xor<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodXor>, options: T, params?: string | $ZodXorParams): schemas.$ZodXor<T>;
228
- export interface $ZodTypeDiscriminableInternals extends schemas.$ZodTypeInternals {
228
+ export interface $ZodTypeDiscriminableInternals<Disc extends string = string> extends schemas.$ZodTypeInternals<unknown, {
229
+ [K in Disc]?: unknown;
230
+ }> {
229
231
  propValues: util.PropValues;
230
232
  }
231
- export interface $ZodTypeDiscriminable extends schemas.$ZodType {
232
- _zod: $ZodTypeDiscriminableInternals;
233
+ export interface $ZodTypeDiscriminable<Disc extends string = string> extends schemas.$ZodType {
234
+ _zod: $ZodTypeDiscriminableInternals<Disc>;
233
235
  }
234
236
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
235
- export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
237
+ export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable<Disc>, ...$ZodTypeDiscriminable<Disc>[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
236
238
  export type $ZodIntersectionParams = TypeParams<schemas.$ZodIntersection, "left" | "right">;
237
239
  export declare function _intersection<T extends schemas.$ZodObject, U extends schemas.$ZodObject>(Class: util.SchemaClass<schemas.$ZodIntersection>, left: T, right: U): schemas.$ZodIntersection<T, U>;
238
240
  export type $ZodTupleParams = TypeParams<schemas.$ZodTuple, "items" | "rest">;
@@ -525,7 +525,8 @@ const catchProcessor = (schema, ctx, json, params) => {
525
525
  exports.catchProcessor = catchProcessor;
526
526
  const pipeProcessor = (schema, ctx, _json, params) => {
527
527
  const def = schema._zod.def;
528
- const innerType = ctx.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
528
+ const inIsTransform = def.in._zod.traits.has("$ZodTransform");
529
+ const innerType = ctx.io === "input" ? (inIsTransform ? def.out : def.in) : def.out;
529
530
  (0, to_json_schema_js_1.process)(innerType, ctx, params);
530
531
  const seen = ctx.seen.get(schema);
531
532
  seen.ref = innerType;
@@ -487,7 +487,8 @@ export const catchProcessor = (schema, ctx, json, params) => {
487
487
  };
488
488
  export const pipeProcessor = (schema, ctx, _json, params) => {
489
489
  const def = schema._zod.def;
490
- const innerType = ctx.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
490
+ const inIsTransform = def.in._zod.traits.has("$ZodTransform");
491
+ const innerType = ctx.io === "input" ? (inIsTransform ? def.out : def.in) : def.out;
491
492
  process(innerType, ctx, params);
492
493
  const seen = ctx.seen.get(schema);
493
494
  seen.ref = innerType;
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = exports.$ZodXor = exports.$ZodUnion = exports.$ZodObjectJIT = exports.$ZodObject = exports.$ZodArray = exports.$ZodDate = exports.$ZodVoid = exports.$ZodNever = exports.$ZodUnknown = exports.$ZodAny = exports.$ZodNull = exports.$ZodUndefined = exports.$ZodSymbol = exports.$ZodBigIntFormat = exports.$ZodBigInt = exports.$ZodBoolean = exports.$ZodNumberFormat = exports.$ZodNumber = exports.$ZodCustomStringFormat = exports.$ZodJWT = exports.$ZodE164 = exports.$ZodBase64URL = exports.$ZodBase64 = exports.$ZodCIDRv6 = exports.$ZodCIDRv4 = exports.$ZodMAC = exports.$ZodIPv6 = exports.$ZodIPv4 = exports.$ZodISODuration = exports.$ZodISOTime = exports.$ZodISODate = exports.$ZodISODateTime = exports.$ZodKSUID = exports.$ZodXID = exports.$ZodULID = exports.$ZodCUID2 = exports.$ZodCUID = exports.$ZodNanoID = exports.$ZodEmoji = exports.$ZodURL = exports.$ZodEmail = exports.$ZodUUID = exports.$ZodGUID = exports.$ZodStringFormat = exports.$ZodString = exports.clone = exports.$ZodType = void 0;
27
- exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodFunction = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodCodec = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodExactOptional = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = void 0;
27
+ exports.$ZodCustom = exports.$ZodLazy = exports.$ZodPromise = exports.$ZodFunction = exports.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodPreprocess = exports.$ZodCodec = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodExactOptional = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = void 0;
28
28
  exports.isValidBase64 = isValidBase64;
29
29
  exports.isValidBase64URL = isValidBase64URL;
30
30
  exports.isValidJWT = isValidJWT;
@@ -1748,6 +1748,7 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
1748
1748
  });
1749
1749
  exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
1750
1750
  exports.$ZodType.init(inst, def);
1751
+ inst._zod.optin = "optional";
1751
1752
  inst._zod.parse = (payload, ctx) => {
1752
1753
  if (ctx.direction === "backward") {
1753
1754
  throw new core.$ZodEncodeError(inst.constructor.name);
@@ -1757,6 +1758,7 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
1757
1758
  const output = _out instanceof Promise ? _out : Promise.resolve(_out);
1758
1759
  return output.then((output) => {
1759
1760
  payload.value = output;
1761
+ payload.fallback = true;
1760
1762
  return payload;
1761
1763
  });
1762
1764
  }
@@ -1764,11 +1766,12 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
1764
1766
  throw new core.$ZodAsyncError();
1765
1767
  }
1766
1768
  payload.value = _out;
1769
+ payload.fallback = true;
1767
1770
  return payload;
1768
1771
  };
1769
1772
  });
1770
1773
  function handleOptionalResult(result, input) {
1771
- if (result.issues.length && input === undefined) {
1774
+ if (input === undefined && (result.issues.length || result.fallback)) {
1772
1775
  return { issues: [], value: undefined };
1773
1776
  }
1774
1777
  return result;
@@ -1786,10 +1789,11 @@ exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
1786
1789
  });
1787
1790
  inst._zod.parse = (payload, ctx) => {
1788
1791
  if (def.innerType._zod.optin === "optional") {
1792
+ const input = payload.value;
1789
1793
  const result = def.innerType._zod.run(payload, ctx);
1790
1794
  if (result instanceof Promise)
1791
- return result.then((r) => handleOptionalResult(r, payload.value));
1792
- return handleOptionalResult(result, payload.value);
1795
+ return result.then((r) => handleOptionalResult(r, input));
1796
+ return handleOptionalResult(result, input);
1793
1797
  }
1794
1798
  if (payload.value === undefined) {
1795
1799
  return payload;
@@ -1916,7 +1920,7 @@ exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
1916
1920
  });
1917
1921
  exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
1918
1922
  exports.$ZodType.init(inst, def);
1919
- util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1923
+ inst._zod.optin = "optional";
1920
1924
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1921
1925
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1922
1926
  inst._zod.parse = (payload, ctx) => {
@@ -1937,6 +1941,7 @@ exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
1937
1941
  input: payload.value,
1938
1942
  });
1939
1943
  payload.issues = [];
1944
+ payload.fallback = true;
1940
1945
  }
1941
1946
  return payload;
1942
1947
  });
@@ -1951,6 +1956,7 @@ exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
1951
1956
  input: payload.value,
1952
1957
  });
1953
1958
  payload.issues = [];
1959
+ payload.fallback = true;
1954
1960
  }
1955
1961
  return payload;
1956
1962
  };
@@ -1997,7 +2003,7 @@ function handlePipeResult(left, next, ctx) {
1997
2003
  left.aborted = true;
1998
2004
  return left;
1999
2005
  }
2000
- return next._zod.run({ value: left.value, issues: left.issues }, ctx);
2006
+ return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
2001
2007
  }
2002
2008
  exports.$ZodCodec = core.$constructor("$ZodCodec", (inst, def) => {
2003
2009
  exports.$ZodType.init(inst, def);
@@ -2053,6 +2059,9 @@ function handleCodecTxResult(left, value, nextSchema, ctx) {
2053
2059
  }
2054
2060
  return nextSchema._zod.run({ value, issues: left.issues }, ctx);
2055
2061
  }
2062
+ exports.$ZodPreprocess = core.$constructor("$ZodPreprocess", (inst, def) => {
2063
+ exports.$ZodPipe.init(inst, def);
2064
+ });
2056
2065
  exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
2057
2066
  exports.$ZodType.init(inst, def);
2058
2067
  util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
@@ -25,6 +25,11 @@ export interface ParsePayload<T = unknown> {
25
25
  issues: errors.$ZodRawIssue[];
26
26
  /** A way to mark a whole payload as aborted. Used in codecs/pipes. */
27
27
  aborted?: boolean;
28
+ /** @internal Marks a value as a fallback that an outer wrapper (e.g.
29
+ * $ZodOptional) may override with its own interpretation when input was
30
+ * undefined. Set by $ZodCatch when catchValue substitutes and by every
31
+ * $ZodTransform invocation. */
32
+ fallback?: boolean | undefined;
28
33
  }
29
34
  export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
30
35
  export interface $ZodTypeDef {
@@ -1030,6 +1035,19 @@ export interface $ZodCodec<A extends SomeType = $ZodType, B extends SomeType = $
1030
1035
  _zod: $ZodCodecInternals<A, B>;
1031
1036
  }
1032
1037
  export declare const $ZodCodec: core.$constructor<$ZodCodec>;
1038
+ export interface $ZodPreprocessDef<B extends SomeType = $ZodType> extends $ZodPipeDef<$ZodTransform, B> {
1039
+ in: $ZodTransform;
1040
+ out: B;
1041
+ }
1042
+ export interface $ZodPreprocessInternals<B extends SomeType = $ZodType> extends $ZodPipeInternals<$ZodTransform, B> {
1043
+ def: $ZodPreprocessDef<B>;
1044
+ optin: B["_zod"]["optin"];
1045
+ optout: B["_zod"]["optout"];
1046
+ }
1047
+ export interface $ZodPreprocess<B extends SomeType = $ZodType> extends $ZodPipe<$ZodTransform, B> {
1048
+ _zod: $ZodPreprocessInternals<B>;
1049
+ }
1050
+ export declare const $ZodPreprocess: core.$constructor<$ZodPreprocess>;
1033
1051
  export interface $ZodReadonlyDef<T extends SomeType = $ZodType> extends $ZodTypeDef {
1034
1052
  type: "readonly";
1035
1053
  innerType: T;
@@ -25,6 +25,11 @@ export interface ParsePayload<T = unknown> {
25
25
  issues: errors.$ZodRawIssue[];
26
26
  /** A way to mark a whole payload as aborted. Used in codecs/pipes. */
27
27
  aborted?: boolean;
28
+ /** @internal Marks a value as a fallback that an outer wrapper (e.g.
29
+ * $ZodOptional) may override with its own interpretation when input was
30
+ * undefined. Set by $ZodCatch when catchValue substitutes and by every
31
+ * $ZodTransform invocation. */
32
+ fallback?: boolean | undefined;
28
33
  }
29
34
  export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
30
35
  export interface $ZodTypeDef {
@@ -1030,6 +1035,19 @@ export interface $ZodCodec<A extends SomeType = $ZodType, B extends SomeType = $
1030
1035
  _zod: $ZodCodecInternals<A, B>;
1031
1036
  }
1032
1037
  export declare const $ZodCodec: core.$constructor<$ZodCodec>;
1038
+ export interface $ZodPreprocessDef<B extends SomeType = $ZodType> extends $ZodPipeDef<$ZodTransform, B> {
1039
+ in: $ZodTransform;
1040
+ out: B;
1041
+ }
1042
+ export interface $ZodPreprocessInternals<B extends SomeType = $ZodType> extends $ZodPipeInternals<$ZodTransform, B> {
1043
+ def: $ZodPreprocessDef<B>;
1044
+ optin: B["_zod"]["optin"];
1045
+ optout: B["_zod"]["optout"];
1046
+ }
1047
+ export interface $ZodPreprocess<B extends SomeType = $ZodType> extends $ZodPipe<$ZodTransform, B> {
1048
+ _zod: $ZodPreprocessInternals<B>;
1049
+ }
1050
+ export declare const $ZodPreprocess: core.$constructor<$ZodPreprocess>;
1033
1051
  export interface $ZodReadonlyDef<T extends SomeType = $ZodType> extends $ZodTypeDef {
1034
1052
  type: "readonly";
1035
1053
  innerType: T;
@@ -1717,6 +1717,7 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
1717
1717
  });
1718
1718
  export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (inst, def) => {
1719
1719
  $ZodType.init(inst, def);
1720
+ inst._zod.optin = "optional";
1720
1721
  inst._zod.parse = (payload, ctx) => {
1721
1722
  if (ctx.direction === "backward") {
1722
1723
  throw new core.$ZodEncodeError(inst.constructor.name);
@@ -1726,6 +1727,7 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
1726
1727
  const output = _out instanceof Promise ? _out : Promise.resolve(_out);
1727
1728
  return output.then((output) => {
1728
1729
  payload.value = output;
1730
+ payload.fallback = true;
1729
1731
  return payload;
1730
1732
  });
1731
1733
  }
@@ -1733,11 +1735,12 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
1733
1735
  throw new core.$ZodAsyncError();
1734
1736
  }
1735
1737
  payload.value = _out;
1738
+ payload.fallback = true;
1736
1739
  return payload;
1737
1740
  };
1738
1741
  });
1739
1742
  function handleOptionalResult(result, input) {
1740
- if (result.issues.length && input === undefined) {
1743
+ if (input === undefined && (result.issues.length || result.fallback)) {
1741
1744
  return { issues: [], value: undefined };
1742
1745
  }
1743
1746
  return result;
@@ -1755,10 +1758,11 @@ export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (ins
1755
1758
  });
1756
1759
  inst._zod.parse = (payload, ctx) => {
1757
1760
  if (def.innerType._zod.optin === "optional") {
1761
+ const input = payload.value;
1758
1762
  const result = def.innerType._zod.run(payload, ctx);
1759
1763
  if (result instanceof Promise)
1760
- return result.then((r) => handleOptionalResult(r, payload.value));
1761
- return handleOptionalResult(result, payload.value);
1764
+ return result.then((r) => handleOptionalResult(r, input));
1765
+ return handleOptionalResult(result, input);
1762
1766
  }
1763
1767
  if (payload.value === undefined) {
1764
1768
  return payload;
@@ -1885,7 +1889,7 @@ export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst,
1885
1889
  });
1886
1890
  export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
1887
1891
  $ZodType.init(inst, def);
1888
- util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1892
+ inst._zod.optin = "optional";
1889
1893
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1890
1894
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1891
1895
  inst._zod.parse = (payload, ctx) => {
@@ -1906,6 +1910,7 @@ export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def
1906
1910
  input: payload.value,
1907
1911
  });
1908
1912
  payload.issues = [];
1913
+ payload.fallback = true;
1909
1914
  }
1910
1915
  return payload;
1911
1916
  });
@@ -1920,6 +1925,7 @@ export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def
1920
1925
  input: payload.value,
1921
1926
  });
1922
1927
  payload.issues = [];
1928
+ payload.fallback = true;
1923
1929
  }
1924
1930
  return payload;
1925
1931
  };
@@ -1966,7 +1972,7 @@ function handlePipeResult(left, next, ctx) {
1966
1972
  left.aborted = true;
1967
1973
  return left;
1968
1974
  }
1969
- return next._zod.run({ value: left.value, issues: left.issues }, ctx);
1975
+ return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
1970
1976
  }
1971
1977
  export const $ZodCodec = /*@__PURE__*/ core.$constructor("$ZodCodec", (inst, def) => {
1972
1978
  $ZodType.init(inst, def);
@@ -2022,6 +2028,9 @@ function handleCodecTxResult(left, value, nextSchema, ctx) {
2022
2028
  }
2023
2029
  return nextSchema._zod.run({ value, issues: left.issues }, ctx);
2024
2030
  }
2031
+ export const $ZodPreprocess = /*@__PURE__*/ core.$constructor("$ZodPreprocess", (inst, def) => {
2032
+ $ZodPipe.init(inst, def);
2033
+ });
2025
2034
  export const $ZodReadonly = /*@__PURE__*/ core.$constructor("$ZodReadonly", (inst, def) => {
2026
2035
  $ZodType.init(inst, def);
2027
2036
  util.defineLazy(inst._zod, "propValues", () => def.innerType._zod.propValues);
@@ -407,6 +407,8 @@ function isTransforming(_schema, _ctx) {
407
407
  return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
408
408
  }
409
409
  if (def.type === "pipe") {
410
+ if (_schema._zod.traits.has("$ZodCodec"))
411
+ return true;
410
412
  return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
411
413
  }
412
414
  if (def.type === "object") {
@@ -400,6 +400,8 @@ function isTransforming(_schema, _ctx) {
400
400
  return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
401
401
  }
402
402
  if (def.type === "pipe") {
403
+ if (_schema._zod.traits.has("$ZodCodec"))
404
+ return true;
403
405
  return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
404
406
  }
405
407
  if (def.type === "object") {
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 4,
7
- patch: 1,
7
+ patch: 3,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 4,
4
- patch: 1,
4
+ patch: 3,
5
5
  };
@@ -260,7 +260,7 @@ export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] =
260
260
  _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
261
261
  }
262
262
  export declare const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUnion>;
263
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
263
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
264
264
  export interface ZodMiniIntersection<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodIntersectionInternals<A, B>> {
265
265
  }
266
266
  export declare const ZodMiniIntersection: core.$constructor<ZodMiniIntersection>;
@@ -260,7 +260,7 @@ export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] =
260
260
  _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
261
261
  }
262
262
  export declare const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUnion>;
263
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
263
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable<Disc>, ...core.$ZodTypeDiscriminable<Disc>[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
264
264
  export interface ZodMiniIntersection<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodIntersectionInternals<A, B>> {
265
265
  }
266
266
  export declare const ZodMiniIntersection: core.$constructor<ZodMiniIntersection>;