nest icon indicating copy to clipboard operation
nest copied to clipboard

Pipe validation for `@UploadFiles()` not working

Open shreyas-segu opened this issue 1 year ago • 1 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

The pipe validation for the @UploadFiles annotation is not working, whereas the @UploadFile() annotation is working as expected

Minimum reproduction code

https://github.com/shreyas-segu/multiple-files-upload-validation-bug

Steps to reproduce

  1. npm ci
  2. npm run start
  3. use the request.txt to execute the curl request which is not working as expected, use the SamplePNGImage_100kbmb.png for the sample file
  4. use the request-working.txt for the curl request which is working, you can use the same file

Expected behavior

The multiple files upload endpoint with the pipe validation should not throw any validation errors.

Package

  • [ ] I don't know. Or some 3rd-party package
  • [X] @nestjs/common
  • [X] @nestjs/core
  • [ ] @nestjs/microservices
  • [ ] @nestjs/platform-express
  • [ ] @nestjs/platform-fastify
  • [ ] @nestjs/platform-socket.io
  • [ ] @nestjs/platform-ws
  • [ ] @nestjs/testing
  • [ ] @nestjs/websockets
  • [ ] Other (see below)

Other package

No response

NestJS version

10.3.2

Packages versions

[Nest Platform Information]
platform-express version : 10.3.8
schematics version       : 10.1.1
testing version          : 10.3.8
common version           : 10.3.8
core version             : 10.3.8
cli version              : 10.3.2

Node.js version

v20.11.0

In which operating systems have you tested?

  • [X] macOS
  • [ ] Windows
  • [ ] Linux

Other

No response

shreyas-segu avatar May 03 '24 16:05 shreyas-segu

Hi @shreyas-segu this is already discussed here: https://github.com/nestjs/docs.nestjs.com/issues/2424

I would suggest du use a custom validation pipe until it is implemented in the framework directly.

This is a adjusted copy of what was already mentioned in the original thread - but it should work with your example as I am using the exact same structure / definition of payload.

import { ParseFilePipe, PipeTransform } from '@nestjs/common';

export class ParseFilesPipe implements PipeTransform<Express.Multer.File[]> {
    constructor(private readonly pipe: ParseFilePipe) {}

    async transform(
        files: Express.Multer.File[] | { [key: string]: Express.Multer.File[] },
    ) {
        for (const file of Object.values(files).flat())
            await this.pipe.transform(file);

        return files;
    }
}

RobinKemna avatar May 16 '24 06:05 RobinKemna