Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export ZodSerializationException so consumers can log serialization errors #115

Merged
merged 2 commits into from
Nov 1, 2024
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ export class UserController {

In the above example, despite the `userService.findOne` method returns `password`, the `password` property will be stripped out thanks to the `@ZodSerializerDto` decorator.

### Logging serialization errors using `ZodSerializationException`

You can catch serialization errors using `ZodSerializationException` and log them using your preferred logger.

```ts
if (exception instanceof ZodSerializationException) {
const zodError = exception.getZodError();
this.logger.error(`ZodSerializationException: ${zodError.message}`);
}
```
See the example app [here](/packages/example/src/http-exception.filter.ts) for more information.

## Extended Zod

> [!CAUTION]
Expand Down
13 changes: 11 additions & 2 deletions packages/example/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Module } from '@nestjs/common';
import { ZodValidationPipe } from 'nestjs-zod'
import { APP_PIPE } from '@nestjs/core'
import { ZodSerializerInterceptor, ZodValidationPipe } from 'nestjs-zod'
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PostsModule } from './posts/posts.module';
import { HttpExceptionFilter } from './http-exception.filter';


@Module({
Expand All @@ -15,6 +16,14 @@ import { PostsModule } from './posts/posts.module';
provide: APP_PIPE,
useClass: ZodValidationPipe,
},
{
provide: APP_INTERCEPTOR,
useClass: ZodSerializerInterceptor,
},
{
provide: APP_FILTER,
useClass: HttpExceptionFilter,
},
]
})
export class AppModule {}
17 changes: 17 additions & 0 deletions packages/example/src/http-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Logger, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { ZodSerializationException } from 'nestjs-zod';

@Catch(HttpException)
export class HttpExceptionFilter extends BaseExceptionFilter {
private readonly logger = new Logger(HttpExceptionFilter.name);

catch(exception: HttpException, host: ArgumentsHost) {
if (exception instanceof ZodSerializationException) {
const zodError = exception.getZodError();
this.logger.error(`ZodSerializationException: ${zodError.message}`);
}

super.catch(exception, host);
}
}
3 changes: 2 additions & 1 deletion packages/example/src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiOkResponse } from '@nestjs/swagger';
import { createZodDto } from 'nestjs-zod'
import { createZodDto, ZodSerializerDto } from 'nestjs-zod'
import { z } from 'zod'

class PostDto extends createZodDto(z.object({
Expand All @@ -23,6 +23,7 @@ export class PostsController {
}

@Get(':id')
@ZodSerializerDto(PostDto)
@ApiOkResponse({ type: PostDto, description: 'Get a post by ID' })
getById(@Param('id') id: string) {
return {
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs-zod/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { ZodDto } from './dto'
export { createZodDto } from './dto'
export { ZodValidationException } from './exception'
export { ZodValidationException, ZodSerializationException } from './exception'
export { createZodGuard, UseZodGuard, ZodGuard } from './guard'
export { patchNestJsSwagger, zodToOpenAPI } from './openapi'
export { createZodValidationPipe, ZodValidationPipe } from './pipe'
Expand Down