zod 4.2.0-canary.20251213T203150 → 4.2.0

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 (50) hide show
  1. package/package.json +1 -1
  2. package/src/v4/classic/external.ts +2 -1
  3. package/src/v4/classic/from-json-schema.ts +527 -0
  4. package/src/v4/classic/schemas.ts +43 -0
  5. package/src/v4/classic/tests/from-json-schema.test.ts +537 -0
  6. package/src/v4/classic/tests/record.test.ts +37 -0
  7. package/src/v4/classic/tests/union.test.ts +38 -0
  8. package/src/v4/core/api.ts +15 -0
  9. package/src/v4/core/errors.ts +12 -1
  10. package/src/v4/core/json-schema-processors.ts +5 -5
  11. package/src/v4/core/schemas.ts +99 -10
  12. package/src/v4/core/versions.ts +2 -2
  13. package/src/v4/mini/external.ts +1 -1
  14. package/src/v4/mini/schemas.ts +39 -0
  15. package/v4/classic/external.cjs +5 -2
  16. package/v4/classic/external.d.cts +3 -1
  17. package/v4/classic/external.d.ts +3 -1
  18. package/v4/classic/external.js +3 -1
  19. package/v4/classic/from-json-schema.cjs +503 -0
  20. package/v4/classic/from-json-schema.d.cts +10 -0
  21. package/v4/classic/from-json-schema.d.ts +10 -0
  22. package/v4/classic/from-json-schema.js +477 -0
  23. package/v4/classic/schemas.cjs +30 -2
  24. package/v4/classic/schemas.d.cts +10 -0
  25. package/v4/classic/schemas.d.ts +10 -0
  26. package/v4/classic/schemas.js +26 -0
  27. package/v4/core/api.cjs +9 -0
  28. package/v4/core/api.d.cts +2 -0
  29. package/v4/core/api.d.ts +2 -0
  30. package/v4/core/api.js +8 -0
  31. package/v4/core/errors.d.cts +10 -1
  32. package/v4/core/errors.d.ts +10 -1
  33. package/v4/core/json-schema-processors.cjs +5 -5
  34. package/v4/core/json-schema-processors.js +5 -5
  35. package/v4/core/schemas.cjs +76 -11
  36. package/v4/core/schemas.d.cts +9 -0
  37. package/v4/core/schemas.d.ts +9 -0
  38. package/v4/core/schemas.js +74 -9
  39. package/v4/core/versions.cjs +2 -2
  40. package/v4/core/versions.d.cts +1 -1
  41. package/v4/core/versions.d.ts +1 -1
  42. package/v4/core/versions.js +2 -2
  43. package/v4/mini/external.cjs +3 -2
  44. package/v4/mini/external.d.cts +2 -1
  45. package/v4/mini/external.d.ts +2 -1
  46. package/v4/mini/external.js +2 -1
  47. package/v4/mini/schemas.cjs +28 -2
  48. package/v4/mini/schemas.d.cts +8 -0
  49. package/v4/mini/schemas.d.ts +8 -0
  50. package/v4/mini/schemas.js +24 -0
@@ -2012,6 +2012,7 @@ export type $InferUnionInput<T extends SomeType> = T extends any ? core.input<T>
2012
2012
  export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodTypeDef {
2013
2013
  type: "union";
2014
2014
  options: Options;
2015
+ inclusive?: boolean;
2015
2016
  }
2016
2017
 
2017
2018
  type IsOptionalIn<T extends SomeType> = T extends OptionalInSchema ? true : false;
@@ -2119,6 +2120,85 @@ export const $ZodUnion: core.$constructor<$ZodUnion> = /*@__PURE__*/ core.$const
2119
2120
  };
2120
2121
  });
2121
2122
 
