zod 4.1.0-canary.20250821T014930 → 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 (103) hide show
  1. package/package.json +1 -1
  2. package/src/v4/classic/external.ts +0 -1
  3. package/src/v4/classic/parse.ts +49 -0
  4. package/src/v4/classic/schemas.ts +145 -7
  5. package/src/v4/classic/tests/catch.test.ts +25 -0
  6. package/src/v4/classic/tests/codec-examples.test.ts +538 -0
  7. package/src/v4/classic/tests/codec.test.ts +532 -0
  8. package/src/v4/classic/tests/continuability.test.ts +1 -1
  9. package/src/v4/classic/tests/default.test.ts +32 -0
  10. package/src/v4/classic/tests/firstparty.test.ts +4 -0
  11. package/src/v4/classic/tests/function.test.ts +31 -31
  12. package/src/v4/classic/tests/hash.test.ts +68 -0
  13. package/src/v4/classic/tests/nonoptional.test.ts +15 -0
  14. package/src/v4/classic/tests/object.test.ts +31 -0
  15. package/src/v4/classic/tests/pipe.test.ts +25 -5
  16. package/src/v4/classic/tests/prefault.test.ts +25 -0
  17. package/src/v4/classic/tests/preprocess.test.ts +1 -6
  18. package/src/v4/classic/tests/refine.test.ts +76 -3
  19. package/src/v4/classic/tests/string-formats.test.ts +16 -0
  20. package/src/v4/classic/tests/string.test.ts +82 -1
  21. package/src/v4/classic/tests/stringbool.test.ts +40 -0
  22. package/src/v4/classic/tests/template-literal.test.ts +1 -1
  23. package/src/v4/classic/tests/to-json-schema.test.ts +21 -2
  24. package/src/v4/classic/tests/transform.test.ts +7 -0
  25. package/src/v4/classic/tests/union.test.ts +1 -1
  26. package/src/v4/core/api.ts +25 -35
  27. package/src/v4/core/core.ts +7 -26
  28. package/src/v4/core/index.ts +0 -1
  29. package/src/v4/core/json-schema.ts +1 -0
  30. package/src/v4/core/parse.ts +101 -0
  31. package/src/v4/core/regexes.ts +40 -1
  32. package/src/v4/core/schemas.ts +521 -129
  33. package/src/v4/core/to-json-schema.ts +43 -8
  34. package/src/v4/core/util.ts +73 -0
  35. package/src/v4/mini/external.ts +0 -1
  36. package/src/v4/mini/parse.ts +14 -1
  37. package/src/v4/mini/schemas.ts +153 -12
  38. package/src/v4/mini/tests/codec.test.ts +499 -0
  39. package/src/v4/mini/tests/object.test.ts +9 -0
  40. package/src/v4/mini/tests/string.test.ts +16 -0
  41. package/v4/classic/external.cjs +1 -2
  42. package/v4/classic/external.d.cts +1 -1
  43. package/v4/classic/external.d.ts +1 -1
  44. package/v4/classic/external.js +1 -1
  45. package/v4/classic/parse.cjs +10 -1
  46. package/v4/classic/parse.d.cts +8 -0
  47. package/v4/classic/parse.d.ts +8 -0
  48. package/v4/classic/parse.js +9 -0
  49. package/v4/classic/schemas.cjs +59 -4
  50. package/v4/classic/schemas.d.cts +48 -2
  51. package/v4/classic/schemas.d.ts +48 -2
  52. package/v4/classic/schemas.js +51 -3
  53. package/v4/core/api.cjs +19 -24
  54. package/v4/core/api.d.cts +3 -4
  55. package/v4/core/api.d.ts +3 -4
  56. package/v4/core/api.js +19 -24
  57. package/v4/core/core.cjs +8 -1
  58. package/v4/core/core.d.cts +3 -0
  59. package/v4/core/core.d.ts +3 -0
  60. package/v4/core/core.js +6 -0
  61. package/v4/core/index.cjs +0 -1
  62. package/v4/core/index.d.cts +0 -1
  63. package/v4/core/index.d.ts +0 -1
  64. package/v4/core/index.js +0 -1
  65. package/v4/core/json-schema.d.cts +1 -0
  66. package/v4/core/json-schema.d.ts +1 -0
  67. package/v4/core/parse.cjs +45 -1
  68. package/v4/core/parse.d.cts +24 -0
  69. package/v4/core/parse.d.ts +24 -0
  70. package/v4/core/parse.js +36 -0
  71. package/v4/core/regexes.cjs +34 -2
  72. package/v4/core/regexes.d.cts +16 -0
  73. package/v4/core/regexes.d.ts +16 -0
  74. package/v4/core/regexes.js +32 -1
  75. package/v4/core/schemas.cjs +309 -77
  76. package/v4/core/schemas.d.cts +61 -3
  77. package/v4/core/schemas.d.ts +61 -3
  78. package/v4/core/schemas.js +308 -76
  79. package/v4/core/to-json-schema.cjs +42 -5
  80. package/v4/core/to-json-schema.d.cts +4 -3
  81. package/v4/core/to-json-schema.d.ts +4 -3
  82. package/v4/core/to-json-schema.js +42 -5
  83. package/v4/core/util.cjs +69 -0
  84. package/v4/core/util.d.cts +10 -0
  85. package/v4/core/util.d.ts +10 -0
  86. package/v4/core/util.js +62 -0
  87. package/v4/mini/external.cjs +1 -2
  88. package/v4/mini/external.d.cts +1 -1
  89. package/v4/mini/external.d.ts +1 -1
  90. package/v4/mini/external.js +1 -1
  91. package/v4/mini/parse.cjs +9 -1
  92. package/v4/mini/parse.d.cts +1 -1
  93. package/v4/mini/parse.d.ts +1 -1
  94. package/v4/mini/parse.js +1 -1
  95. package/v4/mini/schemas.cjs +58 -3
  96. package/v4/mini/schemas.d.cts +49 -1
  97. package/v4/mini/schemas.d.ts +49 -1
  98. package/v4/mini/schemas.js +49 -2
  99. package/src/v4/core/function.ts +0 -176
  100. package/v4/core/function.cjs +0 -102
  101. package/v4/core/function.d.cts +0 -52
  102. package/v4/core/function.d.ts +0 -52
  103. package/v4/core/function.js +0 -75
