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.
- 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/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 +12 -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 +104 -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 +91 -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/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 +48 -88
- package/v4/core/schemas.d.cts +9 -4
- package/v4/core/schemas.d.ts +9 -4
- package/v4/core/schemas.js +48 -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.cjs
CHANGED
|
@@ -187,9 +187,10 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
|
|
|
187
187
|
exports.$ZodStringFormat.init(inst, def);
|
|
188
188
|
inst._zod.check = (payload) => {
|
|
189
189
|
try {
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
190
|
+
// Trim whitespace from input
|
|
191
|
+
const trimmed = payload.value.trim();
|
|
192
|
+
// @ts-ignore
|
|
193
|
+
const url = new URL(trimmed);
|
|
193
194
|
if (def.hostname) {
|
|
194
195
|
def.hostname.lastIndex = 0;
|
|
195
196
|
if (!def.hostname.test(url.hostname)) {
|
|
@@ -218,12 +219,14 @@ exports.$ZodURL = core.$constructor("$ZodURL", (inst, def) => {
|
|
|
218
219
|
});
|
|
219
220
|
}
|
|
220
221
|
}
|
|
221
|
-
//
|
|
222
|
-
if (
|
|
223
|
-
|
|
222
|
+
// Set the output value based on normalize flag
|
|
223
|
+
if (def.normalize) {
|
|
224
|
+
// Use normalized URL
|
|
225
|
+
payload.value = url.href;
|
|
224
226
|
}
|
|
225
227
|
else {
|
|
226
|
-
|
|
228
|
+
// Preserve the original input (trimmed)
|
|
229
|
+
payload.value = trimmed;
|
|
227
230
|
}
|
|
228
231
|
return;
|
|
229
232
|
}
|
|
@@ -299,6 +302,7 @@ exports.$ZodIPv6 = core.$constructor("$ZodIPv6", (inst, def) => {
|
|
|
299
302
|
});
|
|
300
303
|
inst._zod.check = (payload) => {
|
|
301
304
|
try {
|
|
305
|
+
// @ts-ignore
|
|
302
306
|
new URL(`http://[${payload.value}]`);
|
|
303
307
|
// return;
|
|
304
308
|
}
|
|
@@ -330,6 +334,7 @@ exports.$ZodCIDRv6 = core.$constructor("$ZodCIDRv6", (inst, def) => {
|
|
|
330
334
|
throw new Error();
|
|
331
335
|
if (prefixNum < 0 || prefixNum > 128)
|
|
332
336
|
throw new Error();
|
|
337
|
+
// @ts-ignore
|
|
333
338
|
new URL(`http://[${address}]`);
|
|
334
339
|
}
|
|
335
340
|
catch {
|
|
@@ -350,6 +355,7 @@ function isValidBase64(data) {
|
|
|
350
355
|
if (data.length % 4 !== 0)
|
|
351
356
|
return false;
|
|
352
357
|
try {
|
|
358
|
+
// @ts-ignore
|
|
353
359
|
atob(data);
|
|
354
360
|
return true;
|
|
355
361
|
}
|
|
@@ -414,6 +420,7 @@ function isValidJWT(token, algorithm = null) {
|
|
|
414
420
|
const [header] = tokensParts;
|
|
415
421
|
if (!header)
|
|
416
422
|
return false;
|
|
423
|
+
// @ts-ignore
|
|
417
424
|
const parsedHeader = JSON.parse(atob(header));
|
|
418
425
|
if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT")
|
|
419
426
|
return false;
|
|
@@ -684,36 +691,16 @@ exports.$ZodArray = core.$constructor("$ZodArray", (inst, def) => {
|
|
|
684
691
|
return payload; //handleArrayResultsAsync(parseResults, final);
|
|
685
692
|
};
|
|
686
693
|
});
|
|
687
|
-
function
|
|
688
|
-
// if(isOptional)
|
|
694
|
+
function handlePropertyResult(result, final, key, input) {
|
|
689
695
|
if (result.issues.length) {
|
|
690
696
|
final.issues.push(...util.prefixIssues(key, result.issues));
|
|
691
697
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
function handleOptionalObjectResult(result, final, key, input) {
|
|
695
|
-
if (result.issues.length) {
|
|
696
|
-
// validation failed against value schema
|
|
697
|
-
if (input[key] === undefined) {
|
|
698
|
-
// if input was undefined, ignore the error
|
|
699
|
-
if (key in input) {
|
|
700
|
-
final.value[key] = undefined;
|
|
701
|
-
}
|
|
702
|
-
else {
|
|
703
|
-
final.value[key] = result.value;
|
|
704
|
-
}
|
|
705
|
-
}
|
|
706
|
-
else {
|
|
707
|
-
final.issues.push(...util.prefixIssues(key, result.issues));
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
else if (result.value === undefined) {
|
|
711
|
-
// validation returned `undefined`
|
|
712
|
-
if (key in input)
|
|
698
|
+
if (result.value === undefined) {
|
|
699
|
+
if (key in input) {
|
|
713
700
|
final.value[key] = undefined;
|
|
701
|
+
}
|
|
714
702
|
}
|
|
715
703
|
else {
|
|
716
|
-
// non-undefined value
|
|
717
704
|
final.value[key] = result.value;
|
|
718
705
|
}
|
|
719
706
|
}
|
|
@@ -765,42 +752,25 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
765
752
|
// A: preserve key order {
|
|
766
753
|
doc.write(`const newResult = {}`);
|
|
767
754
|
for (const key of normalized.keys) {
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
doc.write(`
|
|
755
|
+
const id = ids[key];
|
|
756
|
+
const k = util.esc(key);
|
|
757
|
+
doc.write(`const ${id} = ${parseStr(key)};`);
|
|
758
|
+
doc.write(`
|
|
773
759
|
if (${id}.issues.length) {
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
}))
|
|
784
|
-
);
|
|
760
|
+
payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
|
|
761
|
+
...iss,
|
|
762
|
+
path: iss.path ? [${k}, ...iss.path] : [${k}]
|
|
763
|
+
})));
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
if (${id}.value === undefined) {
|
|
767
|
+
if (${k} in input) {
|
|
768
|
+
newResult[${k}] = undefined;
|
|
785
769
|
}
|
|
786
|
-
} else if (${id}.value === undefined) {
|
|
787
|
-
if (${k} in input) newResult[${k}] = undefined;
|
|
788
770
|
} else {
|
|
789
771
|
newResult[${k}] = ${id}.value;
|
|
790
772
|
}
|
|
791
|
-
|
|
792
|
-
}
|
|
793
|
-
else {
|
|
794
|
-
const id = ids[key];
|
|
795
|
-
// const id = ids[key];
|
|
796
|
-
doc.write(`const ${id} = ${parseStr(key)};`);
|
|
797
|
-
doc.write(`
|
|
798
|
-
if (${id}.issues.length) payload.issues = payload.issues.concat(${id}.issues.map(iss => ({
|
|
799
|
-
...iss,
|
|
800
|
-
path: iss.path ? [${util.esc(key)}, ...iss.path] : [${util.esc(key)}]
|
|
801
|
-
})));`);
|
|
802
|
-
doc.write(`newResult[${util.esc(key)}] = ${id}.value`);
|
|
803
|
-
}
|
|
773
|
+
`);
|
|
804
774
|
}
|
|
805
775
|
doc.write(`payload.value = newResult;`);
|
|
806
776
|
doc.write(`return payload;`);
|
|
@@ -838,33 +808,16 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
838
808
|
const shape = value.shape;
|
|
839
809
|
for (const key of value.keys) {
|
|
840
810
|
const el = shape[key];
|
|
841
|
-
// do not add omitted optional keys
|
|
842
|
-
// if (!(key in input)) {
|
|
843
|
-
// if (optionalKeys.has(key)) continue;
|
|
844
|
-
// payload.issues.push({
|
|
845
|
-
// code: "invalid_type",
|
|
846
|
-
// path: [key],
|
|
847
|
-
// expected: "nonoptional",
|
|
848
|
-
// note: `Missing required key: "${key}"`,
|
|
849
|
-
// input,
|
|
850
|
-
// inst,
|
|
851
|
-
// });
|
|
852
|
-
// }
|
|
853
811
|
const r = el._zod.run({ value: input[key], issues: [] }, ctx);
|
|
854
|
-
const isOptional = el._zod.optin === "optional" && el._zod.optout === "optional";
|
|
855
812
|
if (r instanceof Promise) {
|
|
856
|
-
proms.push(r.then((r) =>
|
|
857
|
-
}
|
|
858
|
-
else if (isOptional) {
|
|
859
|
-
handleOptionalObjectResult(r, payload, key, input);
|
|
813
|
+
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
|
|
860
814
|
}
|
|
861
815
|
else {
|
|
862
|
-
|
|
816
|
+
handlePropertyResult(r, payload, key, input);
|
|
863
817
|
}
|
|
864
818
|
}
|
|
865
819
|
}
|
|
866
820
|
if (!catchall) {
|
|
867
|
-
// return payload;
|
|
868
821
|
return proms.length ? Promise.all(proms).then(() => payload) : payload;
|
|
869
822
|
}
|
|
870
823
|
const unrecognized = [];
|
|
@@ -881,10 +834,10 @@ exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
|
|
|
881
834
|
}
|
|
882
835
|
const r = _catchall.run({ value: input[key], issues: [] }, ctx);
|
|
883
836
|
if (r instanceof Promise) {
|
|
884
|
-
proms.push(r.then((r) =>
|
|
837
|
+
proms.push(r.then((r) => handlePropertyResult(r, payload, key, input)));
|
|
885
838
|
}
|
|
886
839
|
else {
|
|
887
|
-
|
|
840
|
+
handlePropertyResult(r, payload, key, input);
|
|
888
841
|
}
|
|
889
842
|
}
|
|
890
843
|
if (unrecognized.length) {
|
|
@@ -909,6 +862,11 @@ function handleUnionResults(results, final, inst, ctx) {
|
|
|
909
862
|
return final;
|
|
910
863
|
}
|
|
911
864
|
}
|
|
865
|
+
const nonaborted = results.filter((r) => !util.aborted(r));
|
|
866
|
+
if (nonaborted.length > 0) {
|
|
867
|
+
final.value = nonaborted[0].value;
|
|
868
|
+
return nonaborted[0];
|
|
869
|
+
}
|
|
912
870
|
final.issues.push({
|
|
913
871
|
code: "invalid_union",
|
|
914
872
|
input: final.value,
|
|
@@ -1374,14 +1332,15 @@ function handleSetResult(result, final) {
|
|
|
1374
1332
|
exports.$ZodEnum = core.$constructor("$ZodEnum", (inst, def) => {
|
|
1375
1333
|
exports.$ZodType.init(inst, def);
|
|
1376
1334
|
const values = util.getEnumValues(def.entries);
|
|
1377
|
-
|
|
1335
|
+
const valuesSet = new Set(values);
|
|
1336
|
+
inst._zod.values = valuesSet;
|
|
1378
1337
|
inst._zod.pattern = new RegExp(`^(${values
|
|
1379
1338
|
.filter((k) => util.propertyKeyTypes.has(typeof k))
|
|
1380
1339
|
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o.toString()))
|
|
1381
1340
|
.join("|")})$`);
|
|
1382
1341
|
inst._zod.parse = (payload, _ctx) => {
|
|
1383
1342
|
const input = payload.value;
|
|
1384
|
-
if (
|
|
1343
|
+
if (valuesSet.has(input)) {
|
|
1385
1344
|
return payload;
|
|
1386
1345
|
}
|
|
1387
1346
|
payload.issues.push({
|
|
@@ -1397,7 +1356,7 @@ exports.$ZodLiteral = core.$constructor("$ZodLiteral", (inst, def) => {
|
|
|
1397
1356
|
exports.$ZodType.init(inst, def);
|
|
1398
1357
|
inst._zod.values = new Set(def.values);
|
|
1399
1358
|
inst._zod.pattern = new RegExp(`^(${def.values
|
|
1400
|
-
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? o.toString() : String(o)))
|
|
1359
|
+
.map((o) => (typeof o === "string" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))
|
|
1401
1360
|
.join("|")})$`);
|
|
1402
1361
|
inst._zod.parse = (payload, _ctx) => {
|
|
1403
1362
|
const input = payload.value;
|
|
@@ -1417,6 +1376,7 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
|
|
|
1417
1376
|
exports.$ZodType.init(inst, def);
|
|
1418
1377
|
inst._zod.parse = (payload, _ctx) => {
|
|
1419
1378
|
const input = payload.value;
|
|
1379
|
+
// @ts-ignore
|
|
1420
1380
|
if (input instanceof File)
|
|
1421
1381
|
return payload;
|
|
1422
1382
|
payload.issues.push({
|
|
@@ -1562,7 +1522,7 @@ exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
|
|
|
1562
1522
|
});
|
|
1563
1523
|
exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
|
|
1564
1524
|
exports.$ZodType.init(inst, def);
|
|
1565
|
-
inst._zod
|
|
1525
|
+
util.defineLazy(inst._zod, "optin", () => def.innerType._zod.optin);
|
|
1566
1526
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1567
1527
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1568
1528
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -1627,7 +1587,7 @@ exports.$ZodPipe = core.$constructor("$ZodPipe", (inst, def) => {
|
|
|
1627
1587
|
};
|
|
1628
1588
|
});
|
|
1629
1589
|
function handlePipeResult(left, def, ctx) {
|
|
1630
|
-
if (
|
|
1590
|
+
if (left.issues.length) {
|
|
1631
1591
|
return left;
|
|
1632
1592
|
}
|
|
1633
1593
|
return def.out._zod.run({ value: left.value, issues: left.issues }, ctx);
|
package/v4/core/schemas.d.cts
CHANGED
|
@@ -54,6 +54,7 @@ export interface _$ZodTypeInternals {
|
|
|
54
54
|
* Todo: unions?
|
|
55
55
|
*/
|
|
56
56
|
values?: util.PrimitiveSet | undefined;
|
|
57
|
+
/** Default value bubbled up from */
|
|
57
58
|
/** @internal A set of literal discriminators used for the fast path in discriminated unions. */
|
|
58
59
|
propValues?: util.PropValues | undefined;
|
|
59
60
|
/** @internal This flag indicates that a schema validation can be represented with a regular expression. Used to determine allowable schemas in z.templateLiteral(). */
|
|
@@ -148,6 +149,7 @@ export declare const $ZodEmail: core.$constructor<$ZodEmail>;
|
|
|
148
149
|
export interface $ZodURLDef extends $ZodStringFormatDef<"url"> {
|
|
149
150
|
hostname?: RegExp | undefined;
|
|
150
151
|
protocol?: RegExp | undefined;
|
|
152
|
+
normalize?: boolean | undefined;
|
|
151
153
|
}
|
|
152
154
|
export interface $ZodURLInternals extends $ZodStringFormatInternals<"url"> {
|
|
153
155
|
def: $ZodURLDef;
|
|
@@ -762,9 +764,12 @@ export interface $ZodLiteral<T extends util.Literal = util.Literal> extends $Zod
|
|
|
762
764
|
}
|
|
763
765
|
export declare const $ZodLiteral: core.$constructor<$ZodLiteral>;
|
|
764
766
|
type _File = typeof globalThis extends {
|
|
765
|
-
File: new (...args: any[]) => any;
|
|
766
|
-
} ? InstanceType<
|
|
767
|
-
|
|
767
|
+
File: infer F extends new (...args: any[]) => any;
|
|
768
|
+
} ? InstanceType<F> : {};
|
|
769
|
+
/** Do not reference this directly. */
|
|
770
|
+
export interface File extends _File {
|
|
771
|
+
type: string;
|
|
772
|
+
size: number;
|
|
768
773
|
}
|
|
769
774
|
export interface $ZodFileDef extends $ZodTypeDef {
|
|
770
775
|
type: "file";
|
|
@@ -902,7 +907,7 @@ export interface $ZodCatchDef<T extends SomeType = $ZodType> extends $ZodTypeDef
|
|
|
902
907
|
innerType: T;
|
|
903
908
|
catchValue: (ctx: $ZodCatchCtx) => unknown;
|
|
904
909
|
}
|
|
905
|
-
export interface $ZodCatchInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<T>, core.input<T
|
|
910
|
+
export interface $ZodCatchInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<T>, core.input<T>> {
|
|
906
911
|
def: $ZodCatchDef<T>;
|
|
907
912
|
optin: T["_zod"]["optin"];
|
|
908
913
|
optout: T["_zod"]["optout"];
|
package/v4/core/schemas.d.ts
CHANGED
|
@@ -54,6 +54,7 @@ export interface _$ZodTypeInternals {
|
|
|
54
54
|
* Todo: unions?
|
|
55
55
|
*/
|
|
56
56
|
values?: util.PrimitiveSet | undefined;
|
|
57
|
+
/** Default value bubbled up from */
|
|
57
58
|
/** @internal A set of literal discriminators used for the fast path in discriminated unions. */
|
|
58
59
|
propValues?: util.PropValues | undefined;
|
|
59
60
|
/** @internal This flag indicates that a schema validation can be represented with a regular expression. Used to determine allowable schemas in z.templateLiteral(). */
|
|
@@ -148,6 +149,7 @@ export declare const $ZodEmail: core.$constructor<$ZodEmail>;
|
|
|
148
149
|
export interface $ZodURLDef extends $ZodStringFormatDef<"url"> {
|
|
149
150
|
hostname?: RegExp | undefined;
|
|
150
151
|
protocol?: RegExp | undefined;
|
|
152
|
+
normalize?: boolean | undefined;
|
|
151
153
|
}
|
|
152
154
|
export interface $ZodURLInternals extends $ZodStringFormatInternals<"url"> {
|
|
153
155
|
def: $ZodURLDef;
|
|
@@ -762,9 +764,12 @@ export interface $ZodLiteral<T extends util.Literal = util.Literal> extends $Zod
|
|
|
762
764
|
}
|
|
763
765
|
export declare const $ZodLiteral: core.$constructor<$ZodLiteral>;
|
|
764
766
|
type _File = typeof globalThis extends {
|
|
765
|
-
File: new (...args: any[]) => any;
|
|
766
|
-
} ? InstanceType<
|
|
767
|
-
|
|
767
|
+
File: infer F extends new (...args: any[]) => any;
|
|
768
|
+
} ? InstanceType<F> : {};
|
|
769
|
+
/** Do not reference this directly. */
|
|
770
|
+
export interface File extends _File {
|
|
771
|
+
type: string;
|
|
772
|
+
size: number;
|
|
768
773
|
}
|
|
769
774
|
export interface $ZodFileDef extends $ZodTypeDef {
|
|
770
775
|
type: "file";
|
|
@@ -902,7 +907,7 @@ export interface $ZodCatchDef<T extends SomeType = $ZodType> extends $ZodTypeDef
|
|
|
902
907
|
innerType: T;
|
|
903
908
|
catchValue: (ctx: $ZodCatchCtx) => unknown;
|
|
904
909
|
}
|
|
905
|
-
export interface $ZodCatchInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<T>, core.input<T
|
|
910
|
+
export interface $ZodCatchInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<T>, core.input<T>> {
|
|
906
911
|
def: $ZodCatchDef<T>;
|
|
907
912
|
optin: T["_zod"]["optin"];
|
|
908
913
|
optout: T["_zod"]["optout"];
|
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,
|
|
@@ -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) => {
|
|
@@ -1596,7 +1556,7 @@ export const $ZodPipe = /*@__PURE__*/ core.$constructor("$ZodPipe", (inst, def)
|
|
|
1596
1556
|
};
|
|
1597
1557
|
});
|
|
1598
1558
|
function handlePipeResult(left, def, ctx) {
|
|
1599
|
-
if (
|
|
1559
|
+
if (left.issues.length) {
|
|
1600
1560
|
return left;
|
|
1601
1561
|
}
|
|
1602
1562
|
return def.out._zod.run({ value: left.value, issues: left.issues }, ctx);
|