Skip to content

Skip undefined properties #353

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

Closed
Closed
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
6 changes: 5 additions & 1 deletion src/validation/ValidationExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ 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 || value === undefined) && this.validatorOptions && this.validatorOptions.skipMissingProperties === true) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/validation/ValidatorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
export interface ValidatorOptions {

/**
* 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 undefined in the validating object.
*/
skipUndefinedProperties?: boolean;

/**
* 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
8 changes: 8 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ 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 skipMissingProperties set to true", function(done) {
checkValidValues(new MyClass(), validValues, done, { skipMissingProperties: true });
});
Expand Down