zod 4.1.0-canary.20250821T014902 → 4.1.0-canary.20250823T064644

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 (106) hide show
  1. package/package.json +1 -1
  2. package/src/v3/tests/object.test.ts +2 -2
  3. package/src/v4/classic/external.ts +0 -1
  4. package/src/v4/classic/parse.ts +49 -0
  5. package/src/v4/classic/schemas.ts +145 -7
  6. package/src/v4/classic/tests/array.test.ts +6 -6
  7. package/src/v4/classic/tests/catch.test.ts +25 -0
  8. package/src/v4/classic/tests/codec-examples.test.ts +538 -0
  9. package/src/v4/classic/tests/codec.test.ts +532 -0
  10. package/src/v4/classic/tests/continuability.test.ts +1 -1
  11. package/src/v4/classic/tests/default.test.ts +32 -0
  12. package/src/v4/classic/tests/firstparty.test.ts +4 -0
  13. package/src/v4/classic/tests/function.test.ts +31 -31
  14. package/src/v4/classic/tests/hash.test.ts +68 -0
  15. package/src/v4/classic/tests/nonoptional.test.ts +15 -0
  16. package/src/v4/classic/tests/object.test.ts +33 -2
  17. package/src/v4/classic/tests/pipe.test.ts +25 -5
  18. package/src/v4/classic/tests/prefault.test.ts +25 -0
  19. package/src/v4/classic/tests/preprocess.test.ts +1 -6
  20. package/src/v4/classic/tests/refine.test.ts +78 -5
  21. package/src/v4/classic/tests/set.test.ts +1 -1
  22. package/src/v4/classic/tests/string-formats.test.ts +16 -0
  23. package/src/v4/classic/tests/string.test.ts +82 -1
  24. package/src/v4/classic/tests/stringbool.test.ts +40 -0
  25. package/src/v4/classic/tests/template-literal.test.ts +1 -1
  26. package/src/v4/classic/tests/to-json-schema.test.ts +21 -2
  27. package/src/v4/classic/tests/transform.test.ts +7 -0
  28. package/src/v4/classic/tests/union.test.ts +1 -1
  29. package/src/v4/core/api.ts +25 -35
  30. package/src/v4/core/core.ts +7 -26
  31. package/src/v4/core/index.ts +0 -1
  32. package/src/v4/core/json-schema.ts +1 -0
  33. package/src/v4/core/parse.ts +101 -0
  34. package/src/v4/core/regexes.ts +40 -1
  35. package/src/v4/core/schemas.ts +521 -129
  36. package/src/v4/core/to-json-schema.ts +43 -8
  37. package/src/v4/core/util.ts +73 -0
  38. package/src/v4/mini/external.ts +0 -1
  39. package/src/v4/mini/parse.ts +14 -1
  40. package/src/v4/mini/schemas.ts +153 -12
  41. package/src/v4/mini/tests/codec.test.ts +499 -0
  42. package/src/v4/mini/tests/object.test.ts +9 -0
  43. package/src/v4/mini/tests/string.test.ts +16 -0
  44. package/v4/classic/external.cjs +1 -2
  45. package/v4/classic/external.d.cts +1 -1
  46. package/v4/classic/external.d.ts +1 -1
  47. package/v4/classic/external.js +1 -1
  48. package/v4/classic/parse.cjs +10 -1
  49. package/v4/classic/parse.d.cts +8 -0
  50. package/v4/classic/parse.d.ts +8 -0
  51. package/v4/classic/parse.js +9 -0
  52. package/v4/classic/schemas.cjs +59 -4
  53. package/v4/classic/schemas.d.cts +48 -2
  54. package/v4/classic/schemas.d.ts +48 -2
  55. package/v4/classic/schemas.js +51 -3
  56. package/v4/core/api.cjs +19 -24
  57. package/v4/core/api.d.cts +3 -4
  58. package/v4/core/api.d.ts +3 -4
  59. package/v4/core/api.js +19 -24
  60. package/v4/core/core.cjs +8 -1
  61. package/v4/core/core.d.cts +3 -0
  62. package/v4/core/core.d.ts +3 -0
  63. package/v4/core/core.js +6 -0
  64. package/v4/core/index.cjs +0 -1
  65. package/v4/core/index.d.cts +0 -1
  66. package/v4/core/index.d.ts +0 -1
  67. package/v4/core/index.js +0 -1
  68. package/v4/core/json-schema.d.cts +1 -0
  69. package/v4/core/json-schema.d.ts +1 -0
  70. package/v4/core/parse.cjs +45 -1
  71. package/v4/core/parse.d.cts +24 -0
  72. package/v4/core/parse.d.ts +24 -0
  73. package/v4/core/parse.js +36 -0
  74. package/v4/core/regexes.cjs +34 -2
  75. package/v4/core/regexes.d.cts +16 -0
  76. package/v4/core/regexes.d.ts +16 -0
  77. package/v4/core/regexes.js +32 -1
  78. package/v4/core/schemas.cjs +309 -77
  79. package/v4/core/schemas.d.cts +61 -3
  80. package/v4/core/schemas.d.ts +61 -3
  81. package/v4/core/schemas.js +308 -76
  82. package/v4/core/to-json-schema.cjs +42 -5
  83. package/v4/core/to-json-schema.d.cts +4 -3
  84. package/v4/core/to-json-schema.d.ts +4 -3
  85. package/v4/core/to-json-schema.js +42 -5
  86. package/v4/core/util.cjs +69 -0
  87. package/v4/core/util.d.cts +10 -0
  88. package/v4/core/util.d.ts +10 -0
  89. package/v4/core/util.js +62 -0
  90. package/v4/mini/external.cjs +1 -2
  91. package/v4/mini/external.d.cts +1 -1
  92. package/v4/mini/external.d.ts +1 -1
  93. package/v4/mini/external.js +1 -1
  94. package/v4/mini/parse.cjs +9 -1
  95. package/v4/mini/parse.d.cts +1 -1
  96. package/v4/mini/parse.d.ts +1 -1
  97. package/v4/mini/parse.js +1 -1
  98. package/v4/mini/schemas.cjs +58 -3
  99. package/v4/mini/schemas.d.cts +49 -1
  100. package/v4/mini/schemas.d.ts +49 -1
  101. package/v4/mini/schemas.js +49 -2
  102. package/src/v4/core/function.ts +0 -176
  103. package/v4/core/function.cjs +0 -102
  104. package/v4/core/function.d.cts +0 -52
  105. package/v4/core/function.d.ts +0 -52
  106. package/v4/core/function.js +0 -75
@@ -15,14 +15,18 @@ export interface ParseContext<T extends errors.$ZodIssueBase = never> {
15
15
  /** @internal */
16
16
  export interface ParseContextInternal<T extends errors.$ZodIssueBase = never> extends ParseContext<T> {
17
17
  readonly async?: boolean | undefined;
18
+ readonly direction?: "forward" | "backward";
19
+ readonly skipChecks?: boolean;
18
20
  }
19
21
  export interface ParsePayload<T = unknown> {
20
22
  value: T;
21
23
  issues: errors.$ZodRawIssue[];
24
+ /** A may to mark a whole payload as aborted. Used in codecs/pipes. */
25
+ aborted?: boolean;
22
26
  }
23
27
  export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
24
28
  export interface $ZodTypeDef {
25
- type: "string" | "number" | "int" | "boolean" | "bigint" | "symbol" | "null" | "undefined" | "void" | "never" | "any" | "unknown" | "date" | "object" | "record" | "file" | "array" | "tuple" | "union" | "intersection" | "map" | "set" | "enum" | "literal" | "nullable" | "optional" | "nonoptional" | "success" | "transform" | "default" | "prefault" | "catch" | "nan" | "pipe" | "readonly" | "template_literal" | "promise" | "lazy" | "custom";
29
+ type: "string" | "number" | "int" | "boolean" | "bigint" | "symbol" | "null" | "undefined" | "void" | "never" | "any" | "unknown" | "date" | "object" | "record" | "file" | "array" | "tuple" | "union" | "intersection" | "map" | "set" | "enum" | "literal" | "nullable" | "optional" | "nonoptional" | "success" | "transform" | "default" | "prefault" | "catch" | "nan" | "pipe" | "readonly" | "template_literal" | "promise" | "lazy" | "function" | "custom";
26
30
  error?: errors.$ZodErrorMap<never> | undefined;
27
31
  checks?: checks.$ZodCheck<never>[];
28
32
  }
@@ -597,6 +601,7 @@ out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>, out Params extends
597
601
  "~standard": $ZodStandardSchema<this>;
598
602
  }
599
603
  export declare const $ZodObject: core.$constructor<$ZodObject>;
604
+ export declare const $ZodObjectJIT: core.$constructor<$ZodObject>;
600
605
  export type $InferUnionOutput<T extends SomeType> = T extends any ? core.output<T> : never;
601
606
  export type $InferUnionInput<T extends SomeType> = T extends any ? core.input<T> : never;
602
607
  export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodTypeDef {
@@ -939,6 +944,10 @@ export interface $ZodPipeDef<A extends SomeType = $ZodType, B extends SomeType =
939
944
  type: "pipe";
940
945
  in: A;
941
946
  out: B;
947
+ /** Only defined inside $ZodCodec instances. */
948
+ transform?: (value: core.output<A>, payload: ParsePayload<core.output<A>>) => core.input<B>;
949
+ /** Only defined inside $ZodCodec instances. */
950
+ reverseTransform?: (value: core.input<B>, payload: ParsePayload<core.input<B>>) => core.output<A>;
942
951
  }
943
952
  export interface $ZodPipeInternals<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<B>, core.input<A>> {
944
953
  def: $ZodPipeDef<A, B>;
@@ -952,6 +961,22 @@ export interface $ZodPipe<A extends SomeType = $ZodType, B extends SomeType = $Z
952
961
  _zod: $ZodPipeInternals<A, B>;
953
962
  }
954
963
  export declare const $ZodPipe: core.$constructor<$ZodPipe>;
