diff --git a/src/express/index.ts b/src/express/index.ts index 65bcbba..f64edf9 100644 --- a/src/express/index.ts +++ b/src/express/index.ts @@ -8,7 +8,7 @@ import { ToApiEndpoints, ApiSpec, } from "../index"; -import { ZodValidator, ZodValidators } from "../zod"; +import { ZodValidators } from "../zod"; import { NextFunction, ParamsDictionary, @@ -131,30 +131,28 @@ export const newValidator = (endpoints: E) => { ) => { const spec: E[Path][M] = endpoints[req.route.path][req.method.toLowerCase() as Method]; - return { - params: () => - spec?.params?.safeParse(req.params) as E[Path][M] extends ZodApiSpec - ? ZodValidator - : undefined, - body: () => - spec?.body?.safeParse(req.body) as E[Path][M] extends ZodApiSpec - ? ZodValidator - : undefined, - query: () => - spec?.query?.safeParse(req.query) as E[Path][M] extends ZodApiSpec - ? ZodValidator - : undefined, - headers: () => - spec?.headers?.safeParse(req.headers) as E[Path][M] extends ZodApiSpec - ? ZodValidator - : undefined, - resHeaders: () => - spec?.resHeaders?.safeParse( - req.headers, - ) as E[Path][M] extends ZodApiSpec - ? ZodValidator - : undefined, - }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const zodValidators: Record = {}; + + if (spec?.params !== undefined) { + const params = spec.params; + zodValidators["params"] = () => params.safeParse(req.params); + } + if (spec?.query !== undefined) { + const query = spec.query; + zodValidators["query"] = () => query.safeParse(req.query); + } + if (spec?.body !== undefined) { + const body = spec.body; + zodValidators["body"] = () => body.safeParse(req.body); + } + if (spec?.headers !== undefined) { + const headers = spec.headers; + zodValidators["headers"] = () => headers.safeParse(req.headers); + } + return zodValidators as E[Path][M] extends ZodApiSpec + ? ZodValidators + : Record; }; }; diff --git a/src/zod/index.ts b/src/zod/index.ts index 0f21316..13b5a9a 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -18,7 +18,7 @@ export type ZodValidators< query: ZodValidator; body: ZodValidator; headers: ZodValidator; - resHeaders: ZodValidator; + // resHeaders: ZodValidator; }>; type ZodTypeWithKey = z.ZodType< // eslint-disable-next-line @typescript-eslint/no-explicit-any