zod 4.1.0-canary.20250723T222937 → 4.1.0-canary.20250725T001018

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 (58) hide show
  1. package/package.json +1 -1
  2. package/src/v3/types.ts +3 -1
  3. package/src/v4/classic/errors.ts +9 -2
  4. package/src/v4/classic/schemas.ts +10 -7
  5. package/src/v4/classic/tests/error-utils.test.ts +43 -0
  6. package/src/v4/classic/tests/file.test.ts +5 -2
  7. package/src/v4/classic/tests/partial.test.ts +193 -0
  8. package/src/v4/classic/tests/pickomit.test.ts +5 -5
  9. package/src/v4/classic/tests/preprocess.test.ts +4 -15
  10. package/src/v4/classic/tests/record.test.ts +15 -1
  11. package/src/v4/classic/tests/recursive-types.test.ts +67 -0
  12. package/src/v4/classic/tests/string.test.ts +77 -0
  13. package/src/v4/classic/tests/to-json-schema.test.ts +1 -0
  14. package/src/v4/classic/tests/transform.test.ts +104 -0
  15. package/src/v4/classic/tests/union.test.ts +90 -3
  16. package/src/v4/core/checks.ts +2 -2
  17. package/src/v4/core/errors.ts +8 -15
  18. package/src/v4/core/registries.ts +3 -2
  19. package/src/v4/core/schemas.ts +92 -94
  20. package/src/v4/core/tests/extend.test.ts +18 -0
  21. package/src/v4/core/to-json-schema.ts +1 -0
  22. package/src/v4/core/util.ts +135 -98
  23. package/src/v4/core/versions.ts +1 -1
  24. package/src/v4/mini/schemas.ts +3 -1
  25. package/v3/types.cjs +2 -0
  26. package/v3/types.d.cts +4 -1
  27. package/v3/types.d.ts +4 -1
  28. package/v3/types.js +2 -0
  29. package/v4/classic/errors.cjs +9 -2
  30. package/v4/classic/errors.js +9 -2
  31. package/v4/classic/schemas.cjs +5 -3
  32. package/v4/classic/schemas.d.cts +2 -1
  33. package/v4/classic/schemas.d.ts +2 -1
  34. package/v4/classic/schemas.js +5 -3
  35. package/v4/core/checks.d.cts +2 -2
  36. package/v4/core/checks.d.ts +2 -2
  37. package/v4/core/errors.cjs +4 -9
  38. package/v4/core/errors.d.cts +4 -6
  39. package/v4/core/errors.d.ts +4 -6
  40. package/v4/core/errors.js +4 -9
  41. package/v4/core/registries.cjs +2 -1
  42. package/v4/core/registries.d.cts +1 -1
  43. package/v4/core/registries.d.ts +1 -1
  44. package/v4/core/registries.js +2 -1
  45. package/v4/core/schemas.cjs +50 -87
  46. package/v4/core/schemas.d.cts +8 -3
  47. package/v4/core/schemas.d.ts +8 -3
  48. package/v4/core/schemas.js +50 -87
  49. package/v4/core/to-json-schema.cjs +1 -0
  50. package/v4/core/to-json-schema.js +1 -0
  51. package/v4/core/util.cjs +123 -97
  52. package/v4/core/util.d.cts +2 -0
  53. package/v4/core/util.d.ts +2 -0
  54. package/v4/core/util.js +121 -97
  55. package/v4/core/versions.cjs +1 -1
  56. package/v4/core/versions.js +1 -1
  57. package/v4/mini/schemas.cjs +3 -1
  58. package/v4/mini/schemas.js +3 -1
package/v4/core/util.js CHANGED
@@ -83,6 +83,17 @@ export function assignProp(target, prop, value) {
83
83
  configurable: true,
84
84
  });
85
85
  }
