zod 3.25.44 → 3.25.46

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.
@@ -935,6 +935,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
935
935
  const propValues = {};
936
936
  for (const option of def.options) {
937
937
  const pv = option._zod.propValues;
938
+ console.dir(pv, { depth: null });
938
939
  if (!pv || Object.keys(pv).length === 0)
939
940
  throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(option)}"`);
940
941
  for (const [k, v] of Object.entries(pv)) {
@@ -952,6 +953,8 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
952
953
  const map = new Map();
953
954
  for (const o of opts) {
954
955
  const values = o._zod.propValues[def.discriminator];
956
+ if (!values || values.size === 0)
957
+ throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
955
958
  for (const v of values) {
956
959
  if (map.has(v)) {
957
960
  throw new Error(`Duplicate discriminator value "${String(v)}"`);
@@ -1338,10 +1341,7 @@ function handleSetResult(result, final) {
1338
1341
  }
1339
1342
  exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
1340
1343
  exports.$ZodType.init(inst, def);
1341
- const numericValues = Object.values(def.entries).filter((v) => typeof v === "number");
1342
- const values = Object.entries(def.entries)
1343
- .filter(([k, _]) => numericValues.indexOf(+k) === -1)
1344
- .map(([_, v]) => v);
1344
+ const values = util.getEnumValues(def.entries);
1345
1345
  inst._zod.values = new Set(values);
1346
1346
  inst._zod.pattern = new RegExp(`^(${values
1347
1347
  .filter((k) => util.propertyKeyTypes.has(typeof k))
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JSONSchemaGenerator = void 0;
4
4
  exports.toJSONSchema = toJSONSchema;
5
5
  const registries_js_1 = require("./registries.js");
6
+ const util_js_1 = require("./util.js");
6
7
  class JSONSchemaGenerator {
7
8
  constructor(params) {
8
9
  this.counter = 0;
@@ -322,7 +323,13 @@ class JSONSchemaGenerator {
322
323
  }
323
324
  case "enum": {
324
325
  const json = _json;
325
- json.enum = Object.values(def.entries);
326
+ const values = (0, util_js_1.getEnumValues)(def.entries);
327
+ // Number enums can have both string and number values
328
+ if (values.every((v) => typeof v === "number"))
329
+ json.type = "number";
330
+ if (values.every((v) => typeof v === "string"))
331
+ json.type = "string";
332
+ json.enum = values;
326
333
  break;
327
334
  }
328
335
  case "literal": {
@@ -6,7 +6,7 @@ exports.assertNotEqual = assertNotEqual;
6
6
  exports.assertIs = assertIs;
7
7
  exports.assertNever = assertNever;
8
8
  exports.assert = assert;
9
- exports.getValidEnumValues = getValidEnumValues;
9
+ exports.getEnumValues = getEnumValues;
10
10
  exports.joinValues = joinValues;
11
11
  exports.jsonStringifyReplacer = jsonStringifyReplacer;
12
12
  exports.cached = cached;
@@ -54,13 +54,12 @@ function assertNever(_x) {
54
54
  throw new Error();
55
55
  }
56
56
  function assert(_) { }
57
- function getValidEnumValues(obj) {
58
- const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
59
- const filtered = {};
60
- for (const k of validKeys) {
61
- filtered[k] = obj[k];
62
- }
63
- return Object.values(filtered);
57
+ function getEnumValues(entries) {
58
+ const numericValues = Object.values(entries).filter((v) => typeof v === "number");
59
+ const values = Object.entries(entries)
60
+ .filter(([k, _]) => numericValues.indexOf(+k) === -1)
61
+ .map(([_, v]) => v);
62
+ return values;
64
63
  }
65
64
  function joinValues(array, separator = "|") {
66
65
  return array.map((val) => stringifyPrimitive(val)).join(separator);
@@ -904,6 +904,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
904
904
  const propValues = {};
905
905
  for (const option of def.options) {
906
906
  const pv = option._zod.propValues;
907
+ console.dir(pv, { depth: null });
907
908
  if (!pv || Object.keys(pv).length === 0)
908
909
  throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(option)}"`);
909
910
  for (const [k, v] of Object.entries(pv)) {
@@ -921,6 +922,8 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
921
922
  const map = new Map();
922
923
  for (const o of opts) {
923
924
  const values = o._zod.propValues[def.discriminator];
925
+ if (!values || values.size === 0)
926
+ throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
924
927
  for (const v of values) {
925
928
  if (map.has(v)) {
926
929
  throw new Error(`Duplicate discriminator value "${String(v)}"`);
@@ -1307,10 +1310,7 @@ function handleSetResult(result, final) {
1307
1310
  }
1308
1311
  export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def) => {
1309
1312
  $ZodType.init(inst, def);
1310
- const numericValues = Object.values(def.entries).filter((v) => typeof v === "number");
1311
- const values = Object.entries(def.entries)
1312
- .filter(([k, _]) => numericValues.indexOf(+k) === -1)
1313
- .map(([_, v]) => v);
1313
+ const values = util.getEnumValues(def.entries);
1314
1314
  inst._zod.values = new Set(values);
1315
1315
  inst._zod.pattern = new RegExp(`^(${values
1316
1316
  .filter((k) => util.propertyKeyTypes.has(typeof k))
@@ -1,4 +1,5 @@
1
1
  import { $ZodRegistry, globalRegistry } from "./registries.js";
2
+ import { getEnumValues } from "./util.js";
2
3
  export class JSONSchemaGenerator {
3
4
  constructor(params) {
4
5
  this.counter = 0;
@@ -318,7 +319,13 @@ export class JSONSchemaGenerator {
318
319
  }
319
320
  case "enum": {
320
321
  const json = _json;
321
- json.enum = Object.values(def.entries);
322
+ const values = getEnumValues(def.entries);
323
+ // Number enums can have both string and number values
324
+ if (values.every((v) => typeof v === "number"))
325
+ json.type = "number";
326
+ if (values.every((v) => typeof v === "string"))
327
+ json.type = "string";
328
+ json.enum = values;
322
329
  break;
323
330
  }
324
331
  case "literal": {
@@ -10,13 +10,12 @@ export function assertNever(_x) {
10
10
  throw new Error();
11
11
  }
12
12
  export function assert(_) { }
13
- export function getValidEnumValues(obj) {
14
- const validKeys = Object.keys(obj).filter((k) => typeof obj[obj[k]] !== "number");
15
- const filtered = {};
16
- for (const k of validKeys) {
17
- filtered[k] = obj[k];
18
- }
19
- return Object.values(filtered);
13
+ export function getEnumValues(entries) {
14
+ const numericValues = Object.values(entries).filter((v) => typeof v === "number");
15
+ const values = Object.entries(entries)
16
+ .filter(([k, _]) => numericValues.indexOf(+k) === -1)
17
+ .map(([_, v]) => v);
18
+ return values;
20
19
  }
21
20
  export function joinValues(array, separator = "|") {
22
21
  return array.map((val) => stringifyPrimitive(val)).join(separator);
@@ -49,7 +49,9 @@ export interface $ZodTypeInternals<out O = unknown, out I = unknown> {
49
49
  /** @internal Indicates that a schema output type should be considered optional inside objects.
50
50
  * @default Required
51
51
  */
52
+ /** @internal */
52
53
  optin?: "optional" | undefined;
54
+ /** @internal */
53
55
  optout?: "optional" | undefined;
54
56
  /** @internal The set of literal values that will pass validation. Must be an exhaustive set. Used to determine optionality in z.record().
55
57
  *
@@ -515,7 +517,7 @@ export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $Zod
515
517
  }
516
518
  export interface $ZodObjectInternals<
517
519
  /** @ts-ignore Cast variance */
518
- out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends $ZodTypeInternals<any, any> {
520
+ out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends $ZodTypeInternals<$InferObjectOutputFallback<Shape, Config["out"]>, $InferObjectInputFallback<Shape, Config["in"]>> {
519
521
  def: $ZodObjectDef<Shape>;
520
522
  config: Config;
521
523
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
@@ -534,6 +536,12 @@ type OptionalInSchema = {
534
536
  optin: "optional";
535
537
  };
536
538
  };
539
+ export type $InferObjectOutputFallback<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T ? object : keyof (T & Extra) extends never ? Record<string, never> : util.Prettify<{
540
+ -readonly [k in keyof T]: core.output<T[k]>;
541
+ } & Extra>;
542
+ export type $InferObjectInputFallback<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T ? object : keyof (T & Extra) extends never ? Record<string, never> : util.Prettify<{
543
+ -readonly [k in keyof T]: core.input<T[k]>;
544
+ } & Extra>;
537
545
  export type $InferObjectOutput<T extends $ZodLooseShape, Extra extends Record<string, unknown>> = string extends keyof T ? object : keyof (T & Extra) extends never ? Record<string, never> : util.Prettify<{
538
546
  -readonly [k in keyof T as T[k] extends OptionalOutSchema ? never : k]: core.output<T[k]>;
539
547
  } & {
@@ -655,8 +663,8 @@ export interface $ZodRecordDef extends $ZodTypeDef {
655
663
  keyType: $ZodRecordKey;
656
664
  valueType: $ZodType;
657
665
  }
658
- type $InferZodRecordOutput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType> = undefined extends Key["_zod"]["values"] ? string extends Key["_zod"]["output"] ? Record<Key["_zod"]["output"], core.output<Value>> : number extends Key["_zod"]["output"] ? Record<Key["_zod"]["output"], core.output<Value>> : symbol extends Key["_zod"]["output"] ? Record<Key["_zod"]["output"], core.output<Value>> : Partial<Record<Key["_zod"]["output"], core.output<Value>>> : Record<Key["_zod"]["output"], core.output<Value>>;
659
- type $InferZodRecordInput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType> = undefined extends Key["_zod"]["values"] ? string extends Key["_zod"]["input"] ? Record<Key["_zod"]["input"], Value["_zod"]["input"]> : number extends Key["_zod"]["input"] ? Record<Key["_zod"]["input"], Value["_zod"]["input"]> : symbol extends Key["_zod"]["input"] ? Record<Key["_zod"]["input"], Value["_zod"]["input"]> : Partial<Record<Key["_zod"]["input"], Value["_zod"]["input"]>> : Record<Key["_zod"]["input"], Value["_zod"]["input"]>;
666
+ export type $InferZodRecordOutput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType> = undefined extends Key["_zod"]["values"] ? string extends core.output<Key> ? Record<core.output<Key>, core.output<Value>> : number extends core.output<Key> ? Record<core.output<Key>, core.output<Value>> : symbol extends core.output<Key> ? Record<core.output<Key>, core.output<Value>> : Partial<Record<core.output<Key>, core.output<Value>>> : Record<core.output<Key>, core.output<Value>>;
667
+ export type $InferZodRecordInput<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType> = undefined extends Key["_zod"]["values"] ? string extends core.input<Key> ? Record<core.input<Key>, core.input<Value>> : number extends core.input<Key> ? Record<core.input<Key>, core.input<Value>> : symbol extends core.input<Key> ? Record<core.input<Key>, core.input<Value>> : Partial<Record<core.input<Key>, core.input<Value>>> : Record<core.input<Key>, core.input<Value>>;
660
668
  export interface $ZodRecordInternals<Key extends $ZodRecordKey = $ZodRecordKey, Value extends $ZodType = $ZodType> extends $ZodTypeInternals<$InferZodRecordOutput<Key, Value>, $InferZodRecordInput<Key, Value>> {
661
669
  def: $ZodRecordDef;
662
670
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey<Record<PropertyKey, unknown>>;
@@ -111,7 +111,7 @@ export declare function assertNotEqual<A, B>(val: AssertNotEqual<A, B>): AssertN
111
111
  export declare function assertIs<T>(_arg: T): void;
112
112
  export declare function assertNever(_x: never): never;
113
113
  export declare function assert<T>(_: any): asserts _ is T;
114
- export declare function getValidEnumValues(obj: any): any;
114
+ export declare function getEnumValues(entries: EnumLike): EnumValue[];
115
115
  export declare function joinValues<T extends Primitive[]>(array: T, separator?: string): string;
116
116
  export declare function jsonStringifyReplacer(_: string, value: any): any;
117
117
  export declare function cached<T>(getter: () => T): {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.25.44",
3
+ "version": "3.25.46",
4
4
  "type": "module",
5
5
  "author": "Colin McDonnell <zod@colinhacks.com>",
6
6
  "description": "TypeScript-first schema declaration and validation library with static type inference",