Skip to content

Commit 233bc86

Browse files
fix(common): revert back to nil value when acceptable
1 parent 5913727 commit 233bc86

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

packages/common/pipes/validation.pipe.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ export class ValidationPipe implements PipeTransform<any> {
6565
if (!metatype || !this.toValidate(metadata)) {
6666
return value;
6767
}
68+
const originalValue = value;
6869
value = this.toEmptyIfNil(value);
6970

71+
const isNil = value !== originalValue;
7072
const isPrimitive = this.isPrimitive(value);
7173
this.stripProtoKeys(value);
7274
let entity = classTransformer.plainToClass(
@@ -96,9 +98,14 @@ export class ValidationPipe implements PipeTransform<any> {
9698
// we have to revert the original value passed through the pipe
9799
entity = originalEntity;
98100
}
99-
return this.isTransformEnabled
100-
? entity
101-
: Object.keys(this.validatorOptions).length > 0
101+
if (this.isTransformEnabled) {
102+
return entity;
103+
}
104+
if (isNil) {
105+
// if the value was originally undefined or null, revert it back
106+
return originalValue;
107+
}
108+
return Object.keys(this.validatorOptions).length > 0
102109
? classTransformer.classToPlain(entity, this.transformOptions)
103110
: value;
104111
}

0 commit comments

Comments
 (0)