Skip to content

fix(parser): DynamoDBStream schema & envelope #3482

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

Merged
merged 12 commits into from
Jan 17, 2025
Merged

fix(parser): DynamoDBStream schema & envelope #3482

merged 12 commits into from
Jan 17, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jan 16, 2025

Summary

Changes

Please provide a summary of what's being changed

This PR updates the logic of the DynamoDBStreamSchema and DynamoDBStreamEnvelope built-in schemas so that they automatically handle DynamoDB attribute values for the customer and convert them to JavaScript native objects during the parsing and validation.

As part of the PR, I have also improved the error messages in the parser to include the index of the record being processed as well as the full path of the issue when parsing fails. For example, now the error would be something like:

new ParseError('Failed to parse DynamoDB Stream envelope', {
  cause: new ZodError([
    {
      code: 'invalid_type',
      expected: 'object',
      received: 'undefined',
      path: ['Records', 0, 'dynamodb'],
      message: 'Required',
    }
  ]),
})

and like this for failed parsing of Keys, NewImage, and OldImage:

new ParseError('Failed to parse DynamoDB Stream envelope', {
  cause: new ZodError([
    {
      code: 'invalid_type',
      expected: 'number',
      received: 'object',
      path: ['Records', 0, 'dynamodb', 'NewImage'],
      message: 'Expected number, received object',
    }
  ]),
})

Likewise, when parsing an envelope using safeParse, the errors can also be combined when multiple records have issues:

new ParseError('Failed to parse records at indexes 0, 1', {
    cause: new ZodError([
      {
        code: 'invalid_type',
        expected: 'string',
        received: 'undefined',
        path: ['Records', 0, 'dynamodb', 'NewImage', 'Message'],
        message: 'Required',
      },
      {
        code: 'invalid_type',
        expected: 'string',
        received: 'number',
        path: ['Records', 1, 'dynamodb', 'NewImage', 'Message'],
        message: 'Expected string, received number',
      },
    ]),
  }),
  originalEvent: event,
})

Note that in all cases the full path of the object is preserved even though we are performing multiple Zod validations under the hood.

While working on this PR, I also brought into the commons package an implementation of the unmarshall function inspired by the one in the AWS SDK. This is similar to what Powertools for AWS Lambda (Python) does, and allows us to provide this behavior without forcing an extra dependency. Our implementation also differs from the reference one in a couple minor ways since we don't have to support old Node.js versions.

This new behavior is compatible with the DynamoDBHelper introduced in the previous release. This is because in order for customers to use the helper they will have to extend & replace the schema, thus bypassing the transform introduced in this PR.

Finally, we're treating this change as a fix because this should've been the default behavior all along.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: fixes #3481


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this Jan 16, 2025
@dreamorosi dreamorosi requested a review from a team January 16, 2025 14:44
@dreamorosi dreamorosi requested a review from a team as a code owner January 16, 2025 14:44
@boring-cyborg boring-cyborg bot added commons This item relates to the Commons Utility dependencies Changes that touch dependencies, e.g. Dependabot, etc. parser This item relates to the Parser Utility tests PRs that add or change tests labels Jan 16, 2025
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Jan 16, 2025
@github-actions github-actions bot added bug Something isn't working labels Jan 16, 2025
@dreamorosi dreamorosi requested a review from am29d January 17, 2025 08:41
@pull-request-size pull-request-size bot added size/XXL PRs with 1K+ LOC, largely documentation related and removed size/XL PRs between 500-999 LOC, often PRs that grown with feedback labels Jan 17, 2025
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR, thanks for the work bringing the unmarschalling into envelope and schema!

Just a minor code duplication we'd need to fix, otherwise ready to go.

@dreamorosi dreamorosi requested a review from am29d January 17, 2025 13:21
@pull-request-size pull-request-size bot added size/XL PRs between 500-999 LOC, often PRs that grown with feedback and removed size/XXL PRs with 1K+ LOC, largely documentation related labels Jan 17, 2025
Copy link
Contributor

@am29d am29d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful!

@am29d am29d merged commit 7f7f8ce into main Jan 17, 2025
38 checks passed
@am29d am29d deleted the fix/dynamodb_parser branch January 17, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working commons This item relates to the Commons Utility dependencies Changes that touch dependencies, e.g. Dependabot, etc. parser This item relates to the Parser Utility size/XL PRs between 500-999 LOC, often PRs that grown with feedback tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: DynamoDB Stream schema & envelope should unmarshall payloads
2 participants