Skip to content

Commit

Permalink
Merge pull request #58 from nicolabovolato/fix/zod-serializer-interce…
Browse files Browse the repository at this point in the history
…ptor-return-500

fix: ZodSerializerInterceptor should return 500
  • Loading branch information
Evgeny Zakharov authored Aug 24, 2023
2 parents 57527ad + d24982e commit c4d00a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/exception.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { BadRequestException, HttpStatus } from '@nestjs/common'
import {
BadRequestException,
HttpStatus,
InternalServerErrorException,
} from '@nestjs/common'
import { ZodError } from './z'

export class ZodValidationException extends BadRequestException {
Expand All @@ -15,8 +19,22 @@ export class ZodValidationException extends BadRequestException {
}
}

export class ZodSerializationException extends InternalServerErrorException {
constructor(private error: ZodError) {
super()
}

public getZodError() {
return this.error
}
}

export type ZodExceptionCreator = (error: ZodError) => Error

export const createZodValidationException: ZodExceptionCreator = (error) => {
return new ZodValidationException(error)
}

export const createZodSerializationException: ZodExceptionCreator = (error) => {
return new ZodSerializationException(error)
}
6 changes: 3 additions & 3 deletions src/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Reflector } from '@nestjs/core'
import { lastValueFrom, of } from 'rxjs'
import { z } from 'zod'
import { createZodDto } from './dto'
import { ZodValidationException } from './exception'
import { ZodSerializationException } from './exception'
import { ZodSerializerInterceptor } from './serializer'

describe('ZodSerializerInterceptor', () => {
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('ZodSerializerInterceptor', () => {
expect(user.username).toBe('test')
})

test('wrong response shape should throw ZodValidationException', async () => {
test('wrong response shape should throw ZodSerializationException', async () => {
const handler = createMock<CallHandler>({
handle: () => of({ user: 'test' }),
})
Expand All @@ -52,7 +52,7 @@ describe('ZodSerializerInterceptor', () => {

const userObservable = interceptor.intercept(context, handler)
expect(lastValueFrom(userObservable)).rejects.toBeInstanceOf(
ZodValidationException
ZodSerializationException
)
})

Expand Down
7 changes: 5 additions & 2 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { map, Observable } from 'rxjs'
import { ZodSchema } from 'zod'
import { ZodDto } from './dto'
import { validate } from './validate'
import { createZodSerializationException } from './exception'

// NOTE (external)
// We need to deduplicate them here due to the circular dependency
Expand All @@ -35,8 +36,10 @@ export class ZodSerializerInterceptor implements NestInterceptor {
if (typeof res !== 'object' || res instanceof StreamableFile) return res

return Array.isArray(res)
? res.map((item) => validate(item, responseSchema))
: validate(res, responseSchema)
? res.map((item) =>
validate(item, responseSchema, createZodSerializationException)
)
: validate(res, responseSchema, createZodSerializationException)
})
)
}
Expand Down

0 comments on commit c4d00a8

Please sign in to comment.