From 84bd87c86ad4d02ae70a8cdffbd04c7b96b8b4b4 Mon Sep 17 00:00:00 2001 From: Anjorin Damilare Date: Sun, 23 Apr 2023 06:57:52 +0100 Subject: [PATCH 1/5] chore: add ability to pass custom message in file size validator --- packages/common/pipes/file/max-file-size.validator.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index e45bf46782d..f693c19a5d7 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -2,6 +2,7 @@ import { FileValidator } from './file-validator.interface'; export type MaxFileSizeValidatorOptions = { maxSize: number; + message?: string | (file?: any) => string; }; /** @@ -12,7 +13,15 @@ export type MaxFileSizeValidatorOptions = { * @publicApi */ export class MaxFileSizeValidator extends FileValidator { - buildErrorMessage(): string { + buildErrorMessage(file: any): string { + if ('message' in this.validationOptions) { + if (typeof this.validationOptions.message === 'function') { + return this.validationOptions.message(file); + } + + return this.validationOptions.message; + } + return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`; } From 619b98caf7bfb2a55c60a2cbb65aecec4684c120 Mon Sep 17 00:00:00 2001 From: Anjorin Damilare Date: Sun, 23 Apr 2023 07:04:50 +0100 Subject: [PATCH 2/5] Update max-file-size.validator.ts --- packages/common/pipes/file/max-file-size.validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index f693c19a5d7..c8dd5640869 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -2,7 +2,7 @@ import { FileValidator } from './file-validator.interface'; export type MaxFileSizeValidatorOptions = { maxSize: number; - message?: string | (file?: any) => string; + message?: string | ((file?: any) => string); }; /** From 93a2b48be710bf1ca00793227abe05931ed90ca0 Mon Sep 17 00:00:00 2001 From: Anjorin Damilare Date: Sun, 23 Apr 2023 07:10:15 +0100 Subject: [PATCH 3/5] chore: remove function to prevent breaking change --- packages/common/pipes/file/max-file-size.validator.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index c8dd5640869..5f420e49455 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -2,7 +2,7 @@ import { FileValidator } from './file-validator.interface'; export type MaxFileSizeValidatorOptions = { maxSize: number; - message?: string | ((file?: any) => string); + message?: string; }; /** @@ -15,10 +15,6 @@ export type MaxFileSizeValidatorOptions = { export class MaxFileSizeValidator extends FileValidator { buildErrorMessage(file: any): string { if ('message' in this.validationOptions) { - if (typeof this.validationOptions.message === 'function') { - return this.validationOptions.message(file); - } - return this.validationOptions.message; } From df0270af04807818e5b81a25ba4000cef5aca806 Mon Sep 17 00:00:00 2001 From: Anjorin Damilare Date: Sun, 23 Apr 2023 07:13:25 +0100 Subject: [PATCH 4/5] Update max-file-size.validator.ts --- packages/common/pipes/file/max-file-size.validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index 5f420e49455..e4c1a152f9f 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -13,7 +13,7 @@ export type MaxFileSizeValidatorOptions = { * @publicApi */ export class MaxFileSizeValidator extends FileValidator { - buildErrorMessage(file: any): string { + buildErrorMessage(): string { if ('message' in this.validationOptions) { return this.validationOptions.message; } From d46db256afa829a7e6586b59d64bfb7dad3a4016 Mon Sep 17 00:00:00 2001 From: Anjorin Damilare Date: Sun, 23 Apr 2023 07:26:04 +0100 Subject: [PATCH 5/5] Update max-file-size.validator.ts --- packages/common/pipes/file/max-file-size.validator.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/common/pipes/file/max-file-size.validator.ts b/packages/common/pipes/file/max-file-size.validator.ts index e4c1a152f9f..dc5fa1b6709 100644 --- a/packages/common/pipes/file/max-file-size.validator.ts +++ b/packages/common/pipes/file/max-file-size.validator.ts @@ -2,7 +2,7 @@ import { FileValidator } from './file-validator.interface'; export type MaxFileSizeValidatorOptions = { maxSize: number; - message?: string; + message?: string | ((maxSize: number) => string); }; /** @@ -15,6 +15,10 @@ export type MaxFileSizeValidatorOptions = { export class MaxFileSizeValidator extends FileValidator { buildErrorMessage(): string { if ('message' in this.validationOptions) { + if (typeof this.validationOptions.message === 'function') { + return this.validationOptions.message(this.validationOptions.maxSize); + } + return this.validationOptions.message; }