nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.96k stars 7.66k forks source link

Body with files and textfields #14032

Closed mak7an closed 2 months ago

mak7an commented 2 months ago

Is there an existing issue for this?

Current behavior

  // @UseInterceptors(FileInterceptor('file'))
  @Post('file')
  uploadFile(
    @Body() body: SampleDto,
    // @UploadedFile() file: Express.Multer.File,
  ) {
    return {
      body,
      // file: file, // .buffer.toString(),
    };
  }

With FileInterceptor response is {"body":{"data":"test.mp3","test":"true"}}. Test property if string but it is boolean.

Without FileInterceptor response is `{"body":{"data":"test.mp3","test":true}}. Test is boolean.

If I use that body to update data this.userRepository.assign(user, data) I get errors from mikro-orm (bacause id is string and boolean values are string type) validation and nest validation pipe.

ValidationError: Trying to set User.test of type 'boolean' to 'true' of type 'string'.

Minimum reproduction code

https://github.com/nestjs/nest/tree/master/sample/29-file-upload

Steps to reproduce

No response

Expected behavior

Boolean to be boolean and integer integer.

Package

Other package

No response

NestJS version

No response

Packages versions

Just run sample

Node.js version

21.0.0

In which operating systems have you tested?

Other

No response

jmcdo29 commented 2 months ago

Multer (the underlying form body parser) makes everything that is not a file a string. You'll need to add an explicit @Transform() if you want the ValidationPipe to handle that

mak7an commented 2 months ago

@jmcdo29 thank you, I didn't think about that