Skip to content

Commit 8ddca35

Browse files
committed
Dev - passed validatorArguments to validate function
1 parent 819740b commit 8ddca35

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "class-validator-custom-errors",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Decorator-based property validation for classes with the ability to customize the error messages.",
55
"author": "Aritra Sadhukhan",
66
"license": "MIT",

src/validation/ValidationExecutor.ts

+11-11
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

@@ -76,19 +76,19 @@ export class ValidationExecutor {
7676
e.message = () =>
7777
(this.validatorOptions?.validationError?.transformFunction as Function)(
7878
e.transformKey ||
79-
e.name ||
80-
e.type ||
81-
'This validation does not have an name or type associated with it, please add one.'
79+
e.name ||
80+
e.type ||
81+
'This validation does not have an name or type associated with it, please add one.'
8282
);
8383
});
8484
Object.keys(groupedMetadatas).forEach(prop => {
8585
groupedMetadatas[prop].forEach(e => {
8686
e.message = () =>
8787
(this.validatorOptions?.validationError?.transformFunction as Function)(
8888
e.transformKey ||
89-
e.name ||
90-
e.type ||
91-
'This validation does not have an name or type associated with it, please add one.'
89+
e.name ||
90+
e.type ||
91+
'This validation does not have an name or type associated with it, please add one.'
9292
);
9393
});
9494
});
@@ -295,7 +295,7 @@ export class ValidationExecutor {
295295
};
296296

297297
if (!metadata.each || !(Array.isArray(value) || value instanceof Set || value instanceof Map)) {
298-
const validatedValue = customConstraintMetadata.instance.validate(value, validationArguments);
298+
const validatedValue = customConstraintMetadata.instance.validate(value, validationArguments, this.validatorOptions);
299299
if (isPromise(validatedValue)) {
300300
const promise = validatedValue.then(isValid => {
301301
if (!isValid) {
@@ -324,7 +324,7 @@ export class ValidationExecutor {
324324
const arrayValue = convertToArray(value);
325325
// Validation needs to be applied to each array item
326326
const validatedSubValues = arrayValue.map((subValue: any) =>
327-
customConstraintMetadata.instance.validate(subValue, validationArguments)
327+
customConstraintMetadata.instance.validate(subValue, validationArguments, this.validatorOptions)
328328
);
329329
const validationIsAsync = validatedSubValues.some((validatedSubValue: boolean | Promise<boolean>) =>
330330
isPromise(validatedSubValue)

src/validation/ValidatorConstraintInterface.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { ValidationArguments } from './ValidationArguments';
2+
import { ValidatorOptions } from './ValidatorOptions';
23
/**
34
* Custom validators must implement this interface to provide custom validation logic.
45
*/
56
export interface ValidatorConstraintInterface {
67
/**
78
* Method to be called to perform custom validation over given value.
89
*/
9-
validate(value: any, validationArguments?: ValidationArguments): Promise<boolean> | boolean;
10+
validate(value: any, validationArguments?: ValidationArguments, validatorOptions?: ValidatorOptions): Promise<boolean> | boolean;
1011

1112
/**
1213
* Gets default message when validation for this constraint fail.

0 commit comments

Comments
 (0)