export class ZodValidationPipe implements PipeTransform {
constructor(private schema: ZodSchema) {}
transform(value: unknown, metadata: ArgumentMetadata) {
const result = this.schema.safeParse(value);
if (!result.success) {
throw new BadRequestException(result.error.issues);
}
return result.data;
}
}
@Post()
@UsePipes(new ZodValidationPipe(createCatSchema))
create(@Body() dto: CreateCatDto) {}
A custom pipe implements PipeTransform. Its transform method receives two things:
- the incoming value
- an
ArgumentMetadataobject. Itstypesays where the value came from (body,query,paramorcustom), and itsmetatypegives the declared type.
Whatever the pipe returns replaces the argument. If the pipe throws an HttpException, such as BadRequestException, the request ends with that error response.
In this example, the pipe parses the value with a schema and returns the parsed data. So any defaults and type conversions from the schema reach the handler.
Write a custom pipe when:
- you prefer schema-based validation (like Zod) over class decorators
- a value needs parsing specific to your domain, for example turning an id into a loaded entity
- the validation logic doesn't fit
class-validator
Otherwise, ValidationPipe covers most DTO validation.
- Implement
PipeTransform.transform(value, metadata). ArgumentMetadatagives thetypeand themetatypeof the value.- The pipe's return value replaces the argument.
- Throw a
BadRequestExceptionon invalid input. - Use a custom pipe for schema validation or custom parsing.
Read more
- Pipes (https://docs.nestjs.com/pipes)docs.nestjs.com