Skip to content

Commit

Permalink
feat: add skipUndefinedProperties, skipNullProperties options (#414)
Browse files Browse the repository at this point in the history
Close #308

Co-authored-by: Coroliov Oleg <[email protected]>
  • Loading branch information
2 people authored and vlapo committed Sep 6, 2019
1 parent 204b7df commit 76c948a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 11 additions & 2 deletions src/validation/ValidatorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,4 +62,4 @@ export interface ValidatorOptions {
*/
forbidUnknownValues?: boolean;

}
}
16 changes: 16 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
Expand Down

0 comments on commit 76c948a

Please sign in to comment.