zod 4.0.4 → 4.0.6
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/tests/string.test.ts +2 -2
- package/src/v3/types.ts +3 -1
- package/src/v4/classic/compat.ts +2 -39
- package/src/v4/classic/errors.ts +9 -2
- package/src/v4/classic/schemas.ts +11 -9
- package/src/v4/classic/tests/catch.test.ts +4 -5
- package/src/v4/classic/tests/discriminated-unions.test.ts +41 -0
- package/src/v4/classic/tests/error-utils.test.ts +43 -0
- package/src/v4/classic/tests/literal.test.ts +25 -0
- 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 +81 -4
- package/src/v4/classic/tests/template-literal.test.ts +3 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +1 -0
- package/src/v4/classic/tests/transform.test.ts +110 -0
- package/src/v4/classic/tests/union.test.ts +45 -3
- package/src/v4/core/checks.ts +2 -2
- package/src/v4/core/errors.ts +8 -15
- package/src/v4/core/regexes.ts +1 -1
- package/src/v4/core/registries.ts +3 -2
- package/src/v4/core/schemas.ts +93 -99
- package/src/v4/core/to-json-schema.ts +1 -0
- package/src/v4/core/util.ts +175 -115
- package/src/v4/core/versions.ts +1 -1
- package/src/v4/locales/bg.ts +136 -0
- package/src/v4/locales/da.ts +141 -0
- package/src/v4/locales/index.ts +2 -0
- package/src/v4/locales/is.ts +127 -0
- 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/compat.cjs +1 -37
- package/v4/classic/compat.d.cts +1 -37
- package/v4/classic/compat.d.ts +1 -37
- package/v4/classic/compat.js +1 -37
- 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 +3 -3
- package/v4/classic/schemas.d.ts +3 -3
- 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/regexes.cjs +1 -1
- package/v4/core/regexes.d.cts +1 -1
- package/v4/core/regexes.d.ts +1 -1
- package/v4/core/regexes.js +1 -1
- 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 +49 -88
- package/v4/core/schemas.d.cts +10 -4
- package/v4/core/schemas.d.ts +10 -4
- package/v4/core/schemas.js +49 -88
- package/v4/core/to-json-schema.cjs +1 -0
- package/v4/core/to-json-schema.js +1 -0
- package/v4/core/util.cjs +163 -112
- package/v4/core/util.d.cts +1 -0
- package/v4/core/util.d.ts +1 -0
- package/v4/core/util.js +162 -112
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
- package/v4/locales/bg.cjs +156 -0
- package/v4/locales/bg.d.cts +5 -0
- package/v4/locales/bg.d.ts +5 -0
- package/v4/locales/bg.js +128 -0
- package/v4/locales/da.cjs +157 -0
- package/v4/locales/da.d.cts +4 -0
- package/v4/locales/da.d.ts +4 -0
- package/v4/locales/da.js +131 -0
- package/v4/locales/index.cjs +5 -1
- package/v4/locales/index.d.cts +2 -0
- package/v4/locales/index.d.ts +2 -0
- package/v4/locales/index.js +2 -0
- package/v4/locales/is.cjs +145 -0
- package/v4/locales/is.d.cts +5 -0
- package/v4/locales/is.d.ts +5 -0
- package/v4/locales/is.js +117 -0
- package/v4/mini/schemas.cjs +3 -1
- package/v4/mini/schemas.js +3 -1
package/v4/core/schemas.js
CHANGED
|
@@ -156,9 +156,10 @@ export const $ZodURL = /*@__PURE__*/ core.$constructor("$ZodURL", (inst, def) =>
|
|
|
156
156
|
$ZodStringFormat.init(inst, def);
|
|
157
157
|
inst._zod.check = (payload) => {
|
|
158
158
|
try {
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
159
|
+
// Trim whitespace from input
|
|
160
|
+
const trimmed = payload.value.trim();
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
const url = new URL(trimmed);
|
|
162
163
|
if (def.hostname) {
|
|
163
164
|
def.hostname.lastIndex = 0;
|
|
164
165
|
if (!def.hostname.test(url.hostname)) {
|
|
@@ -187,12 +188,14 @@ export const $ZodURL = /*@__PURE__*/ core.$constructor("$ZodURL", (inst, def) =>
|
|
|
187
188
|
});
|
|
188
189
|
}
|
|
189
190
|
}
|
|
190
|
-
//
|
|
191
|
-
if (
|
|
192
|
-
|
|
191
|
+
// Set the output value based on normalize flag
|
|
192
|
+
if (def.normalize) {
|
|
193
|
+
// Use normalized URL
|
|
194
|
+
payload.value = url.href;
|
|
193
195
|
}
|
|
194
196
|
else {
|
|
195
|
-
|
|
197
|
+
// Preserve the original input (trimmed)
|
|
198
|
+
payload.value = trimmed;
|
|
196
199
|
}
|
|
197
200
|
return;
|
|
198
201
|
}
|
|
@@ -268,6 +271,7 @@ export const $ZodIPv6 = /*@__PURE__*/ core.$constructor("$ZodIPv6", (inst, def)
|
|
|
268
271
|
});
|
|
269
272
|
inst._zod.check = (payload) => {
|
|
270
273
|
try {
|
|
274
|
+
// @ts-ignore
|
|
271
275
|
new URL(`http://[${payload.value}]`);
|
|
272
276
|
// return;
|
|
273
277
|
}
|
|
@@ -299,6 +303,7 @@ export const $ZodCIDRv6 = /*@__PURE__*/ core.$constructor("$ZodCIDRv6", (inst, d
|
|
|
299
303
|
throw new Error();
|
|
300
304
|
if (prefixNum < 0 || prefixNum > 128)
|
|
301
305
|
throw new Error();
|
|
306
|
+
// @ts-ignore
|
|
302
307
|
new URL(`http://[${address}]`);
|
|
303
308
|
}
|
|
304
309
|
catch {
|
|
@@ -319,6 +324,7 @@ export function isValidBase64(data) {
|
|
|
319
324
|
if (data.length % 4 !== 0)
|
|
320
325
|
return false;
|
|
321
326
|
try {
|
|
327
|
+
// @ts-ignore
|
|
322
328
|
atob(data);
|
|
323
329
|
return true;
|
|
324
330
|
}
|
|
@@ -383,6 +389,7 @@ export function isValidJWT(token, algorithm = null) {
|
|
|
383
389
|
const [header] = tokensParts;
|
|
384
390
|
if (!header)
|
|
385
391
|
return false;
|
|
392
|
+
// @ts-ignore
|
|
386
393
|
const parsedHeader = JSON.parse(atob(header));
|
|
387
394
|
if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT")
|
|
388
395
|
return false;
|
|
@@ -653,36 +660,16 @@ export const $ZodArray = /*@__PURE__*/ core.$constructor("$ZodArray", (inst, def
|
|
|
653
660
|
return payload; //handleArrayResultsAsync(parseResults, final);
|
|
654
661
|
};
|
|
655
662
|
});
|
|
656
|
-
function
|
|
657
|
-
// if(isOptional)
|
|
663
|
+
function handlePropertyResult(result, final, key, input) {
|
|
658
664
|
if (result.issues.length) {
|
|
659
665
|
final.issues.push(...util.prefixIssues(key, result.issues));
|
|
660
666
|
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
function handleOptionalObjectResult(result, final, key, input) {
|
|
664
|
-
if (result.issues.length) {
|
|
665
|
-
// validation failed against value schema
|
|
666
|
-
if (input[key] === undefined) {
|
|
667
|
-
// if input was undefined, ignore the error
|
|
668
|
-
if (key in input) {
|
|
669
|
-
final.value[key] = undefined;
|
|
670
|
-
}
|
|
671
|
-
else {
|
|
672
|
-
final.value[key] = result.value;
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
else {
|
|
676
|
-
final.issues.push(...util.prefixIssues(key, result.issues));
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
else if (result.value === undefined) {
|
|
680
|
-
// validation returned `undefined`
|
|
681
|
-
if (key in input)
|
|
667
|
+
if (result.value === undefined) {
|
|
668
|
+
if (key in input) {
|
|
682
669
|
final.value[key] = undefined;
|
|
670
|
+
}
|
|
683
671
|
}
|
|
684
672
|
else {
|
|
685
|
-
// non-undefined value
|
|
686
673
|
final.value[key] = result.value;
|
|
687
674
|
}
|
|
688
675
|
}
|
|
@@ -734,42 +721,25 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
|
|
|
734
721
|
// A: preserve key order {
|
|
735
722
|
doc.write(`const newResult = {}`);
|
|
736
723
|
for (const key of normalized.keys) {
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
doc.write(`
|
|
724
|
+
const id = ids[key];
|
|
725
|
+
const k = util.esc(key);
|
|
726
|
+
doc.write(`const ${id} = ${parseStr(key)};`);
|
|
727
|
+
doc.write(`
|
|
742
728
|
if (${id}.issues.length) {
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
}))
|
|
753
|
-
);
|
|
729
|
+
payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
|
|
730
|
+
...iss,
|
|
731
|
+
path: iss.path ? [${k}, ...iss.path] : [${k}]
|
|
732
|
+
})));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (${id}.value === undefined) {
|
|
736
|
+
if (${k} in input) {
|
|
737
|
+
newResult[${k}] = undefined;
|
|
754
738
|
}
|
|
755
|
-
} else if (${id}.value === undefined) {
|
|
756
|
-
if (${k} in input) newResult[${k}] = undefined;
|
|
757
739
|
} else {
|
|
758
740
|
newResult[${k}] = ${id}.value;
|
|
759
741
|
}
|
|
760
|
-
|
|
761
|
-
}
|
|
762
|
-
else {
|
|
763
|
-
const id = ids[key];
|
|
764
|
-
// const id = ids[key];
|
|
765
|
-
doc.write(`const ${id} = ${parseStr(key)};`);
|
|
766
|
-
doc.write(`
|
|
767
|
-
if (${id}.issues.length) payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
|
|
768
|
-
...iss,
|
|
769
|
-
path: iss.path ? [${util.esc(key)}, ...iss.path] : [${util.esc(key)}]
|
|
770
|
-
})));`);
|
|
771
|
-
doc.write(`newResult[${util.esc(key)}] = ${id}.value`);
|
|
772
|
-
}
|
|
742
|
+
`);
|
|
773
743
|
}
|
|
774
744
|
doc.write(`payload.value = newResult;`);
|
|
775
745
|
doc.write(`return payload;`);
|
|
@@ -807,33 +777,16 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
|
|
|
807
777
|
const shape = value.shape;
|
|
808
778
|
for (const key of value.keys) {
|
|
809
779
|
const el = shape[key];
|
|
810
|
-
// do not add omitted optional keys
|
|
811
|
-
// if (!(key in input)) {
|
|
812
|
-
// if (optionalKeys.has(key)) continue;
|
|
813
|
-
// payload.issues.push({
|
|
814
|
-
// code: "invalid_type",
|
|
815
|
-
// path: [key],
|
|
816
|
-
// expected: "nonoptional",
|
|
817
|
-
// note: `Missing required key: "${key}"`,
|
|
818
|
-
// input,
|
|
819
|
-
// inst,
|
|
820
|
-
// });
|
|
821
|
-
// }
|
|
822
780
|
const r = el._zod.run({ value: input[key], issues: [] }, ctx);
|
|
823
|
-
const isOptional = el._zod.optin === "optional" && el._zod.optout === "optional";
|
|
824
781
|
if (r instanceof Promise) {
|
|
825
|
-
proms.push(r.then((r) =>
|
|
826
|
-
}
|
|
827
|
-
else if (isOptional) {
|
|
828
|
-
handleOptionalObjectResult(r, payload, key, input);
|
|
782
|
+
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
|
|
829
783
|
}
|
|
830
784
|
else {
|
|
831
|
-
|
|
785
|
+
handlePropertyResult(r, payload, key, input);
|
|
832
786
|
}
|
|
833
787
|
}
|
|
834
788
|
}
|
|
835
789
|
if (!catchall) {
|
|
836
|
-
// return payload;
|
|
837
790
|
return proms.length ? Promise.all(proms).then(() => payload) : payload;
|
|
838
791
|
}
|
|
839
792
|
const unrecognized = [];
|
|
@@ -850,10 +803,10 @@ export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, d
|
|
|
850
803
|
}
|
|
851
804
|
const r = _catchall.run({ value: input[key], issues: [] }, ctx);
|
|
852
805
|
if (r instanceof Promise) {
|
|
853
|
-
proms.push(r.then((r) =>
|
|
806
|
+
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
|
|
854
807
|
}
|
|
855
808
|
else {
|
|
856
|
-
|
|
809
|
+
handlePropertyResult(r, payload, key, input);
|
|
857
810
|
}
|
|
858
811
|
}
|
|
859
812
|
if (unrecognized.length) {
|
|
@@ -878,6 +831,11 @@ function handleUnionResults(results, final, inst, ctx) {
|
|
|
878
831
|
return final;
|
|
879
832
|
}
|
|
880
833
|
}
|
|
834
|
+
const nonaborted = results.filter((r) => !util.aborted(r));
|
|
835
|
+
if (nonaborted.length > 0) {
|
|
836
|
+
final.value = nonaborted[0].value;
|
|
837
|
+
return nonaborted[0];
|
|
838
|
+
}
|
|
881
839
|
final.issues.push({
|
|
882
840
|
code: "invalid_union",
|
|
883
841
|
input: final.value,
|
|
@@ -953,7 +911,7 @@ core.$constructor("$ZodDiscriminatedUnion", (inst, def) => {
|
|
|
953
911
|
const opts = def.options;
|
|
954
912
|
const map = new Map();
|
|
955
913
|
for (const o of opts) {
|
|
956
|
-
const values = o._zod.propValues[def.discriminator];
|
|
914
|
+
const values = o._zod.propValues?.[def.discriminator];
|
|
957
915
|
if (!values || values.size === 0)
|
|
958
916
|
throw new Error(`Invalid discriminated union option at index "${def.options.indexOf(o)}"`);
|
|
959
917
|
for (const v of values) {
|
|
@@ -1343,14 +1301,15 @@ function handleSetResult(result, final) {
|
|
|
1343
1301
|
export const $ZodEnum = /*@__PURE__*/ core.$constructor("$ZodEnum", (inst, def) => {
|
|
1344
1302
|
$ZodType.init(inst, def);
|
|
1345
1303
|
const values = util.getEnumValues(def.entries);
|
|
1346
|
-
|
|
1304
|
+
const valuesSet = new Set(values);
|
|
1305
|
+
inst._zod.values = valuesSet;
|
|
1347
1306
|
inst._zod.pattern = new RegExp(`^(${values
|
|
1348
1307
|
.filter((k) => util.propertyKeyTypes.has(typeof k))
|
|
1349
1308
|
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o.toString()))
|
|
1350
1309
|
.join("|")})$`);
|
|
1351
1310
|
inst._zod.parse = (payload, _ctx) => {
|
|
1352
1311
|
const input = payload.value;
|
|
1353
|
-
if (
|
|
1312
|
+
if (valuesSet.has(input)) {
|
|
1354
1313
|
return payload;
|
|
1355
1314
|
}
|
|
1356
1315
|
payload.issues.push({
|
|
@@ -1366,7 +1325,7 @@ export const $ZodLiteral = /*@__PURE__*/ core.$constructor("$ZodLiteral", (inst,
|
|
|
1366
1325
|
$ZodType.init(inst, def);
|
|
1367
1326
|
inst._zod.values = new Set(def.values);
|
|
1368
1327
|
inst._zod.pattern = new RegExp(`^(${def.values
|
|
1369
|
-
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? o.toString() : String(o)))
|
|
1328
|
+
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
|
|
1370
1329
|
.join("|")})$`);
|
|
1371
1330
|
inst._zod.parse = (payload, _ctx) => {
|
|
1372
1331
|
const input = payload.value;
|
|
@@ -1386,6 +1345,7 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
|
|
|
1386
1345
|
$ZodType.init(inst, def);
|
|
1387
1346
|
inst._zod.parse = (payload, _ctx) => {
|
|
1388
1347
|
const input = payload.value;
|
|
1348
|
+
// @ts-ignore
|
|
1389
1349
|
if (input instanceof File)
|
|
1390
1350
|
return payload;
|
|
1391
1351
|
payload.issues.push({
|
|
@@ -1531,7 +1491,7 @@ export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst,
|
|
|
1531
1491
|
});
|
|
1532
1492
|
export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
|
|
1533
1493
|
$ZodType.init(inst, def);
|
|
1534
|
-
inst._zod
|
|
1494
|
+
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
|
|
1535
1495
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1536
1496
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1537
1497
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -1586,6 +1546,7 @@ export const $ZodPipe = /*@__PURE__*/ core.$constructor("$ZodPipe", (inst, def)
|
|
|
1586
1546
|
util.defineLazy(inst._zod, "values", () => def.in._zod.values);
|
|
1587
1547
|
util.defineLazy(inst._zod, "optin", () => def.in._zod.optin);
|
|
1588
1548
|
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
|
|
1549
|
+
util.defineLazy(inst._zod, "propValues", () => def.in._zod.propValues);
|
|
1589
1550
|
inst._zod.parse = (payload, ctx) => {
|
|
1590
1551
|
const left = def.in._zod.run(payload, ctx);
|
|
1591
1552
|
if (left instanceof Promise) {
|
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
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
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
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
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
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
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:
|
|
378
|
-
|
|
400
|
+
checks: {
|
|
401
|
+
value: [],
|
|
402
|
+
},
|
|
403
|
+
});
|
|
379
404
|
return clone(schema, def);
|
|
380
405
|
}
|
|
381
406
|
function merge(a, b) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
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";
|
package/v4/core/util.d.cts
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]>;
|
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]>;
|