Skip to content

Commit 15e8ab7

Browse files
Fix casting from empty string to number or boolean (#22)
1 parent 2649728 commit 15e8ab7

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

python/rpdk/typescript/templates/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// {{ type_name }}
1+
# {{ type_name }}
22

33
Congratulations on starting development! Next steps:
44

python/rpdk/typescript/templates/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "{{ name }}",
3-
"version": "1.0.0",
3+
"version": "0.1.0",
44
"description": "{{ description }}",
55
"main": "dist/handlers.js",
66
"files": [

src/recast.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export const recastPrimitive = (
1717
// so we leave it as a string
1818
return v;
1919
}
20+
if (
21+
(Object.is(cls, Boolean) ||
22+
Object.is(cls, Number) ||
23+
Object.is(cls, BigInt) ||
24+
Object.is(cls, Integer)) &&
25+
v.length === 0
26+
) {
27+
return null;
28+
}
2029
if (Object.is(cls, Boolean)) {
2130
if (v.toLowerCase() === 'true') {
2231
return true;

tests/lib/recast.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('when recasting objects', () => {
3131
test('recast complex object', () => {
3232
const payload = {
3333
ListListAny: [[{ key: 'val' }]],
34-
ListListInt: [['1', '2', '3']],
34+
ListListInt: [['1', '2', '3', '']],
3535
ListSetInt: [['1', '2', '3']],
3636
ASet: ['1', '2', '3'],
3737
AnotherSet: ['a', 'b', 'c'],
@@ -63,7 +63,7 @@ describe('when recasting objects', () => {
6363
};
6464
const expected = {
6565
ListSetInt: [new Set([1, 2, 3])],
66-
ListListInt: [[1, 2, 3]],
66+
ListListInt: [[1, 2, 3, null]],
6767
ListListAny: [[{ key: 'val' }]],
6868
ASet: new Set(['1', '2', '3']),
6969
AnotherSet: new Set(['a', 'b', 'c']),
@@ -140,4 +140,15 @@ describe('when recasting objects', () => {
140140
const num = recastPrimitive(Number, k, v);
141141
expect(num).toBe(1252.53);
142142
});
143+
144+
test('recast primitive boolean/number empty string', () => {
145+
const k = 'key';
146+
const v = '';
147+
const bool = recastPrimitive(Boolean, k, v);
148+
const num = recastPrimitive(Number, k, v);
149+
const int = recastPrimitive(BigInt, k, v);
150+
expect(bool).toBeNull();
151+
expect(num).toBeNull();
152+
expect(int).toBeNull();
153+
});
143154
});

0 commit comments

Comments
 (0)