1 |
rakinar2 |
577 |
import { NextFunction, Request, Response } from "express"; |
2 |
|
|
import { ZodSchema } from "zod"; |
3 |
|
|
import type Controller from "../api/Controller"; |
4 |
|
|
import ValidateMiddleware from "../api/middleware/ValidateMiddleware"; |
5 |
|
|
import type Client from "../core/Client"; |
6 |
|
|
|
7 |
|
|
export function Validate(schema: ZodSchema) { |
8 |
|
|
return (target: Controller, propertyKey: string, descriptor: PropertyDescriptor) => { |
9 |
|
|
const metadata = Reflect.getMetadata("validation_middleware", target) ?? {}; |
10 |
|
|
|
11 |
|
|
const middleware = (client: Client, req: Request, res: Response, next: NextFunction) => |
12 |
|
|
ValidateMiddleware(schema, req, res, next); |
13 |
|
|
|
14 |
|
|
metadata[propertyKey] ??= middleware; |
15 |
|
|
|
16 |
|
|
Reflect.defineMetadata("validation_middleware", metadata, target); |
17 |
|
|
}; |
18 |
|
|
} |