diff --git a/src/pipes/zod.pipe.filter.ts b/src/pipes/zod.pipe.filter.ts new file mode 100644 index 0000000..c08c61a --- /dev/null +++ b/src/pipes/zod.pipe.filter.ts @@ -0,0 +1,17 @@ +import { ArgumentsHost, Catch, ExceptionFilter, HttpStatus } from "@nestjs/common"; +import { ZodError } from "zod"; + + +@Catch(ZodError) +export class ZodFilter implements ExceptionFilter { + catch(exception: T, host: ArgumentsHost) { + const ctx = host.switchToHttp(); + const response = ctx.getResponse(); + const status = HttpStatus.BAD_REQUEST; + response.status(status).json({ + errors: exception.errors, + message: exception.message, + statusCode: status, + }); + } +} \ No newline at end of file diff --git a/src/pipes/zod.pipe.ts b/src/pipes/zod.pipe.ts new file mode 100644 index 0000000..c96e68f --- /dev/null +++ b/src/pipes/zod.pipe.ts @@ -0,0 +1,13 @@ +import { ArgumentMetadata, Injectable, PipeTransform } from "@nestjs/common"; +import { z } from "zod"; + + +@Injectable() +export class ZodPipe implements PipeTransform { + constructor(private readonly schema: z.ZodObject) {} + + transform(value: any, metadata: ArgumentMetadata) { + this.schema.parse(value); + return value; + } +} \ No newline at end of file