nest icon indicating copy to clipboard operation
nest copied to clipboard

Support multiple file upload validation

Open blangley28 opened this issue 3 years ago • 4 comments
trafficstars

PR Checklist

Please check if your PR fulfills the following requirements:

  • [ ] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [x] Bugfix
  • [ ] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

Currently file validation via the ParseFilePipe only works when validating a single file. When using an array of files it always throws an error.

Issue Number: N/A

What is the new behavior?

Single file validation continues to work unchanged, but now when uploading multiple files:

  1. fileIsRequired works
  2. Each file in the array is validated

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

blangley28 avatar Oct 03 '22 14:10 blangley28

Pull Request Test Coverage Report for Build eb1f30dd-90b6-4c45-9f27-fe86506c8ee6

  • 4 of 5 (80.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 93.787%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/pipes/file/parse-file.pipe.ts 4 5 80.0%
<!-- Total: 4 5
Totals Coverage Status
Change from base Build fe8b361f-7c4e-4666-8553-05c4501d57f0: -0.01%
Covered Lines: 6114
Relevant Lines: 6519

💛 - Coveralls

coveralls avatar Oct 03 '22 14:10 coveralls

Hey @blangley28 , could you work on the changes I've asked? Thanks!

thiagomini avatar Oct 06 '22 20:10 thiagomini

@thiagomini sorry for the delay. Just pushed some updated. I also found that while using FileFieldInterceptor the type of value is an object. So I added some handling to that here. Because both the object and array use the same Promise.all snippet I wanted to break that out into a function. There are quite alot of validate% methods now... let me know if you have any name suggestions!

blangley28 avatar Oct 07 '22 19:10 blangley28

@thiagomini sorry for the delay. Just pushed some updated. I also found that while using FileFieldInterceptor the type of value is an object. So I added some handling to that here. Because both the object and array use the same Promise.all snippet I wanted to break that out into a function. There are quite alot of validate% methods now... let me know if you have any name suggestions!

No problem @blangley28! Now you only have to fix the tests and add the new ones.

thiagomini avatar Oct 10 '22 12:10 thiagomini

Hello,

Is this being continued? I really would like to have this since I need to validate multiple files. I made my own implementation, but would rather not use that and just use the native stuff.

I hope it gets merged soon!

PhelixTaken avatar Dec 02 '22 23:12 PhelixTaken

Hey @PhelixTaken. We cannot merge this PR until it has all tests passing. You are welcome to create a branch that resolves this problem as well, I would be happy to review it! I can work on that issue too once I have some time, but I can't promise you a date.

thiagomini avatar Dec 05 '22 12:12 thiagomini

Thank you for the great efforts.

I want to add that thereAreNoFilesIn() method is not working if the value is array but empty. this worked for me:

private thereAreNoFilesIn(value: any): boolean {
    if (Array.isArray(value)) {
        return value.length === 0;
    } else if (isObject(value)) {
        return Object.keys(value).length === 0;
    } else {
        return isEmpty(value);
    }
}

also isUndefined() is deprecated, I have edited this to use value === undefined

my code passed test and test:cov, you can see it here: Parse-File-Pipe-Fix

I would love to help in this PR. just let me know :)

mahkassem avatar Dec 07 '22 20:12 mahkassem

Let's track this here https://github.com/nestjs/nest/pull/10737

kamilmysliwiec avatar Feb 01 '23 12:02 kamilmysliwiec