We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I was using ZodSerializerDto decorator and when it comes to array schema like z.array(schema) or schema.array it is throwing an error like
z.array(schema)
schema.array
Firstly I didn't know that it because of ZodSerializerDto so I checked the output with schema.parse(response) and it didn't throw any error.
schema.parse(response)
Until it fixes I'll create and use interceptor
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, InternalServerErrorException, Logger, } from '@nestjs/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ZodSchema } from 'zod'; @Injectable() export class ZodValidationInterceptor<T> implements NestInterceptor { constructor(private schema: ZodSchema<T>) {} intercept(context: ExecutionContext, next: CallHandler): Observable<T> { return next.handle().pipe( map((data) => { const result = this.schema.safeParse(data); if (!result.success) { Logger.error(result.error) throw new InternalServerErrorException('Oops something went wrong'); } return result.data; }), ); } }
interceptor code
you should call it as
@UseInterceptors(new ZodValidationInterceptor(YOUR_SCHEMA))
The text was updated successfully, but these errors were encountered:
Related to #89 , though I would like to see this use-case fixed.
Sorry, something went wrong.
No branches or pull requests
I was using ZodSerializerDto decorator and when it comes to array schema like
z.array(schema)
orschema.array
it is throwing an error likeFirstly I didn't know that it because of ZodSerializerDto so I checked the output with
schema.parse(response)
and it didn't throw any error.Until it fixes I'll create and use interceptor
interceptor code
you should call it as
The text was updated successfully, but these errors were encountered: