nest
nest copied to clipboard
Pipe validation for `@UploadFiles()` not working
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
- npm ci
- npm run start
- use the
request.txtto execute the curl request which is not working as expected, use theSamplePNGImage_100kbmb.pngfor the sample file - use the
request-working.txtfor 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
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;
}
}