Skip to content

Commit 76c948a

Browse files
rusconCoroliov Oleg
authored andcommitted
feat: add skipUndefinedProperties, skipNullProperties options (#414)
Close #308 Co-authored-by: Coroliov Oleg <coroliov.o@goparrot.ai>
1 parent 204b7df commit 76c948a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/validation/ValidationExecutor.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,17 @@ export class ValidationExecutor {
166166
return;
167167
}
168168

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

172+
if (value === undefined && this.validatorOptions && this.validatorOptions.skipUndefinedProperties === true) {
173+
return;
174+
}
175+
176+
if (value === null && this.validatorOptions && this.validatorOptions.skipNullProperties === true) {
177+
return;
178+
}
179+
172180
if ((value === null || value === undefined) && this.validatorOptions && this.validatorOptions.skipMissingProperties === true) {
173181
return;
174182
}

src/validation/ValidatorOptions.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
* Options passed to validator during validation.
33
*/
44
export interface ValidatorOptions {
5+
/**
6+
* If set to true then validator will skip validation of all properties that are undefined in the validating object.
7+
*/
8+
skipUndefinedProperties?: boolean;
9+
10+
/**
11+
* If set to true then validator will skip validation of all properties that are null in the validating object.
12+
*/
13+
skipNullProperties?: boolean;
514

615
/**
7-
* If set to true than validator will skip validation of all properties that are missing in the validating object.
16+
* If set to true then validator will skip validation of all properties that are null or undefined in the validating object.
817
*/
918
skipMissingProperties?: boolean;
1019

@@ -53,4 +62,4 @@ export interface ValidatorOptions {
5362
*/
5463
forbidUnknownValues?: boolean;
5564

56-
}
65+
}

test/functional/validation-functions-and-decorators.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ describe("IsDefined", function() {
157157
checkInvalidValues(new MyClass(), invalidValues, done);
158158
});
159159

160+
it("should not fail if validator.validate said that its valid with skipUndefinedProperties set to true", function(done) {
161+
checkValidValues(new MyClass(), validValues, done, { skipUndefinedProperties: true });
162+
});
163+
164+
it("should fail if validator.validate said that its invalid with skipUndefinedProperties set to true", function(done) {
165+
checkInvalidValues(new MyClass(), invalidValues, done, { skipUndefinedProperties: true });
166+
});
167+
168+
it("should not fail if validator.validate said that its valid with skipNullProperties set to true", function(done) {
169+
checkValidValues(new MyClass(), validValues, done, { skipNullProperties: true });
170+
});
171+
172+
it("should fail if validator.validate said that its invalid with skipNullProperties set to true", function(done) {
173+
checkInvalidValues(new MyClass(), invalidValues, done, { skipNullProperties: true });
174+
});
175+
160176
it("should not fail if validator.validate said that its valid with skipMissingProperties set to true", function(done) {
161177
checkValidValues(new MyClass(), validValues, done, { skipMissingProperties: true });
162178
});

0 commit comments

Comments
 (0)