18 lines
536 B
TypeScript
18 lines
536 B
TypeScript
![]() |
import { PipeTransform, BadRequestException } from '@nestjs/common';
|
||
|
import { ZodSchema } from 'zod';
|
||
|
|
||
|
export class ZodValidationPipe implements PipeTransform {
|
||
|
constructor(private schema: ZodSchema) { }
|
||
|
|
||
|
transform(value: unknown) {
|
||
|
try {
|
||
|
const result = this.schema.parse(value);
|
||
|
return result;
|
||
|
} catch (error: any) {
|
||
|
throw new BadRequestException('Validation failed', {
|
||
|
cause: error,
|
||
|
description: error.errors
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|