Skip to content

Commit 18b9624

Browse files
committed
Fix transform of null primitive values
1 parent 10ff288 commit 18b9624

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/TransformOperationExecutor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ export class TransformOperationExecutor {
5757
return newValue;
5858

5959
} else if (targetType === String && !isMap) {
60+
if (value === null || value === undefined)
61+
return value;
6062
return String(value);
6163

6264
} else if (targetType === Number && !isMap) {
65+
if (value === null || value === undefined)
66+
return value;
6367
return Number(value);
6468

6569
} else if (targetType === Boolean && !isMap) {
70+
if (value === null || value === undefined)
71+
return value;
6672
return Boolean(value);
6773

6874
} else if ((targetType === Date || value instanceof Date) && !isMap) {

test/functional/basic-functionality.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ describe("basic functionality", () => {
507507

508508
@Type(type => String)
509509
lastVisitDate: string;
510+
511+
@Type(type => String)
512+
signature?: null | string;
510513
}
511514

512515
const date = new Date();
@@ -517,14 +520,16 @@ describe("basic functionality", () => {
517520
user.isActive = "1" as any;
518521
user.registrationDate = date.toString() as any;
519522
user.lastVisitDate = date as any;
523+
user.signature = null as any;
520524

521525
const fromPlainUser = {
522526
firstName: 321,
523527
lastName: 123,
524528
password: "123",
525529
isActive: "1",
526530
registrationDate: date.toString(),
527-
lastVisitDate: date
531+
lastVisitDate: date,
532+
signature: null as null | string,
528533
};
529534

530535
const fromExistUser = new User();
@@ -539,6 +544,7 @@ describe("basic functionality", () => {
539544
isActive: true,
540545
registrationDate: new Date(date.toString()),
541546
lastVisitDate: date.toString(),
547+
signature: null,
542548
});
543549

544550
const existUser = { id: 1, age: 27 };
@@ -553,6 +559,7 @@ describe("basic functionality", () => {
553559
isActive: true,
554560
registrationDate: new Date(date.toString()),
555561
lastVisitDate: date.toString(),
562+
signature: null,
556563
});
557564
plainUser2.should.be.equal(existUser);
558565

@@ -565,6 +572,7 @@ describe("basic functionality", () => {
565572
isActive: true,
566573
registrationDate: new Date(date.toString()),
567574
lastVisitDate: date.toString(),
575+
signature: null,
568576
});
569577

570578
const fromExistTransformedUser = plainToClassFromExist(fromExistUser, fromPlainUser, { strategy: "exposeAll" });
@@ -577,6 +585,7 @@ describe("basic functionality", () => {
577585
isActive: true,
578586
registrationDate: new Date(date.toString()),
579587
lastVisitDate: date.toString(),
588+
signature: null,
580589
});
581590

582591
const classToClassUser = classToClass(user, { strategy: "exposeAll" });
@@ -589,6 +598,7 @@ describe("basic functionality", () => {
589598
isActive: true,
590599
registrationDate: new Date(date.toString()),
591600
lastVisitDate: date.toString(),
601+
signature: null,
592602
});
593603

594604
const classToClassFromExistUser = classToClassFromExist(user, fromExistUser, { strategy: "exposeAll" });
@@ -603,6 +613,7 @@ describe("basic functionality", () => {
603613
isActive: true,
604614
registrationDate: new Date(date.toString()),
605615
lastVisitDate: date.toString(),
616+
signature: null,
606617
});
607618
});
608619

@@ -1681,4 +1692,4 @@ describe("basic functionality", () => {
16811692

16821693
});
16831694

1684-
});
1695+
});

0 commit comments

Comments
 (0)