Skip to content

Commit 361db4f

Browse files
committed
fix(param-util): more strict regex for mergeUpdateExpressions
1 parent 09f6ed7 commit 361db4f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/dynamo/expression/param-util.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,15 @@ describe('ParamUtils', () => {
8181
'SET #doRemove = :doRemove, #doAdd = :doAdd REMOVE c, d',
8282
)
8383
})
84+
it('should work correctly even if a property is named DELETE', () => {
85+
expect(
86+
mergeUpdateExpressions(
87+
'DELETE #myProp, #myProp2, #DELETE ADD #myOtherProp :myOtherVal',
88+
'SET #addresses = list_append(#addresses, :addresses), #name = :name',
89+
),
90+
).toBe(
91+
'DELETE #myProp, #myProp2, #DELETE ADD #myOtherProp :myOtherVal SET #addresses = list_append(#addresses, :addresses), #name = :name',
92+
)
93+
})
8494
})
8595
})

src/dynamo/expression/param-util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ export function mergeUpdateExpressions(expression1: string, expression2: string)
6262
}
6363

6464
function splitUpdateExpressionToActionKeyword(updateExpression: string): UpdateExpressionsByKeyword {
65-
return updateExpression
66-
.split(/\s?(SET|REMOVE|ADD|DELETE)\s/g)
65+
// add a whitespace at the beginning of the expression to be able to work with a more stricter regex
66+
return ` ${updateExpression}`
67+
// the regex ensures a whitespace at the beginning of the ActionWord
68+
// -> to not have problems with properties named exactly as an ActionKeyword
69+
.split(/\s(SET|REMOVE|ADD|DELETE)\s/g)
6770
.reduce((u, e, i, arr) => {
6871
if (isUpdateActionKeyword(e)) {
6972
u[e] = arr[i + 1]

0 commit comments

Comments
 (0)