Skip to content

feat: add skipUndefinedProperties and skipNullProperties options #414

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

Merged
merged 1 commit into from
Sep 6, 2019
Merged
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
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