Skip to content

Commit

Permalink
Pass context for isDefined and custom validators
Browse files Browse the repository at this point in the history
Context wasn't being passed in for isDefined and custom validators.

typestack#292
  • Loading branch information
chiangf committed Oct 23, 2019
1 parent a98f5dd commit 4aed74d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export class ValidationExecutor {

// handle IS_DEFINED validation type the special way - it should work no matter skipUndefinedProperties/skipMissingProperties is set or not
this.defaultValidations(object, value, definedMetadatas, validationError.constraints);
this.mapContexts(object, value, definedMetadatas, validationError);

if (value === undefined && this.validatorOptions && this.validatorOptions.skipUndefinedProperties === true) {
return;
Expand All @@ -186,6 +187,7 @@ export class ValidationExecutor {
this.nestedValidations(value, nestedValidationMetadatas, validationError.children);

this.mapContexts(object, value, metadatas, validationError);
this.mapContexts(object, value, customValidationMetadatas, validationError);
}

private generateValidationError(object: Object, value: any, propertyName: string) {
Expand Down
13 changes: 10 additions & 3 deletions test/functional/validation-options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "es6-shim";
import {Contains, Matches, MinLength, ValidateNested, ValidatorConstraint, Validate } from "../../src/decorator/decorators";
import {Contains, IsDefined, Matches, MinLength, ValidateNested, ValidatorConstraint, Validate } from "../../src/decorator/decorators";
import {Validator} from "../../src/validation/Validator";
import {ValidationError, ValidatorConstraintInterface} from "../../src";

Expand Down Expand Up @@ -953,14 +953,21 @@ describe("validation options", function() {
}
})
someOtherProperty: string;

@IsDefined({
context: {
foo: "bar"
}
})
requiredProperty: string;
}

const model = new MyClass();
// model.someProperty = "hell no world";
return validator.validate(model).then(errors => {
errors.length.should.be.equal(2);
errors.length.should.be.equal(3);
errors[0].contexts["contains"].should.be.eql({ hi: "there" });
errors[1].contexts["contains"].should.be.eql({ bye: "now" });
errors[2].contexts["isDefined"].should.be.eql({ foo: "bar" });
});
});

Expand Down

0 comments on commit 4aed74d

Please sign in to comment.