Skip to content

Commit 515aae3

Browse files
simluLukas Siemon
andauthored
fix: corrected "object but not array"-checks (dynamodb-toolbox#291)
This was a problem because `typeof null === 'object'`. In particular this was causing issues with `default: null` on list fields. Reference: https://stackoverflow.com/questions/8834126/how-to-efficiently-check-if-variable-is-array-or-object-in-nodejs-v8 Co-authored-by: Lukas Siemon <lukas.siemon@blackflux.com>
1 parent c0b41d2 commit 515aae3

File tree

6 files changed

+11
-13
lines changed

6 files changed

+11
-13
lines changed

src/classes/Entity/Entity.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Entity<
121121
>
122122
) {
123123
// Sanity check the entity object
124-
if (typeof entity !== 'object' || Array.isArray(entity)) {
124+
if (entity?.constructor !== Object) {
125125
error('Please provide a valid entity definition')
126126
}
127127

@@ -926,10 +926,10 @@ class Entity<
926926
if (!Array.isArray(DELETE)) error('DELETE must be an array')
927927

928928
// Validate attribute names and values
929-
if (typeof ExpressionAttributeNames !== 'object' || Array.isArray(ExpressionAttributeNames)) {
929+
if (ExpressionAttributeNames?.constructor !== Object) {
930930
error('ExpressionAttributeNames must be an object')
931931
}
932-
if (typeof ExpressionAttributeValues !== 'object' || Array.isArray(ExpressionAttributeValues)) {
932+
if (ExpressionAttributeValues?.constructor !== Object) {
933933
error('ExpressionAttributeValues must be an object')
934934
}
935935
// if (ConditionExpression && typeof ConditionExpression !== 'string')
@@ -1136,8 +1136,7 @@ class Entity<
11361136
// if a list and updating by index
11371137
} else if (
11381138
mapping.type === 'list' &&
1139-
!Array.isArray(data[field]) &&
1140-
typeof data[field] === 'object'
1139+
data[field]?.constructor === Object
11411140
) {
11421141
Object.keys(data[field]).forEach(i => {
11431142
if (String(parseInt(i)) !== i) {

src/classes/Table/Table.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,8 +1070,7 @@ class Table<Name extends string, PartitionKey extends A.Key, SortKey extends A.K
10701070
item.Table &&
10711071
item.Table.Table &&
10721072
item.Key &&
1073-
typeof item.Key === 'object' &&
1074-
!Array.isArray(item.Key)
1073+
item.Key?.constructor === Object
10751074
) {
10761075
// Set the table
10771076
const table = item.Table.name
@@ -1097,7 +1096,7 @@ class Table<Name extends string, PartitionKey extends A.Key, SortKey extends A.K
10971096
// If true, add to all table mappings
10981097
if (consistent === true) {
10991098
for (const tbl in RequestItems) RequestItems[tbl].ConsistentRead = true
1100-
} else if (typeof consistent === 'object' && !Array.isArray(consistent)) {
1099+
} else if (consistent?.constructor === Object) {
11011100
for (const tbl in consistent as Object) {
11021101
const tbl_name = TableAliases[tbl] || tbl
11031102
if (RequestItems[tbl_name]) {

src/lib/parseCompositeKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const parseCompositeKey = <
3535
? { type: 'string' }
3636
: ['string', 'number', 'boolean'].includes(config[2].toString())
3737
? { type: config[2] }
38-
: typeof config[2] === 'object' && !Array.isArray(config[2])
38+
: config[2]?.constructor === Object
3939
? config[2]
4040
: error(
4141
`'${field}' type must be 'string', 'number', 'boolean' or a configuration object`

src/lib/parseEntity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function parseEntity<
115115

116116
// Sanity check the attributes
117117
attributes =
118-
typeof attributes === 'object' && !Array.isArray(attributes)
118+
attributes?.constructor === Object
119119
? attributes
120120
: error(`Please provide a valid 'attributes' object`)
121121

src/lib/parseTable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const parseTable = <
7979

8080
// Parse table attributes
8181
attributes =
82-
hasValue(attributes) && typeof attributes === 'object' && !Array.isArray(attributes)
82+
hasValue(attributes) && attributes?.constructor === Object
8383
? attributes
8484
: attributes
8585
? error(`Please provide a valid 'attributes' object`)
@@ -90,7 +90,7 @@ export const parseTable = <
9090

9191
// Parse indexes (optional)
9292
indexes =
93-
hasValue(indexes) && typeof indexes === 'object' && !Array.isArray(indexes)
93+
hasValue(indexes) && indexes?.constructor === Object
9494
? // 🔨 TOIMPROVE: Allow numbers & symbols in parseIndexes ?
9595
parseIndexes(indexes, partitionKey as string)
9696
: indexes

src/lib/validateTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default (DocumentClient: DocumentClient) => (mapping: any, field: any, va
4242
.map(x => x.trim())
4343
: error(`'${field}' must be a list (array)`)
4444
case 'map':
45-
return typeof value === 'object' && !Array.isArray(value)
45+
return value?.constructor === Object
4646
? value
4747
: error(`'${field}' must be a map (object)`)
4848
case 'set':

0 commit comments

Comments
 (0)