nest
nest copied to clipboard
Support multiple file upload validation
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:
fileIsRequiredworks- Each file in the array is validated
Does this PR introduce a breaking change?
- [ ] Yes
- [x] No
Other information
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 | |
|---|---|
| Change from base Build fe8b361f-7c4e-4666-8553-05c4501d57f0: | -0.01% |
| Covered Lines: | 6114 |
| Relevant Lines: | 6519 |
💛 - Coveralls
Hey @blangley28 , could you work on the changes I've asked? Thanks!
@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!
@thiagomini sorry for the delay. Just pushed some updated. I also found that while using
FileFieldInterceptorthe type ofvalueis an object. So I added some handling to that here. Because both the object and array use the samePromise.allsnippet I wanted to break that out into a function. There are quite alot ofvalidate%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.
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!
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.
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 :)
Let's track this here https://github.com/nestjs/nest/pull/10737