Skip to content

Commit 14ffab1

Browse files
author
Tony Joblin
committed
Fix getKey handling of falsy pk and sk
getKey checks for the presence of pk and sk and throws an error if they are not present when they should be present. The code errors when the keys are falsy. This is a problem for falsy values like 0 for numeric types which are allowed in dynamo. Updated the code to allow 0 but disallow undefined, null and ''.
1 parent 7c91705 commit 14ffab1

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/getKey.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ module.exports = (DocumentClient) => (data,schema,partitionKey,sortKey) => {
1919
// Intialize validate type
2020
let validateType = validateTypes(DocumentClient)
2121

22-
let pk = data[partitionKey] ||
23-
error(`'${partitionKey}'${schema[partitionKey].alias ? ` or '${schema[partitionKey].alias}'` : ''} is required`)
24-
25-
let sk = sortKey === null || data[sortKey] ||
26-
error(`'${sortKey}'${schema[sortKey].alias ? ` or '${schema[sortKey].alias}'` : ''} is required`)
22+
let pk = data[partitionKey]
23+
if (pk === undefined || pk === null || pk === '') {
24+
error(`'${partitionKey}'${schema[partitionKey].alias ? ` or '${schema[partitionKey].alias}'` : ''} is required`)
25+
}
26+
27+
const sk = data[sortKey]
28+
if (sortKey && (sk === undefined || sk === null || sk === '')) {
29+
error(`'${sortKey}'${schema[sortKey].alias ? ` or '${schema[sortKey].alias}'` : ''} is required`)
30+
}
2731

2832
return Object.assign(
2933
{ [partitionKey]: transformAttr(schema[partitionKey],validateType(schema[partitionKey],partitionKey,pk,data),data) },

0 commit comments

Comments
 (0)