How to make avatar field optional in controller?

13 Views Asked by At

here I am trying to upload a file :

  @UseGuards(JwtAuthGuard)
  @Post()
  @UseInterceptors(
    FileInterceptor('avatar', {
      storage: diskStorage({
        destination: './uploads',
      }),
    }),
  )
  create(
    @Body() body: CreateMediaDto,
    @UploadedFile(
      new ParseFilePipeBuilder()
        .addFileTypeValidator({
          fileType: /(jpeg|png)$/i,
        })
        .addMaxSizeValidator({
          maxSize: 3000000,
        })
        .build({
          errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        }),
    )
    avatar: Express.Multer.File,
    @Request() req,
  ) {
    // console.log('file', file);
    console.log('body', body);
    return this.mediaService.create(
      {
        ...body,
        avatarPath: avatar ? avatar.path : null,
      },
      req.user,
    );
  }

the problem is that avatar field is required now

But I wanna it to be optional

I think this validator cause it to be required

      new ParseFilePipeBuilder()
        .addFileTypeValidator({
          fileType: /(jpeg|png)$/i,
        })
        .addMaxSizeValidator({
          maxSize: 3000000,
        })
        .build({
          errorHttpStatusCode: HttpStatus.UNPROCESSABLE_ENTITY,
        }),

How can I do it ?

0

There are 0 best solutions below