964
+ export interface $ZodCodecDef<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodPipeDef<A, B> {
965
+ transform: (value: core.output<A>, payload: ParsePayload<core.output<A>>) => core.input<B>;
966
+ reverseTransform: (value: core.input<B>, payload: ParsePayload<core.input<B>>) => core.output<A>;
967
+ }
968
+ export interface $ZodCodecInternals<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<B>, core.input<A>> {
969
+ def: $ZodCodecDef<A, B>;
970
+ isst: never;
971
+ values: A["_zod"]["values"];
972
+ optin: A["_zod"]["optin"];
973
+ optout: B["_zod"]["optout"];
974
+ propValues: A["_zod"]["propValues"];
975
+ }
976
+ export interface $ZodCodec<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodType {
977
+ _zod: $ZodCodecInternals<A, B>;
978
+ }
979
+ export declare const $ZodCodec: core.$constructor<$ZodCodec>;
955
980
  export interface $ZodReadonlyDef<T extends SomeType = $ZodType> extends $ZodTypeDef {
956
981
  type: "readonly";
957
982
  innerType: T;
@@ -1001,6 +1026,39 @@ export type ConvertPartsToStringTuple<Parts extends $ZodTemplateLiteralPart[]> =
1001
1026
  export type ToTemplateLiteral<Parts extends $ZodTemplateLiteralPart[]> = ConcatenateTupleOfStrings<ConvertPartsToStringTuple<Parts>>;
1002
1027
  export type $PartsToTemplateLiteral<Parts extends $ZodTemplateLiteralPart[]> = [] extends Parts ? `` : Parts extends [...infer Rest, infer Last extends $ZodTemplateLiteralPart] ? Rest extends $ZodTemplateLiteralPart[] ? AppendToTemplateLiteral<$PartsToTemplateLiteral<Rest>, Last> : never : never;
1003
1028
  export declare const $ZodTemplateLiteral: core.$constructor<$ZodTemplateLiteral>;
1029
+ export type $ZodFunctionArgs = $ZodType<unknown[], unknown[]>;
1030
+ export type $ZodFunctionIn = $ZodFunctionArgs;
1031
+ export type $ZodFunctionOut = $ZodType;
1032
+ export type $InferInnerFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => core.input<Returns>;
1033
+ export type $InferInnerFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>) => util.MaybeAsync<core.input<Returns>>;
1034
+ export type $InferOuterFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => core.output<Returns>;
1035
+ export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>) => util.MaybeAsync<core.output<Returns>>;
1036
+ export interface $ZodFunctionDef<In extends $ZodFunctionIn = $ZodFunctionIn, Out extends $ZodFunctionOut = $ZodFunctionOut> extends $ZodTypeDef {
1037
+ type: "function";
1038
+ input: In;
1039
+ output: Out;
1040
+ }
1041
+ export interface $ZodFunctionInternals<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> extends $ZodTypeInternals<$InferOuterFunctionType<Args, Returns>, $InferInnerFunctionType<Args, Returns>> {
1042
+ def: $ZodFunctionDef<Args, Returns>;
1043
+ isst: errors.$ZodIssueInvalidType;
1044
+ }
1045
+ export interface $ZodFunction<Args extends $ZodFunctionIn = $ZodFunctionIn, Returns extends $ZodFunctionOut = $ZodFunctionOut> extends $ZodType<any, any, $ZodFunctionInternals<Args, Returns>> {
1046
+ /** @deprecated */
1047
+ _def: $ZodFunctionDef<Args, Returns>;
1048
+ _input: $InferInnerFunctionType<Args, Returns>;
1049
+ _output: $InferOuterFunctionType<Args, Returns>;
1050
+ implement<F extends $InferInnerFunctionType<Args, Returns>>(func: F): (...args: Parameters<this["_output"]>) => ReturnType<F> extends ReturnType<this["_output"]> ? ReturnType<F> : ReturnType<this["_output"]>;
1051
+ implementAsync<F extends $InferInnerFunctionTypeAsync<Args, Returns>>(func: F): F extends $InferOuterFunctionTypeAsync<Args, Returns> ? F : $InferOuterFunctionTypeAsync<Args, Returns>;
1052
+ input<const Items extends util.TupleItems, const Rest extends $ZodFunctionOut = $ZodFunctionOut>(args: Items, rest?: Rest): $ZodFunction<$ZodTuple<Items, Rest>, Returns>;
1053
+ input<NewArgs extends $ZodFunctionIn>(args: NewArgs): $ZodFunction<NewArgs, Returns>;
1054
+ input(...args: any[]): $ZodFunction<any, Returns>;
1055
+ output<NewReturns extends $ZodType>(output: NewReturns): $ZodFunction<Args, NewReturns>;
1056
+ }
1057
+ export interface $ZodFunctionParams<I extends $ZodFunctionIn, O extends $ZodType> {
1058
+ input?: I;
1059
+ output?: O;
1060
+ }
1061
+ export declare const $ZodFunction: core.$constructor<$ZodFunction>;
1004
1062
  export interface $ZodPromiseDef<T extends SomeType = $ZodType> extends $ZodTypeDef {
1005
1063
  type: "promise";
1006
1064
  innerType: T;
@@ -1051,5 +1109,5 @@ export interface $ZodCustom<O = unknown, I = unknown> extends $ZodType {
1051
1109
  _zod: $ZodCustomInternals<O, I>;
1052
1110
  }
1053
1111
  export declare const $ZodCustom: core.$constructor<$ZodCustom>;
1054
- export type $ZodTypes = $ZodString | $ZodNumber | $ZodBigInt | $ZodBoolean | $ZodDate | $ZodSymbol | $ZodUndefined | $ZodNullable | $ZodNull | $ZodAny | $ZodUnknown | $ZodNever | $ZodVoid | $ZodArray | $ZodObject | $ZodUnion | $ZodIntersection | $ZodTuple | $ZodRecord | $ZodMap | $ZodSet | $ZodLiteral | $ZodEnum | $ZodPromise | $ZodLazy | $ZodOptional | $ZodDefault | $ZodPrefault | $ZodTemplateLiteral | $ZodCustom | $ZodTransform | $ZodNonOptional | $ZodReadonly | $ZodNaN | $ZodPipe | $ZodSuccess | $ZodCatch | $ZodFile;
1055
- export type $ZodStringFormatTypes = $ZodGUID | $ZodUUID | $ZodEmail | $ZodURL | $ZodEmoji | $ZodNanoID | $ZodCUID | $ZodCUID2 | $ZodULID | $ZodXID | $ZodKSUID | $ZodISODateTime | $ZodISODate | $ZodISOTime | $ZodISODuration | $ZodIPv4 | $ZodIPv6 | $ZodCIDRv4 | $ZodCIDRv6 | $ZodBase64 | $ZodBase64URL | $ZodE164 | $ZodJWT;
1112
+ export type $ZodTypes = $ZodString | $ZodNumber | $ZodBigInt | $ZodBoolean | $ZodDate | $ZodSymbol | $ZodUndefined | $ZodNullable | $ZodNull | $ZodAny | $ZodUnknown | $ZodNever | $ZodVoid | $ZodArray | $ZodObject | $ZodUnion | $ZodIntersection | $ZodTuple | $ZodRecord | $ZodMap | $ZodSet | $ZodLiteral | $ZodEnum | $ZodFunction | $ZodPromise | $ZodLazy | $ZodOptional | $ZodDefault | $ZodPrefault | $ZodTemplateLiteral | $ZodCustom | $ZodTransform | $ZodNonOptional | $ZodReadonly | $ZodNaN | $ZodPipe | $ZodSuccess | $ZodCatch | $ZodFile;
1113
+ export type $ZodStringFormatTypes = $ZodGUID | $ZodUUID | $ZodEmail | $ZodURL | $ZodEmoji | $ZodNanoID | $ZodCUID | $ZodCUID2 | $ZodULID | $ZodXID | $ZodKSUID | $ZodISODateTime | $ZodISODate | $ZodISOTime | $ZodISODuration | $ZodIPv4 | $ZodIPv6 | $ZodCIDRv4 | $ZodCIDRv6 | $ZodBase64 | $ZodBase64URL | $ZodE164 | $ZodJWT | $ZodCustomStringFormat<"hex"> | $ZodCustomStringFormat<util.HashFormat> | $ZodCustomStringFormat<"hostname">;
@@ -1,7 +1,7 @@
1
1
  import * as checks from "./checks.js";
2
2
  import * as core from "./core.js";
3
3
  import { Doc } from "./doc.js";
4
- import { safeParse, safeParseAsync } from "./parse.js";
4
+ import { parse, parseAsync, safeParse, safeParseAsync } from "./parse.js";
5
5
  import * as regexes from "./regexes.js";
6
6
  import * as util from "./util.js";
7
7
  import { version } from "./versions.js";
@@ -16,7 +16,6 @@ export const $ZodType = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def)
16
16
  if (inst._zod.traits.has("$ZodCheck")) {
17
17
  checks.unshift(inst);
18
18
  }
