zod 4.1.0-canary.20250723T222937 → 4.1.0-canary.20250724T211341
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.
- package/package.json +1 -1
- package/src/v3/types.ts +3 -1
- package/src/v4/classic/errors.ts +9 -2
- package/src/v4/classic/schemas.ts +10 -7
- package/src/v4/classic/tests/error-utils.test.ts +43 -0
- package/src/v4/classic/tests/file.test.ts +0 -1
- package/src/v4/classic/tests/partial.test.ts +193 -0
- package/src/v4/classic/tests/pickomit.test.ts +5 -5
- package/src/v4/classic/tests/preprocess.test.ts +4 -15
- package/src/v4/classic/tests/record.test.ts +15 -1
- package/src/v4/classic/tests/recursive-types.test.ts +67 -0
- package/src/v4/classic/tests/string.test.ts +77 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +1 -0
- package/src/v4/classic/tests/transform.test.ts +104 -0
- package/src/v4/classic/tests/union.test.ts +90 -3
- package/src/v4/core/checks.ts +2 -2
- package/src/v4/core/errors.ts +8 -15
- package/src/v4/core/registries.ts +3 -2
- package/src/v4/core/schemas.ts +92 -94
- package/src/v4/core/tests/extend.test.ts +18 -0
- package/src/v4/core/to-json-schema.ts +1 -0
- package/src/v4/core/util.ts +135 -98
- package/src/v4/core/versions.ts +1 -1
- package/src/v4/mini/schemas.ts +3 -1
- package/v3/types.cjs +2 -0
- package/v3/types.d.cts +4 -1
- package/v3/types.d.ts +4 -1
- package/v3/types.js +2 -0
- package/v4/classic/errors.cjs +9 -2
- package/v4/classic/errors.js +9 -2
- package/v4/classic/schemas.cjs +5 -3
- package/v4/classic/schemas.d.cts +2 -1
- package/v4/classic/schemas.d.ts +2 -1
- package/v4/classic/schemas.js +5 -3
- package/v4/core/checks.d.cts +2 -2
- package/v4/core/checks.d.ts +2 -2
- package/v4/core/errors.cjs +4 -9
- package/v4/core/errors.d.cts +4 -6
- package/v4/core/errors.d.ts +4 -6
- package/v4/core/errors.js +4 -9
- package/v4/core/registries.cjs +2 -1
- package/v4/core/registries.d.cts +1 -1
- package/v4/core/registries.d.ts +1 -1
- package/v4/core/registries.js +2 -1
- package/v4/core/schemas.cjs +50 -87
- package/v4/core/schemas.d.cts +8 -3
- package/v4/core/schemas.d.ts +8 -3
- package/v4/core/schemas.js +50 -87
- package/v4/core/to-json-schema.cjs +1 -0
- package/v4/core/to-json-schema.js +1 -0
- package/v4/core/util.cjs +123 -97
- package/v4/core/util.d.cts +2 -0
- package/v4/core/util.d.ts +2 -0
- package/v4/core/util.js +121 -97
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
- package/v4/mini/schemas.cjs +3 -1
- 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
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
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: [],
|
|
333
|
-
};
|
|
347
|
+
checks: [],
|
|
348
|
+
});
|
|
334
349
|
return clone(schema, def);
|
|
335
350
|
}
|
|
336
351
|
export function merge(a, b) {
|
|
337
|
-
|
|
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
|
|
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
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (
|
|
354
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (
|
|
390
|
-
|
|
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
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
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";
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED
package/v4/mini/schemas.cjs
CHANGED
|
@@ -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:
|
|
569
|
+
keyType: k,
|
|
568
570
|
valueType: valueType,
|
|
569
571
|
...index_js_1.util.normalizeParams(params),
|
|
570
572
|
});
|
package/v4/mini/schemas.js
CHANGED
|
@@ -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:
|
|
459
|
+
keyType: k,
|
|
458
460
|
valueType: valueType,
|
|
459
461
|
...util.normalizeParams(params),
|
|
460
462
|
});
|