zod 4.1.0-canary.20250821T014930 → 4.1.0-canary.20250823T071040
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/index.cjs +17 -7
- package/package.json +1 -1
- package/src/v4/classic/external.ts +0 -1
- package/src/v4/classic/parse.ts +49 -0
- package/src/v4/classic/schemas.ts +145 -7
- package/src/v4/classic/tests/catch.test.ts +25 -0
- package/src/v4/classic/tests/codec-examples.test.ts +538 -0
- package/src/v4/classic/tests/codec.test.ts +532 -0
- package/src/v4/classic/tests/continuability.test.ts +1 -1
- package/src/v4/classic/tests/default.test.ts +32 -0
- package/src/v4/classic/tests/firstparty.test.ts +4 -0
- package/src/v4/classic/tests/function.test.ts +31 -31
- package/src/v4/classic/tests/hash.test.ts +68 -0
- package/src/v4/classic/tests/nonoptional.test.ts +15 -0
- package/src/v4/classic/tests/object.test.ts +31 -0
- package/src/v4/classic/tests/pipe.test.ts +25 -5
- package/src/v4/classic/tests/prefault.test.ts +25 -0
- package/src/v4/classic/tests/preprocess.test.ts +1 -6
- package/src/v4/classic/tests/refine.test.ts +76 -3
- package/src/v4/classic/tests/string-formats.test.ts +16 -0
- package/src/v4/classic/tests/string.test.ts +82 -1
- package/src/v4/classic/tests/stringbool.test.ts +40 -0
- package/src/v4/classic/tests/template-literal.test.ts +1 -1
- package/src/v4/classic/tests/to-json-schema.test.ts +21 -2
- package/src/v4/classic/tests/transform.test.ts +7 -0
- package/src/v4/classic/tests/union.test.ts +1 -1
- package/src/v4/core/api.ts +25 -35
- package/src/v4/core/core.ts +7 -26
- package/src/v4/core/index.ts +0 -1
- package/src/v4/core/json-schema.ts +1 -0
- package/src/v4/core/parse.ts +101 -0
- package/src/v4/core/regexes.ts +40 -1
- package/src/v4/core/schemas.ts +521 -129
- package/src/v4/core/to-json-schema.ts +43 -8
- package/src/v4/core/util.ts +73 -0
- package/src/v4/mini/external.ts +0 -1
- package/src/v4/mini/parse.ts +14 -1
- package/src/v4/mini/schemas.ts +153 -12
- package/src/v4/mini/tests/codec.test.ts +499 -0
- package/src/v4/mini/tests/object.test.ts +9 -0
- package/src/v4/mini/tests/string.test.ts +16 -0
- package/v3/index.cjs +17 -7
- package/v4/classic/coerce.cjs +17 -7
- package/v4/classic/compat.cjs +17 -7
- package/v4/classic/errors.cjs +17 -7
- package/v4/classic/external.cjs +18 -9
- package/v4/classic/external.d.cts +1 -1
- package/v4/classic/external.d.ts +1 -1
- package/v4/classic/external.js +1 -1
- package/v4/classic/index.cjs +17 -7
- package/v4/classic/iso.cjs +17 -7
- package/v4/classic/parse.cjs +27 -8
- package/v4/classic/parse.d.cts +8 -0
- package/v4/classic/parse.d.ts +8 -0
- package/v4/classic/parse.js +9 -0
- package/v4/classic/schemas.cjs +76 -11
- package/v4/classic/schemas.d.cts +48 -2
- package/v4/classic/schemas.d.ts +48 -2
- package/v4/classic/schemas.js +51 -3
- package/v4/core/api.cjs +36 -35
- package/v4/core/api.d.cts +3 -4
- package/v4/core/api.d.ts +3 -4
- package/v4/core/api.js +19 -24
- package/v4/core/checks.cjs +17 -7
- package/v4/core/core.cjs +8 -1
- package/v4/core/core.d.cts +3 -0
- package/v4/core/core.d.ts +3 -0
- package/v4/core/core.js +6 -0
- package/v4/core/errors.cjs +17 -7
- package/v4/core/index.cjs +17 -8
- package/v4/core/index.d.cts +0 -1
- package/v4/core/index.d.ts +0 -1
- package/v4/core/index.js +0 -1
- package/v4/core/json-schema.d.cts +1 -0
- package/v4/core/json-schema.d.ts +1 -0
- package/v4/core/parse.cjs +62 -8
- package/v4/core/parse.d.cts +24 -0
- package/v4/core/parse.d.ts +24 -0
- package/v4/core/parse.js +36 -0
- package/v4/core/regexes.cjs +34 -2
- package/v4/core/regexes.d.cts +16 -0
- package/v4/core/regexes.d.ts +16 -0
- package/v4/core/regexes.js +32 -1
- package/v4/core/schemas.cjs +326 -84
- package/v4/core/schemas.d.cts +61 -3
- package/v4/core/schemas.d.ts +61 -3
- package/v4/core/schemas.js +308 -76
- package/v4/core/to-json-schema.cjs +42 -5
- package/v4/core/to-json-schema.d.cts +4 -3
- package/v4/core/to-json-schema.d.ts +4 -3
- package/v4/core/to-json-schema.js +42 -5
- package/v4/core/util.cjs +69 -0
- package/v4/core/util.d.cts +10 -0
- package/v4/core/util.d.ts +10 -0
- package/v4/core/util.js +62 -0
- package/v4/locales/ar.cjs +17 -7
- package/v4/locales/az.cjs +17 -7
- package/v4/locales/be.cjs +17 -7
- package/v4/locales/bg.cjs +17 -7
- package/v4/locales/ca.cjs +17 -7
- package/v4/locales/cs.cjs +17 -7
- package/v4/locales/da.cjs +17 -7
- package/v4/locales/de.cjs +17 -7
- package/v4/locales/en.cjs +17 -7
- package/v4/locales/eo.cjs +17 -7
- package/v4/locales/es.cjs +17 -7
- package/v4/locales/fa.cjs +17 -7
- package/v4/locales/fi.cjs +17 -7
- package/v4/locales/fr-CA.cjs +17 -7
- package/v4/locales/fr.cjs +17 -7
- package/v4/locales/he.cjs +17 -7
- package/v4/locales/hu.cjs +17 -7
- package/v4/locales/id.cjs +17 -7
- package/v4/locales/is.cjs +17 -7
- package/v4/locales/it.cjs +17 -7
- package/v4/locales/ja.cjs +17 -7
- package/v4/locales/kh.cjs +17 -7
- package/v4/locales/ko.cjs +17 -7
- package/v4/locales/mk.cjs +17 -7
- package/v4/locales/ms.cjs +17 -7
- package/v4/locales/nl.cjs +17 -7
- package/v4/locales/no.cjs +17 -7
- package/v4/locales/ota.cjs +17 -7
- package/v4/locales/pl.cjs +17 -7
- package/v4/locales/ps.cjs +17 -7
- package/v4/locales/pt.cjs +17 -7
- package/v4/locales/ru.cjs +17 -7
- package/v4/locales/sl.cjs +17 -7
- package/v4/locales/sv.cjs +17 -7
- package/v4/locales/ta.cjs +17 -7
- package/v4/locales/th.cjs +17 -7
- package/v4/locales/tr.cjs +17 -7
- package/v4/locales/ua.cjs +17 -7
- package/v4/locales/ur.cjs +17 -7
- package/v4/locales/vi.cjs +17 -7
- package/v4/locales/yo.cjs +17 -7
- package/v4/locales/zh-CN.cjs +17 -7
- package/v4/locales/zh-TW.cjs +17 -7
- package/v4/mini/coerce.cjs +17 -7
- package/v4/mini/external.cjs +18 -9
- package/v4/mini/external.d.cts +1 -1
- package/v4/mini/external.d.ts +1 -1
- package/v4/mini/external.js +1 -1
- package/v4/mini/index.cjs +17 -7
- package/v4/mini/iso.cjs +17 -7
- package/v4/mini/parse.cjs +9 -1
- package/v4/mini/parse.d.cts +1 -1
- package/v4/mini/parse.d.ts +1 -1
- package/v4/mini/parse.js +1 -1
- package/v4/mini/schemas.cjs +75 -10
- package/v4/mini/schemas.d.cts +49 -1
- package/v4/mini/schemas.d.ts +49 -1
- package/v4/mini/schemas.js +49 -2
- package/src/v4/core/function.ts +0 -176
- package/v4/core/function.cjs +0 -102
- package/v4/core/function.d.cts +0 -52
- package/v4/core/function.d.ts +0 -52
- package/v4/core/function.js +0 -75
|
@@ -11,8 +11,9 @@ interface JSONSchemaGeneratorParams {
|
|
|
11
11
|
/** The JSON Schema version to target.
|
|
12
12
|
* - `"draft-2020-12"` — Default. JSON Schema Draft 2020-12
|
|
13
13
|
* - `"draft-7"` — JSON Schema Draft 7
|
|
14
|
-
* - `"draft-4"` — JSON Schema Draft 4
|
|
15
|
-
|
|
14
|
+
* - `"draft-4"` — JSON Schema Draft 4
|
|
15
|
+
* - `"openapi-3.0"` — OpenAPI 3.0 Schema Object */
|
|
16
|
+
target?: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
|
|
16
17
|
/** How to handle unrepresentable types.
|
|
17
18
|
* - `"throw"` — Default. Unrepresentable types throw an error
|
|
18
19
|
* - `"any"` — Unrepresentable types become `{}` */
|
|
@@ -72,7 +73,7 @@ interface Seen {
|
|
|
72
73
|
|
|
73
74
|
export class JSONSchemaGenerator {
|
|
74
75
|
metadataRegistry: $ZodRegistry<Record<string, any>>;
|
|
75
|
-
target: "draft-4" | "draft-7" | "draft-2020-12";
|
|
76
|
+
target: "draft-4" | "draft-7" | "draft-2020-12" | "openapi-3.0";
|
|
76
77
|
unrepresentable: "throw" | "any";
|
|
77
78
|
override: (ctx: {
|
|
78
79
|
zodSchema: schemas.$ZodTypes;
|
|
@@ -164,7 +165,9 @@ export class JSONSchemaGenerator {
|
|
|
164
165
|
else if (regexes.length > 1) {
|
|
165
166
|
result.schema.allOf = [
|
|
166
167
|
...regexes.map((regex) => ({
|
|
167
|
-
...(this.target === "draft-7" || this.target === "draft-4"
|
|
168
|
+
...(this.target === "draft-7" || this.target === "draft-4" || this.target === "openapi-3.0"
|
|
169
|
+
? ({ type: "string" } as const)
|
|
170
|
+
: {}),
|
|
168
171
|
pattern: regex.source,
|
|
169
172
|
})),
|
|
170
173
|
];
|
|
@@ -323,12 +326,24 @@ export class JSONSchemaGenerator {
|
|
|
323
326
|
}
|
|
324
327
|
case "union": {
|
|
325
328
|
const json: JSONSchema.BaseSchema = _json as any;
|
|
326
|
-
|
|
329
|
+
const options = def.options.map((x, i) =>
|
|
327
330
|
this.process(x, {
|
|
328
331
|
...params,
|
|
329
332
|
path: [...params.path, "anyOf", i],
|
|
330
333
|
})
|
|
331
334
|
);
|
|
335
|
+
if (this.target === "openapi-3.0") {
|
|
336
|
+
const nonNull = options.filter((x) => (x as any).type !== "null");
|
|
337
|
+
const hasNull = nonNull.length !== options.length;
|
|
338
|
+
if (nonNull.length === 1) {
|
|
339
|
+
Object.assign(json, nonNull[0]!);
|
|
340
|
+
} else {
|
|
341
|
+
json.anyOf = nonNull;
|
|
342
|
+
}
|
|
343
|
+
if (hasNull) (json as any).nullable = true;
|
|
344
|
+
} else {
|
|
345
|
+
json.anyOf = options;
|
|
346
|
+
}
|
|
332
347
|
break;
|
|
333
348
|
}
|
|
334
349
|
case "intersection": {
|
|
@@ -452,7 +467,7 @@ export class JSONSchemaGenerator {
|
|
|
452
467
|
} else if (vals.length === 1) {
|
|
453
468
|
const val = vals[0]!;
|
|
454
469
|
json.type = val === null ? ("null" as const) : (typeof val as any);
|
|
455
|
-
if (this.target === "draft-4") {
|
|
470
|
+
if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
456
471
|
json.enum = [val];
|
|
457
472
|
} else {
|
|
458
473
|
json.const = val;
|
|
@@ -506,7 +521,13 @@ export class JSONSchemaGenerator {
|
|
|
506
521
|
|
|
507
522
|
case "nullable": {
|
|
508
523
|
const inner = this.process(def.innerType, params);
|
|
509
|
-
|
|
524
|
+
if (this.target === "openapi-3.0") {
|
|
525
|
+
Object.assign(_json, inner);
|
|
526
|
+
(_json as any).nullable = true;
|
|
527
|
+
result.ref = def.innerType;
|
|
528
|
+
} else {
|
|
529
|
+
_json.anyOf = [inner, { type: "null" }];
|
|
530
|
+
}
|
|
510
531
|
break;
|
|
511
532
|
}
|
|
512
533
|
case "nonoptional": {
|
|
@@ -594,6 +615,12 @@ export class JSONSchemaGenerator {
|
|
|
594
615
|
}
|
|
595
616
|
break;
|
|
596
617
|
}
|
|
618
|
+
case "function": {
|
|
619
|
+
if (this.unrepresentable === "throw") {
|
|
620
|
+
throw new Error("Function types cannot be represented in JSON Schema");
|
|
621
|
+
}
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
597
624
|
default: {
|
|
598
625
|
def satisfies never;
|
|
599
626
|
}
|
|
@@ -773,7 +800,10 @@ export class JSONSchemaGenerator {
|
|
|
773
800
|
|
|
774
801
|
// merge referenced schema into current
|
|
775
802
|
const refSchema = this.seen.get(ref)!.schema;
|
|
776
|
-
if (
|
|
803
|
+
if (
|
|
804
|
+
refSchema.$ref &&
|
|
805
|
+
(params.target === "draft-7" || params.target === "draft-4" || params.target === "openapi-3.0")
|
|
806
|
+
) {
|
|
777
807
|
schema.allOf = schema.allOf ?? [];
|
|
778
808
|
schema.allOf.push(refSchema);
|
|
779
809
|
} else {
|
|
@@ -802,6 +832,8 @@ export class JSONSchemaGenerator {
|
|
|
802
832
|
result.$schema = "http://json-schema.org/draft-07/schema#";
|
|
803
833
|
} else if (this.target === "draft-4") {
|
|
804
834
|
result.$schema = "http://json-schema.org/draft-04/schema#";
|
|
835
|
+
} else if (this.target === "openapi-3.0") {
|
|
836
|
+
// OpenAPI 3.0 schema objects should not include a $schema property
|
|
805
837
|
} else {
|
|
806
838
|
// @ts-ignore
|
|
807
839
|
console.warn(`Invalid target: ${this.target}`);
|
|
@@ -996,6 +1028,9 @@ function isTransforming(
|
|
|
996
1028
|
case "catch": {
|
|
997
1029
|
return false;
|
|
998
1030
|
}
|
|
1031
|
+
case "function": {
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
999
1034
|
|
|
1000
1035
|
default:
|
|
1001
1036
|
def satisfies never;
|
package/src/v4/core/util.ts
CHANGED
|
@@ -20,6 +20,10 @@ export type JWTAlgorithm =
|
|
|
20
20
|
| "PS512"
|
|
21
21
|
| "EdDSA"
|
|
22
22
|
| (string & {});
|
|
23
|
+
|
|
24
|
+
export type HashAlgorithm = "md5" | "sha1" | "sha256" | "sha384" | "sha512";
|
|
25
|
+
export type HashEncoding = "hex" | "base64" | "base64url";
|
|
26
|
+
export type HashFormat = `${HashAlgorithm}_${HashEncoding}`;
|
|
23
27
|
export type IPVersion = "v4" | "v6";
|
|
24
28
|
export type MimeTypes =
|
|
25
29
|
| "application/json"
|
|
@@ -625,6 +629,12 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
|
|
|
625
629
|
throw new Error("Invalid input to extend: expected a plain object");
|
|
626
630
|
}
|
|
627
631
|
|
|
632
|
+
const checks = schema._zod.def.checks;
|
|
633
|
+
const hasChecks = checks && checks.length > 0;
|
|
634
|
+
if (hasChecks) {
|
|
635
|
+
throw new Error("Object schemas containing refinements cannot be extended. Use `.safeExtend()` instead.");
|
|
636
|
+
}
|
|
637
|
+
|
|
628
638
|
const def = mergeDefs(schema._zod.def, {
|
|
629
639
|
get shape() {
|
|
630
640
|
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
@@ -636,6 +646,22 @@ export function extend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): an
|
|
|
636
646
|
return clone(schema, def) as any;
|
|
637
647
|
}
|
|
638
648
|
|
|
649
|
+
export function safeExtend(schema: schemas.$ZodObject, shape: schemas.$ZodShape): any {
|
|
650
|
+
if (!isPlainObject(shape)) {
|
|
651
|
+
throw new Error("Invalid input to safeExtend: expected a plain object");
|
|
652
|
+
}
|
|
653
|
+
const def = {
|
|
654
|
+
...schema._zod.def,
|
|
655
|
+
get shape() {
|
|
656
|
+
const _shape = { ...schema._zod.def.shape, ...shape };
|
|
657
|
+
assignProp(this, "shape", _shape); // self-caching
|
|
658
|
+
return _shape;
|
|
659
|
+
},
|
|
660
|
+
checks: schema._zod.def.checks,
|
|
661
|
+
} as any;
|
|
662
|
+
return clone(schema, def) as any;
|
|
663
|
+
}
|
|
664
|
+
|
|
639
665
|
export function merge(a: schemas.$ZodObject, b: schemas.$ZodObject): any {
|
|
640
666
|
const def = mergeDefs(a._zod.def, {
|
|
641
667
|
get shape() {
|
|
@@ -742,6 +768,7 @@ export type Constructor<T, Def extends any[] = any[]> = new (...args: Def) => T;
|
|
|
742
768
|
|
|
743
769
|
// invalid_type | too_big | too_small | invalid_format | not_multiple_of | unrecognized_keys | invalid_union | invalid_key | invalid_element | invalid_value | custom
|
|
744
770
|
export function aborted(x: schemas.ParsePayload, startIndex = 0): boolean {
|
|
771
|
+
if (x.aborted === true) return true;
|
|
745
772
|
for (let i = startIndex; i < x.issues.length; i++) {
|
|
746
773
|
if (x.issues[i]?.continue !== true) {
|
|
747
774
|
return true;
|
|
@@ -830,6 +857,52 @@ export function cleanEnum(obj: Record<string, EnumValue>): EnumValue[] {
|
|
|
830
857
|
.map((el) => el[1]);
|
|
831
858
|
}
|
|
832
859
|
|
|
860
|
+
// Codec utility functions
|
|
861
|
+
export function base64ToUint8Array(base64: string): Uint8Array<ArrayBuffer> {
|
|
862
|
+
const binaryString = atob(base64);
|
|
863
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
864
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
865
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
866
|
+
}
|
|
867
|
+
return bytes;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
export function uint8ArrayToBase64(bytes: Uint8Array): string {
|
|
871
|
+
let binaryString = "";
|
|
872
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
873
|
+
binaryString += String.fromCharCode(bytes[i]);
|
|
874
|
+
}
|
|
875
|
+
return btoa(binaryString);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
export function base64urlToUint8Array(base64url: string): Uint8Array<ArrayBuffer> {
|
|
879
|
+
const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/");
|
|
880
|
+
const padding = "=".repeat((4 - (base64.length % 4)) % 4);
|
|
881
|
+
return base64ToUint8Array(base64 + padding);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
export function uint8ArrayToBase64url(bytes: Uint8Array): string {
|
|
885
|
+
return uint8ArrayToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
export function hexToUint8Array(hex: string): Uint8Array<ArrayBuffer> {
|
|
889
|
+
const cleanHex = hex.replace(/^0x/, "");
|
|
890
|
+
if (cleanHex.length % 2 !== 0) {
|
|
891
|
+
throw new Error("Invalid hex string length");
|
|
892
|
+
}
|
|
893
|
+
const bytes = new Uint8Array(cleanHex.length / 2);
|
|
894
|
+
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
895
|
+
bytes[i / 2] = Number.parseInt(cleanHex.slice(i, i + 2), 16);
|
|
896
|
+
}
|
|
897
|
+
return bytes;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
export function uint8ArrayToHex(bytes: Uint8Array): string {
|
|
901
|
+
return Array.from(bytes)
|
|
902
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
903
|
+
.join("");
|
|
904
|
+
}
|
|
905
|
+
|
|
833
906
|
// instanceof
|
|
834
907
|
export abstract class Class {
|
|
835
908
|
constructor(..._args: any[]) {}
|
package/src/v4/mini/external.ts
CHANGED
package/src/v4/mini/parse.ts
CHANGED
|
@@ -1 +1,14 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
parse,
|
|
3
|
+
safeParse,
|
|
4
|
+
parseAsync,
|
|
5
|
+
safeParseAsync,
|
|
6
|
+
encode,
|
|
7
|
+
decode,
|
|
8
|
+
encodeAsync,
|
|
9
|
+
decodeAsync,
|
|
10
|
+
safeEncode,
|
|
11
|
+
safeDecode,
|
|
12
|
+
safeEncodeAsync,
|
|
13
|
+
safeDecodeAsync,
|
|
14
|
+
} from "../core/index.js";
|
package/src/v4/mini/schemas.ts
CHANGED
|
@@ -185,6 +185,14 @@ export function url(params?: string | core.$ZodURLParams): ZodMiniURL {
|
|
|
185
185
|
return core._url(ZodMiniURL, params);
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
export function httpUrl(params?: string | Omit<core.$ZodURLParams, "protocol" | "hostname">): ZodMiniURL {
|
|
189
|
+
return core._url(ZodMiniURL, {
|
|
190
|
+
protocol: /^https?$/,
|
|
191
|
+
hostname: core.regexes.domain,
|
|
192
|
+
...util.normalizeParams(params),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
188
196
|
// ZodMiniEmoji
|
|
189
197
|
export interface ZodMiniEmoji extends _ZodMiniString<core.$ZodEmojiInternals> {
|
|
190
198
|
// _zod: core.$ZodEmojiInternals;
|
|
@@ -443,6 +451,24 @@ export function hostname(_params?: string | core.$ZodStringFormatParams): ZodMin
|
|
|
443
451
|
return core._stringFormat(ZodMiniCustomStringFormat, "hostname", core.regexes.hostname, _params) as any;
|
|
444
452
|
}
|
|
445
453
|
|
|
454
|
+
export function hex(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hex"> {
|
|
455
|
+
return core._stringFormat(ZodMiniCustomStringFormat, "hex", core.regexes.hex, _params) as any;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function hash<Alg extends util.HashAlgorithm, Enc extends util.HashEncoding = "hex">(
|
|
459
|
+
alg: Alg,
|
|
460
|
+
params?: {
|
|
461
|
+
enc?: Enc;
|
|
462
|
+
} & core.$ZodStringFormatParams
|
|
463
|
+
): ZodMiniCustomStringFormat<`${Alg}_${Enc}`> {
|
|
464
|
+
const enc = params?.enc ?? "hex";
|
|
465
|
+
const format = `${alg}_${enc}` as const;
|
|
466
|
+
const regex = core.regexes[format as keyof typeof core.regexes] as RegExp;
|
|
467
|
+
// check for unrecognized format
|
|
468
|
+
if (!regex) throw new Error(`Unrecognized hash format: ${format}`);
|
|
469
|
+
return core._stringFormat(ZodMiniCustomStringFormat, format, regex, params) as any;
|
|
470
|
+
}
|
|
471
|
+
|
|
446
472
|
// ZodMiniNumber
|
|
447
473
|
interface _ZodMiniNumber<T extends core.$ZodNumberInternals<unknown> = core.$ZodNumberInternals<unknown>>
|
|
448
474
|
extends _ZodMiniType<T>,
|
|
@@ -797,6 +823,23 @@ export function extend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(
|
|
|
797
823
|
return util.extend(schema, shape);
|
|
798
824
|
}
|
|
799
825
|
|
|
826
|
+
export type SafeExtendShape<Base extends core.$ZodShape, Ext extends core.$ZodLooseShape> = {
|
|
827
|
+
[K in keyof Ext]: K extends keyof Base
|
|
828
|
+
? core.output<Ext[K]> extends core.output<Base[K]>
|
|
829
|
+
? core.input<Ext[K]> extends core.input<Base[K]>
|
|
830
|
+
? Ext[K]
|
|
831
|
+
: never
|
|
832
|
+
: never
|
|
833
|
+
: Ext[K];
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
export function safeExtend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(
|
|
837
|
+
schema: T,
|
|
838
|
+
shape: SafeExtendShape<T["shape"], U>
|
|
839
|
+
): ZodMiniObject<util.Extend<T["shape"], U>, T["_zod"]["config"]> {
|
|
840
|
+
return util.safeExtend(schema, shape as any);
|
|
841
|
+
}
|
|
842
|
+
|
|
800
843
|
/** @deprecated Identical to `z.extend(A, B)` */
|
|
801
844
|
export function merge<T extends ZodMiniObject, U extends ZodMiniObject>(
|
|
802
845
|
a: T,
|
|
@@ -1087,12 +1130,15 @@ export function set<Value extends SomeType>(valueType: Value, params?: string |
|
|
|
1087
1130
|
// ZodMiniEnum
|
|
1088
1131
|
export interface ZodMiniEnum<T extends util.EnumLike = util.EnumLike> extends _ZodMiniType<core.$ZodEnumInternals<T>> {
|
|
1089
1132
|
// _zod: core.$ZodEnumInternals<T>;
|
|
1133
|
+
options: Array<T[keyof T]>;
|
|
1090
1134
|
}
|
|
1091
1135
|
export const ZodMiniEnum: core.$constructor<ZodMiniEnum> = /*@__PURE__*/ core.$constructor(
|
|
1092
1136
|
"ZodMiniEnum",
|
|
1093
1137
|
(inst, def) => {
|
|
1094
1138
|
core.$ZodEnum.init(inst, def);
|
|
1095
1139
|
ZodMiniType.init(inst, def);
|
|
1140
|
+
|
|
1141
|
+
inst.options = Object.values(def.entries);
|
|
1096
1142
|
}
|
|
1097
1143
|
);
|
|
1098
1144
|
|
|
@@ -1395,6 +1441,38 @@ export function pipe<
|
|
|
1395
1441
|
}) as any;
|
|
1396
1442
|
}
|
|
1397
1443
|
|
|
1444
|
+
// ZodMiniCodec
|
|
1445
|
+
export interface ZodMiniCodec<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType>
|
|
1446
|
+
extends ZodMiniPipe<A, B>,
|
|
1447
|
+
core.$ZodCodec<A, B> {
|
|
1448
|
+
_zod: core.$ZodCodecInternals<A, B>;
|
|
1449
|
+
def: core.$ZodCodecDef<A, B>;
|
|
1450
|
+
}
|
|
1451
|
+
export const ZodMiniCodec: core.$constructor<ZodMiniCodec> = /*@__PURE__*/ core.$constructor(
|
|
1452
|
+
"ZodMiniCodec",
|
|
1453
|
+
(inst, def) => {
|
|
1454
|
+
ZodMiniPipe.init(inst, def);
|
|
1455
|
+
core.$ZodCodec.init(inst, def);
|
|
1456
|
+
}
|
|
1457
|
+
);
|
|
1458
|
+
|
|
1459
|
+
export function codec<const A extends SomeType, B extends core.SomeType = core.$ZodType>(
|
|
1460
|
+
in_: A,
|
|
1461
|
+
out: B,
|
|
1462
|
+
params: {
|
|
1463
|
+
decode: (value: core.output<A>, payload: core.ParsePayload<core.output<A>>) => core.input<B>;
|
|
1464
|
+
encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.output<A>;
|
|
1465
|
+
}
|
|
1466
|
+
): ZodMiniCodec<A, B> {
|
|
1467
|
+
return new ZodMiniCodec({
|
|
1468
|
+
type: "pipe",
|
|
1469
|
+
in: in_ as any as core.$ZodType,
|
|
1470
|
+
out: out as any as core.$ZodType,
|
|
1471
|
+
transform: params.decode as any,
|
|
1472
|
+
reverseTransform: params.encode as any,
|
|
1473
|
+
}) as any;
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1398
1476
|
// /** @deprecated Use `z.pipe()` and `z.transform()` instead. */
|
|
1399
1477
|
// export function preprocess<A, U extends core.$ZodType>(
|
|
1400
1478
|
// fn: (arg: unknown, ctx: core.ParsePayload) => A,
|
|
@@ -1554,18 +1632,16 @@ function _instanceof<T extends typeof Class>(
|
|
|
1554
1632
|
export { _instanceof as instanceof };
|
|
1555
1633
|
|
|
1556
1634
|
// stringbool
|
|
1557
|
-
export const stringbool: (
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
...args
|
|
1568
|
-
) as any;
|
|
1635
|
+
export const stringbool: (_params?: string | core.$ZodStringBoolParams) => ZodMiniCodec<ZodMiniString, ZodMiniBoolean> =
|
|
1636
|
+
(...args) =>
|
|
1637
|
+
core._stringbool(
|
|
1638
|
+
{
|
|
1639
|
+
Codec: ZodMiniCodec,
|
|
1640
|
+
Boolean: ZodMiniBoolean,
|
|
1641
|
+
String: ZodMiniString,
|
|
1642
|
+
},
|
|
1643
|
+
...args
|
|
1644
|
+
) as any;
|
|
1569
1645
|
|
|
1570
1646
|
// json
|
|
1571
1647
|
|
|
@@ -1596,3 +1672,68 @@ export function json(): ZodMiniJSONSchema {
|
|
|
1596
1672
|
});
|
|
1597
1673
|
return jsonSchema;
|
|
1598
1674
|
}
|
|
1675
|
+
|
|
1676
|
+
// ZodMiniFunction
|
|
1677
|
+
export interface ZodMiniFunction<
|
|
1678
|
+
Args extends core.$ZodFunctionIn = core.$ZodFunctionIn,
|
|
1679
|
+
Returns extends core.$ZodFunctionOut = core.$ZodFunctionOut,
|
|
1680
|
+
> extends _ZodMiniType<core.$ZodFunctionInternals<Args, Returns>>,
|
|
1681
|
+
core.$ZodFunction<Args, Returns> {
|
|
1682
|
+
_def: core.$ZodFunctionDef<Args, Returns>;
|
|
1683
|
+
_input: core.$InferInnerFunctionType<Args, Returns>;
|
|
1684
|
+
_output: core.$InferOuterFunctionType<Args, Returns>;
|
|
1685
|
+
|
|
1686
|
+
input<const Items extends util.TupleItems, const Rest extends core.$ZodFunctionOut = core.$ZodFunctionOut>(
|
|
1687
|
+
args: Items,
|
|
1688
|
+
rest?: Rest
|
|
1689
|
+
): ZodMiniFunction<ZodMiniTuple<Items, Rest>, Returns>;
|
|
1690
|
+
input<NewArgs extends core.$ZodFunctionIn>(args: NewArgs): ZodMiniFunction<NewArgs, Returns>;
|
|
1691
|
+
input(...args: any[]): ZodMiniFunction<any, Returns>;
|
|
1692
|
+
|
|
1693
|
+
output<NewReturns extends core.$ZodFunctionOut>(output: NewReturns): ZodMiniFunction<Args, NewReturns>;
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
export const ZodMiniFunction: core.$constructor<ZodMiniFunction> = /*@__PURE__*/ core.$constructor(
|
|
1697
|
+
"ZodMiniFunction",
|
|
1698
|
+
(inst, def) => {
|
|
1699
|
+
core.$ZodFunction.init(inst, def);
|
|
1700
|
+
ZodMiniType.init(inst, def);
|
|
1701
|
+
}
|
|
1702
|
+
);
|
|
1703
|
+
|
|
1704
|
+
export function _function(): ZodMiniFunction;
|
|
1705
|
+
export function _function<const In extends Array<SomeType> = Array<SomeType>>(params: {
|
|
1706
|
+
input: In;
|
|
1707
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, core.$ZodFunctionOut>;
|
|
1708
|
+
export function _function<
|
|
1709
|
+
const In extends Array<SomeType> = Array<SomeType>,
|
|
1710
|
+
const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut,
|
|
1711
|
+
>(params: {
|
|
1712
|
+
input: In;
|
|
1713
|
+
output: Out;
|
|
1714
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, Out>;
|
|
1715
|
+
export function _function<const In extends core.$ZodFunctionIn = core.$ZodFunctionIn>(params: {
|
|
1716
|
+
input: In;
|
|
1717
|
+
}): ZodMiniFunction<In, core.$ZodFunctionOut>;
|
|
1718
|
+
export function _function<const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
|
1719
|
+
output: Out;
|
|
1720
|
+
}): ZodMiniFunction<core.$ZodFunctionIn, Out>;
|
|
1721
|
+
export function _function<
|
|
1722
|
+
In extends core.$ZodFunctionIn = core.$ZodFunctionIn,
|
|
1723
|
+
Out extends core.$ZodFunctionOut = core.$ZodFunctionOut,
|
|
1724
|
+
>(params?: {
|
|
1725
|
+
input: In;
|
|
1726
|
+
output: Out;
|
|
1727
|
+
}): ZodMiniFunction<In, Out>;
|
|
1728
|
+
export function _function(params?: {
|
|
1729
|
+
output?: core.$ZodFunctionOut;
|
|
1730
|
+
input?: core.$ZodFunctionArgs | Array<SomeType>;
|
|
1731
|
+
}): ZodMiniFunction {
|
|
1732
|
+
return new ZodMiniFunction({
|
|
1733
|
+
type: "function",
|
|
1734
|
+
input: Array.isArray(params?.input) ? tuple(params?.input as any) : (params?.input ?? array(unknown())),
|
|
1735
|
+
output: params?.output ?? unknown(),
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
export { _function as function };
|