86
+ export function mergeDefs(...defs) {
87
+ const mergedDescriptors = {};
88
+ for (const def of defs) {
89
+ const descriptors = Object.getOwnPropertyDescriptors(def);
90
+ Object.assign(mergedDescriptors, descriptors);
91
+ }
92
+ return Object.defineProperties({}, mergedDescriptors);
93
+ }
94
+ export function cloneDef(schema) {
95
+ return mergeDefs(schema._zod.def);
96
+ }
86
97
  export function getElementAtPath(obj, path) {
87
98
  if (!path)
88
99
  return obj;
@@ -110,13 +121,12 @@ export function randomString(length = 10) {
110
121
  export function esc(str) {
111
122
  return JSON.stringify(str);
112
123
  }
113
- export const captureStackTrace = Error.captureStackTrace
114
- ? Error.captureStackTrace
115
- : (..._args) => { };
124
+ export const captureStackTrace = ("captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => { });
116
125
  export function isObject(data) {
117
126
  return typeof data === "object" && data !== null && !Array.isArray(data);
118
127
  }
119
128
  export const allowsEval = cached(() => {
129
+ // @ts-ignore
120
130
  if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) {
121
131
  return false;
122
132
  }
@@ -191,6 +201,7 @@ export const getParsedType = (data) => {
191
201
  if (typeof Date !== "undefined" && data instanceof Date) {
192
202
  return "date";
193
203
  }
204
+ // @ts-ignore
194
205
  if (typeof File !== "undefined" && data instanceof File) {
195
206
  return "file";
196
207
  }
@@ -284,140 +295,152 @@ export const BIGINT_FORMAT_RANGES = {
284
295
  uint64: [/* @__PURE__*/ BigInt(0), /* @__PURE__*/ BigInt("18446744073709551615")],
285
296
  };
286
297
  export function pick(schema, mask) {
287
- const newShape = {};
288
- const currDef = schema._zod.def; //.shape;
289
- for (const key in mask) {
290
- if (!(key in currDef.shape)) {
291
- throw new Error(`Unrecognized key: "${key}"`);
292
- }
293
- if (!mask[key])
294
- continue;
295
- // pick key
296
- newShape[key] = currDef.shape[key];
297
- }
298
- return clone(schema, {
299
- ...schema._zod.def,
300
- shape: newShape,
298
+ const currDef = schema._zod.def;
299
+ const def = mergeDefs(schema._zod.def, {
300
+ get shape() {
301
+ const newShape = {};
302
+ for (const key in mask) {
303
+ if (!(key in currDef.shape)) {
304
+ throw new Error(`Unrecognized key: "${key}"`);
305
+ }
306
+ if (!mask[key])
307
+ continue;
308
+ newShape[key] = currDef.shape[key];
309
+ }
310
+ assignProp(this, "shape", newShape); // self-caching
311
+ return newShape;
312
+ },
301
313
  checks: [],
302
314
  });
315
+ return clone(schema, def);
303
316
  }
304
317
  export function omit(schema, mask) {
305
- const newShape = { ...schema._zod.def.shape };
306
- const currDef = schema._zod.def; //.shape;
307
- for (const key in mask) {
308
- if (!(key in currDef.shape)) {
309
- throw new Error(`Unrecognized key: "${key}"`);
310
- }
311
- if (!mask[key])
312
- continue;
313
- delete newShape[key];
314
- }
315
- return clone(schema, {
316
- ...schema._zod.def,
317
- shape: newShape,
318
+ const currDef = schema._zod.def;
319
+ const def = mergeDefs(schema._zod.def, {
320
+ get shape() {
321
+ const newShape = { ...schema._zod.def.shape };
322
+ for (const key in mask) {
323
+ if (!(key in currDef.shape)) {
324
+ throw new Error(`Unrecognized key: "${key}"`);
325
+ }
326
+ if (!mask[key])
327
+ continue;
328
+ delete newShape[key];
329
+ }
330
+ assignProp(this, "shape", newShape); // self-caching
331
+ return newShape;
332
+ },
318
333
  checks: [],
319
334
  });
335
+ return clone(schema, def);
320
336
  }
321
337
  export function extend(schema, shape) {
322
338
  if (!isPlainObject(shape)) {
323
339
  throw new Error("Invalid input to extend: expected a plain object");
324
340
  }
325
- const def = {
326
- ...schema._zod.def,
341
+ const def = mergeDefs(schema._zod.def, {
327
342
  get shape() {
328
343
  const _shape = { ...schema._zod.def.shape, ...shape };
329
344
  assignProp(this, "shape", _shape); // self-caching
330
345
  return _shape;
331
346
  },
332
- checks: [], // delete existing checks
333
- };
347
+ checks: [],
348
+ });
334
349
  return clone(schema, def);
335
350
  }
336
351
  export function merge(a, b) {
337
- return clone(a, {
338
- ...a._zod.def,
352
+ const def = mergeDefs(a._zod.def, {
339
353
  get shape() {
340
354
  const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
341
355
  assignProp(this, "shape", _shape); // self-caching
342
356
  return _shape;
343
357
  },
344
- catchall: b._zod.def.catchall,
358
+ get catchall() {
359
+ return b._zod.def.catchall;
360
+ },
345
361
  checks: [], // delete existing checks
346
362
  });
363
+ return clone(a, def);
347
364
  }
348
365
  export function partial(Class, schema, mask) {
349
- const oldShape = schema._zod.def.shape;
350
- const shape = { ...oldShape };
351
- if (mask) {
352
- for (const key in mask) {
353
- if (!(key in oldShape)) {
354
- throw new Error(`Unrecognized key: "${key}"`);
366
+ const def = mergeDefs(schema._zod.def, {
367
+ get shape() {
368
+ const oldShape = schema._zod.def.shape;
369
+ const shape = { ...oldShape };
370
+ if (mask) {
371
+ for (const key in mask) {
372
+ if (!(key in oldShape)) {
373
+ throw new Error(`Unrecognized key: "${key}"`);
374
+ }
375
+ if (!mask[key])
376
+ continue;
377
+ // if (oldShape[key]!._zod.optin === "optional") continue;
378
+ shape[key] = Class
379
+ ? new Class({
380
+ type: "optional",
381
+ innerType: oldShape[key],
382
+ })
383
+ : oldShape[key];
384
+ }
355
385
  }
356
- if (!mask[key])
357
- continue;
358
- // if (oldShape[key]!._zod.optin === "optional") continue;
359
- shape[key] = Class
360
- ? new Class({
361
- type: "optional",
362
- innerType: oldShape[key],
363
- })
364
- : oldShape[key];
365
- }
366
- }
367
- else {
368
- for (const key in oldShape) {
369
- // if (oldShape[key]!._zod.optin === "optional") continue;
370
- shape[key] = Class
371
- ? new Class({
372
- type: "optional",
373
- innerType: oldShape[key],
374
- })
375
- : oldShape[key];
376
- }
377
- }
378
- return clone(schema, {
379
- ...schema._zod.def,
380
- shape,
386
+ else {
387
+ for (const key in oldShape) {
388
+ // if (oldShape[key]!._zod.optin === "optional") continue;
389
+ shape[key] = Class
390
+ ? new Class({
391
+ type: "optional",
392
+ innerType: oldShape[key],
393
+ })
394
+ : oldShape[key];
395
+ }
396
+ }
397
+ assignProp(this, "shape", shape); // self-caching
398
+ return shape;
399
+ },
381
400
  checks: [],
382
401
  });
402
+ return clone(schema, def);
383
403
  }
384
404
  export function required(Class, schema, mask) {
385
- const oldShape = schema._zod.def.shape;
386
- const shape = { ...oldShape };
387
- if (mask) {
388
- for (const key in mask) {
389
- if (!(key in shape)) {
390
- throw new Error(`Unrecognized key: "${key}"`);
405
+ const def = mergeDefs(schema._zod.def, {
406
+ get shape() {
407
+ const oldShape = schema._zod.def.shape;
408
+ const shape = { ...oldShape };
409
+ if (mask) {
410
+ for (const key in mask) {
411
+ if (!(key in shape)) {
412
+ throw new Error(`Unrecognized key: "${key}"`);
413
+ }
414
+ if (!mask[key])
415
+ continue;
416
+ // overwrite with non-optional
417
+ shape[key] = new Class({
418
+ type: "nonoptional",
419
+ innerType: oldShape[key],
420
+ });
421
+ }
391
422
  }
392
- if (!mask[key])
393
- continue;
394
- // overwrite with non-optional
395
- shape[key] = new Class({
396
- type: "nonoptional",
397
- innerType: oldShape[key],
398
- });
399
- }
400
- }
401
- else {
402
- for (const key in oldShape) {
403
- // overwrite with non-optional
404
- shape[key] = new Class({
405
- type: "nonoptional",
406
- innerType: oldShape[key],
407
- });
408
- }
409
- }
410
- return clone(schema, {
411
- ...schema._zod.def,
412
- shape,
413
- // optional: [],
423
+ else {
424
+ for (const key in oldShape) {
425
+ // overwrite with non-optional
426
+ shape[key] = new Class({
427
+ type: "nonoptional",
428
+ innerType: oldShape[key],
429
+ });
430
+ }
431
+ }
432
+ assignProp(this, "shape", shape); // self-caching
433
+ return shape;
434
+ },
414
435
  checks: [],
415
436
  });
437
+ return clone(schema, def);
416
438
  }
417
439
  export function aborted(x, startIndex = 0) {
418
440
  for (let i = startIndex; i < x.issues.length; i++) {
419
- if (x.issues[i]?.continue !== true)
441
+ if (x.issues[i]?.continue !== true) {
420
442
  return true;
443
+ }
421
444
  }
422
445
  return false;
423
446
  }
@@ -456,6 +479,7 @@ export function getSizableOrigin(input) {
456
479
  return "set";
457
480
  if (input instanceof Map)
458
481
  return "map";
482
+ // @ts-ignore
459
483
  if (input instanceof File)
460
484
  return "file";
461
485
  return "unknown";
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 0,
7
- patch: 5,
7
+ patch: 9,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 5,
4
+ patch: 9,
5
5
  };
@@ -562,9 +562,11 @@ function record(keyType, valueType, params) {
562
562
  });
563
563
  }
564
564
  function partialRecord(keyType, valueType, params) {
565
+ const k = core.clone(keyType);
566
+ k._zod.values = undefined;
565
567
  return new exports.ZodMiniRecord({
566
568
  type: "record",
567
- keyType: union([keyType, never()]),
569
+ keyType: k,
568
570
  valueType: valueType,
569
571
  ...index_js_1.util.normalizeParams(params),
570
572
  });
@@ -452,9 +452,11 @@ export function record(keyType, valueType, params) {
452
452
  });
453
453
  }
454
454
  export function partialRecord(keyType, valueType, params) {
455
+ const k = core.clone(keyType);
456
+ k._zod.values = undefined;
455
457
  return new ZodMiniRecord({
456
458
  type: "record",
457
- keyType: union([keyType, never()]),
459
+ keyType: k,
458
460
  valueType: valueType,
459
461
  ...util.normalizeParams(params),
460
462
  });