diff --git a/src/validation/ValidationExecutor.ts b/src/validation/ValidationExecutor.ts index 8f70bd1b65..8965db4dc3 100644 --- a/src/validation/ValidationExecutor.ts +++ b/src/validation/ValidationExecutor.ts @@ -166,9 +166,17 @@ export class ValidationExecutor { return; } - // handle IS_DEFINED validation type the special way - it should work no matter skipMissingProperties is set or not + // 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); + if (value === undefined && this.validatorOptions && this.validatorOptions.skipUndefinedProperties === true) { + return; + } + + if (value === null && this.validatorOptions && this.validatorOptions.skipNullProperties === true) { + return; + } + if ((value === null || value === undefined) && this.validatorOptions && this.validatorOptions.skipMissingProperties === true) { return; } diff --git a/src/validation/ValidatorOptions.ts b/src/validation/ValidatorOptions.ts index 67cc143038..d610985eb3 100644 --- a/src/validation/ValidatorOptions.ts +++ b/src/validation/ValidatorOptions.ts @@ -2,9 +2,18 @@ * Options passed to validator during validation. */ export interface ValidatorOptions { + /** + * If set to true then validator will skip validation of all properties that are undefined in the validating object. + */ + skipUndefinedProperties?: boolean; + + /** + * If set to true then validator will skip validation of all properties that are null in the validating object. + */ + skipNullProperties?: boolean; /** - * If set to true than validator will skip validation of all properties that are missing in the validating object. + * If set to true then validator will skip validation of all properties that are null or undefined in the validating object. */ skipMissingProperties?: boolean; @@ -53,4 +62,4 @@ export interface ValidatorOptions { */ forbidUnknownValues?: boolean; -} \ No newline at end of file +} diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index d6f86d5ca3..79a2f9ab94 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -157,6 +157,22 @@ describe("IsDefined", function() { checkInvalidValues(new MyClass(), invalidValues, done); }); + it("should not fail if validator.validate said that its valid with skipUndefinedProperties set to true", function(done) { + checkValidValues(new MyClass(), validValues, done, { skipUndefinedProperties: true }); + }); + + it("should fail if validator.validate said that its invalid with skipUndefinedProperties set to true", function(done) { + checkInvalidValues(new MyClass(), invalidValues, done, { skipUndefinedProperties: true }); + }); + + it("should not fail if validator.validate said that its valid with skipNullProperties set to true", function(done) { + checkValidValues(new MyClass(), validValues, done, { skipNullProperties: true }); + }); + + it("should fail if validator.validate said that its invalid with skipNullProperties set to true", function(done) { + checkInvalidValues(new MyClass(), invalidValues, done, { skipNullProperties: true }); + }); + it("should not fail if validator.validate said that its valid with skipMissingProperties set to true", function(done) { checkValidValues(new MyClass(), validValues, done, { skipMissingProperties: true }); });