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
package/v4/core/api.d.ts CHANGED
@@ -208,6 +208,8 @@ export declare function _array<T extends schemas.$ZodType>(Class: util.SchemaCla
208
208
  export type $ZodObjectParams = TypeParams<schemas.$ZodObject, "shape" | "catchall">;
209
209
  export type $ZodUnionParams = TypeParams<schemas.$ZodUnion, "options">;
210
210
  export declare function _union<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodUnion>, options: T, params?: string | $ZodUnionParams): schemas.$ZodUnion<T>;
211
+ export type $ZodXorParams = TypeParams<schemas.$ZodXor, "options">;
212
+ export declare function _xor<const T extends readonly schemas.$ZodObject[]>(Class: util.SchemaClass<schemas.$ZodXor>, options: T, params?: string | $ZodXorParams): schemas.$ZodXor<T>;
211
213
  export interface $ZodTypeDiscriminableInternals extends schemas.$ZodTypeInternals {
212
214
  propValues: util.PropValues;
213
215
  }
package/v4/core/api.js CHANGED
@@ -638,6 +638,14 @@ export function _union(Class, options, params) {
638
638
  ...util.normalizeParams(params),
639
639
  });
640
640
  }
641
+ export function _xor(Class, options, params) {
642
+ return new Class({
643
+ type: "union",
644
+ options,
645
+ inclusive: false,
646
+ ...util.normalizeParams(params),
647
+ });
648
+ }
641
649
  export function _discriminatedUnion(Class, discriminator, options, params) {
642
650
  return new Class({
643
651
  type: "union",
@@ -48,12 +48,21 @@ export interface $ZodIssueUnrecognizedKeys extends $ZodIssueBase {
48
48
  readonly keys: string[];
49
49
  readonly input?: Record<string, unknown>;
50
50
  }
51
- export interface $ZodIssueInvalidUnion extends $ZodIssueBase {
51
+ interface $ZodIssueInvalidUnionNoMatch extends $ZodIssueBase {
52
52
  readonly code: "invalid_union";
53
53
  readonly errors: $ZodIssue[][];
54
54
  readonly input?: unknown;
55
55
  readonly discriminator?: string | undefined;
56
+ readonly inclusive?: true;
56
57
  }
58
+ interface $ZodIssueInvalidUnionMultipleMatch extends $ZodIssueBase {
59
+ readonly code: "invalid_union";
60
+ readonly errors: [];
61
+ readonly input?: unknown;
62
+ readonly discriminator?: string | undefined;
63
+ readonly inclusive: false;
64
+ }
65
+ export type $ZodIssueInvalidUnion = $ZodIssueInvalidUnionNoMatch | $ZodIssueInvalidUnionMultipleMatch;
57
66
  export interface $ZodIssueInvalidKey<Input = unknown> extends $ZodIssueBase {
58
67
  readonly code: "invalid_key";
59
68
  readonly origin: "map" | "record";
@@ -48,12 +48,21 @@ export interface $ZodIssueUnrecognizedKeys extends $ZodIssueBase {
48
48
  readonly keys: string[];
49
49
  readonly input?: Record<string, unknown>;
50
50
  }
51
- export interface $ZodIssueInvalidUnion extends $ZodIssueBase {
51
+ interface $ZodIssueInvalidUnionNoMatch extends $ZodIssueBase {
52
52
  readonly code: "invalid_union";
53
53
  readonly errors: $ZodIssue[][];
54
54
  readonly input?: unknown;
55
55
  readonly discriminator?: string | undefined;
56
+ readonly inclusive?: true;
56
57
  }
58
+ interface $ZodIssueInvalidUnionMultipleMatch extends $ZodIssueBase {
59
+ readonly code: "invalid_union";
60
+ readonly errors: [];
61
+ readonly input?: unknown;
62
+ readonly discriminator?: string | undefined;
63
+ readonly inclusive: false;
64
+ }
65
+ export type $ZodIssueInvalidUnion = $ZodIssueInvalidUnionNoMatch | $ZodIssueInvalidUnionMultipleMatch;
57
66
  export interface $ZodIssueInvalidKey<Input = unknown> extends $ZodIssueBase {
58
67
  readonly code: "invalid_key";
59
68
  readonly origin: "map" | "record";
@@ -348,14 +348,14 @@ const objectProcessor = (schema, ctx, _json, params) => {
348
348
  exports.objectProcessor = objectProcessor;
349
349
  const unionProcessor = (schema, ctx, json, params) => {
350
350
  const def = schema._zod.def;
351
- // Discriminated unions use oneOf (exactly one match) instead of anyOf (one or more matches)
352
- // because the discriminator field ensures mutual exclusivity between options in JSON Schema
353
- const isDiscriminated = def.discriminator !== undefined;
351
+ // Exclusive unions (inclusive === false) use oneOf (exactly one match) instead of anyOf (one or more matches)
352
+ // This includes both z.xor() and discriminated unions
353
+ const isExclusive = def.inclusive === false;
354
354
  const options = def.options.map((x, i) => (0, to_json_schema_js_1.process)(x, ctx, {
355
355
  ...params,
356
- path: [...params.path, isDiscriminated ? "oneOf" : "anyOf", i],
356
+ path: [...params.path, isExclusive ? "oneOf" : "anyOf", i],
357
357
  }));
358
- if (isDiscriminated) {
358
+ if (isExclusive) {
359
359
  json.oneOf = options;
360
360
  }
361
361
  else {
@@ -319,14 +319,14 @@ export const objectProcessor = (schema, ctx, _json, params) => {
319
319
  };
320
320
  export const unionProcessor = (schema, ctx, json, params) => {
321
321
  const def = schema._zod.def;
322
- // Discriminated unions use oneOf (exactly one match) instead of anyOf (one or more matches)
323
- // because the discriminator field ensures mutual exclusivity between options in JSON Schema
324
- const isDiscriminated = def.discriminator !== undefined;
322
+ // Exclusive unions (inclusive === false) use oneOf (exactly one match) instead of anyOf (one or more matches)
323
+ // This includes both z.xor() and discriminated unions
324
+ const isExclusive = def.inclusive === false;
325
325
  const options = def.options.map((x, i) => process(x, ctx, {
326
326
  ...params,
327
- path: [...params.path, isDiscriminated ? "oneOf" : "anyOf", i],
327
+ path: [...params.path, isExclusive ? "oneOf" : "anyOf", i],
328
328
  }));
329
- if (isDiscriminated) {
329
+ if (isExclusive) {
330
330
  json.oneOf = options;
331
331
  }
332
332
  else {
@@ -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.$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.$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.$ZodOptional = exports.$ZodTransform = exports.$ZodFile = exports.$ZodLiteral = exports.$ZodEnum = exports.$ZodSet = exports.$ZodMap = void 0;
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.$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;
@@ -993,9 +993,68 @@ exports.$ZodUnion = core.$constructor("$ZodUnion", (inst, def) => {
993
993
  });
994
994
  };
995
995
  });
996
+ function handleExclusiveUnionResults(results, final, inst, ctx) {
997
+ const successes = results.filter((r) => r.issues.length === 0);
998
+ if (successes.length === 1) {
999
+ final.value = successes[0].value;
1000
+ return final;
1001
+ }
1002
+ if (successes.length === 0) {
1003
+ // No matches - same as regular union
1004
+ final.issues.push({
1005
+ code: "invalid_union",
1006
+ input: final.value,
1007
+ inst,
1008
+ errors: results.map((result) => result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
1009
+ });
1010
+ }
1011
+ else {
1012
+ // Multiple matches - exclusive union failure
1013
+ final.issues.push({
1014
+ code: "invalid_union",
1015
+ input: final.value,
1016
+ inst,
1017
+ errors: [],
1018
+ inclusive: false,
1019
+ });
1020
+ }
1021
+ return final;
1022
+ }
1023
+ exports.$ZodXor = core.$constructor("$ZodXor", (inst, def) => {
1024
+ exports.$ZodUnion.init(inst, def);
1025
+ def.inclusive = false;
1026
+ const single = def.options.length === 1;
1027
+ const first = def.options[0]._zod.run;
1028
+ inst._zod.parse = (payload, ctx) => {
1029
+ if (single) {
1030
+ return first(payload, ctx);
1031
+ }
1032
+ let async = false;
1033
+ const results = [];
1034
+ for (const option of def.options) {
1035
+ const result = option._zod.run({
1036
+ value: payload.value,
1037
+ issues: [],
1038
+ }, ctx);
1039
+ if (result instanceof Promise) {
1040
+ results.push(result);
1041
+ async = true;
1042
+ }
1043
+ else {
1044
+ results.push(result);
1045
+ }
1046
+ }
1047
+ if (!async)
1048
+ return handleExclusiveUnionResults(results, payload, inst, ctx);
1049
+ return Promise.all(results).then((results) => {
1050
+ return handleExclusiveUnionResults(results, payload, inst, ctx);
1051
+ });
1052
+ };
1053
+ });
996
1054
  exports.$ZodDiscriminatedUnion =
997
1055
  /*@__PURE__*/
998
1056
  core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
1057
+ def.inclusive = false;
999
1058
  exports.$ZodUnion.init(inst, def);
1000
1059
  const _super = inst._zod.parse;
1001
1060
  util.defineLazy(inst._zod, "propValues", () => {
@@ -1277,15 +1336,21 @@ exports.$ZodRecord = core.$constructor("$ZodRecord", (inst, def) => {
1277
1336
  throw new Error("Async schemas not supported in object keys currently");
1278
1337
  }
1279
1338
  if (keyResult.issues.length) {
1280
- payload.issues.push({
1281
- code: "invalid_key",
1282
- origin: "record",
1283
- issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
1284
- input: key,
1285
- path: [key],
1286
- inst,
1287
- });
1288
- payload.value[keyResult.value] = keyResult.value;
1339
+ if (def.mode === "loose") {
1340
+ // Pass through unchanged
1341
+ payload.value[key] = input[key];
1342
+ }
1343
+ else {
1344
+ // Default "strict" behavior: error on invalid key
1345
+ payload.issues.push({
1346
+ code: "invalid_key",
1347
+ origin: "record",
1348
+ issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
1349
+ input: key,
1350
+ path: [key],
1351
+ inst,
1352
+ });
1353
+ }
1289
1354
  continue;
1290
1355
  }
1291
1356
  const result = def.valueType._zod.run({ value: input[key], issues: [] }, ctx);
@@ -620,6 +620,7 @@ export type $InferUnionInput<T extends SomeType> = T extends any ? core.input<T>
620
620
  export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodTypeDef {
621
621
  type: "union";
622
622
  options: Options;
623
+ inclusive?: boolean;
623
624
  }
624
625
  type IsOptionalIn<T extends SomeType> = T extends OptionalInSchema ? true : false;
625
626
  type IsOptionalOut<T extends SomeType> = T extends OptionalOutSchema ? true : false;
@@ -637,6 +638,12 @@ export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
637
638
  _zod: $ZodUnionInternals<T>;
638
639
  }
639
640
  export declare const $ZodUnion: core.$constructor<$ZodUnion>;
641
+ export interface $ZodXorInternals<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionInternals<T> {
642
+ }
643
+ export interface $ZodXor<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodType<any, any, $ZodXorInternals<T>> {
644
+ _zod: $ZodXorInternals<T>;
645
+ }
646
+ export declare const $ZodXor: core.$constructor<$ZodXor>;
640
647
  export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionDef<Options> {
641
648
  discriminator: Disc;
642
649
  unionFallback?: boolean;
@@ -708,6 +715,8 @@ export interface $ZodRecordDef<Key extends $ZodRecordKey = $ZodRecordKey, Value
708
715
  type: "record";
709
716
  keyType: Key;
710
717
  valueType: Value;
718
+ /** @default "strict" - errors on keys not matching keyType. "loose" passes through non-matching keys unchanged. */
719
+ mode?: "strict" | "loose";
711
720
  }
712
721
  export type $InferZodRecordOutput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType> = Key extends $partial ? Partial<Record<core.output<Key>, core.output<Value>>> : Record<core.output<Key>, core.output<Value>>;
713
722
  export type $InferZodRecordInput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType> = Key extends $partial ? Partial<Record<core.input<Key>, core.input<Value>>> : Record<core.input<Key>, core.input<Value>>;
@@ -620,6 +620,7 @@ export type $InferUnionInput<T extends SomeType> = T extends any ? core.input<T>
620
620
  export interface $ZodUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodTypeDef {
621
621
  type: "union";
622
622
  options: Options;
623
+ inclusive?: boolean;
623
624
  }
624
625
  type IsOptionalIn<T extends SomeType> = T extends OptionalInSchema ? true : false;
625
626
  type IsOptionalOut<T extends SomeType> = T extends OptionalOutSchema ? true : false;
@@ -637,6 +638,12 @@ export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
637
638
  _zod: $ZodUnionInternals<T>;
638
639
  }
639
640
  export declare const $ZodUnion: core.$constructor<$ZodUnion>;
641
+ export interface $ZodXorInternals<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionInternals<T> {
642
+ }
643
+ export interface $ZodXor<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodType<any, any, $ZodXorInternals<T>> {
644
+ _zod: $ZodXorInternals<T>;
645
+ }
646
+ export declare const $ZodXor: core.$constructor<$ZodXor>;
640
647
  export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionDef<Options> {
641
648
  discriminator: Disc;
642
649
  unionFallback?: boolean;
@@ -708,6 +715,8 @@ export interface $ZodRecordDef<Key extends $ZodRecordKey = $ZodRecordKey, Value
708
715
  type: "record";
709
716
  keyType: Key;
710
717
  valueType: Value;
718
+ /** @default "strict" - errors on keys not matching keyType. "loose" passes through non-matching keys unchanged. */
719
+ mode?: "strict" | "loose";
711
720
  }
712
721
  export type $InferZodRecordOutput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType> = Key extends $partial ? Partial<Record<core.output<Key>, core.output<Value>>> : Record<core.output<Key>, core.output<Value>>;
713
722
  export type $InferZodRecordInput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType> = Key extends $partial ? Partial<Record<core.input<Key>, core.input<Value>>> : Record<core.input<Key>, core.input<Value>>;
@@ -962,9 +962,68 @@ export const $ZodUnion = /*@__PURE__*/ core.$constructor("$ZodUnion", (inst, def
962
962
  });
963
963
  };
964
964
  });
965
+ function handleExclusiveUnionResults(results, final, inst, ctx) {
966
+ const successes = results.filter((r) => r.issues.length === 0);
967
+ if (successes.length === 1) {
968
+ final.value = successes[0].value;
969
+ return final;
970
+ }
971
+ if (successes.length === 0) {
972
+ // No matches - same as regular union
973
+ final.issues.push({
974
+ code: "invalid_union",
975
+ input: final.value,
976
+ inst,
977
+ errors: results.map((result) => result.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config()))),
978
+ });
979
+ }
980
+ else {
981
+ // Multiple matches - exclusive union failure
982
+ final.issues.push({
983
+ code: "invalid_union",
984
+ input: final.value,
985
+ inst,
986
+ errors: [],
987
+ inclusive: false,
988
+ });
989
+ }
990
+ return final;
991
+ }
992
+ export const $ZodXor = /*@__PURE__*/ core.$constructor("$ZodXor", (inst, def) => {
993
+ $ZodUnion.init(inst, def);
994
+ def.inclusive = false;
995
+ const single = def.options.length === 1;
996
+ const first = def.options[0]._zod.run;
997
+ inst._zod.parse = (payload, ctx) => {
998
+ if (single) {
999
+ return first(payload, ctx);
1000
+ }
1001
+ let async = false;
1002
+ const results = [];
1003
+ for (const option of def.options) {
1004
+ const result = option._zod.run({
1005
+ value: payload.value,
1006
+ issues: [],
1007
+ }, ctx);
1008
+ if (result instanceof Promise) {
1009
+ results.push(result);
1010
+ async = true;
1011
+ }
1012
+ else {
1013
+ results.push(result);
1014
+ }
1015
+ }
1016
+ if (!async)
1017
+ return handleExclusiveUnionResults(results, payload, inst, ctx);
1018
+ return Promise.all(results).then((results) => {
1019
+ return handleExclusiveUnionResults(results, payload, inst, ctx);
1020
+ });
1021
+ };
1022
+ });
965
1023
  export const $ZodDiscriminatedUnion =
966
1024
  /*@__PURE__*/
967
1025
  core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
1026
+ def.inclusive = false;
968
1027
  $ZodUnion.init(inst, def);
969
1028
  const _super = inst._zod.parse;
970
1029
  util.defineLazy(inst._zod, "propValues", () => {
@@ -1246,15 +1305,21 @@ export const $ZodRecord = /*@__PURE__*/ core.$constructor("$ZodRecord", (inst, d
1246
1305
  throw new Error("Async schemas not supported in object keys currently");
1247
1306
  }
1248
1307
  if (keyResult.issues.length) {
1249
- payload.issues.push({
1250
- code: "invalid_key",
1251
- origin: "record",
1252
- issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
1253
- input: key,
1254
- path: [key],
1255
- inst,
1256
- });
1257
- payload.value[keyResult.value] = keyResult.value;
1308
+ if (def.mode === "loose") {
1309
+ // Pass through unchanged
1310
+ payload.value[key] = input[key];
1311
+ }
1312
+ else {
1313
+ // Default "strict" behavior: error on invalid key
1314
+ payload.issues.push({
1315
+ code: "invalid_key",
1316
+ origin: "record",
1317
+ issues: keyResult.issues.map((iss) => util.finalizeIssue(iss, ctx, core.config())),
1318
+ input: key,
1319
+ path: [key],
1320
+ inst,
1321
+ });
1322
+ }
1258
1323
  continue;
1259
1324
  }
1260
1325
  const result = def.valueType._zod.run({ value: input[key], issues: [] }, ctx);
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
- minor: 1,
7
- patch: 13,
6
+ minor: 2,
7
+ patch: 0,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export declare const version: {
2
2
  readonly major: 4;
3
- readonly minor: 1;
3
+ readonly minor: 2;
4
4
  readonly patch: number;
5
5
  };
@@ -1,5 +1,5 @@
1
1
  export declare const version: {
2
2
  readonly major: 4;
3
- readonly minor: 1;
3
+ readonly minor: 2;
4
4
  readonly patch: number;
5
5
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
- minor: 1,
4
- patch: 13,
3
+ minor: 2,
4
+ patch: 0,
5
5
  };
@@ -26,7 +26,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.coerce = exports.ZodMiniISODuration = exports.ZodMiniISOTime = exports.ZodMiniISODate = exports.ZodMiniISODateTime = exports.iso = 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;
29
+ exports.coerce = exports.ZodMiniISODuration = exports.ZodMiniISOTime = exports.ZodMiniISODate = exports.ZodMiniISODateTime = exports.iso = exports.locales = 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;
30
30
  exports.core = __importStar(require("../core/index.cjs"));
31
31
  __exportStar(require("./parse.cjs"), exports);
32
32
  __exportStar(require("./schemas.cjs"), exports);
@@ -44,10 +44,11 @@ Object.defineProperty(exports, "treeifyError", { enumerable: true, get: function
44
44
  Object.defineProperty(exports, "prettifyError", { enumerable: true, get: function () { return index_js_1.prettifyError; } });
45
45
  Object.defineProperty(exports, "formatError", { enumerable: true, get: function () { return index_js_1.formatError; } });
46
46
  Object.defineProperty(exports, "flattenError", { enumerable: true, get: function () { return index_js_1.flattenError; } });
47
- Object.defineProperty(exports, "toJSONSchema", { enumerable: true, get: function () { return index_js_1.toJSONSchema; } });
48
47
  Object.defineProperty(exports, "TimePrecision", { enumerable: true, get: function () { return index_js_1.TimePrecision; } });
49
48
  Object.defineProperty(exports, "util", { enumerable: true, get: function () { return index_js_1.util; } });
50
49
  Object.defineProperty(exports, "NEVER", { enumerable: true, get: function () { return index_js_1.NEVER; } });
50
+ var json_schema_processors_js_1 = require("../core/json-schema-processors.cjs");
51
+ Object.defineProperty(exports, "toJSONSchema", { enumerable: true, get: function () { return json_schema_processors_js_1.toJSONSchema; } });
51
52
  exports.locales = __importStar(require("../locales/index.cjs"));
52
53
  /** A special constant with type `never` */
53
54
  // export const NEVER = {} as never;
@@ -3,7 +3,8 @@ export * from "./parse.cjs";
3
3
  export * from "./schemas.cjs";
4
4
  export * from "./checks.cjs";
5
5
  export type { infer, output, input } from "../core/index.cjs";
6
- export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.cjs";
6
+ export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.cjs";
7
+ export { toJSONSchema } from "../core/json-schema-processors.cjs";
7
8
  export * as locales from "../locales/index.cjs";
8
9
  /** A special constant with type `never` */
9
10
  export * as iso from "./iso.cjs";
@@ -3,7 +3,8 @@ export * from "./parse.js";
3
3
  export * from "./schemas.js";
4
4
  export * from "./checks.js";
5
5
  export type { infer, output, input } from "../core/index.js";
6
- export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.js";
6
+ export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.js";
7
+ export { toJSONSchema } from "../core/json-schema-processors.js";
7
8
  export * as locales from "../locales/index.js";
8
9
  /** A special constant with type `never` */
9
10
  export * as iso from "./iso.js";
@@ -2,7 +2,8 @@ export * as core from "../core/index.js";
2
2
  export * from "./parse.js";
3
3
  export * from "./schemas.js";
4
4
  export * from "./checks.js";
5
- export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, toJSONSchema, TimePrecision, util, NEVER, } from "../core/index.js";
5
+ export { globalRegistry, registry, config, $output, $input, $brand, clone, regexes, treeifyError, prettifyError, formatError, flattenError, TimePrecision, util, NEVER, } from "../core/index.js";
6
+ export { toJSONSchema } from "../core/json-schema-processors.js";
6
7
  export * as locales from "../locales/index.js";
7
8
  /** A special constant with type `never` */
8
9
  // export const NEVER = {} as never;
@@ -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.ZodMiniTransform = exports.ZodMiniFile = exports.ZodMiniLiteral = exports.ZodMiniEnum = exports.ZodMiniSet = exports.ZodMiniMap = exports.ZodMiniRecord = exports.ZodMiniTuple = exports.ZodMiniIntersection = exports.ZodMiniDiscriminatedUnion = exports.ZodMiniUnion = exports.ZodMiniObject = exports.ZodMiniArray = exports.ZodMiniDate = exports.ZodMiniVoid = exports.ZodMiniNever = exports.ZodMiniUnknown = exports.ZodMiniAny = exports.ZodMiniNull = exports.ZodMiniUndefined = exports.ZodMiniSymbol = exports.ZodMiniBigIntFormat = exports.ZodMiniBigInt = exports.ZodMiniBoolean = exports.ZodMiniNumberFormat = exports.ZodMiniNumber = exports.ZodMiniCustomStringFormat = exports.ZodMiniJWT = exports.ZodMiniE164 = exports.ZodMiniBase64URL = exports.ZodMiniBase64 = exports.ZodMiniMAC = exports.ZodMiniCIDRv6 = exports.ZodMiniCIDRv4 = exports.ZodMiniIPv6 = exports.ZodMiniIPv4 = exports.ZodMiniKSUID = exports.ZodMiniXID = exports.ZodMiniULID = exports.ZodMiniCUID2 = exports.ZodMiniCUID = exports.ZodMiniNanoID = exports.ZodMiniEmoji = exports.ZodMiniURL = exports.ZodMiniUUID = exports.ZodMiniGUID = exports.ZodMiniEmail = exports.ZodMiniStringFormat = exports.ZodMiniString = exports.ZodMiniType = void 0;
27
- exports.ZodMiniFunction = exports.stringbool = exports.meta = exports.describe = exports.ZodMiniCustom = exports.ZodMiniPromise = exports.ZodMiniLazy = exports.ZodMiniTemplateLiteral = exports.ZodMiniReadonly = exports.ZodMiniCodec = exports.ZodMiniPipe = exports.ZodMiniNaN = exports.ZodMiniCatch = exports.ZodMiniSuccess = exports.ZodMiniNonOptional = exports.ZodMiniPrefault = exports.ZodMiniDefault = exports.ZodMiniNullable = exports.ZodMiniOptional = void 0;
26
+ exports.ZodMiniFile = exports.ZodMiniLiteral = exports.ZodMiniEnum = exports.ZodMiniSet = exports.ZodMiniMap = exports.ZodMiniRecord = exports.ZodMiniTuple = exports.ZodMiniIntersection = exports.ZodMiniDiscriminatedUnion = exports.ZodMiniXor = exports.ZodMiniUnion = exports.ZodMiniObject = exports.ZodMiniArray = exports.ZodMiniDate = exports.ZodMiniVoid = exports.ZodMiniNever = exports.ZodMiniUnknown = exports.ZodMiniAny = exports.ZodMiniNull = exports.ZodMiniUndefined = exports.ZodMiniSymbol = exports.ZodMiniBigIntFormat = exports.ZodMiniBigInt = exports.ZodMiniBoolean = exports.ZodMiniNumberFormat = exports.ZodMiniNumber = exports.ZodMiniCustomStringFormat = exports.ZodMiniJWT = exports.ZodMiniE164 = exports.ZodMiniBase64URL = exports.ZodMiniBase64 = exports.ZodMiniMAC = exports.ZodMiniCIDRv6 = exports.ZodMiniCIDRv4 = exports.ZodMiniIPv6 = exports.ZodMiniIPv4 = exports.ZodMiniKSUID = exports.ZodMiniXID = exports.ZodMiniULID = exports.ZodMiniCUID2 = exports.ZodMiniCUID = exports.ZodMiniNanoID = exports.ZodMiniEmoji = exports.ZodMiniURL = exports.ZodMiniUUID = exports.ZodMiniGUID = exports.ZodMiniEmail = exports.ZodMiniStringFormat = exports.ZodMiniString = exports.ZodMiniType = void 0;
27
+ exports.ZodMiniFunction = exports.stringbool = exports.meta = exports.describe = exports.ZodMiniCustom = exports.ZodMiniPromise = exports.ZodMiniLazy = exports.ZodMiniTemplateLiteral = exports.ZodMiniReadonly = exports.ZodMiniCodec = exports.ZodMiniPipe = exports.ZodMiniNaN = exports.ZodMiniCatch = exports.ZodMiniSuccess = exports.ZodMiniNonOptional = exports.ZodMiniPrefault = exports.ZodMiniDefault = exports.ZodMiniNullable = exports.ZodMiniOptional = exports.ZodMiniTransform = void 0;
28
28
  exports.string = string;
29
29
  exports.email = email;
30
30
  exports.guid = guid;
@@ -86,11 +86,13 @@ exports.partial = partial;
86
86
  exports.required = required;
87
87
  exports.catchall = catchall;
88
88
  exports.union = union;
89
+ exports.xor = xor;
89
90
  exports.discriminatedUnion = discriminatedUnion;
90
91
  exports.intersection = intersection;
91
92
  exports.tuple = tuple;
92
93
  exports.record = record;
93
94
  exports.partialRecord = partialRecord;
95
+ exports.looseRecord = looseRecord;
94
96
  exports.map = map;
95
97
  exports.set = set;
96
98
  exports.enum = _enum;
@@ -542,6 +544,21 @@ function union(options, params) {
542
544
  ...index_js_1.util.normalizeParams(params),
543
545
  });
544
546
  }
547
+ exports.ZodMiniXor = core.$constructor("ZodMiniXor", (inst, def) => {
548
+ exports.ZodMiniUnion.init(inst, def);
549
+ core.$ZodXor.init(inst, def);
550
+ });
551
+ /** Creates an exclusive union (XOR) where exactly one option must match.
552
+ * Unlike regular unions that succeed when any option matches, xor fails if
553
+ * zero or more than one option matches the input. */
554
+ function xor(options, params) {
555
+ return new exports.ZodMiniXor({
556
+ type: "union",
557
+ options: options,
558
+ inclusive: false,
559
+ ...index_js_1.util.normalizeParams(params),
560
+ });
561
+ }
545
562
  exports.ZodMiniDiscriminatedUnion = core.$constructor("ZodMiniDiscriminatedUnion", (inst, def) => {
546
563
  core.$ZodDiscriminatedUnion.init(inst, def);
547
564
  exports.ZodMiniType.init(inst, def);
@@ -602,6 +619,15 @@ function partialRecord(keyType, valueType, params) {
602
619
  ...index_js_1.util.normalizeParams(params),
603
620
  });
604
621
  }
622
+ function looseRecord(keyType, valueType, params) {
623
+ return new exports.ZodMiniRecord({
624
+ type: "record",
625
+ keyType,
626
+ valueType: valueType,
627
+ mode: "loose",
628
+ ...index_js_1.util.normalizeParams(params),
629
+ });
630
+ }
605
631
  exports.ZodMiniMap = core.$constructor("ZodMiniMap", (inst, def) => {
606
632
  core.$ZodMap.init(inst, def);
607
633
  exports.ZodMiniType.init(inst, def);
@@ -230,6 +230,13 @@ export interface ZodMiniUnion<T extends readonly SomeType[] = readonly core.$Zod
230
230
  }
231
231
  export declare const ZodMiniUnion: core.$constructor<ZodMiniUnion>;
232
232
  export declare function union<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodMiniUnion<T>;
233
+ export interface ZodMiniXor<T extends readonly SomeType[] = readonly core.$ZodType[]> extends _ZodMiniType<core.$ZodXorInternals<T>> {
234
+ }
235
+ export declare const ZodMiniXor: core.$constructor<ZodMiniXor>;
236
+ /** Creates an exclusive union (XOR) where exactly one option must match.
237
+ * Unlike regular unions that succeed when any option matches, xor fails if
238
+ * zero or more than one option matches the input. */
239
+ export declare function xor<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodXorParams): ZodMiniXor<T>;
233
240
  export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodMiniUnion<Options> {
234
241
  _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
235
242
  }
@@ -250,6 +257,7 @@ export interface ZodMiniRecord<Key extends core.$ZodRecordKey = core.$ZodRecordK
250
257
  export declare const ZodMiniRecord: core.$constructor<ZodMiniRecord>;
251
258
  export declare function record<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key, Value>;
252
259
  export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key & core.$partial, Value>;
260
+ export declare function looseRecord<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key, Value>;
253
261
  export interface ZodMiniMap<Key extends SomeType = core.$ZodType, Value extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodMapInternals<Key, Value>> {
254
262
  }
255
263
  export declare const ZodMiniMap: core.$constructor<ZodMiniMap>;
@@ -230,6 +230,13 @@ export interface ZodMiniUnion<T extends readonly SomeType[] = readonly core.$Zod
230
230
  }
231
231
  export declare const ZodMiniUnion: core.$constructor<ZodMiniUnion>;
232
232
  export declare function union<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodMiniUnion<T>;
233
+ export interface ZodMiniXor<T extends readonly SomeType[] = readonly core.$ZodType[]> extends _ZodMiniType<core.$ZodXorInternals<T>> {
234
+ }
235
+ export declare const ZodMiniXor: core.$constructor<ZodMiniXor>;
236
+ /** Creates an exclusive union (XOR) where exactly one option must match.
237
+ * Unlike regular unions that succeed when any option matches, xor fails if
238
+ * zero or more than one option matches the input. */
239
+ export declare function xor<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodXorParams): ZodMiniXor<T>;
233
240
  export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodMiniUnion<Options> {
234
241
  _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
235
242
  }
@@ -250,6 +257,7 @@ export interface ZodMiniRecord<Key extends core.$ZodRecordKey = core.$ZodRecordK
250
257
  export declare const ZodMiniRecord: core.$constructor<ZodMiniRecord>;
251
258
  export declare function record<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key, Value>;
252
259
  export declare function partialRecord<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key & core.$partial, Value>;
260
+ export declare function looseRecord<Key extends core.$ZodRecordKey, Value extends SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodRecordParams): ZodMiniRecord<Key, Value>;
253
261
  export interface ZodMiniMap<Key extends SomeType = core.$ZodType, Value extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodMapInternals<Key, Value>> {
254
262
  }
255
263
  export declare const ZodMiniMap: core.$constructor<ZodMiniMap>;
@@ -420,6 +420,21 @@ export function union(options, params) {
420
420
  ...util.normalizeParams(params),
421
421
  });
422
422
  }
423
+ export const ZodMiniXor = /*@__PURE__*/ core.$constructor("ZodMiniXor", (inst, def) => {
424
+ ZodMiniUnion.init(inst, def);
425
+ core.$ZodXor.init(inst, def);
426
+ });
427
+ /** Creates an exclusive union (XOR) where exactly one option must match.
428
+ * Unlike regular unions that succeed when any option matches, xor fails if
429
+ * zero or more than one option matches the input. */
430
+ export function xor(options, params) {
431
+ return new ZodMiniXor({
432
+ type: "union",
433
+ options: options,
434
+ inclusive: false,
435
+ ...util.normalizeParams(params),
436
+ });
437
+ }
423
438
  export const ZodMiniDiscriminatedUnion = /*@__PURE__*/ core.$constructor("ZodMiniDiscriminatedUnion", (inst, def) => {
424
439
  core.$ZodDiscriminatedUnion.init(inst, def);
425
440
  ZodMiniType.init(inst, def);
@@ -480,6 +495,15 @@ export function partialRecord(keyType, valueType, params) {
480
495
  ...util.normalizeParams(params),
481
496
  });
482
497
  }
498
+ export function looseRecord(keyType, valueType, params) {
499
+ return new ZodMiniRecord({
500
+ type: "record",
501
+ keyType,
502
+ valueType: valueType,
503
+ mode: "loose",
504
+ ...util.normalizeParams(params),
505
+ });
506
+ }
483
507
  export const ZodMiniMap = /*@__PURE__*/ core.$constructor("ZodMiniMap", (inst, def) => {
484
508
  core.$ZodMap.init(inst, def);
485
509
  ZodMiniType.init(inst, def);