Skip to content

Improved error messages for file validtors #14070

@phlogisticfugu

Description

@phlogisticfugu

Is there an existing issue that is already proposing this?

  • 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

buildErrorMessage(): string {
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
}

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

buildErrorMessage(): string {
if ('message' in this.validationOptions) {
if (typeof this.validationOptions.message === 'function') {
return this.validationOptions.message(this.validationOptions.maxSize);
}
return this.validationOptions.message;
}

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions