#@UploadedFiles Validation Pipe does not work

6 messages · Page 1 of 1 (latest)

cursive adder
#

Heya - I assume that this is either not documented well or I am just not skilled enough to solve it. Using the FileFieldsInterceptor in combination with @UploadedFiles as documented and applying a ParseFilePipe instance to it, the validation fails every time. Debugging this shows that the single validators do not receive a file at all and thus every check fails. Any hints on how to implement it? 🙂

My expectation is, that the validation pipe is applied to each file in the request.

UPDATE - seems to be related to: https://github.com/nestjs/docs.nestjs.com/issues/2424

@Post('upload')
    @UseInterceptors(
        FileFieldsInterceptor([
            { name: 'frontImage', maxCount: 1 },
            { name: 'backImage', maxCount: 1 },
        ]),
    )
    async uploadFile(
        @UploadedFiles(new ParseFilePipe({
            fileIsRequired: true,
            validators: [
                new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 20 }),
                new FileTypeValidator({
                    fileType: /^image\/(png|jp(e)?g)$/,
                }),
            ],
        }))
        files: {
            frontImage?: Express.Multer.File[];
            backImage?: Express.Multer.File[];
        },
    ) {
        this.logger.debug(files.frontImage?.at(0)?.mimetype);
GitHub

Is there an existing issue that is already proposing this? I have searched the existing issues Is your feature request related to a problem? Please describe it When using FilesInterceptor for doing...

stoic wasp
#

What's the format of data comming in the request ?

cursive adder
#

@stoic wasp it is a formdata request containing the two required values for frontImage and backImage
Looks as follows:

stoic wasp
#

can i have the whole file please ?

cursive adder
#

@stoic wasp I assume it is not necessary as the file is transferred correctly to the backend - when removing the validators and directly print out the content, all is fine. The validators fail, cause of the payload structure. As defined in the controller decorator FileFieldsInterceptor - the result passed into the Action is of type Record<string, Array<Files>> thus the simple validation pipe is not able to parse / validate the payload and always fails. Using the approach, mentioned in the github discussion, of wrapping the validator pipes into another pipe for preprocessing the data into a format that works for the original validators does the trick and now it works for me.

Here is the preprocessor pipe:

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;
    }
}