2123
+ function handleExclusiveUnionResults(
2124
+ results: ParsePayload[],
2125
+ final: ParsePayload,
2126
+ inst: $ZodUnion,
2127
+ ctx?: ParseContext
2128
+ ) {
2129
+ const successes = results.filter((r) => r.issues.length === 0);
2130
+
2131
+ if (successes.length === 1) {
2132
+ final.value = successes[0].value;
2133
+ return final;
2134
+ }
2135
+
2136
+ if (successes.length === 0) {
2137
+ // No matches - same as regular union
2138
+ final.issues.push({
2139
+ code: "invalid_union",
2140
+ input: final.value,
2141
+ inst,
2142
+ errors: results.map((result) => result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
2143
+ });
2144
+ } else {
2145
+ // Multiple matches - exclusive union failure
2146
+ final.issues.push({
2147
+ code: "invalid_union",
2148
+ input: final.value,
2149
+ inst,
2150
+ errors: [],
2151
+ inclusive: false,
2152
+ });
2153
+ }
2154
+
2155
+ return final;
2156
+ }
2157
+
2158
+ export interface $ZodXorInternals<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionInternals<T> {}
2159
+
2160
+ export interface $ZodXor<T extends readonly SomeType[] = readonly $ZodType[]>
2161
+ extends $ZodType<any, any, $ZodXorInternals<T>> {
2162
+ _zod: $ZodXorInternals<T>;
2163
+ }
2164
+
2165
+ export const $ZodXor: core.$constructor<$ZodXor> = /*@__PURE__*/ core.$constructor("$ZodXor", (inst, def) => {
2166
+ $ZodUnion.init(inst, def);
2167
+ def.inclusive = false;
2168
+
2169
+ const single = def.options.length === 1;
2170
+ const first = def.options[0]._zod.run;
2171
+
2172
+ inst._zod.parse = (payload, ctx) => {
2173
+ if (single) {
2174
+ return first(payload, ctx);
2175
+ }
2176
+ let async = false;
2177
+
2178
+ const results: util.MaybeAsync<ParsePayload>[] = [];
2179
+ for (const option of def.options) {
2180
+ const result = option._zod.run(
2181
+ {
2182
+ value: payload.value,
2183
+ issues: [],
2184
+ },
2185
+ ctx
2186
+ );
2187
+ if (result instanceof Promise) {
2188
+ results.push(result);
2189
+ async = true;
2190
+ } else {
2191
+ results.push(result);
2192
+ }
2193
+ }
2194
+
2195
+ if (!async) return handleExclusiveUnionResults(results as ParsePayload[], payload, inst, ctx);
2196
+ return Promise.all(results).then((results) => {
2197
+ return handleExclusiveUnionResults(results as ParsePayload[], payload, inst, ctx);
2198
+ });
2199
+ };
2200
+ });
2201
+
2122
2202
  //////////////////////////////////////////////////////
2123
2203
  //////////////////////////////////////////////////////
2124
2204
  ////////// //////////
@@ -2153,6 +2233,8 @@ export interface $ZodDiscriminatedUnion<
2153
2233
  export const $ZodDiscriminatedUnion: core.$constructor<$ZodDiscriminatedUnion> =
2154
2234
  /*@__PURE__*/
2155
2235
  core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
2236
+ def.inclusive = false;
2237
+
2156
2238
  $ZodUnion.init(inst, def);
2157
2239
 
2158
2240
  const _super = inst._zod.parse;
@@ -2525,6 +2607,8 @@ export interface $ZodRecordDef<Key extends $ZodRecordKey = $ZodRecordKey, Value
2525
2607
  type: "record";
2526
2608
  keyType: Key;
2527
2609
  valueType: Value;
2610
+ /** @default "strict" - errors on keys not matching keyType. "loose" passes through non-matching keys unchanged. */
2611
+ mode?: "strict" | "loose";
2528
2612
  }
2529
2613
 
2530
2614
  // export type $InferZodRecordOutput<
@@ -2652,16 +2736,21 @@ export const $ZodRecord: core.$constructor<$ZodRecord> = /*@__PURE__*/ core.$con
2652
2736
  }
2653
2737
 
2654
2738
  if (keyResult.issues.length) {
2655
- payload.issues.push({
2656
- code: "invalid_key",
2657
-
2658
- origin: "record",
2659
- issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
2660
- input: key,
2661
- path: [key],
2662
- inst,
2663
- });
2664
- payload.value[keyResult.value as PropertyKey] = keyResult.value;
2739
+ if (def.mode === "loose") {
2740
+ // Pass through unchanged
2741
+ payload.value[key] = input[key];
2742
+ } else {
2743
+ // Default "strict" behavior: error on invalid key
2744
+ payload.issues.push({
2745
+ code: "invalid_key",
2746
+
2747
+ origin: "record",
2748
+ issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
2749
+ input: key,
2750
+ path: [key],
2751
+ inst,
2752
+ });
2753
+ }
2665
2754
  continue;
2666
2755
  }
2667
2756
 
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
- minor: 1,
4
- patch: 13 as number,
3
+ minor: 2,
4
+ patch: 0 as number,
5
5
  } as const;
@@ -17,11 +17,11 @@ export {
17
17
  prettifyError,
18
18
  formatError,
19
19
  flattenError,
20
- toJSONSchema,
21
20
  TimePrecision,
22
21
  util,
23
22
  NEVER,
24
23
  } from "../core/index.js";
24
+ export { toJSONSchema } from "../core/json-schema-processors.js";
25
25
 
26
26
  export * as locales from "../locales/index.js";
27
27
  /** A special constant with type `never` */
@@ -951,6 +951,31 @@ export function union<const T extends readonly SomeType[]>(
951
951
  }) as any;
952
952
  }
953
953
 
954
+ // ZodMiniXor
955
+ export interface ZodMiniXor<T extends readonly SomeType[] = readonly core.$ZodType[]>
956
+ extends _ZodMiniType<core.$ZodXorInternals<T>> {
957
+ // _zod: core.$ZodXorInternals<T>;
958
+ }
959
+ export const ZodMiniXor: core.$constructor<ZodMiniXor> = /*@__PURE__*/ core.$constructor("ZodMiniXor", (inst, def) => {
960
+ ZodMiniUnion.init(inst, def);
961
+ core.$ZodXor.init(inst, def);
962
+ });
963
+
964
+ /** Creates an exclusive union (XOR) where exactly one option must match.
965
+ * Unlike regular unions that succeed when any option matches, xor fails if
966
+ * zero or more than one option matches the input. */
967
+ export function xor<const T extends readonly SomeType[]>(
968
+ options: T,
969
+ params?: string | core.$ZodXorParams
970
+ ): ZodMiniXor<T> {
971
+ return new ZodMiniXor({
972
+ type: "union",
973
+ options: options as any as core.$ZodType[],
974
+ inclusive: false,
975
+ ...util.normalizeParams(params),
976
+ }) as any;
977
+ }
978
+
954
979
  // ZodMiniDiscriminatedUnion
955
980
  export interface ZodMiniDiscriminatedUnion<
956
981
  Options extends readonly SomeType[] = readonly core.$ZodType[],
@@ -1086,6 +1111,20 @@ export function partialRecord<Key extends core.$ZodRecordKey, Value extends Some
1086
1111
  }) as any;
1087
1112
  }
1088
1113
 
1114
+ export function looseRecord<Key extends core.$ZodRecordKey, Value extends SomeType>(
1115
+ keyType: Key,
1116
+ valueType: Value,
1117
+ params?: string | core.$ZodRecordParams
1118
+ ): ZodMiniRecord<Key, Value> {
1119
+ return new ZodMiniRecord({
1120
+ type: "record",
1121
+ keyType,
1122
+ valueType: valueType as any as core.$ZodType,
1123
+ mode: "loose",
1124
+ ...util.normalizeParams(params),
1125
+ }) as any;
1126
+ }
1127
+
1089
1128
  // ZodMiniMap
1090
1129
  export interface ZodMiniMap<Key extends SomeType = core.$ZodType, Value extends SomeType = core.$ZodType>
1091
1130
  extends _ZodMiniType<core.$ZodMapInternals<Key, Value>> {
@@ -29,7 +29,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
29
29
  return (mod && mod.__esModule) ? mod : { "default": mod };
30
30
  };
31
31
  Object.defineProperty(exports, "__esModule", { value: true });
32
- exports.coerce = exports.iso = exports.ZodISODuration = exports.ZodISOTime = exports.ZodISODate = exports.ZodISODateTime = exports.locales = exports.NEVER = exports.util = exports.TimePrecision = exports.toJSONSchema = exports.flattenError = exports.formatError = exports.prettifyError = exports.treeifyError = exports.regexes = exports.clone = exports.$brand = exports.$input = exports.$output = exports.config = exports.registry = exports.globalRegistry = exports.core = void 0;
32
+ exports.coerce = exports.iso = exports.ZodISODuration = exports.ZodISOTime = exports.ZodISODate = exports.ZodISODateTime = exports.locales = exports.fromJSONSchema = exports.toJSONSchema = exports.NEVER = exports.util = exports.TimePrecision = exports.flattenError = exports.formatError = exports.prettifyError = exports.treeifyError = exports.regexes = exports.clone = exports.$brand = exports.$input = exports.$output = exports.config = exports.registry = exports.globalRegistry = exports.core = void 0;
33
33
  exports.core = __importStar(require("../core/index.cjs"));
