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

feat(common): add error messages for file validators #14213

Conversation

mag123c
Copy link
Contributor

@mag123c mag123c commented Nov 27, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • 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, the file validation system (e.g., MaxFileSizeValidator and FileTypeValidator) provides error messages that are not context-aware.

Specifically:

MaxFileSizeValidator uses a default error message that does not include the current file size if available.
FileTypeValidator uses a default error message without referencing the current file's MIME type.
This limits the usefulness of the error messages for developers, as they lack specific context regarding what caused the validation failure.

Issue Number: #14070

What is the new behavior?

This PR enhances the error message generation for file validators:

example code:

@Post('upload-image')
@UseInterceptors(FileInterceptor('file'))
uploadFile(
  @UploadedFile(
    new ParseFilePipe({
      validators: [
        new MaxFileSizeValidator({ maxSize: 1024 }),
        new FileTypeValidator({ fileType: '.(png|jpeg|jpg)' }),
      ],
    }),
  )
  file: Express.Multer.File,
) {
  console.log(file);
}

MaxFileSizeValidator:
Error messages now dynamically include the current file size if provided, improving the clarity of validation failures.
Example:
image

FileTypeValidator:
Error messages now include the current file's MIME type, allowing developers to understand which type was detected during validation.
Example:
image

Does this PR introduce a breaking change?

  • Yes
  • No

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1e41e624-18ea-4ab6-9e2e-37016498fb6d

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 2 files are covered.
  • 37 unchanged lines in 4 files lost coverage.
  • Overall coverage increased (+0.05%) to 91.993%

Files with Coverage Reduction New Missed Lines %
packages/core/router/router-explorer.ts 1 83.2%
packages/common/pipes/validation.pipe.ts 5 95.28%
packages/microservices/listeners-controller.ts 5 80.41%
packages/core/injector/module.ts 26 84.12%
Totals Coverage Status
Change from base Build 2b363121-90cb-4f40-a4d9-a72aefb3e9d9: 0.05%
Covered Lines: 6813
Relevant Lines: 7406

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

Could you rebase to 11.0.0 branch? 🙏

@mag123c mag123c force-pushed the feature/14070-add-error-message-file-validator branch from 047832c to 11f8a4a Compare November 27, 2024 08:59
@mag123c
Copy link
Contributor Author

mag123c commented Nov 27, 2024

Of course! I'll do

@kamilmysliwiec kamilmysliwiec added this to the 11.0.0 milestone Nov 27, 2024
@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec kamilmysliwiec merged commit 6f7d470 into nestjs:11.0.0 Nov 27, 2024
1 check failed
@kamilmysliwiec kamilmysliwiec mentioned this pull request Nov 27, 2024
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants