zod 4.0.5 → 4.0.7

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 (86) hide show
  1. package/package.json +1 -1
  2. package/src/v3/tests/string.test.ts +2 -2
  3. package/src/v3/types.ts +3 -1
  4. package/src/v4/classic/errors.ts +9 -2
  5. package/src/v4/classic/schemas.ts +11 -9
  6. package/src/v4/classic/tests/catch.test.ts +4 -5
  7. package/src/v4/classic/tests/discriminated-unions.test.ts +12 -0
  8. package/src/v4/classic/tests/error-utils.test.ts +43 -0
  9. package/src/v4/classic/tests/literal.test.ts +25 -0
  10. package/src/v4/classic/tests/partial.test.ts +193 -0
  11. package/src/v4/classic/tests/pickomit.test.ts +5 -5
  12. package/src/v4/classic/tests/preprocess.test.ts +4 -15
  13. package/src/v4/classic/tests/record.test.ts +15 -1
  14. package/src/v4/classic/tests/recursive-types.test.ts +67 -0
  15. package/src/v4/classic/tests/string.test.ts +81 -4
  16. package/src/v4/classic/tests/template-literal.test.ts +3 -0
  17. package/src/v4/classic/tests/to-json-schema.test.ts +1 -0
  18. package/src/v4/classic/tests/transform.test.ts +104 -0
  19. package/src/v4/classic/tests/union.test.ts +45 -3
  20. package/src/v4/core/checks.ts +2 -2
  21. package/src/v4/core/errors.ts +8 -15
  22. package/src/v4/core/regexes.ts +1 -1
  23. package/src/v4/core/registries.ts +3 -2
  24. package/src/v4/core/schemas.ts +91 -99
  25. package/src/v4/core/to-json-schema.ts +1 -0
  26. package/src/v4/core/util.ts +175 -115
  27. package/src/v4/core/versions.ts +1 -1
  28. package/src/v4/locales/bg.ts +136 -0
  29. package/src/v4/locales/da.ts +141 -0
  30. package/src/v4/locales/index.ts +2 -0
  31. package/src/v4/locales/is.ts +127 -0
  32. package/src/v4/mini/schemas.ts +3 -1
  33. package/v3/types.cjs +2 -0
  34. package/v3/types.d.cts +4 -1
  35. package/v3/types.d.ts +4 -1
  36. package/v3/types.js +2 -0
  37. package/v4/classic/errors.cjs +9 -2
  38. package/v4/classic/errors.js +9 -2
  39. package/v4/classic/schemas.cjs +5 -3
  40. package/v4/classic/schemas.d.cts +3 -3
  41. package/v4/classic/schemas.d.ts +3 -3
  42. package/v4/classic/schemas.js +5 -3
  43. package/v4/core/checks.d.cts +2 -2
  44. package/v4/core/checks.d.ts +2 -2
  45. package/v4/core/errors.cjs +4 -9
  46. package/v4/core/errors.d.cts +4 -6
  47. package/v4/core/errors.d.ts +4 -6
  48. package/v4/core/errors.js +4 -9
  49. package/v4/core/regexes.cjs +1 -1
  50. package/v4/core/regexes.d.cts +1 -1
  51. package/v4/core/regexes.d.ts +1 -1
  52. package/v4/core/regexes.js +1 -1
  53. package/v4/core/registries.cjs +2 -1
  54. package/v4/core/registries.d.cts +1 -1
  55. package/v4/core/registries.d.ts +1 -1
  56. package/v4/core/registries.js +2 -1
  57. package/v4/core/schemas.cjs +48 -88
  58. package/v4/core/schemas.d.cts +9 -4
  59. package/v4/core/schemas.d.ts +9 -4
  60. package/v4/core/schemas.js +48 -88
  61. package/v4/core/to-json-schema.cjs +1 -0
  62. package/v4/core/to-json-schema.js +1 -0
  63. package/v4/core/util.cjs +163 -112
  64. package/v4/core/util.d.cts +1 -0
  65. package/v4/core/util.d.ts +1 -0
  66. package/v4/core/util.js +162 -112
  67. package/v4/core/versions.cjs +1 -1
  68. package/v4/core/versions.js +1 -1
  69. package/v4/locales/bg.cjs +156 -0
  70. package/v4/locales/bg.d.cts +5 -0
  71. package/v4/locales/bg.d.ts +5 -0
  72. package/v4/locales/bg.js +128 -0
  73. package/v4/locales/da.cjs +157 -0
  74. package/v4/locales/da.d.cts +4 -0
  75. package/v4/locales/da.d.ts +4 -0
  76. package/v4/locales/da.js +131 -0
  77. package/v4/locales/index.cjs +5 -1
  78. package/v4/locales/index.d.cts +2 -0
  79. package/v4/locales/index.d.ts +2 -0
  80. package/v4/locales/index.js +2 -0
  81. package/v4/locales/is.cjs +145 -0
  82. package/v4/locales/is.d.cts +5 -0
  83. package/v4/locales/is.d.ts +5 -0
  84. package/v4/locales/is.js +117 -0
  85. package/v4/mini/schemas.cjs +3 -1
  86. package/v4/mini/schemas.js +3 -1
