Skip to content

Commit 2504825

Browse files
Merge pull request #1 from arizworld/feat/customize_error_messages
Feat - implemented customizing error messages at the time of validation
2 parents 573942d + b94654c commit 2504825

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/decorator/ValidationOptions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export interface ValidationOptions {
2929
* A transient set of data passed through to the validation result for response mapping
3030
*/
3131
context?: any;
32+
33+
/**
34+
* While using transformFunction if provided then the transformFunction will be called with this.
35+
*/
36+
transformKey?: string
3237
}
3338

3439
export function isValidationOptions(val: any): val is ValidationOptions {

src/metadata/ValidationMetadata.ts

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class ValidationMetadata {
4444
*/
4545
message: string | ((args: ValidationArguments) => string);
4646

47+
/**
48+
* While using transformFunction to customize error messages the transformFunction will be called with this key.
49+
*/
50+
transformKey?: string
4751
/**
4852
* Validation groups used for this validation.
4953
*/
@@ -87,6 +91,7 @@ export class ValidationMetadata {
8791
this.always = args.validationOptions.always;
8892
this.each = args.validationOptions.each;
8993
this.context = args.validationOptions.context;
94+
this.transformKey = args.validationOptions.transformKey;
9095
}
9196
}
9297
}

src/validation/ValidationExecutor.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ValidationExecutor {
3030
// Constructor
3131
// -------------------------------------------------------------------------
3232

33-
constructor(private validator: Validator, private validatorOptions?: ValidatorOptions) {}
33+
constructor(private validator: Validator, private validatorOptions?: ValidatorOptions) { }
3434

3535
// -------------------------------------------------------------------------
3636
// Public Methods
@@ -46,8 +46,8 @@ export class ValidationExecutor {
4646
if (!this.metadataStorage.hasValidationMetaData && this.validatorOptions?.enableDebugMessages === true) {
4747
console.warn(
4848
`No validation metadata found. No validation will be performed. There are multiple possible reasons:\n` +
49-
` - There may be multiple class-validator versions installed. You will need to flatten your dependencies to fix the issue.\n` +
50-
` - This validation runs before any file with validation decorator was parsed by NodeJS.`
49+
` - There may be multiple class-validator versions installed. You will need to flatten your dependencies to fix the issue.\n` +
50+
` - This validation runs before any file with validation decorator was parsed by NodeJS.`
5151
);
5252
}
5353

@@ -67,6 +67,18 @@ export class ValidationExecutor {
6767
);
6868
const groupedMetadatas = this.metadataStorage.groupByPropertyName(targetMetadatas);
6969

70+
/**if transformationFunction is provided then overwrite the message with the transformationFunction */
71+
if (this.validatorOptions?.validationError?.transformFunction && typeof this.validatorOptions?.validationError?.transformFunction === 'function') {
72+
targetMetadatas.forEach(e => {
73+
e.message = () => (this.validatorOptions?.validationError?.transformFunction as Function)(e.transformKey || e.name || e.type || 'This validation does not have an name or type associated with it, please add one.')
74+
});
75+
Object.keys(groupedMetadatas).forEach(prop => {
76+
groupedMetadatas[prop].forEach(e => {
77+
e.message = () => (this.validatorOptions?.validationError?.transformFunction as Function)(e.transformKey || e.name || e.type || 'This validation does not have an name or type associated with it, please add one.')
78+
});
79+
})
80+
}
81+
7082
if (forbidUnknownValues && !targetMetadatas.length) {
7183
const validationError = new ValidationError();
7284

src/validation/ValidatorOptions.ts

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ export interface ValidatorOptions {
6969
* Indicates if validated value should be exposed in ValidationError.
7070
*/
7171
value?: boolean;
72+
73+
/**
74+
* A function to modify the error message.The function will be called with the transformKey or the name of the validation.
75+
* @param key the name of the error.
76+
* @returns string. it will be shown instead of the default error message.
77+
*/
78+
transformFunction?: (key: string) => string
7279
};
7380

7481
/**

0 commit comments

Comments
 (0)