Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't validate array of files size wise nor mimetype wise #51

Open
Yuniac opened this issue Oct 12, 2023 · 3 comments
Open

Can't validate array of files size wise nor mimetype wise #51

Yuniac opened this issue Oct 12, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@Yuniac
Copy link

Yuniac commented Oct 12, 2023

Hello. Thanks for creating this library.

A while ago, I had an issue where files sent as an array weren't appropriately received on the server and to fix this I used this hack from another issue that was opened about this: #47 (comment)

Now, when using version 1.9.1 I can't send an array of files, the validation always fails for the size and type of the files. It would only work if I send one file. Please how can I solve this? I have used the code from the readme even to see if it would work but it still doesn't work.

DTO:

export class ImagePostDto extends BasePostDto {
  @IsFiles({ each: true })
  @MaxFileSize(1e7, { each: true, message: 'The maximum size for a file is 10mb' })
  @HasMimeType(types, {
    each: true,
    message: (e) => {
      return `Error message`;
    },
  })
  files: FileSystemStoredFile[];
}

And I tried to send both via Postman and via my code, neither would work:

Code:

   const formData = new FormData();
   // files is just an array of files
    for (const file of files) {
      formData.append('files[]', file);
    }

   axios.post(url, formData);

PostMan:

Screenshot 2023-10-12 at 15 27 09

And this is the error I get:

{"message":["Error message","The maximum size for a file is 10mb","Field \"files\" does not contain file"],"error":"Bad Request","statusCode":400}

Here are the images that I'm sending from the request body log:

body: {
      files: [
        {
          originalName: '3e132..png',
          encoding: '7bit',
          busBoyMimeType: 'image/png',
          path: '/tmp/nestjs-tmp-storage/3e132.-79988c.png',
          size: 6368,
          fileType: { ext: 'webp', mime: 'image/webp' }
        },
        {
          originalName: '512818_IGDB-272x380.jpg',
          encoding: '7bit',
          busBoyMimeType: 'image/jpeg',
          path: '/tmp/nestjs-tmp-storage/512818_IGDB-272x380-9988c5.jpg',
          size: 33564,
          fileType: { ext: 'jpg', mime: 'image/jpeg' }
        }
      ]
    }

Thanks in advance. I appreciate any further guidance on those to properly use your library

@Yuniac
Copy link
Author

Yuniac commented Oct 17, 2023

This is still broken, I degraded the library to 1.9.0 and used this solution : #47 (comment)

This is the only thing that works

@dmitriy-nz
Copy link
Owner

@Yuniac Hi, I am trying to reproduce your issue but it is not reproducible.
Automatic tests will not allow you to certify a version with obvious behavioral errors.
Please provide more details about your environment, nest version, nodejs, controller.
Or create a small repository where the issue is reproduced.
Here is my environment where I tried:

package.json

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "nestjs-form-data": "^1.9.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },

app.controller.ts

import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Post,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { ImagePostDto } from './ImagePostDto';
import { FileSystemStoredFile, FormDataRequest } from 'nestjs-form-data';

@Controller()
export class AppController {
  @Post('files')
  @UsePipes(new ValidationPipe({ transform: true }))
  @FormDataRequest({ storage: FileSystemStoredFile })
  @HttpCode(HttpStatus.OK)
  getHello(@Body() body: ImagePostDto): any {
    console.log(body);
    return body;
  }
}

ImagePostDto.ts

import {
  FileSystemStoredFile,
  HasMimeType,
  IsFiles,
  MaxFileSize,
} from 'nestjs-form-data';

export class ImagePostDto {
  @IsFiles({ each: true })
  @MaxFileSize(1e7, {
    each: true,
    message: 'The maximum size for a file is 10mb',
  })
  @HasMimeType(['image/png', 'image/jpeg'], {
    each: true,
    message: (e) => {
      return `Error message`;
    },
  })
  files: FileSystemStoredFile[];
}

Request & response:
image

@dmitriy-nz
Copy link
Owner

dmitriy-nz commented Jun 23, 2024

Hi everyone, thanks for your feedback
This issue should be also fixed in the version 1.9.9, please try it out
#60 (comment)
@Yuniac

@dmitriy-nz dmitriy-nz self-assigned this Jun 23, 2024
@dmitriy-nz dmitriy-nz added the bug Something isn't working label Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants