Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/express/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ToApiEndpoints,
ApiSpec,
} from "../index";
import { ZodValidator, ZodValidators } from "../zod";
import { ZodValidators } from "../zod";
import {
NextFunction,
ParamsDictionary,
Expand Down Expand Up @@ -131,30 +131,28 @@ export const newValidator = <E extends ZodApiEndpoints>(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<E[Path][M]["params"]>
: undefined,
body: () =>
spec?.body?.safeParse(req.body) as E[Path][M] extends ZodApiSpec
? ZodValidator<E[Path][M]["body"]>
: undefined,
query: () =>
spec?.query?.safeParse(req.query) as E[Path][M] extends ZodApiSpec
? ZodValidator<E[Path][M]["query"]>
: undefined,
headers: () =>
spec?.headers?.safeParse(req.headers) as E[Path][M] extends ZodApiSpec
? ZodValidator<E[Path][M]["headers"]>
: undefined,
resHeaders: () =>
spec?.resHeaders?.safeParse(
req.headers,
) as E[Path][M] extends ZodApiSpec
? ZodValidator<E[Path][M]["resHeaders"]>
: undefined,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const zodValidators: Record<string, any> = {};

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<E[Path][M], "">
: Record<string, unknown>;
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type ZodValidators<
query: ZodValidator<AS["query"]>;
body: ZodValidator<AS["body"]>;
headers: ZodValidator<AS["headers"]>;
resHeaders: ZodValidator<AS["resHeaders"]>;
// resHeaders: ZodValidator<AS["resHeaders"]>;
}>;
type ZodTypeWithKey<Key extends string> = z.ZodType<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down