19
- //
20
19
  for (const ch of checks) {
21
20
  for (const fn of ch._zod.onattach) {
22
21
  fn(inst);
@@ -73,7 +72,47 @@ export const $ZodType = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def)
73
72
  }
74
73
  return payload;
75
74
  };
75
+ // const handleChecksResult = (
76
+ // checkResult: ParsePayload,
77
+ // originalResult: ParsePayload,
78
+ // ctx: ParseContextInternal
79
+ // ): util.MaybeAsync<ParsePayload> => {
80
+ // // if the checks mutated the value && there are no issues, re-parse the result
81
+ // if (checkResult.value !== originalResult.value && !checkResult.issues.length)
82
+ // return inst._zod.parse(checkResult, ctx);
83
+ // return originalResult;
84
+ // };
85
+ const handleCanaryResult = (canary, payload, ctx) => {
86
+ // abort if the canary is aborted
87
+ if (util.aborted(canary)) {
88
+ canary.aborted = true;
89
+ return canary;
90
+ }
91
+ // run checks first, then
92
+ const checkResult = runChecks(payload, checks, ctx);
93
+ if (checkResult instanceof Promise) {
94
+ if (ctx.async === false)
95
+ throw new core.$ZodAsyncError();
96
+ return checkResult.then((checkResult) => inst._zod.parse(checkResult, ctx));
97
+ }
98
+ return inst._zod.parse(checkResult, ctx);
99
+ };
76
100
  inst._zod.run = (payload, ctx) => {
101
+ if (ctx.skipChecks) {
102
+ return inst._zod.parse(payload, ctx);
103
+ }
104
+ if (ctx.direction === "backward") {
105
+ // run canary
106
+ // initial pass (no checks)
107
+ const canary = inst._zod.parse({ value: payload.value, issues: [] }, { ...ctx, skipChecks: true });
108
+ if (canary instanceof Promise) {
109
+ return canary.then((canary) => {
110
+ return handleCanaryResult(canary, payload, ctx);
111
+ });
112
+ }
113
+ return handleCanaryResult(canary, payload, ctx);
114
+ }
115
+ // forward
77
116
  const result = inst._zod.parse(payload, ctx);
78
117
  if (result instanceof Promise) {
79
118
  if (ctx.async === false)
@@ -673,25 +712,61 @@ function handlePropertyResult(result, final, key, input) {
673
712
  final.value[key] = result.value;
674
713
  }
675
714
  }
715
+ function normalizeDef(def) {
716
+ const keys = Object.keys(def.shape);
717
+ for (const k of keys) {
718
+ if (!def.shape[k]._zod.traits.has("$ZodType")) {
719
+ throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
720
+ }
721
+ }
722
+ const okeys = util.optionalKeys(def.shape);
723
+ return {
724
+ ...def,
725
+ keys,
726
+ keySet: new Set(keys),
727
+ numKeys: keys.length,
728
+ optionalKeys: new Set(okeys),
729
+ };
730
+ }
731
+ function handleCatchall(proms, input, payload, ctx, def, inst) {
732
+ const unrecognized = [];
733
+ // iterate over input keys
734
+ const keySet = def.keySet;
735
+ const _catchall = def.catchall._zod;
736
+ const t = _catchall.def.type;
737
+ for (const key of Object.keys(input)) {
738
+ if (keySet.has(key))
739
+ continue;
740
+ if (t === "never") {
741
+ unrecognized.push(key);
742
+ continue;
743
+ }
744
+ const r = _catchall.run({ value: input[key], issues: [] }, ctx);
745
+ if (r instanceof Promise) {
746
+ proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
747
+ }
748
+ else {
749
+ handlePropertyResult(r, payload, key, input);
750
+ }
751
+ }
752
+ if (unrecognized.length) {
753
+ payload.issues.push({
754
+ code: "unrecognized_keys",
755
+ keys: unrecognized,
756
+ input,
757
+ inst,
758
+ });
759
+ }
760
+ if (!proms.length)
761
+ return payload;
762
+ return Promise.all(proms).then(() => {
763
+ return payload;
764
+ });
765
+ }
676
766
  export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, def) => {
677
767
  // requires cast because technically $ZodObject doesn't extend
678
768
  $ZodType.init(inst, def);
679
- const _normalized = util.cached(() => {
680
- const keys = Object.keys(def.shape);
681
- for (const k of keys) {
682
- if (!def.shape[k]._zod.traits.has("$ZodType")) {
683
- throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
684
- }
685
- }
686
- const okeys = util.optionalKeys(def.shape);
687
- return {
688
- shape: def.shape,
689
- keys,
690
- keySet: new Set(keys),
691
- numKeys: keys.length,
692
- optionalKeys: new Set(okeys),
693
- };
694
- });
769
+ const _normalized = util.cached(() => normalizeDef(def));
695
770
  util.defineLazy(inst._zod, "propValues", () => {
696
771
  const shape = def.shape;
697
772
  const propValues = {};
@@ -705,6 +780,45 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
705
780
  }
706
781
  return propValues;
707
782
  });
783
+ const isObject = util.isObject;
784
+ const catchall = def.catchall;
785
+ let value;
786
+ inst._zod.parse = (payload, ctx) => {
787
+ value ?? (value = _normalized.value);
788
+ const input = payload.value;
789
+ if (!isObject(input)) {
790
+ payload.issues.push({
791
+ expected: "object",
792
+ code: "invalid_type",
793
+ input,
794
+ inst,
795
+ });
796
+ return payload;
797
+ }
798
+ payload.value = {};
799
+ const proms = [];
800
+ const shape = value.shape;
801
+ for (const key of value.keys) {
802
+ const el = shape[key];
803
+ const r = el._zod.run({ value: input[key], issues: [] }, ctx);
804
+ if (r instanceof Promise) {
805
+ proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
806
+ }
807
+ else {
808
+ handlePropertyResult(r, payload, key, input);
809
+ }
810
+ }
811
+ if (!catchall) {
812
+ return proms.length ? Promise.all(proms).then(() => payload) : payload;
813
+ }
814
+ return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
815
+ };
816
+ });
817
+ export const $ZodObjectJIT = /*@__PURE__*/ core.$constructor("$ZodObjectJIT", (inst, def) => {
818
+ // requires cast because technically $ZodObject doesn't extend
819
+ $ZodObject.init(inst, def);
820
+ const superParse = inst._zod.parse;
821
+ const _normalized = util.cached(() => normalizeDef(def));
708
822
  const generateFastpass = (shape) => {
709
823
  const doc = new Doc(["shape", "payload", "ctx"]);
710
824
  const normalized = _normalized.value;
@@ -765,63 +879,16 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
765
879
  });
766
880
  return payload;
767
881
  }
