Skip to content

feat(common): add message property to file type validator#16060

Merged
kamilmysliwiec merged 8 commits into
nestjs:masterfrom
Jo-Minseok:feat/filevalidator-custom-message
Dec 21, 2025
Merged

feat(common): add message property to file type validator#16060
kamilmysliwiec merged 8 commits into
nestjs:masterfrom
Jo-Minseok:feat/filevalidator-custom-message

Conversation

@Jo-Minseok

@Jo-Minseok Jo-Minseok commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

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?

Previously, only MaxFileSizeValidator supported custom messages, while FileTypeValidator did not, which caused confusion and made it difficult to deliver service-appropriate messages.
Errors were also returned as simple strings, requiring unnecessary branching through the exceptionFactory.
Now, the usage of MaxFileSizeValidator and FileTypeValidator—specifically the way custom messages are delivered—has been standardized.

What is the new behavior?

By enabling the use of custom messages in FileTypeValidator—as was already possible in MaxFileSizeValidator—it is now possible to deliver service-appropriate messages and maintain consistency in how MaxFileSizeValidator and FileTypeValidator are used.
Without using exceptionFactory, simple strings can now be used in FileTypeValidator to provide messages for Bad Request responses.

In addition, the existing message option has been deprecated in favor of a new
errorMessage option for clearer semantics and consistency across validators.

The deprecated message option is still supported for backward compatibility,
but errorMessage is now the preferred way to provide custom error messages.

new MaxFileSizeValidator({ maxSize: 1000, message: 'Error'}); // 🟢 Although deprecated, this option is still supported.
new MaxFileSizeValidator({ maxSize: 1000, errorMessage: 'Error'}); // 🟢 New Property
new MaxFileSizeValidator({ maxSize: 1000, errorMessage: ctx =>
          `Received file size is ${ctx.file?.size}, but it must be smaller than ${ctx.config.maxSize}.`
}); // 🟢 New Property with factory function
new FileTypeValidator({ fileType: 'image/png', message: 'invalid type'}); // ❌ Since this property did not previously exist, it cannot be used.
new FileTypeValidator({ fileType: 'image/png', errorMessage: 'invalid type'}); // 🟢New Property
new FileTypeValidator({ fileType: 'image/png', errorMessage: ctx =>
          `Received file type '${ctx.file?.mimetype}', but expected '${ctx.config.fileType}'.`
});  // 🟢 New Property with factory function

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls

coveralls commented Dec 12, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 71f3c018-4d14-4733-9975-4a2037f6195e

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 89.365%

Totals Coverage Status
Change from base Build d186cd4d-4808-4470-82f6-43a17b6e3fe1: 0.04%
Covered Lines: 7378
Relevant Lines: 8256

💛 - Coveralls

Comment thread packages/common/pipes/file/file-type.validator.ts Outdated
Comment thread packages/common/pipes/file/file-type.validator.ts Outdated
@kamilmysliwiec

Copy link
Copy Markdown
Member

thanks!

@kamilmysliwiec kamilmysliwiec merged commit cc2659a into nestjs:master Dec 21, 2025
3 checks passed
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.

Customizing FileType Validator Error Messages

4 participants