Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pass propertyName to ValidateIf condition function #2415

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/decorator/common/ValidateIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getMetadataStorage } from '../../metadata/MetadataStorage';
* Ignores the other validators on a property when the provided condition function returns false.
*/
export function ValidateIf(
condition: (object: any, value: any) => boolean,
condition: (object: any, value: any, propertyName: string) => boolean,
validationOptions?: ValidationOptions
): PropertyDecorator {
return function (object: object, propertyName: string): void {
Expand Down
11 changes: 8 additions & 3 deletions src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class ValidationExecutor {
const validationError = this.generateValidationError(object, value, propertyName);
validationErrors.push(validationError);

const canValidate = this.conditionalValidations(object, value, conditionalValidationMetadatas);
const canValidate = this.conditionalValidations(object, value, propertyName, conditionalValidationMetadatas);
if (!canValidate) {
return;
}
Expand Down Expand Up @@ -242,9 +242,14 @@ export class ValidationExecutor {
return validationError;
}

private conditionalValidations(object: object, value: any, metadatas: ValidationMetadata[]): ValidationMetadata[] {
private conditionalValidations(
object: object,
value: any,
propertyName: string,
metadatas: ValidationMetadata[]
): ValidationMetadata[] {
return metadatas
.map(metadata => metadata.constraints[0](object, value))
.map(metadata => metadata.constraints[0](object, value, propertyName))
.reduce((resultA, resultB) => resultA && resultB, true);
}

Expand Down