nest
nest copied to clipboard
Improved error messages for file validtors
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: anyinput supported by theFileValidator - use that to also log the actual
file?.typewhich 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.