@@ -23,8 +23,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.$ZodSet = exports.$ZodMap = exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = 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.$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.$ZodTemplateLiteral = exports.$ZodReadonly = exports.$ZodPipe = exports.$ZodNaN = exports.$ZodCatch = exports.$ZodSuccess = exports.$ZodNonOptional = exports.$ZodPrefault = exports.$ZodDefault = exports.$ZodNullable = exports.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = void 0;
26
+ exports.$ZodMap = exports.$ZodRecord = exports.$ZodTuple = exports.$ZodIntersection = exports.$ZodDiscriminatedUnion = 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.$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.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = void 0;
28
28
  exports.isValidBase64 = isValidBase64;
29
29
  exports.isValidBase64URL = isValidBase64URL;
30
30
  exports.isValidJWT = isValidJWT;
@@ -46,7 +46,6 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
46
46
  if (inst._zod.traits.has("$ZodCheck")) {
47
47
  checks.unshift(inst);
48
48
  }
49
- //
50
49
  for (const ch of checks) {
51
50
  for (const fn of ch._zod.onattach) {
52
51
  fn(inst);
@@ -103,7 +102,47 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
103
102
  }
104
103
  return payload;
105
104
  };
105
+ // const handleChecksResult = (
106
+ // checkResult: ParsePayload,
107
+ // originalResult: ParsePayload,
108
+ // ctx: ParseContextInternal
109
+ // ): util.MaybeAsync<ParsePayload> => {
110
+ // // if the checks mutated the value && there are no issues, re-parse the result
111
+ // if (checkResult.value !== originalResult.value && !checkResult.issues.length)
112
+ // return inst._zod.parse(checkResult, ctx);
113
+ // return originalResult;
114
+ // };
115
+ const handleCanaryResult = (canary, payload, ctx) => {
116
+ // abort if the canary is aborted
117
+ if (util.aborted(canary)) {
118
+ canary.aborted = true;
119
+ return canary;
120
+ }
121
+ // run checks first, then
122
+ const checkResult = runChecks(payload, checks, ctx);
123
+ if (checkResult instanceof Promise) {
124
+ if (ctx.async === false)
125
+ throw new core.$ZodAsyncError();
126
+ return checkResult.then((checkResult) => inst._zod.parse(checkResult, ctx));
127
+ }
128
+ return inst._zod.parse(checkResult, ctx);
129
+ };
106
130
  inst._zod.run = (payload, ctx) => {
131
+ if (ctx.skipChecks) {
132
+ return inst._zod.parse(payload, ctx);
133
+ }
134
+ if (ctx.direction === "backward") {
135
+ // run canary
136
+ // initial pass (no checks)
137
+ const canary = inst._zod.parse({ value: payload.value, issues: [] }, { ...ctx, skipChecks: true });
138
+ if (canary instanceof Promise) {
139
+ return canary.then((canary) => {
140
+ return handleCanaryResult(canary, payload, ctx);
141
+ });
142
+ }
143
+ return handleCanaryResult(canary, payload, ctx);
144
+ }
145
+ // forward
107
146
  const result = inst._zod.parse(payload, ctx);
108
147
  if (result instanceof Promise) {
109
148
  if (ctx.async === false)
@@ -704,25 +743,61 @@ function handlePropertyResult(result, final, key, input) {
704
743
  final.value[key] = result.value;
705
744
  }
706
745
  }
746
+ function normalizeDef(def) {
747
+ const keys = Object.keys(def.shape);
748
+ for (const k of keys) {
749
+ if (!def.shape[k]._zod.traits.has("$ZodType")) {
750
+ throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
751
+ }
752
+ }
753
+ const okeys = util.optionalKeys(def.shape);
754
+ return {
755
+ ...def,
756
+ keys,
757
+ keySet: new Set(keys),
758
+ numKeys: keys.length,
759
+ optionalKeys: new Set(okeys),
760
+ };
761
+ }
762
+ function handleCatchall(proms, input, payload, ctx, def, inst) {
763
+ const unrecognized = [];
764
+ // iterate over input keys
765
+ const keySet = def.keySet;
766
+ const _catchall = def.catchall._zod;
767
+ const t = _catchall.def.type;
768
+ for (const key of Object.keys(input)) {
769
+ if (keySet.has(key))
770
+ continue;
771
+ if (t === "never") {
772
+ unrecognized.push(key);
773
+ continue;
774
+ }
775
+ const r = _catchall.run({ value: input[key], issues: [] }, ctx);
776
+ if (r instanceof Promise) {
777
+ proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
778
+ }
779
+ else {
780
+ handlePropertyResult(r, payload, key, input);
781
+ }
782
+ }
783
+ if (unrecognized.length) {
784
+ payload.issues.push({
785
+ code: "unrecognized_keys",
786
+ keys: unrecognized,
787
+ input,
788
+ inst,
789
+ });
790
+ }
791
+ if (!proms.length)
792
+ return payload;
793
+ return Promise.all(proms).then(() => {
794
+ return payload;
795
+ });
796
+ }
707
797
  exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
708
798
  // requires cast because technically $ZodObject doesn't extend
709
799
  exports.$ZodType.init(inst, def);
710
- const _normalized = util.cached(() => {
711
- const keys = Object.keys(def.shape);
712
- for (const k of keys) {
713
- if (!def.shape[k]._zod.traits.has("$ZodType")) {
714
- throw new Error(`Invalid element at key "${k}": expected a Zod schema`);
715
- }
716
- }
717
- const okeys = util.optionalKeys(def.shape);
718
- return {
719
- shape: def.shape,
720
- keys,
721
- keySet: new Set(keys),
722
- numKeys: keys.length,
723
- optionalKeys: new Set(okeys),
724
- };
725
- });
800
+ const _normalized = util.cached(() => normalizeDef(def));
726
801
  util.defineLazy(inst._zod, "propValues", () => {
727
802
  const shape = def.shape;
728
803
  const propValues = {};
@@ -736,6 +811,45 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
736
811
  }
737
812
  return propValues;
738
813
  });
