Skip to content

Commit b8acd3f

Browse files
committed
fix: skip field if nothing provided and default not set
1 parent d1164cd commit b8acd3f

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

src/TransformOperationExecutor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,11 @@ export class TransformOperationExecutor {
244244
finalValue = this.transform(subSource, finalValue, type, arrayType, isSubValueMap, level + 1);
245245
} else {
246246
if (subValue === undefined) {
247-
// Set default value if nothing provided
248-
finalValue = newValue[newValueKey];
247+
const defaultValue = newValue[newValueKey];
248+
// Skip field if default value not set
249+
if (defaultValue === undefined) continue;
250+
// Set default value
251+
finalValue = defaultValue;
249252
} else {
250253
finalValue = this.transform(subSource, subValue, type, arrayType, isSubValueMap, level + 1);
251254
finalValue = this.applyCustomTransformations(finalValue, (targetType as Function), transformKey, value, this.transformationType);

test/functional/basic-functionality.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe("basic functionality", () => {
121121
const transformedUser = plainToClass(User, fromPlainUser);
122122
transformedUser.should.be.instanceOf(User);
123123
transformedUser.should.have.property("age");
124-
transformedUser.should.have.property("id").that.is.undefined;
124+
transformedUser.should.not.have.property("id");
125125

126126
const transformedUserWithoutExtra = plainToClass(User, fromPlainUser, { excludeExtraneousValues: true });
127127
transformedUserWithoutExtra.should.be.instanceOf(User);
@@ -1858,9 +1858,7 @@ describe("basic functionality", () => {
18581858
transformedUser.should.be.instanceOf(User);
18591859

18601860
transformedUser.should.be.eql({
1861-
age: undefined,
18621861
ageWithDefault: 18,
1863-
firstName: undefined,
18641862
firstNameWithDefault: "default first name",
18651863
adminWithDefault: false,
18661864
lastNameWithDefault: "default last name",

0 commit comments

Comments
 (0)