mirror of
https://github.com/swc-project/swc.git
synced 2024-12-20 20:22:26 +03:00
bbaf619f63
swc_bundler: - [x] Fix wrapped esms. (denoland/deno#9307) - [x] Make test secure.
96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
// Loaded from https://raw.githubusercontent.com/colinhacks/zod/654680afc2ede388e71e09104eac5a0088fe3207/deno/lib/types/function.ts
|
|
|
|
|
|
import { ZodTypes } from "../ZodTypes.ts";
|
|
import { ZodType, ZodTypeDef, ZodTypeAny } from "./base.ts";
|
|
|
|
import { ZodTuple } from "./tuple.ts";
|
|
import { ZodUnknown } from "./unknown.ts";
|
|
|
|
export interface ZodFunctionDef<
|
|
Args extends ZodTuple<any> = ZodTuple<any>,
|
|
Returns extends ZodTypeAny = ZodTypeAny
|
|
> extends ZodTypeDef {
|
|
t: ZodTypes.function;
|
|
args: Args;
|
|
returns: Returns;
|
|
}
|
|
|
|
export type OuterTypeOfFunction<
|
|
Args extends ZodTuple<any>,
|
|
Returns extends ZodTypeAny
|
|
> = Args["_input"] extends Array<any>
|
|
? (...args: Args["_input"]) => Returns["_output"]
|
|
: never;
|
|
|
|
export type InnerTypeOfFunction<
|
|
Args extends ZodTuple<any>,
|
|
Returns extends ZodTypeAny
|
|
> = Args["_output"] extends Array<any>
|
|
? (...args: Args["_output"]) => Returns["_input"]
|
|
: never;
|
|
|
|
// type as df = string extends unknown ? true : false
|
|
export class ZodFunction<
|
|
Args extends ZodTuple<any>,
|
|
Returns extends ZodTypeAny
|
|
> extends ZodType<
|
|
OuterTypeOfFunction<Args, Returns>,
|
|
ZodFunctionDef,
|
|
InnerTypeOfFunction<Args, Returns>
|
|
> {
|
|
readonly _def!: ZodFunctionDef<Args, Returns>;
|
|
// readonly _type!: TypeOfFunction<Args, Returns>;
|
|
|
|
args = <Items extends Parameters<typeof ZodTuple["create"]>[0]>(
|
|
...items: Items
|
|
): ZodFunction<ZodTuple<Items>, Returns> => {
|
|
return new ZodFunction({
|
|
...this._def,
|
|
args: ZodTuple.create(items),
|
|
});
|
|
};
|
|
|
|
returns = <NewReturnType extends ZodType<any, any>>(
|
|
returnType: NewReturnType
|
|
): ZodFunction<Args, NewReturnType> => {
|
|
return new ZodFunction({
|
|
...this._def,
|
|
returns: returnType,
|
|
});
|
|
};
|
|
|
|
implement = <F extends InnerTypeOfFunction<Args, Returns>>(func: F): F => {
|
|
const validatedFunc = this.parse(func);
|
|
return validatedFunc as any;
|
|
};
|
|
|
|
validate = this.implement;
|
|
|
|
static create = <
|
|
T extends ZodTuple<any> = ZodTuple<[]>,
|
|
U extends ZodTypeAny = ZodUnknown
|
|
>(
|
|
args?: T,
|
|
returns?: U
|
|
): ZodFunction<T, U> => {
|
|
return new ZodFunction({
|
|
t: ZodTypes.function,
|
|
args: args || ZodTuple.create([]),
|
|
returns: returns || ZodUnknown.create(),
|
|
});
|
|
};
|
|
|
|
// opt optional: () => ZodUnion<[this, ZodUndefined]> = () => ZodUnion.create([this, ZodUndefined.create()]);
|
|
|
|
// null nullable: () => ZodUnion<[this, ZodNull]> = () => ZodUnion.create([this, ZodNull.create()]);
|
|
|
|
toJSON = () => {
|
|
return {
|
|
t: this._def.t,
|
|
args: this._def.args.toJSON(),
|
|
returns: this._def.returns.toJSON(),
|
|
};
|
|
};
|
|
}
|