Skip to content

Extend expression validation to handle complex UpdateExpressions #6

@wparad

Description

@wparad

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);
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions