Skip to content

feat(common): Add fallbackToMimetype support in FileTypeValidator #14995

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

Conversation

mag123c
Copy link
Contributor

@mag123c mag123c commented Apr 21, 2025

Introduce fallbackToMimetype option to allow fallback to mimetype validation when magic number detection fails (e.g., for small or undetectable buffers like text or CSV files).

Also enhanced buildErrorMessage() to reflect precise validation failure reasons depending on buffer presence and mimetype state.

Added unit tests to cover fallback logic and error message variants.

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, FileTypeValidator only relies on magic number detection.
When the file buffer is too short or lacks a detectable signature, validation fails even if the mimetype is correctly set.

Issue Number: #14977

  • Allows developers to enable fallback to mimetype validation using fallbackToMimetype: true
  • Better error messaging when fallback is triggered or buffer is missing
  • Adds relevant unit tests for fallback behavior and error formatting

What is the new behavior?

Does this PR introduce a breaking change?

Other information

Introduce `fallbackToMimetype` option to allow fallback to mimetype validation
when magic number detection fails (e.g., for small or undetectable buffers like text or CSV files).

Also enhanced `buildErrorMessage()` to reflect precise validation failure reasons
depending on buffer presence and mimetype state.

Added unit tests to cover fallback logic and error message variants.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 837e18fc-8678-46c2-8d75-8d045ee82c96

Details

  • 11 of 12 (91.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.003%) to 89.318%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/pipes/file/file-type.validator.ts 11 12 91.67%
Totals Coverage Status
Change from base Build 600c1ebc-e7f1-48f2-bb00-a8366dddbcf8: -0.003%
Covered Lines: 7166
Relevant Lines: 8023

💛 - Coveralls

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for falling back to mimetype validation when magic number detection fails in FileTypeValidator, along with improved error messaging and additional unit tests.

  • Added fallbackToMimetype option to FileTypeValidator
  • Enhanced buildErrorMessage() to reflect fallback usage
  • Included new unit tests for fallback behavior and error message variants

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/common/test/pipes/file/file-type.validator.spec.ts Added tests for fallbackToMimetype logic and various error message scenarios
packages/common/pipes/file/file-type.validator.ts Introduced fallbackToMimetype option in the validator and updated error messaging logic

@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec kamilmysliwiec merged commit 94bfde9 into nestjs:master Apr 22, 2025
5 checks passed
@mag123c
Copy link
Contributor Author

mag123c commented Apr 22, 2025

Just submitted a backport PR for 10.4.17: #15003
Happy to adjust if needed!

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