Skip to content

Commit

Permalink
Merge pull request #11549 from dammy001/patch-1
Browse files Browse the repository at this point in the history
chore(validation): add ability to pass custom message in file size validator
  • Loading branch information
kamilmysliwiec authored May 16, 2023
2 parents 37f332c + d46db25 commit 927525e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/common/pipes/file/max-file-size.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FileValidator } from './file-validator.interface';

export type MaxFileSizeValidatorOptions = {
maxSize: number;
message?: string | ((maxSize: number) => string);
};

/**
Expand All @@ -13,6 +14,14 @@ export type MaxFileSizeValidatorOptions = {
*/
export class MaxFileSizeValidator extends FileValidator<MaxFileSizeValidatorOptions> {
buildErrorMessage(): string {
if ('message' in this.validationOptions) {
if (typeof this.validationOptions.message === 'function') {
return this.validationOptions.message(this.validationOptions.maxSize);
}

return this.validationOptions.message;
}

return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
}

Expand Down

0 comments on commit 927525e

Please sign in to comment.