// 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 = ZodTuple, Returns extends ZodTypeAny = ZodTypeAny > extends ZodTypeDef { t: ZodTypes.function; args: Args; returns: Returns; } export type OuterTypeOfFunction< Args extends ZodTuple, Returns extends ZodTypeAny > = Args["_input"] extends Array ? (...args: Args["_input"]) => Returns["_output"] : never; export type InnerTypeOfFunction< Args extends ZodTuple, Returns extends ZodTypeAny > = Args["_output"] extends Array ? (...args: Args["_output"]) => Returns["_input"] : never; // type as df = string extends unknown ? true : false export class ZodFunction< Args extends ZodTuple, Returns extends ZodTypeAny > extends ZodType< OuterTypeOfFunction, ZodFunctionDef, InnerTypeOfFunction > { readonly _def!: ZodFunctionDef; // readonly _type!: TypeOfFunction; args = [0]>( ...items: Items ): ZodFunction, Returns> => { return new ZodFunction({ ...this._def, args: ZodTuple.create(items), }); }; returns = >( returnType: NewReturnType ): ZodFunction => { return new ZodFunction({ ...this._def, returns: returnType, }); }; implement = >(func: F): F => { const validatedFunc = this.parse(func); return validatedFunc as any; }; validate = this.implement; static create = < T extends ZodTuple = ZodTuple<[]>, U extends ZodTypeAny = ZodUnknown >( args?: T, returns?: U ): ZodFunction => { 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(), }; }; }