NestJS - Uploading file with dto validation

Viewed 453

I`m uploading a file using FileInterseptor, but along with the file I also pass some createDto. The problem is that i validate Dto and if something goes wrong the file is saved anyway. I want the file not to be saved if there are problems with validation. Heres the code:

Controller endpoint:

@UseInterceptors(FileInterceptor('file', saveDocumentToStorage))
  @Post()
  async createDocument(
    @UploadedFile() file: Express.Multer.File,
    @Body() createDocumentDto: CreateDocumentDto,
    @AuthUser() user: UserEntity,
  ): Promise<DocumentDto> {
    const document = await this._documentsService.createDocument(
      file.path,
      createDocumentDto,
      user,
    );
    return document.toDto();
  }

createDto:

export class CreateDocumentDto {
  @IsString()
  @IsNotEmpty()
  readonly name: string;

  @IsString()
  @IsNotEmpty()
  readonly description: string;
}

Thanks for any help

0 Answers
Related