Skip to content

feat: access the node instance in custom validation #2327

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

Open
wants to merge 2 commits 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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ If you have custom validation logic you can create a _Constraint class_:
which defines validation logic. If validation succeeds, method returns true, otherwise false.
Custom validator can be asynchronous, if you want to perform validation after some asynchronous
operations, simply return a promise with boolean inside in `validate` method.
`ValidationArguments` contains useful information about validation process, like object that is being validated,
or the current object instance to validate value based on parent nodes.

Also we defined optional method `defaultMessage` which defines a default error message,
in the case that the decorator's implementation doesn't set an error message.
Expand Down
5 changes: 5 additions & 0 deletions src/validation/ValidationArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface ValidationArguments {
*/
object: object;

/**
* Instance of the object being validated.
*/
instance: object;

/**
* Name of the object's property being validated.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ValidationExecutor {
// Private Properties
// -------------------------------------------------------------------------

private instance: any = undefined;
private metadataStorage = getMetadataStorage();

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -51,6 +52,11 @@ export class ValidationExecutor {
);
}

// Keep the instance to the original object
if (this.instance === undefined){
this.instance = object;
}

const groups = this.validatorOptions ? this.validatorOptions.groups : undefined;
const strictGroups = (this.validatorOptions && this.validatorOptions.strictGroups) || false;
const always = (this.validatorOptions && this.validatorOptions.always) || false;
Expand Down Expand Up @@ -263,6 +269,7 @@ export class ValidationExecutor {
targetName: object.constructor ? (object.constructor as any).name : undefined,
property: metadata.propertyName,
object: object,
instance: this.instance,
value: value,
constraints: metadata.constraints,
};
Expand Down Expand Up @@ -405,6 +412,7 @@ export class ValidationExecutor {
property: metadata.propertyName,
object: object,
value: value,
instance: this.instance,
constraints: metadata.constraints,
};

Expand Down