zod 4.1.0-canary.20250821T014902 → 4.1.0-canary.20250823T064644
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/object.test.ts +2 -2
- 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/array.test.ts +6 -6
- 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 +33 -2
- 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 +78 -5
- package/src/v4/classic/tests/set.test.ts +1 -1
- 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/v4/classic/external.cjs +1 -2
- 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/parse.cjs +10 -1
- 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 +59 -4
- 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 +19 -24
- 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/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/index.cjs +0 -1
- 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 +45 -1
- 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 +309 -77
- 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/mini/external.cjs +1 -2
- 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/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 +58 -3
- 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
package/v4/mini/schemas.cjs
CHANGED
|
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.ZodMiniOptional = exports.ZodMiniTransform = exports.ZodMiniFile = exports.ZodMiniLiteral = exports.ZodMiniEnum = exports.ZodMiniSet = exports.ZodMiniMap = exports.ZodMiniRecord = exports.ZodMiniTuple = exports.ZodMiniIntersection = exports.ZodMiniDiscriminatedUnion = exports.ZodMiniUnion = exports.ZodMiniObject = exports.ZodMiniArray = exports.ZodMiniDate = exports.ZodMiniVoid = exports.ZodMiniNever = exports.ZodMiniUnknown = exports.ZodMiniAny = exports.ZodMiniNull = exports.ZodMiniUndefined = exports.ZodMiniSymbol = exports.ZodMiniBigIntFormat = exports.ZodMiniBigInt = exports.ZodMiniBoolean = exports.ZodMiniNumberFormat = exports.ZodMiniNumber = exports.ZodMiniCustomStringFormat = exports.ZodMiniJWT = exports.ZodMiniE164 = exports.ZodMiniBase64URL = exports.ZodMiniBase64 = exports.ZodMiniCIDRv6 = exports.ZodMiniCIDRv4 = exports.ZodMiniIPv6 = exports.ZodMiniIPv4 = exports.ZodMiniKSUID = exports.ZodMiniXID = exports.ZodMiniULID = exports.ZodMiniCUID2 = exports.ZodMiniCUID = exports.ZodMiniNanoID = exports.ZodMiniEmoji = exports.ZodMiniURL = exports.ZodMiniUUID = exports.ZodMiniGUID = exports.ZodMiniEmail = exports.ZodMiniStringFormat = exports.ZodMiniString = exports.ZodMiniType = void 0;
|
|
27
|
-
exports.stringbool = exports.ZodMiniCustom = exports.ZodMiniPromise = exports.ZodMiniLazy = exports.ZodMiniTemplateLiteral = exports.ZodMiniReadonly = exports.ZodMiniPipe = exports.ZodMiniNaN = exports.ZodMiniCatch = exports.ZodMiniSuccess = exports.ZodMiniNonOptional = exports.ZodMiniPrefault = exports.ZodMiniDefault = exports.ZodMiniNullable = void 0;
|
|
27
|
+
exports.ZodMiniFunction = exports.stringbool = exports.ZodMiniCustom = exports.ZodMiniPromise = exports.ZodMiniLazy = exports.ZodMiniTemplateLiteral = exports.ZodMiniReadonly = exports.ZodMiniCodec = exports.ZodMiniPipe = exports.ZodMiniNaN = exports.ZodMiniCatch = exports.ZodMiniSuccess = exports.ZodMiniNonOptional = exports.ZodMiniPrefault = exports.ZodMiniDefault = exports.ZodMiniNullable = void 0;
|
|
28
28
|
exports.string = string;
|
|
29
29
|
exports.email = email;
|
|
30
30
|
exports.guid = guid;
|
|
@@ -33,6 +33,7 @@ exports.uuidv4 = uuidv4;
|
|
|
33
33
|
exports.uuidv6 = uuidv6;
|
|
34
34
|
exports.uuidv7 = uuidv7;
|
|
35
35
|
exports.url = url;
|
|
36
|
+
exports.httpUrl = httpUrl;
|
|
36
37
|
exports.emoji = emoji;
|
|
37
38
|
exports.nanoid = nanoid;
|
|
38
39
|
exports.cuid = cuid;
|
|
@@ -50,6 +51,8 @@ exports.e164 = e164;
|
|
|
50
51
|
exports.jwt = jwt;
|
|
51
52
|
exports.stringFormat = stringFormat;
|
|
52
53
|
exports.hostname = hostname;
|
|
54
|
+
exports.hex = hex;
|
|
55
|
+
exports.hash = hash;
|
|
53
56
|
exports.number = number;
|
|
54
57
|
exports.int = int;
|
|
55
58
|
exports.float32 = float32;
|
|
@@ -74,6 +77,7 @@ exports.object = object;
|
|
|
74
77
|
exports.strictObject = strictObject;
|
|
75
78
|
exports.looseObject = looseObject;
|
|
76
79
|
exports.extend = extend;
|
|
80
|
+
exports.safeExtend = safeExtend;
|
|
77
81
|
exports.merge = merge;
|
|
78
82
|
exports.pick = pick;
|
|
79
83
|
exports.omit = omit;
|
|
@@ -103,6 +107,7 @@ exports.success = success;
|
|
|
103
107
|
exports.catch = _catch;
|
|
104
108
|
exports.nan = nan;
|
|
105
109
|
exports.pipe = pipe;
|
|
110
|
+
exports.codec = codec;
|
|
106
111
|
exports.readonly = readonly;
|
|
107
112
|
exports.templateLiteral = templateLiteral;
|
|
108
113
|
exports.lazy = _lazy;
|
|
@@ -113,6 +118,10 @@ exports.refine = refine;
|
|
|
113
118
|
exports.superRefine = superRefine;
|
|
114
119
|
exports.instanceof = _instanceof;
|
|
115
120
|
exports.json = json;
|
|
121
|
+
exports._function = _function;
|
|
122
|
+
exports.function = _function;
|
|
123
|
+
exports._function = _function;
|
|
124
|
+
exports.function = _function;
|
|
116
125
|
const core = __importStar(require("../core/index.cjs"));
|
|
117
126
|
const index_js_1 = require("../core/index.cjs");
|
|
118
127
|
const parse = __importStar(require("./parse.cjs"));
|
|
@@ -194,6 +203,13 @@ exports.ZodMiniURL = core.$constructor("ZodMiniURL", (inst, def) => {
|
|
|
194
203
|
function url(params) {
|
|
195
204
|
return core._url(exports.ZodMiniURL, params);
|
|
196
205
|
}
|
|
206
|
+
function httpUrl(params) {
|
|
207
|
+
return core._url(exports.ZodMiniURL, {
|
|
208
|
+
protocol: /^https?$/,
|
|
209
|
+
hostname: core.regexes.domain,
|
|
210
|
+
...index_js_1.util.normalizeParams(params),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
197
213
|
exports.ZodMiniEmoji = core.$constructor("ZodMiniEmoji", (inst, def) => {
|
|
198
214
|
core.$ZodEmoji.init(inst, def);
|
|
199
215
|
exports.ZodMiniStringFormat.init(inst, def);
|
|
@@ -309,6 +325,18 @@ function stringFormat(format, fnOrRegex, _params = {}) {
|
|
|
309
325
|
function hostname(_params) {
|
|
310
326
|
return core._stringFormat(exports.ZodMiniCustomStringFormat, "hostname", core.regexes.hostname, _params);
|
|
311
327
|
}
|
|
328
|
+
function hex(_params) {
|
|
329
|
+
return core._stringFormat(exports.ZodMiniCustomStringFormat, "hex", core.regexes.hex, _params);
|
|
330
|
+
}
|
|
331
|
+
function hash(alg, params) {
|
|
332
|
+
const enc = params?.enc ?? "hex";
|
|
333
|
+
const format = `${alg}_${enc}`;
|
|
334
|
+
const regex = core.regexes[format];
|
|
335
|
+
// check for unrecognized format
|
|
336
|
+
if (!regex)
|
|
337
|
+
throw new Error(`Unrecognized hash format: ${format}`);
|
|
338
|
+
return core._stringFormat(exports.ZodMiniCustomStringFormat, format, regex, params);
|
|
339
|
+
}
|
|
312
340
|
exports.ZodMiniNumber = core.$constructor("ZodMiniNumber", (inst, def) => {
|
|
313
341
|
core.$ZodNumber.init(inst, def);
|
|
314
342
|
exports.ZodMiniType.init(inst, def);
|
|
@@ -487,6 +515,9 @@ function looseObject(shape, params) {
|
|
|
487
515
|
function extend(schema, shape) {
|
|
488
516
|
return index_js_1.util.extend(schema, shape);
|
|
489
517
|
}
|
|
518
|
+
function safeExtend(schema, shape) {
|
|
519
|
+
return index_js_1.util.safeExtend(schema, shape);
|
|
520
|
+
}
|
|
490
521
|
function merge(schema, shape) {
|
|
491
522
|
return index_js_1.util.extend(schema, shape);
|
|
492
523
|
}
|
|
@@ -603,6 +634,7 @@ function set(valueType, params) {
|
|
|
603
634
|
exports.ZodMiniEnum = core.$constructor("ZodMiniEnum", (inst, def) => {
|
|
604
635
|
core.$ZodEnum.init(inst, def);
|
|
605
636
|
exports.ZodMiniType.init(inst, def);
|
|
637
|
+
inst.options = Object.values(def.entries);
|
|
606
638
|
});
|
|
607
639
|
function _enum(values, params) {
|
|
608
640
|
const entries = Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values;
|
|
@@ -754,6 +786,19 @@ function pipe(in_, out) {
|
|
|
754
786
|
out: out,
|
|
755
787
|
});
|
|
756
788
|
}
|
|
789
|
+
exports.ZodMiniCodec = core.$constructor("ZodMiniCodec", (inst, def) => {
|
|
790
|
+
exports.ZodMiniPipe.init(inst, def);
|
|
791
|
+
core.$ZodCodec.init(inst, def);
|
|
792
|
+
});
|
|
793
|
+
function codec(in_, out, params) {
|
|
794
|
+
return new exports.ZodMiniCodec({
|
|
795
|
+
type: "pipe",
|
|
796
|
+
in: in_,
|
|
797
|
+
out: out,
|
|
798
|
+
transform: params.decode,
|
|
799
|
+
reverseTransform: params.encode,
|
|
800
|
+
});
|
|
801
|
+
}
|
|
757
802
|
exports.ZodMiniReadonly = core.$constructor("ZodMiniReadonly", (inst, def) => {
|
|
758
803
|
core.$ZodReadonly.init(inst, def);
|
|
759
804
|
exports.ZodMiniType.init(inst, def);
|
|
@@ -837,10 +882,9 @@ function _instanceof(cls, params = {
|
|
|
837
882
|
}
|
|
838
883
|
// stringbool
|
|
839
884
|
const stringbool = (...args) => core._stringbool({
|
|
840
|
-
|
|
885
|
+
Codec: exports.ZodMiniCodec,
|
|
841
886
|
Boolean: exports.ZodMiniBoolean,
|
|
842
887
|
String: exports.ZodMiniString,
|
|
843
|
-
Transform: exports.ZodMiniTransform,
|
|
844
888
|
}, ...args);
|
|
845
889
|
exports.stringbool = stringbool;
|
|
846
890
|
function json() {
|
|
@@ -849,3 +893,14 @@ function json() {
|
|
|
849
893
|
});
|
|
850
894
|
return jsonSchema;
|
|
851
895
|
}
|
|
896
|
+
exports.ZodMiniFunction = core.$constructor("ZodMiniFunction", (inst, def) => {
|
|
897
|
+
core.$ZodFunction.init(inst, def);
|
|
898
|
+
exports.ZodMiniType.init(inst, def);
|
|
899
|
+
});
|
|
900
|
+
function _function(params) {
|
|
901
|
+
return new exports.ZodMiniFunction({
|
|
902
|
+
type: "function",
|
|
903
|
+
input: Array.isArray(params?.input) ? tuple(params?.input) : (params?.input ?? array(unknown())),
|
|
904
|
+
output: params?.output ?? unknown(),
|
|
905
|
+
});
|
|
906
|
+
}
|
package/v4/mini/schemas.d.cts
CHANGED
|
@@ -47,6 +47,7 @@ export interface ZodMiniURL extends _ZodMiniString<core.$ZodURLInternals> {
|
|
|
47
47
|
}
|
|
48
48
|
export declare const ZodMiniURL: core.$constructor<ZodMiniURL>;
|
|
49
49
|
export declare function url(params?: string | core.$ZodURLParams): ZodMiniURL;
|
|
50
|
+
export declare function httpUrl(params?: string | Omit<core.$ZodURLParams, "protocol" | "hostname">): ZodMiniURL;
|
|
50
51
|
export interface ZodMiniEmoji extends _ZodMiniString<core.$ZodEmojiInternals> {
|
|
51
52
|
}
|
|
52
53
|
export declare const ZodMiniEmoji: core.$constructor<ZodMiniEmoji>;
|
|
@@ -113,6 +114,10 @@ export interface ZodMiniCustomStringFormat<Format extends string = string> exten
|
|
|
113
114
|
export declare const ZodMiniCustomStringFormat: core.$constructor<ZodMiniCustomStringFormat>;
|
|
114
115
|
export declare function stringFormat<Format extends string>(format: Format, fnOrRegex: ((arg: string) => util.MaybeAsync<unknown>) | RegExp, _params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<Format>;
|
|
115
116
|
export declare function hostname(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hostname">;
|
|
117
|
+
export declare function hex(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hex">;
|
|
118
|
+
export declare function hash<Alg extends util.HashAlgorithm, Enc extends util.HashEncoding = "hex">(alg: Alg, params?: {
|
|
119
|
+
enc?: Enc;
|
|
120
|
+
} & core.$ZodStringFormatParams): ZodMiniCustomStringFormat<`${Alg}_${Enc}`>;
|
|
116
121
|
interface _ZodMiniNumber<T extends core.$ZodNumberInternals<unknown> = core.$ZodNumberInternals<unknown>> extends _ZodMiniType<T>, core.$ZodNumber<T["input"]> {
|
|
117
122
|
_zod: T;
|
|
118
123
|
}
|
|
@@ -191,6 +196,10 @@ export declare function object<T extends core.$ZodLooseShape = Record<never, Som
|
|
|
191
196
|
export declare function strictObject<T extends core.$ZodLooseShape>(shape: T, params?: string | core.$ZodObjectParams): ZodMiniObject<T, core.$strict>;
|
|
192
197
|
export declare function looseObject<T extends core.$ZodLooseShape>(shape: T, params?: string | core.$ZodObjectParams): ZodMiniObject<T, core.$loose>;
|
|
193
198
|
export declare function extend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(schema: T, shape: U): ZodMiniObject<util.Extend<T["shape"], U>, T["_zod"]["config"]>;
|
|
199
|
+
export type SafeExtendShape<Base extends core.$ZodShape, Ext extends core.$ZodLooseShape> = {
|
|
200
|
+
[K in keyof Ext]: K extends keyof Base ? core.output<Ext[K]> extends core.output<Base[K]> ? core.input<Ext[K]> extends core.input<Base[K]> ? Ext[K] : never : never : Ext[K];
|
|
201
|
+
};
|
|
202
|
+
export declare function safeExtend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(schema: T, shape: SafeExtendShape<T["shape"], U>): ZodMiniObject<util.Extend<T["shape"], U>, T["_zod"]["config"]>;
|
|
194
203
|
/** @deprecated Identical to `z.extend(A, B)` */
|
|
195
204
|
export declare function merge<T extends ZodMiniObject, U extends ZodMiniObject>(a: T, b: U): ZodMiniObject<util.Extend<T["shape"], U["shape"]>, T["_zod"]["config"]>;
|
|
196
205
|
export declare function pick<T extends ZodMiniObject, M extends util.Mask<keyof T["shape"]>>(schema: T, mask: M): ZodMiniObject<util.Flatten<Pick<T["shape"], keyof T["shape"] & keyof M>>, T["_zod"]["config"]>;
|
|
@@ -246,6 +255,7 @@ export interface ZodMiniSet<T extends SomeType = core.$ZodType> extends _ZodMini
|
|
|
246
255
|
export declare const ZodMiniSet: core.$constructor<ZodMiniSet>;
|
|
247
256
|
export declare function set<Value extends SomeType>(valueType: Value, params?: string | core.$ZodSetParams): ZodMiniSet<Value>;
|
|
248
257
|
export interface ZodMiniEnum<T extends util.EnumLike = util.EnumLike> extends _ZodMiniType<core.$ZodEnumInternals<T>> {
|
|
258
|
+
options: Array<T[keyof T]>;
|
|
249
259
|
}
|
|
250
260
|
export declare const ZodMiniEnum: core.$constructor<ZodMiniEnum>;
|
|
251
261
|
declare function _enum<const T extends readonly string[]>(values: T, params?: string | core.$ZodEnumParams): ZodMiniEnum<util.ToEnum<T[number]>>;
|
|
@@ -310,6 +320,15 @@ export interface ZodMiniPipe<A extends SomeType = core.$ZodType, B extends SomeT
|
|
|
310
320
|
}
|
|
311
321
|
export declare const ZodMiniPipe: core.$constructor<ZodMiniPipe>;
|
|
312
322
|
export declare function pipe<const A extends SomeType, B extends core.$ZodType<unknown, core.output<A>> = core.$ZodType<unknown, core.output<A>>>(in_: A, out: B | core.$ZodType<unknown, core.output<A>>): ZodMiniPipe<A, B>;
|
|
323
|
+
export interface ZodMiniCodec<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends ZodMiniPipe<A, B>, core.$ZodCodec<A, B> {
|
|
324
|
+
_zod: core.$ZodCodecInternals<A, B>;
|
|
325
|
+
def: core.$ZodCodecDef<A, B>;
|
|
326
|
+
}
|
|
327
|
+
export declare const ZodMiniCodec: core.$constructor<ZodMiniCodec>;
|
|
328
|
+
export declare function codec<const A extends SomeType, B extends core.SomeType = core.$ZodType>(in_: A, out: B, params: {
|
|
329
|
+
decode: (value: core.output<A>, payload: core.ParsePayload<core.output<A>>) => core.input<B>;
|
|
330
|
+
encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.output<A>;
|
|
331
|
+
}): ZodMiniCodec<A, B>;
|
|
313
332
|
export interface ZodMiniReadonly<T extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodReadonlyInternals<T>> {
|
|
314
333
|
}
|
|
315
334
|
export declare const ZodMiniReadonly: core.$constructor<ZodMiniReadonly>;
|
|
@@ -339,7 +358,7 @@ declare abstract class Class {
|
|
|
339
358
|
}
|
|
340
359
|
declare function _instanceof<T extends typeof Class>(cls: T, params?: core.$ZodCustomParams): ZodMiniCustom<InstanceType<T>, InstanceType<T>>;
|
|
341
360
|
export { _instanceof as instanceof };
|
|
342
|
-
export declare const stringbool: (_params?: string | core.$ZodStringBoolParams) =>
|
|
361
|
+
export declare const stringbool: (_params?: string | core.$ZodStringBoolParams) => ZodMiniCodec<ZodMiniString, ZodMiniBoolean>;
|
|
343
362
|
type _ZodMiniJSONSchema = ZodMiniUnion<[
|
|
344
363
|
ZodMiniString,
|
|
345
364
|
ZodMiniNumber,
|
|
@@ -357,3 +376,32 @@ export interface ZodMiniJSONSchema extends _ZodMiniJSONSchema {
|
|
|
357
376
|
_zod: ZodMiniJSONSchemaInternals;
|
|
358
377
|
}
|
|
359
378
|
export declare function json(): ZodMiniJSONSchema;
|
|
379
|
+
export interface ZodMiniFunction<Args extends core.$ZodFunctionIn = core.$ZodFunctionIn, Returns extends core.$ZodFunctionOut = core.$ZodFunctionOut> extends _ZodMiniType<core.$ZodFunctionInternals<Args, Returns>>, core.$ZodFunction<Args, Returns> {
|
|
380
|
+
_def: core.$ZodFunctionDef<Args, Returns>;
|
|
381
|
+
_input: core.$InferInnerFunctionType<Args, Returns>;
|
|
382
|
+
_output: core.$InferOuterFunctionType<Args, Returns>;
|
|
383
|
+
input<const Items extends util.TupleItems, const Rest extends core.$ZodFunctionOut = core.$ZodFunctionOut>(args: Items, rest?: Rest): ZodMiniFunction<ZodMiniTuple<Items, Rest>, Returns>;
|
|
384
|
+
input<NewArgs extends core.$ZodFunctionIn>(args: NewArgs): ZodMiniFunction<NewArgs, Returns>;
|
|
385
|
+
input(...args: any[]): ZodMiniFunction<any, Returns>;
|
|
386
|
+
output<NewReturns extends core.$ZodFunctionOut>(output: NewReturns): ZodMiniFunction<Args, NewReturns>;
|
|
387
|
+
}
|
|
388
|
+
export declare const ZodMiniFunction: core.$constructor<ZodMiniFunction>;
|
|
389
|
+
export declare function _function(): ZodMiniFunction;
|
|
390
|
+
export declare function _function<const In extends Array<SomeType> = Array<SomeType>>(params: {
|
|
391
|
+
input: In;
|
|
392
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, core.$ZodFunctionOut>;
|
|
393
|
+
export declare function _function<const In extends Array<SomeType> = Array<SomeType>, const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
|
394
|
+
input: In;
|
|
395
|
+
output: Out;
|
|
396
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, Out>;
|
|
397
|
+
export declare function _function<const In extends core.$ZodFunctionIn = core.$ZodFunctionIn>(params: {
|
|
398
|
+
input: In;
|
|
399
|
+
}): ZodMiniFunction<In, core.$ZodFunctionOut>;
|
|
400
|
+
export declare function _function<const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
|
401
|
+
output: Out;
|
|
402
|
+
}): ZodMiniFunction<core.$ZodFunctionIn, Out>;
|
|
403
|
+
export declare function _function<In extends core.$ZodFunctionIn = core.$ZodFunctionIn, Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params?: {
|
|
404
|
+
input: In;
|
|
405
|
+
output: Out;
|
|
406
|
+
}): ZodMiniFunction<In, Out>;
|
|
407
|
+
export { _function as function };
|
package/v4/mini/schemas.d.ts
CHANGED
|
@@ -47,6 +47,7 @@ export interface ZodMiniURL extends _ZodMiniString<core.$ZodURLInternals> {
|
|
|
47
47
|
}
|
|
48
48
|
export declare const ZodMiniURL: core.$constructor<ZodMiniURL>;
|
|
49
49
|
export declare function url(params?: string | core.$ZodURLParams): ZodMiniURL;
|
|
50
|
+
export declare function httpUrl(params?: string | Omit<core.$ZodURLParams, "protocol" | "hostname">): ZodMiniURL;
|
|
50
51
|
export interface ZodMiniEmoji extends _ZodMiniString<core.$ZodEmojiInternals> {
|
|
51
52
|
}
|
|
52
53
|
export declare const ZodMiniEmoji: core.$constructor<ZodMiniEmoji>;
|
|
@@ -113,6 +114,10 @@ export interface ZodMiniCustomStringFormat<Format extends string = string> exten
|
|
|
113
114
|
export declare const ZodMiniCustomStringFormat: core.$constructor<ZodMiniCustomStringFormat>;
|
|
114
115
|
export declare function stringFormat<Format extends string>(format: Format, fnOrRegex: ((arg: string) => util.MaybeAsync<unknown>) | RegExp, _params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<Format>;
|
|
115
116
|
export declare function hostname(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hostname">;
|
|
117
|
+
export declare function hex(_params?: string | core.$ZodStringFormatParams): ZodMiniCustomStringFormat<"hex">;
|
|
118
|
+
export declare function hash<Alg extends util.HashAlgorithm, Enc extends util.HashEncoding = "hex">(alg: Alg, params?: {
|
|
119
|
+
enc?: Enc;
|
|
120
|
+
} & core.$ZodStringFormatParams): ZodMiniCustomStringFormat<`${Alg}_${Enc}`>;
|
|
116
121
|
interface _ZodMiniNumber<T extends core.$ZodNumberInternals<unknown> = core.$ZodNumberInternals<unknown>> extends _ZodMiniType<T>, core.$ZodNumber<T["input"]> {
|
|
117
122
|
_zod: T;
|
|
118
123
|
}
|
|
@@ -191,6 +196,10 @@ export declare function object<T extends core.$ZodLooseShape = Record<never, Som
|
|
|
191
196
|
export declare function strictObject<T extends core.$ZodLooseShape>(shape: T, params?: string | core.$ZodObjectParams): ZodMiniObject<T, core.$strict>;
|
|
192
197
|
export declare function looseObject<T extends core.$ZodLooseShape>(shape: T, params?: string | core.$ZodObjectParams): ZodMiniObject<T, core.$loose>;
|
|
193
198
|
export declare function extend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(schema: T, shape: U): ZodMiniObject<util.Extend<T["shape"], U>, T["_zod"]["config"]>;
|
|
199
|
+
export type SafeExtendShape<Base extends core.$ZodShape, Ext extends core.$ZodLooseShape> = {
|
|
200
|
+
[K in keyof Ext]: K extends keyof Base ? core.output<Ext[K]> extends core.output<Base[K]> ? core.input<Ext[K]> extends core.input<Base[K]> ? Ext[K] : never : never : Ext[K];
|
|
201
|
+
};
|
|
202
|
+
export declare function safeExtend<T extends ZodMiniObject, U extends core.$ZodLooseShape>(schema: T, shape: SafeExtendShape<T["shape"], U>): ZodMiniObject<util.Extend<T["shape"], U>, T["_zod"]["config"]>;
|
|
194
203
|
/** @deprecated Identical to `z.extend(A, B)` */
|
|
195
204
|
export declare function merge<T extends ZodMiniObject, U extends ZodMiniObject>(a: T, b: U): ZodMiniObject<util.Extend<T["shape"], U["shape"]>, T["_zod"]["config"]>;
|
|
196
205
|
export declare function pick<T extends ZodMiniObject, M extends util.Mask<keyof T["shape"]>>(schema: T, mask: M): ZodMiniObject<util.Flatten<Pick<T["shape"], keyof T["shape"] & keyof M>>, T["_zod"]["config"]>;
|
|
@@ -246,6 +255,7 @@ export interface ZodMiniSet<T extends SomeType = core.$ZodType> extends _ZodMini
|
|
|
246
255
|
export declare const ZodMiniSet: core.$constructor<ZodMiniSet>;
|
|
247
256
|
export declare function set<Value extends SomeType>(valueType: Value, params?: string | core.$ZodSetParams): ZodMiniSet<Value>;
|
|
248
257
|
export interface ZodMiniEnum<T extends util.EnumLike = util.EnumLike> extends _ZodMiniType<core.$ZodEnumInternals<T>> {
|
|
258
|
+
options: Array<T[keyof T]>;
|
|
249
259
|
}
|
|
250
260
|
export declare const ZodMiniEnum: core.$constructor<ZodMiniEnum>;
|
|
251
261
|
declare function _enum<const T extends readonly string[]>(values: T, params?: string | core.$ZodEnumParams): ZodMiniEnum<util.ToEnum<T[number]>>;
|
|
@@ -310,6 +320,15 @@ export interface ZodMiniPipe<A extends SomeType = core.$ZodType, B extends SomeT
|
|
|
310
320
|
}
|
|
311
321
|
export declare const ZodMiniPipe: core.$constructor<ZodMiniPipe>;
|
|
312
322
|
export declare function pipe<const A extends SomeType, B extends core.$ZodType<unknown, core.output<A>> = core.$ZodType<unknown, core.output<A>>>(in_: A, out: B | core.$ZodType<unknown, core.output<A>>): ZodMiniPipe<A, B>;
|
|
323
|
+
export interface ZodMiniCodec<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends ZodMiniPipe<A, B>, core.$ZodCodec<A, B> {
|
|
324
|
+
_zod: core.$ZodCodecInternals<A, B>;
|
|
325
|
+
def: core.$ZodCodecDef<A, B>;
|
|
326
|
+
}
|
|
327
|
+
export declare const ZodMiniCodec: core.$constructor<ZodMiniCodec>;
|
|
328
|
+
export declare function codec<const A extends SomeType, B extends core.SomeType = core.$ZodType>(in_: A, out: B, params: {
|
|
329
|
+
decode: (value: core.output<A>, payload: core.ParsePayload<core.output<A>>) => core.input<B>;
|
|
330
|
+
encode: (value: core.input<B>, payload: core.ParsePayload<core.input<B>>) => core.output<A>;
|
|
331
|
+
}): ZodMiniCodec<A, B>;
|
|
313
332
|
export interface ZodMiniReadonly<T extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodReadonlyInternals<T>> {
|
|
314
333
|
}
|
|
315
334
|
export declare const ZodMiniReadonly: core.$constructor<ZodMiniReadonly>;
|
|
@@ -339,7 +358,7 @@ declare abstract class Class {
|
|
|
339
358
|
}
|
|
340
359
|
declare function _instanceof<T extends typeof Class>(cls: T, params?: core.$ZodCustomParams): ZodMiniCustom<InstanceType<T>, InstanceType<T>>;
|
|
341
360
|
export { _instanceof as instanceof };
|
|
342
|
-
export declare const stringbool: (_params?: string | core.$ZodStringBoolParams) =>
|
|
361
|
+
export declare const stringbool: (_params?: string | core.$ZodStringBoolParams) => ZodMiniCodec<ZodMiniString, ZodMiniBoolean>;
|
|
343
362
|
type _ZodMiniJSONSchema = ZodMiniUnion<[
|
|
344
363
|
ZodMiniString,
|
|
345
364
|
ZodMiniNumber,
|
|
@@ -357,3 +376,32 @@ export interface ZodMiniJSONSchema extends _ZodMiniJSONSchema {
|
|
|
357
376
|
_zod: ZodMiniJSONSchemaInternals;
|
|
358
377
|
}
|
|
359
378
|
export declare function json(): ZodMiniJSONSchema;
|
|
379
|
+
export interface ZodMiniFunction<Args extends core.$ZodFunctionIn = core.$ZodFunctionIn, Returns extends core.$ZodFunctionOut = core.$ZodFunctionOut> extends _ZodMiniType<core.$ZodFunctionInternals<Args, Returns>>, core.$ZodFunction<Args, Returns> {
|
|
380
|
+
_def: core.$ZodFunctionDef<Args, Returns>;
|
|
381
|
+
_input: core.$InferInnerFunctionType<Args, Returns>;
|
|
382
|
+
_output: core.$InferOuterFunctionType<Args, Returns>;
|
|
383
|
+
input<const Items extends util.TupleItems, const Rest extends core.$ZodFunctionOut = core.$ZodFunctionOut>(args: Items, rest?: Rest): ZodMiniFunction<ZodMiniTuple<Items, Rest>, Returns>;
|
|
384
|
+
input<NewArgs extends core.$ZodFunctionIn>(args: NewArgs): ZodMiniFunction<NewArgs, Returns>;
|
|
385
|
+
input(...args: any[]): ZodMiniFunction<any, Returns>;
|
|
386
|
+
output<NewReturns extends core.$ZodFunctionOut>(output: NewReturns): ZodMiniFunction<Args, NewReturns>;
|
|
387
|
+
}
|
|
388
|
+
export declare const ZodMiniFunction: core.$constructor<ZodMiniFunction>;
|
|
389
|
+
export declare function _function(): ZodMiniFunction;
|
|
390
|
+
export declare function _function<const In extends Array<SomeType> = Array<SomeType>>(params: {
|
|
391
|
+
input: In;
|
|
392
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, core.$ZodFunctionOut>;
|
|
393
|
+
export declare function _function<const In extends Array<SomeType> = Array<SomeType>, const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
|
394
|
+
input: In;
|
|
395
|
+
output: Out;
|
|
396
|
+
}): ZodMiniFunction<ZodMiniTuple<In, null>, Out>;
|
|
397
|
+
export declare function _function<const In extends core.$ZodFunctionIn = core.$ZodFunctionIn>(params: {
|
|
398
|
+
input: In;
|
|
399
|
+
}): ZodMiniFunction<In, core.$ZodFunctionOut>;
|
|
400
|
+
export declare function _function<const Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params: {
|
|
401
|
+
output: Out;
|
|
402
|
+
}): ZodMiniFunction<core.$ZodFunctionIn, Out>;
|
|
403
|
+
export declare function _function<In extends core.$ZodFunctionIn = core.$ZodFunctionIn, Out extends core.$ZodFunctionOut = core.$ZodFunctionOut>(params?: {
|
|
404
|
+
input: In;
|
|
405
|
+
output: Out;
|
|
406
|
+
}): ZodMiniFunction<In, Out>;
|
|
407
|
+
export { _function as function };
|
package/v4/mini/schemas.js
CHANGED
|
@@ -79,6 +79,13 @@ export const ZodMiniURL = /*@__PURE__*/ core.$constructor("ZodMiniURL", (inst, d
|
|
|
79
79
|
export function url(params) {
|
|
80
80
|
return core._url(ZodMiniURL, params);
|
|
81
81
|
}
|
|
82
|
+
export function httpUrl(params) {
|
|
83
|
+
return core._url(ZodMiniURL, {
|
|
84
|
+
protocol: /^https?$/,
|
|
85
|
+
hostname: core.regexes.domain,
|
|
86
|
+
...util.normalizeParams(params),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
82
89
|
export const ZodMiniEmoji = /*@__PURE__*/ core.$constructor("ZodMiniEmoji", (inst, def) => {
|
|
83
90
|
core.$ZodEmoji.init(inst, def);
|
|
84
91
|
ZodMiniStringFormat.init(inst, def);
|
|
@@ -194,6 +201,18 @@ export function stringFormat(format, fnOrRegex, _params = {}) {
|
|
|
194
201
|
export function hostname(_params) {
|
|
195
202
|
return core._stringFormat(ZodMiniCustomStringFormat, "hostname", core.regexes.hostname, _params);
|
|
196
203
|
}
|
|
204
|
+
export function hex(_params) {
|
|
205
|
+
return core._stringFormat(ZodMiniCustomStringFormat, "hex", core.regexes.hex, _params);
|
|
206
|
+
}
|
|
207
|
+
export function hash(alg, params) {
|
|
208
|
+
const enc = params?.enc ?? "hex";
|
|
209
|
+
const format = `${alg}_${enc}`;
|
|
210
|
+
const regex = core.regexes[format];
|
|
211
|
+
// check for unrecognized format
|
|
212
|
+
if (!regex)
|
|
213
|
+
throw new Error(`Unrecognized hash format: ${format}`);
|
|
214
|
+
return core._stringFormat(ZodMiniCustomStringFormat, format, regex, params);
|
|
215
|
+
}
|
|
197
216
|
export const ZodMiniNumber = /*@__PURE__*/ core.$constructor("ZodMiniNumber", (inst, def) => {
|
|
198
217
|
core.$ZodNumber.init(inst, def);
|
|
199
218
|
ZodMiniType.init(inst, def);
|
|
@@ -375,6 +394,9 @@ export function looseObject(shape, params) {
|
|
|
375
394
|
export function extend(schema, shape) {
|
|
376
395
|
return util.extend(schema, shape);
|
|
377
396
|
}
|
|
397
|
+
export function safeExtend(schema, shape) {
|
|
398
|
+
return util.safeExtend(schema, shape);
|
|
399
|
+
}
|
|
378
400
|
export function merge(schema, shape) {
|
|
379
401
|
return util.extend(schema, shape);
|
|
380
402
|
}
|
|
@@ -491,6 +513,7 @@ export function set(valueType, params) {
|
|
|
491
513
|
export const ZodMiniEnum = /*@__PURE__*/ core.$constructor("ZodMiniEnum", (inst, def) => {
|
|
492
514
|
core.$ZodEnum.init(inst, def);
|
|
493
515
|
ZodMiniType.init(inst, def);
|
|
516
|
+
inst.options = Object.values(def.entries);
|
|
494
517
|
});
|
|
495
518
|
function _enum(values, params) {
|
|
496
519
|
const entries = Array.isArray(values) ? Object.fromEntries(values.map((v) => [v, v])) : values;
|
|
@@ -644,6 +667,19 @@ export function pipe(in_, out) {
|
|
|
644
667
|
out: out,
|
|
645
668
|
});
|
|
646
669
|
}
|
|
670
|
+
export const ZodMiniCodec = /*@__PURE__*/ core.$constructor("ZodMiniCodec", (inst, def) => {
|
|
671
|
+
ZodMiniPipe.init(inst, def);
|
|
672
|
+
core.$ZodCodec.init(inst, def);
|
|
673
|
+
});
|
|
674
|
+
export function codec(in_, out, params) {
|
|
675
|
+
return new ZodMiniCodec({
|
|
676
|
+
type: "pipe",
|
|
677
|
+
in: in_,
|
|
678
|
+
out: out,
|
|
679
|
+
transform: params.decode,
|
|
680
|
+
reverseTransform: params.encode,
|
|
681
|
+
});
|
|
682
|
+
}
|
|
647
683
|
export const ZodMiniReadonly = /*@__PURE__*/ core.$constructor("ZodMiniReadonly", (inst, def) => {
|
|
648
684
|
core.$ZodReadonly.init(inst, def);
|
|
649
685
|
ZodMiniType.init(inst, def);
|
|
@@ -729,10 +765,9 @@ function _instanceof(cls, params = {
|
|
|
729
765
|
export { _instanceof as instanceof };
|
|
730
766
|
// stringbool
|
|
731
767
|
export const stringbool = (...args) => core._stringbool({
|
|
732
|
-
|
|
768
|
+
Codec: ZodMiniCodec,
|
|
733
769
|
Boolean: ZodMiniBoolean,
|
|
734
770
|
String: ZodMiniString,
|
|
735
|
-
Transform: ZodMiniTransform,
|
|
736
771
|
}, ...args);
|
|
737
772
|
export function json() {
|
|
738
773
|
const jsonSchema = _lazy(() => {
|
|
@@ -740,3 +775,15 @@ export function json() {
|
|
|
740
775
|
});
|
|
741
776
|
return jsonSchema;
|
|
742
777
|
}
|
|
778
|
+
export const ZodMiniFunction = /*@__PURE__*/ core.$constructor("ZodMiniFunction", (inst, def) => {
|
|
779
|
+
core.$ZodFunction.init(inst, def);
|
|
780
|
+
ZodMiniType.init(inst, def);
|
|
781
|
+
});
|
|
782
|
+
export function _function(params) {
|
|
783
|
+
return new ZodMiniFunction({
|
|
784
|
+
type: "function",
|
|
785
|
+
input: Array.isArray(params?.input) ? tuple(params?.input) : (params?.input ?? array(unknown())),
|
|
786
|
+
output: params?.output ?? unknown(),
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
export { _function as function };
|
package/src/v4/core/function.ts
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import { _array, _tuple, _unknown } from "./api.js";
|
|
2
|
-
import type * as core from "./core.js";
|
|
3
|
-
import { parse, parseAsync } from "./parse.js";
|
|
4
|
-
import * as schemas from "./schemas.js";
|
|
5
|
-
import { $ZodTuple } from "./schemas.js";
|
|
6
|
-
import type * as util from "./util.js";
|
|
7
|
-
|
|
8
|
-
//////////////////////////////////////////
|
|
9
|
-
//////////////////////////////////////////
|
|
10
|
-
////////// //////////
|
|
11
|
-
////////// $ZodFunction //////////
|
|
12
|
-
////////// //////////
|
|
13
|
-
//////////////////////////////////////////
|
|
14
|
-
//////////////////////////////////////////
|
|
15
|
-
export interface $ZodFunctionDef<
|
|
16
|
-
In extends $ZodFunctionIn = $ZodFunctionIn,
|
|
17
|
-
Out extends $ZodFunctionOut = $ZodFunctionOut,
|
|
18
|
-
> {
|
|
19
|
-
type: "function";
|
|
20
|
-
input: In;
|
|
21
|
-
output: Out;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type $ZodFunctionArgs = schemas.$ZodType<unknown[], unknown[]>;
|
|
25
|
-
export type $ZodFunctionIn = $ZodFunctionArgs;
|
|
26
|
-
export type $ZodFunctionOut = schemas.$ZodType;
|
|
27
|
-
|
|
28
|
-
export type $InferInnerFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (
|
|
29
|
-
...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>
|
|
30
|
-
) => core.input<Returns>;
|
|
31
|
-
|
|
32
|
-
export type $InferInnerFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (
|
|
33
|
-
...args: $ZodFunctionIn extends Args ? never[] : core.output<Args>
|
|
34
|
-
) => util.MaybeAsync<core.input<Returns>>;
|
|
35
|
-
|
|
36
|
-
export type $InferOuterFunctionType<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (
|
|
37
|
-
...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>
|
|
38
|
-
) => core.output<Returns>;
|
|
39
|
-
|
|
40
|
-
export type $InferOuterFunctionTypeAsync<Args extends $ZodFunctionIn, Returns extends $ZodFunctionOut> = (
|
|
41
|
-
...args: $ZodFunctionIn extends Args ? never[] : core.input<Args>
|
|
42
|
-
) => util.MaybeAsync<core.output<Returns>>;
|
|
43
|
-
|
|
44
|
-
export class $ZodFunction<
|
|
45
|
-
Args extends $ZodFunctionIn = $ZodFunctionIn,
|
|
46
|
-
Returns extends $ZodFunctionOut = $ZodFunctionOut,
|
|
47
|
-
> {
|
|
48
|
-
def: $ZodFunctionDef<Args, Returns>;
|
|
49
|
-
|
|
50
|
-
/** @deprecated */
|
|
51
|
-
_def!: $ZodFunctionDef<Args, Returns>;
|
|
52
|
-
_input!: $InferInnerFunctionType<Args, Returns>;
|
|
53
|
-
_output!: $InferOuterFunctionType<Args, Returns>;
|
|
54
|
-
|
|
55
|
-
constructor(def: $ZodFunctionDef<Args, Returns>) {
|
|
56
|
-
this._def = def;
|
|
57
|
-
this.def = def;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
implement<F extends $InferInnerFunctionType<Args, Returns>>(
|
|
61
|
-
func: F
|
|
62
|
-
): // allow for return type inference
|
|
63
|
-
(
|
|
64
|
-
...args: Parameters<this["_output"]>
|
|
65
|
-
) => ReturnType<F> extends ReturnType<this["_output"]> ? ReturnType<F> : ReturnType<this["_output"]> {
|
|
66
|
-
if (typeof func !== "function") {
|
|
67
|
-
throw new Error("implement() must be called with a function");
|
|
68
|
-
}
|
|
69
|
-
const impl = ((...args: any[]) => {
|
|
70
|
-
const parsedArgs = this._def.input ? parse(this._def.input, args, undefined, { callee: impl }) : args;
|
|
71
|
-
if (!Array.isArray(parsedArgs)) {
|
|
72
|
-
throw new Error("Invalid arguments schema: not an array or tuple schema.");
|
|
73
|
-
}
|
|
74
|
-
const output = func(...(parsedArgs as any));
|
|
75
|
-
return this._def.output ? parse(this._def.output, output, undefined, { callee: impl }) : output;
|
|
76
|
-
}) as any;
|
|
77
|
-
return impl;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
implementAsync<F extends $InferInnerFunctionTypeAsync<Args, Returns>>(
|
|
81
|
-
func: F
|
|
82
|
-
): F extends $InferOuterFunctionTypeAsync<Args, Returns> ? F : $InferOuterFunctionTypeAsync<Args, Returns> {
|
|
83
|
-
if (typeof func !== "function") {
|
|
84
|
-
throw new Error("implement() must be called with a function");
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const impl = (async (...args: any[]) => {
|
|
88
|
-
const parsedArgs = this._def.input ? await parseAsync(this._def.input, args, undefined, { callee: impl }) : args;
|
|
89
|
-
if (!Array.isArray(parsedArgs)) {
|
|
90
|
-
throw new Error("Invalid arguments schema: not an array or tuple schema.");
|
|
91
|
-
}
|
|
92
|
-
const output = await func(...(parsedArgs as any));
|
|
93
|
-
return this._def.output ? parseAsync(this._def.output, output, undefined, { callee: impl }) : output;
|
|
94
|
-
}) as any;
|
|
95
|
-
return impl;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
input<const Items extends util.TupleItems, const Rest extends $ZodFunctionOut = $ZodFunctionOut>(
|
|
99
|
-
args: Items,
|
|
100
|
-
rest?: Rest
|
|
101
|
-
): $ZodFunction<schemas.$ZodTuple<Items, Rest>, Returns>;
|
|
102
|
-
input<NewArgs extends $ZodFunctionIn>(args: NewArgs): $ZodFunction<NewArgs, Returns>;
|
|
103
|
-
input(...args: any[]): $ZodFunction<any, Returns> {
|
|
104
|
-
const F: any = this.constructor;
|
|
105
|
-
if (Array.isArray(args[0])) {
|
|
106
|
-
return new F({
|
|
107
|
-
type: "function",
|
|
108
|
-
input: new $ZodTuple({
|
|
109
|
-
type: "tuple",
|
|
110
|
-
items: args[0],
|
|
111
|
-
rest: args[1],
|
|
112
|
-
}),
|
|
113
|
-
output: this._def.output,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return new F({
|
|
118
|
-
type: "function",
|
|
119
|
-
input: args[0],
|
|
120
|
-
output: this._def.output,
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
output<NewReturns extends schemas.$ZodType>(output: NewReturns): $ZodFunction<Args, NewReturns> {
|
|
125
|
-
const F: any = this.constructor;
|
|
126
|
-
return new F({
|
|
127
|
-
type: "function",
|
|
128
|
-
input: this._def.input,
|
|
129
|
-
output,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export interface $ZodFunctionParams<I extends $ZodFunctionIn, O extends schemas.$ZodType> {
|
|
135
|
-
input?: I;
|
|
136
|
-
output?: O;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function _function(): $ZodFunction;
|
|
140
|
-
function _function<const In extends Array<schemas.$ZodType> = Array<schemas.$ZodType>>(params: {
|
|
141
|
-
input: In;
|
|
142
|
-
}): $ZodFunction<$ZodTuple<In, null>, $ZodFunctionOut>;
|
|
143
|
-
function _function<
|
|
144
|
-
const In extends Array<schemas.$ZodType> = Array<schemas.$ZodType>,
|
|
145
|
-
const Out extends $ZodFunctionOut = $ZodFunctionOut,
|
|
146
|
-
>(params: {
|
|
147
|
-
input: In;
|
|
148
|
-
output: Out;
|
|
149
|
-
}): $ZodFunction<$ZodTuple<In, null>, Out>;
|
|
150
|
-
function _function<const In extends $ZodFunctionIn = $ZodFunctionIn>(params: {
|
|
151
|
-
input: In;
|
|
152
|
-
}): $ZodFunction<In, $ZodFunctionOut>;
|
|
153
|
-
function _function<const Out extends $ZodFunctionOut = $ZodFunctionOut>(params: {
|
|
154
|
-
output: Out;
|
|
155
|
-
}): $ZodFunction<$ZodFunctionIn, Out>;
|
|
156
|
-
function _function<
|
|
157
|
-
In extends $ZodFunctionIn = $ZodFunctionIn,
|
|
158
|
-
Out extends schemas.$ZodType = schemas.$ZodType,
|
|
159
|
-
>(params?: {
|
|
160
|
-
input: In;
|
|
161
|
-
output: Out;
|
|
162
|
-
}): $ZodFunction<In, Out>;
|
|
163
|
-
function _function(params?: {
|
|
164
|
-
output?: schemas.$ZodType;
|
|
165
|
-
input?: $ZodFunctionArgs | Array<schemas.$ZodType>;
|
|
166
|
-
}): any {
|
|
167
|
-
return new $ZodFunction({
|
|
168
|
-
type: "function",
|
|
169
|
-
input: Array.isArray(params?.input)
|
|
170
|
-
? _tuple(schemas.$ZodTuple, params?.input as any)
|
|
171
|
-
: (params?.input ?? _array(schemas.$ZodArray, _unknown(schemas.$ZodUnknown))),
|
|
172
|
-
output: params?.output ?? _unknown(schemas.$ZodUnknown),
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export { _function as function };
|