814
+ const isObject = util.isObject;
815
+ const catchall = def.catchall;
816
+ let value;
817
+ inst._zod.parse = (payload, ctx) => {
818
+ value ?? (value = _normalized.value);
819
+ const input = payload.value;
820
+ if (!isObject(input)) {
821
+ payload.issues.push({
822
+ expected: "object",
823
+ code: "invalid_type",
824
+ input,
825
+ inst,
826
+ });
827
+ return payload;
828
+ }
829
+ payload.value = {};
830
+ const proms = [];
831
+ const shape = value.shape;
832
+ for (const key of value.keys) {
833
+ const el = shape[key];
834
+ const r = el._zod.run({ value: input[key], issues: [] }, ctx);
835
+ if (r instanceof Promise) {
836
+ proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
837
+ }
838
+ else {
839
+ handlePropertyResult(r, payload, key, input);
840
+ }
841
+ }
842
+ if (!catchall) {
843
+ return proms.length ? Promise.all(proms).then(() => payload) : payload;
844
+ }
845
+ return handleCatchall(proms, input, payload, ctx, _normalized.value, inst);
846
+ };
847
+ });
848
+ exports.$ZodObjectJIT = core.$constructor("$ZodObjectJIT", (inst, def) => {
849
+ // requires cast because technically $ZodObject doesn't extend
850
+ exports.$ZodObject.init(inst, def);
851
+ const superParse = inst._zod.parse;
852
+ const _normalized = util.cached(() => normalizeDef(def));
739
853
  const generateFastpass = (shape) => {
740
854
  const doc = new doc_js_1.Doc(["shape", "payload", "ctx"]);
741
855
  const normalized = _normalized.value;
@@ -796,63 +910,16 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
796
910
  });
797
911
  return payload;
798
912
  }
799
- const proms = [];
800
913
  if (jit && fastEnabled && ctx?.async === false && ctx.jitless !== true) {
801
914
  // always synchronous
802
915
  if (!fastpass)
803
916
  fastpass = generateFastpass(def.shape);
804
917
  payload = fastpass(payload, ctx);
918
+ if (!catchall)
919
+ return payload;
920
+ return handleCatchall([], input, payload, ctx, value, inst);
805
921
  }
806
- else {
807
- payload.value = {};
808
- const shape = value.shape;
809
- for (const key of value.keys) {
810
- const el = shape[key];
811
- const r = el._zod.run({ value: input[key], issues: [] }, ctx);
812
- if (r instanceof Promise) {
813
- proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
814
- }
815
- else {
816
- handlePropertyResult(r, payload, key, input);
817
- }
818
- }
819
- }
820
- if (!catchall) {
821
- return proms.length ? Promise.all(proms).then(() => payload) : payload;
822
- }
823
- const unrecognized = [];
824
- // iterate over input keys
825
- const keySet = value.keySet;
826
- const _catchall = catchall._zod;
827
- const t = _catchall.def.type;
828
- for (const key of Object.keys(input)) {
829
- if (keySet.has(key))
830
- continue;
831
- if (t === "never") {
832
- unrecognized.push(key);
833
- continue;
834
- }
835
- const r = _catchall.run({ value: input[key], issues: [] }, ctx);
836
- if (r instanceof Promise) {
837
- proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
838
- }
839
- else {
840
- handlePropertyResult(r, payload, key, input);
841
- }
842
- }
843
- if (unrecognized.length) {
844
- payload.issues.push({
845
- code: "unrecognized_keys",
846
- keys: unrecognized,
847
- input,
848
- inst,
849
- });
850
- }
851
- if (!proms.length)
852
- return payload;
853
- return Promise.all(proms).then(() => {
854
- return payload;
855
- });
922
+ return superParse(payload, ctx);
856
923
  };
857
924
  });
858
925
  function handleUnionResults(results, final, inst, ctx) {
@@ -1399,9 +1466,12 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
1399
1466
  });
1400
1467
  exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
1401
1468
  exports.$ZodType.init(inst, def);
1402
- inst._zod.parse = (payload, _ctx) => {
1469
+ inst._zod.parse = (payload, ctx) => {
1470
+ if (ctx.direction === "backward") {
1471
+ throw new core.$ZodEncodeError(inst.constructor.name);
1472
+ }
1403
1473
  const _out = def.transform(payload.value, payload);
1404
- if (_ctx.async) {
1474
+ if (ctx.async) {
1405
1475
  const output = _out instanceof Promise ? _out : Promise.resolve(_out);
1406
1476
  return output.then((output) => {
1407
1477
  payload.value = output;
@@ -1457,6 +1527,7 @@ exports.$ZodNullable = core.$constructor("$ZodNullable", (inst, def) => {
1457
1527
  return def.innerType._zod.values ? new Set([...def.innerType._zod.values, null]) : undefined;
1458
1528
  });
1459
1529
  inst._zod.parse = (payload, ctx) => {
1530
+ // Forward direction (decode): allow null to pass through
1460
1531
  if (payload.value === null)
1461
1532
  return payload;
1462
1533
  return def.innerType._zod.run(payload, ctx);
@@ -1468,13 +1539,18 @@ exports.$ZodDefault = core.$constructor("$ZodDefault", (inst, def) => {
1468
1539
  inst._zod.optin = "optional";
1469
1540
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1470
1541
  inst._zod.parse = (payload, ctx) => {
1542
+ if (ctx.direction === "backward") {
1543
+ return def.innerType._zod.run(payload, ctx);
1544
+ }
1545
+ // Forward direction (decode): apply defaults for undefined input
1471
1546
  if (payload.value === undefined) {
1472
1547
  payload.value = def.defaultValue;
1473
1548
  /**
1474
- * $ZodDefault always returns the default value immediately.
1549
+ * $ZodDefault returns the default value immediately in forward direction.
1475
1550
  * 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. */
1476
1551
  return payload;
1477
1552
  }
1553
+ // Forward direction: continue with default handling
1478
1554
  const result = def.innerType._zod.run(payload, ctx);
1479
1555
  if (result instanceof Promise) {
1480
1556
  return result.then((result) => handleDefaultResult(result, def));
@@ -1493,6 +1569,10 @@ exports.$ZodPrefault = core.$constructor("$ZodPrefault", (inst, def) => {
1493
1569
  inst._zod.optin = "optional";
1494
1570
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1495
1571
  inst._zod.parse = (payload, ctx) => {
1572
+ if (ctx.direction === "backward") {
1573
+ return def.innerType._zod.run(payload, ctx);
1574
+ }
1575
+ // Forward direction (decode): apply prefault for undefined input
1496
1576
  if (payload.value === undefined) {
1497
1577
  payload.value = def.defaultValue;
1498
1578
  }
@@ -1527,6 +1607,9 @@ function handleNonOptionalResult(payload, inst) {
1527
1607
  exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
1528
1608
  exports.$ZodType.init(inst, def);
1529
1609
  inst._zod.parse = (payload, ctx) => {
1610
+ if (ctx.direction === "backward") {
1611
+ throw new core.$ZodEncodeError("ZodSuccess");
1612
+ }
1530
1613
  const result = def.innerType._zod.run(payload, ctx);
1531
1614
  if (result instanceof Promise) {
1532
1615
  return result.then((result) => {
@@ -1544,6 +1627,10 @@ exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
1544
1627
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1545
1628
  util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
1546
1629
  inst._zod.parse = (payload, ctx) => {
1630
+ if (ctx.direction === "backward") {
1631
+ return def.innerType._zod.run(payload, ctx);
1632
+ }
1633
+ // Forward direction (decode): apply catch logic
1547
1634
  const result = def.innerType._zod.run(payload, ctx);
1548
1635
  if (result instanceof Promise) {
1549
1636
  return result.then((result) => {
@@ -1597,18 +1684,81 @@ exports.$ZodPipe = core.$constructor("$ZodPipe", (inst, def) => {
1597
1684
  util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
1598
1685
  util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
1599
1686
  inst._zod.parse = (payload, ctx) => {
1687
+ if (ctx.direction === "backward") {
1688
+ const right = def.out._zod.run(payload, ctx);
1689
+ if (right instanceof Promise) {
1690
+ return right.then((right) => handlePipeResult(right, def.in, ctx));
1691
+ }
1692
+ return handlePipeResult(right, def.in, ctx);
1693
+ }
1600
1694
  const left = def.in._zod.run(payload, ctx);
1601
1695
  if (left instanceof Promise) {
1602
- return left.then((left) => handlePipeResult(left, def, ctx));
1696
+ return left.then((left) => handlePipeResult(left, def.out, ctx));
1603
1697
  }
1604
- return handlePipeResult(left, def, ctx);
1698
+ return handlePipeResult(left, def.out, ctx);
1605
1699
  };
1606
1700
  });
1607
- function handlePipeResult(left, def, ctx) {
1701
+ function handlePipeResult(left, next, ctx) {
1608
1702
  if (left.issues.length) {
1703
+ // prevent further checks
1704
+ left.aborted = true;
1609
1705
  return left;
1610
1706
  }
1611
- return def.out._zod.run({ value: left.value, issues: left.issues }, ctx);
1707
+ return next._zod.run({ value: left.value, issues: left.issues }, ctx);
1708
+ }
1709
+ exports.$ZodCodec = core.$constructor("$ZodCodec", (inst, def) => {
1710
+ exports.$ZodType.init(inst, def);
1711
+ util.defineLazy(inst._zod, "values", () => def.in._zod.values);
1712
+ util.defineLazy(inst._zod, "optin", () => def.in._zod.optin);
1713
+ util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
1714
+ util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
1715
+ inst._zod.parse = (payload, ctx) => {
1716
+ const direction = ctx.direction || "forward";
1717
+ if (direction === "forward") {
1718
+ const left = def.in._zod.run(payload, ctx);
1719
+ if (left instanceof Promise) {
1720
+ return left.then((left) => handleCodecAResult(left, def, ctx));
1721
+ }
1722
+ return handleCodecAResult(left, def, ctx);
1723
+ }
1724
+ else {
1725
+ const right = def.out._zod.run(payload, ctx);
1726
+ if (right instanceof Promise) {
1727
+ return right.then((right) => handleCodecAResult(right, def, ctx));
1728
+ }
1729
+ return handleCodecAResult(right, def, ctx);
1730
+ }
1731
+ };
1732
+ });
1733
+ function handleCodecAResult(result, def, ctx) {
1734
+ if (result.issues.length) {
1735
+ // prevent further checks
1736
+ result.aborted = true;
1737
+ return result;
1738
+ }
1739
+ const direction = ctx.direction || "forward";
1740
+ if (direction === "forward") {
1741
+ const transformed = def.transform(result.value, result);
1742
+ if (transformed instanceof Promise) {
1743
+ return transformed.then((value) => handleCodecTxResult(result, value, def.out, ctx));
1744
+ }
1745
+ return handleCodecTxResult(result, transformed, def.out, ctx);
1746
+ }
1747
+ else {
1748
+ const transformed = def.reverseTransform(result.value, result);
1749
+ if (transformed instanceof Promise) {
1750
+ return transformed.then((value) => handleCodecTxResult(result, value, def.in, ctx));
1751
+ }
1752
+ return handleCodecTxResult(result, transformed, def.in, ctx);
1753
+ }
1754
+ }
1755
+ function handleCodecTxResult(left, value, nextSchema, ctx) {
1756
+ // Check if transform added any issues
1757
+ if (left.issues.length) {
1758
+ left.aborted = true;
1759
+ return left;
1760
+ }
1761
+ return nextSchema._zod.run({ value, issues: left.issues }, ctx);
1612
1762
  }
1613
1763
  exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
1614
1764
  exports.$ZodType.init(inst, def);
@@ -1617,6 +1767,9 @@ exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
1617
1767
  util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
1618
1768
  util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
1619
1769
  inst._zod.parse = (payload, ctx) => {
1770
+ if (ctx.direction === "backward") {
1771
+ return def.innerType._zod.run(payload, ctx);
1772
+ }
1620
1773
  const result = def.innerType._zod.run(payload, ctx);
1621
1774
  if (result instanceof Promise) {
1622
1775
  return result.then(handleReadonlyResult);
@@ -1677,6 +1830,85 @@ exports.$ZodTemplateLiteral = core.$constructor("$ZodTemplateLiteral", (inst, de
1677
1830
  return payload;
1678
1831
  };
1679
1832
  });
1833
+ exports.$ZodFunction = core.$constructor("$ZodFunction", (inst, def) => {
1834
+ exports.$ZodType.init(inst, def);
1835
+ inst._def = def;
1836
+ inst._zod.def = def;
1837
+ inst.implement = (func) => {
1838
+ if (typeof func !== "function") {
1839
+ throw new Error("implement() must be called with a function");
1840
+ }
1841
+ return function (...args) {
1842
+ const parsedArgs = inst._def.input ? (0, parse_js_1.parse)(inst._def.input, args) : args;
1843
+ const result = Reflect.apply(func, this, parsedArgs);
1844
+ if (inst._def.output) {
1845
+ return (0, parse_js_1.parse)(inst._def.output, result);
1846
+ }
1847
+ return result;
1848
+ };
1849
+ };
1850
+ inst.implementAsync = (func) => {
1851
+ if (typeof func !== "function") {
1852
+ throw new Error("implementAsync() must be called with a function");
1853
+ }
1854
+ return async function (...args) {
1855
+ const parsedArgs = inst._def.input ? await (0, parse_js_1.parseAsync)(inst._def.input, args) : args;
1856
+ const result = await Reflect.apply(func, this, parsedArgs);
1857
+ if (inst._def.output) {
1858
+ return await (0, parse_js_1.parseAsync)(inst._def.output, result);
1859
+ }
1860
+ return result;
1861
+ };
1862
+ };
1863
+ inst._zod.parse = (payload, _ctx) => {
1864
+ if (typeof payload.value !== "function") {
1865
+ payload.issues.push({
1866
+ code: "invalid_type",
1867
+ expected: "function",
1868
+ input: payload.value,
1869
+ inst,
1870
+ });
1871
+ return payload;
1872
+ }
1873
+ // Check if output is a promise type to determine if we should use async implementation
1874
+ const hasPromiseOutput = inst._def.output && inst._def.output._zod.def.type === "promise";
1875
+ if (hasPromiseOutput) {
1876
+ payload.value = inst.implementAsync(payload.value);
1877
+ }
1878
+ else {
1879
+ payload.value = inst.implement(payload.value);
1880
+ }
1881
+ return payload;
1882
+ };
1883
+ inst.input = (...args) => {
1884
+ const F = inst.constructor;
1885
+ if (Array.isArray(args[0])) {
1886
+ return new F({
1887
+ type: "function",
1888
+ input: new exports.$ZodTuple({
1889
+ type: "tuple",
1890
+ items: args[0],
1891
+ rest: args[1],
1892
+ }),
1893
+ output: inst._def.output,
1894
+ });
1895
+ }
1896
+ return new F({
1897
+ type: "function",
1898
+ input: args[0],
1899
+ output: inst._def.output,
1900
+ });
1901
+ };
1902
+ inst.output = (output) => {
1903
+ const F = inst.constructor;
1904
+ return new F({
1905
+ type: "function",
1906
+ input: inst._def.input,
1907
+ output,
1908
+ });
1909
+ };
1910
+ return inst;
1911
+ });
1680
1912
  exports.$ZodPromise = core.$constructor("$ZodPromise", (inst, def) => {
1681
1913
  exports.$ZodType.init(inst, def);
1682
1914
  inst._zod.parse = (payload, ctx) => {
@@ -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">;