768
- const proms = [];
769
882
  if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
770
883
  // always synchronous
771
884
  if (!fastpass)
772
885
  fastpass = generateFastpass(def.shape);
773
886
  payload = fastpass(payload, ctx);
887
+ if (!catchall)
888
+ return payload;
889
+ return handleCatchall([], input, payload, ctx, value, inst);
774
890
  }
775
- else {
776
- payload.value = {};
777
- const shape = value.shape;
778
- for (const key of value.keys) {
779
- const el = shape[key];
780
- const r = el._zod.run({ value: input[key], issues: [] }, ctx);
781
- if (r instanceof Promise) {
782
- proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
783
- }
784
- else {
785
- handlePropertyResult(r, payload, key, input);
786
- }
787
- }
788
- }
789
- if (!catchall) {
790
- return proms.length ? Promise.all(proms).then(() => payload) : payload;
791
- }
792
- const unrecognized = [];
793
- // iterate over input keys
794
- const keySet = value.keySet;
795
- const _catchall = catchall._zod;
796
- const t = _catchall.def.type;
797
- for (const key of Object.keys(input)) {
798
- if (keySet.has(key))
799
- continue;
800
- if (t === "never") {
801
- unrecognized.push(key);
802
- continue;
803
- }
804
- const r = _catchall.run({ value: input[key], issues: [] }, ctx);
805
- if (r instanceof Promise) {
806
- proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
807
- }
808
- else {
809
- handlePropertyResult(r, payload, key, input);
810
- }
811
- }
812
- if (unrecognized.length) {
813
- payload.issues.push({
814
- code: "unrecognized_keys",
815
- keys: unrecognized,
816
- input,
817
- inst,
818
- });
819
- }
820
- if (!proms.length)
821
- return payload;
822
- return Promise.all(proms).then(() => {
823
- return payload;
824
- });
891
+ return superParse(payload, ctx);
825
892
  };
826
893
  });
827
894
  function handleUnionResults(results, final, inst, ctx) {
@@ -1368,9 +1435,12 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
1368
1435
  });
