diff --git a/README.md b/README.md index 886712dd76..bc4938edc5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/validation/ValidationArguments.ts b/src/validation/ValidationArguments.ts index 5e760187f2..80b1fed56d 100644 --- a/src/validation/ValidationArguments.ts +++ b/src/validation/ValidationArguments.ts @@ -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. */ diff --git a/src/validation/ValidationExecutor.ts b/src/validation/ValidationExecutor.ts index 9d3d312f14..587692cf46 100644 --- a/src/validation/ValidationExecutor.ts +++ b/src/validation/ValidationExecutor.ts @@ -24,6 +24,7 @@ export class ValidationExecutor { // Private Properties // ------------------------------------------------------------------------- + private instance: any = undefined; private metadataStorage = getMetadataStorage(); // ------------------------------------------------------------------------- @@ -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; @@ -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, }; @@ -405,6 +412,7 @@ export class ValidationExecutor { property: metadata.propertyName, object: object, value: value, + instance: this.instance, constraints: metadata.constraints, };