package/v4/core/util.cjs CHANGED
@@ -15,6 +15,7 @@ exports.cleanRegex = cleanRegex;
15
15
  exports.floatSafeRemainder = floatSafeRemainder;
16
16
  exports.defineLazy = defineLazy;
17
17
  exports.assignProp = assignProp;
18
+ exports.cloneDef = cloneDef;
18
19
  exports.getElementAtPath = getElementAtPath;
19
20
  exports.promiseAllObject = promiseAllObject;
20
21
  exports.randomString = randomString;
@@ -127,6 +128,11 @@ function assignProp(target, prop, value) {
127
128
  configurable: true,
128
129
  });
129
130
  }
131
+ function cloneDef(schema) {
132
+ const def = {};
133
+ Object.defineProperties(def, Object.getOwnPropertyDescriptors(schema._zod.def));
134
+ return def;
135
+ }
130
136
  function getElementAtPath(obj, path) {
131
137
  if (!path)
132
138
  return obj;
@@ -154,13 +160,12 @@ function randomString(length = 10) {
154
160
  function esc(str) {
155
161
  return JSON.stringify(str);
156
162
  }
157
- exports.captureStackTrace = Error.captureStackTrace
158
- ? Error.captureStackTrace
159
- : (..._args) => { };
163
+ exports.captureStackTrace = ("captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => { });
160
164
  function isObject(data) {
161
165
  return typeof data === "object" && data !== null && !Array.isArray(data);
162
166
  }
163
167
  exports.allowsEval = cached(() => {
168
+ // @ts-ignore
164
169
  if (typeof navigator !== "undefined" && navigator?.userAgent?.includes("Cloudflare")) {
165
170
  return false;
166
171
  }
@@ -235,6 +240,7 @@ const getParsedType = (data) => {
235
240
  if (typeof Date !== "undefined" && data instanceof Date) {
236
241
  return "date";
237
242
  }
243
+ // @ts-ignore
238
244
  if (typeof File !== "undefined" && data instanceof File) {
239
245
  return "file";
240
246
  }
@@ -329,140 +335,184 @@ exports.BIGINT_FORMAT_RANGES = {
329
335
  uint64: [/* @__PURE__*/ BigInt(0), /* @__PURE__*/ BigInt("18446744073709551615")],
330
336
  };
331
337
  function pick(schema, mask) {
332
- const newShape = {};
333
- const currDef = schema._zod.def; //.shape;
334
- for (const key in mask) {
335
- if (!(key in currDef.shape)) {
336
- throw new Error(`Unrecognized key: "${key}"`);
337
- }
338
- if (!mask[key])
339
- continue;
340
- // pick key
341
- newShape[key] = currDef.shape[key];
342
- }
343
- return clone(schema, {
344
- ...schema._zod.def,
345
- shape: newShape,
346
- checks: [],
338
+ const currDef = schema._zod.def;
339
+ const def = cloneDef(schema);
340
+ Object.defineProperties(def, {
341
+ shape: {
342
+ get() {
343
+ const newShape = {};
344
+ for (const key in mask) {
345
+ if (!(key in currDef.shape)) {
346
+ throw new Error(`Unrecognized key: "${key}"`);
347
+ }
348
+ if (!mask[key])
349
+ continue;
350
+ newShape[key] = currDef.shape[key];
351
+ }
352
+ assignProp(this, "shape", newShape); // self-caching
353
+ return newShape;
354
+ },
355
+ },
356
+ checks: {
357
+ value: [],
358
+ },
347
359
  });
360
+ return clone(schema, def);
348
361
  }
349
362
  function omit(schema, mask) {
350
- const newShape = { ...schema._zod.def.shape };
351
- const currDef = schema._zod.def; //.shape;
352
- for (const key in mask) {
353
- if (!(key in currDef.shape)) {
354
- throw new Error(`Unrecognized key: "${key}"`);
355
- }
356
- if (!mask[key])
357
- continue;
358
- delete newShape[key];
359
- }
360
- return clone(schema, {
361
- ...schema._zod.def,
362
- shape: newShape,
363
- checks: [],
363
+ const currDef = schema._zod.def;
364
+ const def = cloneDef(schema);
365
+ Object.defineProperties(def, {
366
+ shape: {
367
+ get() {
368
+ const newShape = { ...schema._zod.def.shape };
369
+ for (const key in mask) {
370
+ if (!(key in currDef.shape)) {
371
+ throw new Error(`Unrecognized key: "${key}"`);
372
+ }
373
+ if (!mask[key])
374
+ continue;
375
+ delete newShape[key];
376
+ }
377
+ assignProp(this, "shape", newShape); // self-caching
378
+ return newShape;
379
+ },
380
+ },
381
+ checks: {
382
+ value: [],
383
+ },
364
384
  });
385
+ return clone(schema, def);
365
386
  }
366
387
  function extend(schema, shape) {
367
388
  if (!isPlainObject(shape)) {
368
389
  throw new Error("Invalid input to extend: expected a plain object");
369
390
  }
370
- const def = {
371
- ...schema._zod.def,
372
- get shape() {
373
- const _shape = { ...schema._zod.def.shape, ...shape };
374
- assignProp(this, "shape", _shape); // self-caching
375
- return _shape;
391
+ const def = cloneDef(schema);
392
+ Object.defineProperties(def, {
393
+ shape: {
394
+ get() {
395
+ const _shape = { ...schema._zod.def.shape, ...shape };
396
+ assignProp(this, "shape", _shape); // self-caching
397
+ return _shape;
398
+ },
376
399
  },
377
- checks: [], // delete existing checks
378
- };
400
+ checks: {
401
+ value: [],
402
+ },
403
+ });
379
404
  return clone(schema, def);
380
405
  }
381
406
  function merge(a, b) {
382
- return clone(a, {
383
- ...a._zod.def,
384
- get shape() {
385
- const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
386
- assignProp(this, "shape", _shape); // self-caching
387
- return _shape;
407
+ const def = cloneDef(a);
408
+ Object.defineProperties(def, {
409
+ shape: {
410
+ get() {
411
+ const _shape = { ...a._zod.def.shape, ...b._zod.def.shape };
412
+ assignProp(this, "shape", _shape); // self-caching
413
+ return _shape;
414
+ },
415
+ },
416
+ catchall: {
417
+ get() {
418
+ return b._zod.def.catchall;
419
+ },
420
+ },
421
+ checks: {
422
+ value: [], // delete existing checks
388
423
  },
389
- catchall: b._zod.def.catchall,
390
- checks: [], // delete existing checks
391
424
  });
425
+ return clone(a, def);
392
426
  }
393
427
  function partial(Class, schema, mask) {
394
- const oldShape = schema._zod.def.shape;
395
- const shape = { ...oldShape };
396
- if (mask) {
397
- for (const key in mask) {
398
- if (!(key in oldShape)) {
399
- throw new Error(`Unrecognized key: "${key}"`);
400
- }
401
- if (!mask[key])
402
- continue;
403
- // if (oldShape[key]!._zod.optin === "optional") continue;
404
- shape[key] = Class
405
- ? new Class({
406
- type: "optional",
407
- innerType: oldShape[key],
408
- })
409
- : oldShape[key];
410
- }
411
- }
412
- else {
413
- for (const key in oldShape) {
414
- // if (oldShape[key]!._zod.optin === "optional") continue;
415
- shape[key] = Class
416
- ? new Class({
417
- type: "optional",
418
- innerType: oldShape[key],
419
- })
420
- : oldShape[key];
421
- }
422
- }
423
- return clone(schema, {
424
- ...schema._zod.def,
425
- shape,
426
- checks: [],
428
+ const def = cloneDef(schema);
429
+ Object.defineProperties(def, {
430
+ shape: {
431
+ get() {
432
+ const oldShape = schema._zod.def.shape;
433
+ const shape = { ...oldShape };
434
+ if (mask) {
435
+ for (const key in mask) {
436
+ if (!(key in oldShape)) {
437
+ throw new Error(`Unrecognized key: "${key}"`);
438
+ }
439
+ if (!mask[key])
440
+ continue;
441
+ // if (oldShape[key]!._zod.optin === "optional") continue;
442
+ shape[key] = Class
443
+ ? new Class({
444
+ type: "optional",
445
+ innerType: oldShape[key],
446
+ })
447
+ : oldShape[key];
448
+ }
449
+ }
450
+ else {
451
+ for (const key in oldShape) {
452
+ // if (oldShape[key]!._zod.optin === "optional") continue;
453
+ shape[key] = Class
454
+ ? new Class({
455
+ type: "optional",
456
+ innerType: oldShape[key],
457
+ })
458
+ : oldShape[key];
459
+ }
460
+ }
461
+ assignProp(this, "shape", shape); // self-caching
462
+ return shape;
463
+ },
464
+ },
465
+ checks: {
466
+ value: [],
467
+ },
427
468
  });
469
+ return clone(schema, def);
428
470
  }
429
471
  function required(Class, schema, mask) {
430
- const oldShape = schema._zod.def.shape;
431
- const shape = { ...oldShape };
432
- if (mask) {
433
- for (const key in mask) {
434
- if (!(key in shape)) {
435
- throw new Error(`Unrecognized key: "${key}"`);
436
- }
437
- if (!mask[key])
438
- continue;
439
- // overwrite with non-optional
440
- shape[key] = new Class({
441
- type: "nonoptional",
442
- innerType: oldShape[key],
443
- });
444
- }
445
- }
446
- else {
447
- for (const key in oldShape) {
448
- // overwrite with non-optional
449
- shape[key] = new Class({
450
- type: "nonoptional",
451
- innerType: oldShape[key],
452
- });
453
- }
454
- }
455
- return clone(schema, {
456
- ...schema._zod.def,
457
- shape,
458
- // optional: [],
459
- checks: [],
472
+ const def = cloneDef(schema);
473
+ Object.defineProperties(def, {
474
+ shape: {
475
+ get() {
476
+ const oldShape = schema._zod.def.shape;
477
+ const shape = { ...oldShape };
478
+ if (mask) {
479
+ for (const key in mask) {
480
+ if (!(key in shape)) {
481
+ throw new Error(`Unrecognized key: "${key}"`);
482
+ }
483
+ if (!mask[key])
484
+ continue;
485
+ // overwrite with non-optional
486
+ shape[key] = new Class({
487
+ type: "nonoptional",
488
+ innerType: oldShape[key],
489
+ });
490
+ }
491
+ }
492
+ else {
493
+ for (const key in oldShape) {
494
+ // overwrite with non-optional
495
+ shape[key] = new Class({
496
+ type: "nonoptional",
497
+ innerType: oldShape[key],
498
+ });
499
+ }
500
+ }
501
+ assignProp(this, "shape", shape); // self-caching
502
+ return shape;
503
+ },
504
+ },
505
+ checks: {
506
+ value: [],
507
+ },
460
508
  });
509
+ return clone(schema, def);
461
510
  }
462
511
  function aborted(x, startIndex = 0) {
463
512
  for (let i = startIndex; i < x.issues.length; i++) {
464
- if (x.issues[i]?.continue !== true)
513
+ if (x.issues[i]?.continue !== true) {
465
514
  return true;
515
+ }
466
516
  }
467
517
  return false;
468
518
  }
@@ -501,6 +551,7 @@ function getSizableOrigin(input) {
501
551
  return "set";
502
552
  if (input instanceof Map)
503
553
  return "map";
554
+ // @ts-ignore
504
555
  if (input instanceof File)
505
556
  return "file";
506
557
  return "unknown";
@@ -121,6 +121,7 @@ export declare function cleanRegex(source: string): string;
121
121
  export declare function floatSafeRemainder(val: number, step: number): number;
122
122
  export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
123
123
  export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
124
+ export declare function cloneDef(schema: schemas.$ZodType): any;
124
125
  export declare function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any;
125
126
  export declare function promiseAllObject<T extends object>(promisesObj: T): Promise<{
126
127
  [k in keyof T]: Awaited<T[k]>;
package/v4/core/util.d.ts CHANGED
@@ -121,6 +121,7 @@ export declare function cleanRegex(source: string): string;
121
121
  export declare function floatSafeRemainder(val: number, step: number): number;
122
122
  export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
123
123
  export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
124
+ export declare function cloneDef(schema: schemas.$ZodType): any;
124
125
  export declare function getElementAtPath(obj: any, path: (string | number)[] | null | undefined): any;
125
126
  export declare function promiseAllObject<T extends object>(promisesObj: T): Promise<{
126
127
  [k in keyof T]: Awaited<T[k]>;