1369
1436
  export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (inst, def) => {
1370
1437
  $ZodType.init(inst, def);
1371
- inst._zod.parse = (payload, _ctx) => {
1438
+ inst._zod.parse = (payload, ctx) => {
1439
+ if (ctx.direction === "backward") {
1440
+ throw new core.$ZodEncodeError(inst.constructor.name);
1441
+ }
1372
1442
  const _out = def.transform(payload.value, payload);
1373
- if (_ctx.async) {
1443
+ if (ctx.async) {
1374
1444
  const output = _out instanceof Promise ? _out : Promise.resolve(_out);
1375
1445
  return output.then((output) => {
1376
1446
  payload.value = output;
@@ -1426,6 +1496,7 @@ export const $ZodNullable = /*@__PURE__*/ core.$constructor("$ZodNullable", (ins
1426
1496
  return def.innerType._zod.values ? new Set([...def.innerType._zod.values, null]) : undefined;
1427
1497
  });
1428
1498
  inst._zod.parse = (payload, ctx) => {
1499
+ // Forward direction (decode): allow null to pass through
1429
1500
  if (payload.value === null)
1430
1501
  return payload;
1431
1502
  return def.innerType._zod.run(payload, ctx);
@@ -1437,13 +1508,18 @@ export const $ZodDefault = /*@__PURE__*/ core.$constructor("$ZodDefault", (inst,
1437
1508
  inst._zod.optin = "optional";
1438
1509
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1439
1510
  inst._zod.parse = (payload, ctx) => {
1511
+ if (ctx.direction === "backward") {
1512
+ return def.innerType._zod.run(payload, ctx);
1513
+ }
1514
+ // Forward direction (decode): apply defaults for undefined input
1440
1515
  if (payload.value === undefined) {
1441
1516
  payload.value = def.defaultValue;
1442
1517
  /**
1443
- * $ZodDefault always returns the default value immediately.
1518
+ * $ZodDefault returns the default value immediately in forward direction.
1444
1519
  * It doesn't pass the default value into the validator ("prefault"). There's no reason to pass the default value through validation. The validity of the default is enforced by TypeScript statically. Otherwise, it's the responsibility of the user to ensure the default is valid. In the case of pipes with divergent in/out types, you can specify the default on the `in` schema of your ZodPipe to set a "prefault" for the pipe. */
1445
1520
  return payload;
1446
1521
  }
1522
+ // Forward direction: continue with default handling
1447
1523
  const result = def.innerType._zod.run(payload, ctx);
1448
1524
  if (result instanceof Promise) {
1449
1525
  return result.then((result) => handleDefaultResult(result, def));
@@ -1462,6 +1538,10 @@ export const $ZodPrefault = /*@__PURE__*/ core.$constructor("$ZodPrefault", (ins
1462
1538
  inst._zod.optin = "optional";
1463
1539
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1464
1540
  inst._zod.parse = (payload, ctx) => {
1541
+ if (ctx.direction === "backward") {
1542
+ return def.innerType._zod.run(payload, ctx);
1543
+ }
1544
+ // Forward direction (decode): apply prefault for undefined input
1465
1545
  if (payload.value === undefined) {
1466
1546
  payload.value = def.defaultValue;
1467
1547
  }
@@ -1496,6 +1576,9 @@ function handleNonOptionalResult(payload, inst) {
1496
1576
  export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst, def) => {
1497
1577
  $ZodType.init(inst, def);
1498
1578
  inst._zod.parse = (payload, ctx) => {
1579
+ if (ctx.direction === "backward") {
1580
+ throw new core.$ZodEncodeError("ZodSuccess");
1581
+ }
1499
1582
  const result = def.innerType._zod.run(payload, ctx);
1500
1583
  if (result instanceof Promise) {
1501
1584
  return result.then((result) => {
@@ -1513,6 +1596,10 @@ export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def
1513
1596
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1514
1597
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1515
1598
  inst._zod.parse = (payload, ctx) => {
1599
+ if (ctx.direction === "backward") {
1600
+ return def.innerType._zod.run(payload, ctx);
1601
+ }
1602
+ // Forward direction (decode): apply catch logic
1516
1603
  const result = def.innerType._zod.run(payload, ctx);
1517
1604
  if (result instanceof Promise) {
1518
1605
  return result.then((result) => {
@@ -1566,18 +1653,81 @@ export const $ZodPipe = /*@__PURE__*/ core.$constructor("$ZodPipe", (inst, def)
1566
1653
  util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
1567
1654
  util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
1568
1655
  inst._zod.parse = (payload, ctx) => {
1656
+ if (ctx.direction === "backward") {
1657
+ const right = def.out._zod.run(payload, ctx);
1658
+ if (right instanceof Promise) {
1659
+ return right.then((right) => handlePipeResult(right, def.in, ctx));
1660
+ }
1661
+ return handlePipeResult(right, def.in, ctx);
1662
+ }
1569
1663
  const left = def.in._zod.run(payload, ctx);
1570
1664
  if (left instanceof Promise) {
1571
- return left.then((left) => handlePipeResult(left, def, ctx));
1665
+ return left.then((left) => handlePipeResult(left, def.out, ctx));
1572
1666
  }
1573
- return handlePipeResult(left, def, ctx);
1667
+ return handlePipeResult(left, def.out, ctx);
1574
1668
  };
1575
1669
  });
1576
- function handlePipeResult(left, def, ctx) {
1670
+ function handlePipeResult(left, next, ctx) {
1577
1671
  if (left.issues.length) {
1672
+ // prevent further checks
1673
+ left.aborted = true;
1578
1674
  return left;
1579
1675
  }
1580
- return def.out._zod.run({ value: left.value, issues: left.issues }, ctx);
1676
+ return next._zod.run({ value: left.value, issues: left.issues }, ctx);
1677
+ }
1678
+ export const $ZodCodec = /*@__PURE__*/ core.$constructor("$ZodCodec", (inst, def) => {
1679
+ $ZodType.init(inst, def);
1680
+ util.defineLazy(inst._zod, "values", () => def.in._zod.values);
1681
+ util.defineLazy(inst._zod, "optin", () => def.in._zod.optin);
1682
+ util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
1683
+ util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
1684
+ inst._zod.parse = (payload, ctx) => {
1685
+ const direction = ctx.direction || "forward";
1686
+ if (direction === "forward") {
1687
+ const left = def.in._zod.run(payload, ctx);
1688
+ if (left instanceof Promise) {
1689
+ return left.then((left) => handleCodecAResult(left, def, ctx));
1690
+ }
1691
+ return handleCodecAResult(left, def, ctx);
1692
+ }
1693
+ else {
1694
+ const right = def.out._zod.run(payload, ctx);
1695
+ if (right instanceof Promise) {
1696
+ return right.then((right) => handleCodecAResult(right, def, ctx));
1697
+ }
1698
+ return handleCodecAResult(right, def, ctx);
1699
+ }
1700
+ };
1701
+ });
1702
+ function handleCodecAResult(result, def, ctx) {
1703
+ if (result.issues.length) {
1704
+ // prevent further checks
1705
+ result.aborted = true;
1706
+ return result;
1707
+ }
1708
+ const direction = ctx.direction || "forward";
1709
+ if (direction === "forward") {
1710
+ const transformed = def.transform(result.value, result);
1711
+ if (transformed instanceof Promise) {
1712
+ return transformed.then((value) => handleCodecTxResult(result, value, def.out, ctx));
1713
+ }
1714
+ return handleCodecTxResult(result, transformed, def.out, ctx);
1715
+ }
1716
+ else {
1717
+ const transformed = def.reverseTransform(result.value, result);
1718
+ if (transformed instanceof Promise) {
1719
+ return transformed.then((value) => handleCodecTxResult(result, value, def.in, ctx));
1720
+ }
1721
+ return handleCodecTxResult(result, transformed, def.in, ctx);
1722
+ }
1723
+ }
1724
+ function handleCodecTxResult(left, value, nextSchema, ctx) {
1725
+ // Check if transform added any issues
1726
+ if (left.issues.length) {
1727
+ left.aborted = true;
1728
+ return left;
1729
+ }
1730
+ return nextSchema._zod.run({ value, issues: left.issues }, ctx);
1581
1731
  }
1582
1732
  export const $ZodReadonly = /*@__PURE__*/ core.$constructor("$ZodReadonly", (inst, def) => {
1583
1733
  $ZodType.init(inst, def);
@@ -1586,6 +1736,9 @@ export const $ZodReadonly = /*@__PURE__*/ core.$constructor("$ZodReadonly", (ins
1586
1736
  util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1587
1737
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1588
1738
  inst._zod.parse = (payload, ctx) => {
1739
+ if (ctx.direction === "backward") {
1740
+ return def.innerType._zod.run(payload, ctx);
1741
+ }
1589
1742
  const result = def.innerType._zod.run(payload, ctx);
1590
1743
  if (result instanceof Promise) {
1591
1744
  return result.then(handleReadonlyResult);
@@ -1646,6 +1799,85 @@ export const $ZodTemplateLiteral = /*@__PURE__*/ core.$constructor("$ZodTemplate
1646
1799
  return payload;
1647
1800
  };
1648
1801
  });
1802
+ export const $ZodFunction = /*@__PURE__*/ core.$constructor("$ZodFunction", (inst, def) => {
1803
+ $ZodType.init(inst, def);
1804
+ inst._def = def;
1805
+ inst._zod.def = def;
1806
+ inst.implement = (func) => {
1807
+ if (typeof func !== "function") {
1808
+ throw new Error("implement() must be called with a function");
1809
+ }
1810
+ return function (...args) {
1811
+ const parsedArgs = inst._def.input ? parse(inst._def.input, args) : args;
1812
+ const result = Reflect.apply(func, this, parsedArgs);
1813
+ if (inst._def.output) {
1814
+ return parse(inst._def.output, result);
1815
+ }
1816
+ return result;
1817
+ };
1818
+ };
1819
+ inst.implementAsync = (func) => {
1820
+ if (typeof func !== "function") {
1821
+ throw new Error("implementAsync() must be called with a function");
1822
+ }
1823
+ return async function (...args) {
1824
+ const parsedArgs = inst._def.input ? await parseAsync(inst._def.input, args) : args;
1825
+ const result = await Reflect.apply(func, this, parsedArgs);
1826
+ if (inst._def.output) {
1827
+ return await parseAsync(inst._def.output, result);
1828
+ }
1829
+ return result;
1830
+ };
1831
+ };
1832
+ inst._zod.parse = (payload, _ctx) => {
1833
+ if (typeof payload.value !== "function") {
1834
+ payload.issues.push({
1835
+ code: "invalid_type",
1836
+ expected: "function",
1837
+ input: payload.value,
1838
+ inst,
1839
+ });
1840
+ return payload;
1841
+ }
1842
+ // Check if output is a promise type to determine if we should use async implementation
1843
+ const hasPromiseOutput = inst._def.output && inst._def.output._zod.def.type === "promise";
1844
+ if (hasPromiseOutput) {
1845
+ payload.value = inst.implementAsync(payload.value);
1846
+ }
1847
+ else {
1848
+ payload.value = inst.implement(payload.value);
1849
+ }
1850
+ return payload;
1851
+ };
1852
+ inst.input = (...args) => {
1853
+ const F = inst.constructor;
1854
+ if (Array.isArray(args[0])) {
1855
+ return new F({
1856
+ type: "function",
1857
+ input: new $ZodTuple({
1858
+ type: "tuple",
1859
+ items: args[0],
1860
+ rest: args[1],
1861
+ }),
1862
+ output: inst._def.output,
1863
+ });
1864
+ }
1865
+ return new F({
1866
+ type: "function",
1867
+ input: args[0],
1868
+ output: inst._def.output,
1869
+ });
1870
+ };
1871
+ inst.output = (output) => {
1872
+ const F = inst.constructor;
1873
+ return new F({
1874
+ type: "function",
1875
+ input: inst._def.input,
1876
+ output,
1877
+ });
1878
+ };
1879
+ return inst;
1880
+ });
1649
1881
  export const $ZodPromise = /*@__PURE__*/ core.$constructor("$ZodPromise", (inst, def) => {
1650
1882
  $ZodType.init(inst, def);
1651
1883
  inst._zod.parse = (payload, ctx) => {