Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend expression validation to handle complex UpdateExpressions #6

Open
wparad opened this issue Feb 15, 2024 · 0 comments
Open

Extend expression validation to handle complex UpdateExpressions #6

wparad opened this issue Feb 15, 2024 · 0 comments

Comments

@wparad
Copy link
Member

wparad commented Feb 15, 2024

Example:

for (const partialExpression of partialExpressions) {
    const partialExpressionOrderedTokens = (partialExpression || '').replace(/^(DELETE|ADD|SET|REMOVE)\s+/i, '')
    .trim().split(',').filter(token => token).map(token => token.trim());

    const isValidKey = operand => operand.match(/^#\w+(\.#\w)*$/);
    const isValidOperand = operand => operand.match(/^:\w+$/) || operand.match(/^(#\w+(\.#\w)*|:\w+)[-+](#\w+(\.#\w)*|:\w+)$/);
    
    const arrayIterator = partialExpressionOrderedTokens[Symbol.iterator]();
    let result;
    
    while ((result = arrayIterator.next()) && !result.done) {
      const key = arrayIterator.next().value;
      const equalSign = arrayIterator.next().value;
      const operand = arrayIterator.next().value;

      if (!isValidKey(key) || equalSign !== '=' || !isValidOperand(operand)) {
        logger({ code: 'InvalidExpression', expression });
        throw new DynamoDbError({
          title: 'Invalid Expression: the expression does not match what DynamoDB expects: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html', expression,
          tokens: [key, equalSign, operand]
        }, 'InvalidExpression');
      }
      
      // parseExpressionRecursive(token, orderedTokens);
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant