nest icon indicating copy to clipboard operation
nest copied to clipboard

Improved error messages for file validtors

Open phlogisticfugu opened this issue 1 year ago • 1 comments
trafficstars

Is there an existing issue that is already proposing this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe it

Not related to an explicit bug in Nest.js - this is an enhancement request

Describe the solution you'd like

Within the FileTypeValidator https://github.com/nestjs/nest/blob/198eaf7e5c64cf496ad6ddd97b4013d741cf517f/packages/common/pipes/file/file-type.validator.ts#L23-L25

to change the buildErrorMessage() method to:

  • take in the file: any input supported by the FileValidator
  • use that to also log the actual file?.type which was observed - to make troubleshooting easier

While we are at it, the MaxFileSizeValidator https://github.com/nestjs/nest/blob/198eaf7e5c64cf496ad6ddd97b4013d741cf517f/packages/common/pipes/file/max-file-size.validator.ts#L20-L27 could also be modified to improve its error message to show the actual file size, in addition to the expected file size

Teachability, documentation, adoption, migration strategy

for the changelog: Improved error messages for MaxSizeValidator and FileTypeValidator to include the observed actual size or filetype.

What is the motivation / use case for changing the behavior?

We are using the FileTypeValidator() within a @UploadedFile - and are getting errors in production where the user is uploading the wrong file type, and triggering this validator. However, the error message we see in our logs is: BadRequestException: Validation failed (expected type is .(png|jpeg|jpg|pdf)) - which does not describe the actual file type that the user actually used.

phlogisticfugu avatar Oct 17 '24 10:10 phlogisticfugu