dmitriy-nz / nestjs-form-data

NestJS middleware for handling multipart/form-data, which is primarily used for uploading files.
https://www.npmjs.com/package/nestjs-form-data
MIT License
117 stars 23 forks source link

field with array returns error if validation has FileSystemStoredFile[] and works fine when FileSystemStoredFile #60

Closed jamalahmed1122 closed 2 months ago

jamalahmed1122 commented 2 months ago

the below code works perfectly fine:

@MaxFileSize(10e6, { each: true }) // ** 10 MB limit
  @HasExtension(['jpeg', 'png', 'jpg'], MetaSource.bufferMagicNumber, {
    each: true,
  })
  @HasMimeType(['image/jpeg', 'image/png', 'image/jpg'], { each: true })
  @IsFiles({ message: 'areaPhotos must only contain files' })
  @IsNotEmpty()
  areaPhotos: FileSystemStoredFile; 

but when i change areaPhotos from areaPhotos: FileSystemStoredFile; to areaPhotos: FileSystemStoredFile[]; making sure typescript knows it is an array of files, it fails.

in this function

function isFile(value) {
    console.log("πŸš€ ~ isFile ~ value:", value)
    console.log("πŸš€ ~ isFile ~ value:", value && value instanceof StoredFile_1.StoredFile)

    return value && value instanceof StoredFile_1.StoredFile;
}

in the library code i have seen that value looks like this πŸš€ ~ isFile ~ value: [ originalName: 'Screenshot from 2024-06-11 09-44-16.png', encoding: '7bit', busBoyMimeType: 'image/png', path: '/home/jamal/projects/rx-trade-backend/uploads/Screenshot from 2024-06-11 09-44-16-a5fd66.png', size: 320685, fileType: { ext: 'png', mime: 'image/png' } ]

which causes the validation to fail.

dmitriy-nz commented 2 months ago

Thanks for the report! Could you make a minimum reproduce repo with your issue? It would be very useful and I will be able to fix this issue quickly. Thanks!

I should ask it because we have a many test, including array files loading, validation and so on and these were going to success.

minhchi1509 commented 2 months ago

Thanks for the report! Could you make a minimum reproduce repo with your issue? It would be very useful and I will be able to fix this issue quickly. Thanks!

I should ask it because we have a many test, including array files loading, validation and so on and these were going to success.

it seems that all validations will fail if we specified type as an array like this: My DTO:

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

export class FormDataTestDto {
  @IsFiles()
  @HasMimeType(['image/*'], { each: true })
  avatars: MemoryStoredFile[];
}

And it always throws error:

{
    "message": "File must be of one of the types image/*",
    "statusCode": 400,
    "timestamp": "2024-06-22T14:36:59.052Z",
    "path": "/api/v1/auth/upload"
}
dmitriy-nz commented 2 months ago

@minhchi1509 @jamalahmed1122 I updated the Installation section in the readme, please check it out.
Also I created a playground demo for this issue to demo array of files validation based on your dto, please check it:
https://github.com/dmitriy-nz/nestjs-form-data-issue-60-playground Don't shy away from MR in playgroud to demo your issue

minhchi1509 commented 2 months ago

@minhchi1509 @jamalahmed1122 I updated the Installation section in the readme, please check it out. Also I created a playground demo for this issue to demo array of files validation based on your dto, please check it: https://github.com/dmitriy-nz/nestjs-form-data-issue-60-playground Don't shy away from MR in playgroud to demo your issue

Thanks for your playground demo, i have tested and it worked. But when I add option enableImplicitConversion: true to ValidationPipe(), the validation will fail again:

app.useGlobalPipes(
  new ValidationPipe({
    transform: true,
    transformOptions: { enableImplicitConversion: true },
  })
);

I must have that option because it can automatically transform type, such as from type string to number (refer to: https://stackoverflow.com/questions/73238463/nestjs-validationpipetransform-true-does-not-transform-string-to-number-for). Do you have any idea?. Thank you

dmitriy-nz commented 2 months ago

@minhchi1509 Thank you very much for your help, the issue has been reproduced and fixed in version 1.9.9, it is already deployed to npm, please try it out.

1155JamalAhmed commented 2 months ago

@dmitriy-nz thanks, i have updated the package and it is working fine now, thanks for the awesome package, keep it up.