34
34
  __exportStar(require("./schemas.cjs"), exports);
35
35
  __exportStar(require("./checks.cjs"), exports);
@@ -53,10 +53,13 @@ Object.defineProperty(exports, "treeifyError", { enumerable: true, get: function
53
53
  Object.defineProperty(exports, "prettifyError", { enumerable: true, get: function () { return index_js_2.prettifyError; } });
54
54
  Object.defineProperty(exports, "formatError", { enumerable: true, get: function () { return index_js_2.formatError; } });
55
55
  Object.defineProperty(exports, "flattenError", { enumerable: true, get: function () { return index_js_2.flattenError; } });
56
- Object.defineProperty(exports, "toJSONSchema", { enumerable: true, get: function () { return index_js_2.toJSONSchema; } });
57
56
  Object.defineProperty(exports, "TimePrecision", { enumerable: true, get: function () { return index_js_2.TimePrecision; } });
58
57
  Object.defineProperty(exports, "util", { enumerable: true, get: function () { return index_js_2.util; } });
59
58
  Object.defineProperty(exports, "NEVER", { enumerable: true, get: function () { return index_js_2.NEVER; } });
59
+ var json_schema_processors_js_1 = require("../core/json-schema-processors.cjs");
60
+ Object.defineProperty(exports, "toJSONSchema", { enumerable: true, get: function () { return json_schema_processors_js_1.toJSONSchema; } });
61
+ var from_json_schema_js_1 = require("./from-json-schema.cjs");
62
+ Object.defineProperty(exports, "fromJSONSchema", { enumerable: true, get: function () { return from_json_schema_js_1.fromJSONSchema; } });
60
63
  exports.locales = __importStar(require("../locales/index.cjs"));
61
64
  // iso
62
65
  // must be exported from top-level
@@ -5,7 +5,9 @@ export * from "./errors.cjs";
5
5
  export * from "./parse.cjs";
6
6
  export * from "./compat.cjs";
7
7
  export type { infer, output, input } from "../core/index.cjs";
8
- export { globalRegistry, type GlobalMeta, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.cjs";
8
+ export { globalRegistry, type GlobalMeta, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.cjs";
9
+ export { toJSONSchema } from "../core/json-schema-processors.cjs";
10
+ export { fromJSONSchema } from "./from-json-schema.cjs";
9
11
  export * as locales from "../locales/index.cjs";
10
12
  export { ZodISODateTime, ZodISODate, ZodISOTime, ZodISODuration } from "./iso.cjs";
11
13
  export * as iso from "./iso.cjs";
@@ -5,7 +5,9 @@ export * from "./errors.js";
5
5
  export * from "./parse.js";
6
6
  export * from "./compat.js";
7
7
  export type { infer, output, input } from "../core/index.js";
8
- export { globalRegistry, type GlobalMeta, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.js";
8
+ export { globalRegistry, type GlobalMeta, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.js";
9
+ export { toJSONSchema } from "../core/json-schema-processors.js";
10
+ export { fromJSONSchema } from "./from-json-schema.js";
9
11
  export * as locales from "../locales/index.js";
10
12
  export { ZodISODateTime, ZodISODate, ZodISOTime, ZodISODuration } from "./iso.js";
11
13
  export * as iso from "./iso.js";
@@ -8,7 +8,9 @@ export * from "./compat.js";
8
8
  import { config } from "../core/index.js";
9
9
  import en from "../locales/en.js";
10
10
  config(en());
11
- export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.js";
11
+ export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.js";
12
+ export { toJSONSchema } from "../core/json-schema-processors.js";
13
+ export { fromJSONSchema } from "./from-json-schema.js";
12
14
  export * as locales from "../locales/index.js";
13
15
  // iso
14
16
  // must be exported from top-level