Skip to content

Bug: DynamoDBStreamSchema can't be extended properly #3740

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

Closed
blytheaw opened this issue Mar 18, 2025 · 3 comments · Fixed by #3741
Closed

Bug: DynamoDBStreamSchema can't be extended properly #3740

blytheaw opened this issue Mar 18, 2025 · 3 comments · Fixed by #3741
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility

Comments

@blytheaw
Copy link
Contributor

Expected Behavior

I should be able to extend the Zod schema DynamoDBStreamSchema and appropriately access values at Records[*].dynamodb after parsing and in inferred types.

This should work when using the new-ish DynamoDBMarshalled helper which removes the need for the built-in transform that is breaking this currently.

Current Behavior

If you extend DynamoDBStreamSchema as in this example, any other values/types from the DynamoDBStreamChangeRecord (i.e. Records[*].dynamodb.*) are lost.

Ideally the DynamoDBStreamChangeRecord could be extended as well, but because this schema currently has a Zod transform in it, it cannot be extended.

Code snippet

See Discord thread: https://discord.com/channels/1006478942305263677/1006527385409179678/1350204854148010048

Steps to Reproduce

See Discord thread: https://discord.com/channels/1006478942305263677/1006527385409179678/1350204854148010048

Possible Solution

Export a base schema for DynamoDBStream that doesn't have a transform as part of it so it can be extended

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

@blytheaw blytheaw added bug Something isn't working triage This item has not been triaged by a maintainer, please wait labels Mar 18, 2025
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Mar 19, 2025
@dreamorosi dreamorosi self-assigned this Mar 19, 2025
@dreamorosi dreamorosi added the parser This item relates to the Parser Utility label Mar 19, 2025
@dreamorosi dreamorosi moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Mar 19, 2025
@dreamorosi
Copy link
Contributor

Hi @blytheaw, thank you for reporting this on Discord and taking the time to open an issue.

I tried a sample code like this:

import { Logger } from "@aws-lambda-powertools/logger";
import { DynamoDBMarshalled } from "@aws-lambda-powertools/parser/helpers/dynamodb";
import {
	DynamoDBStreamRecord,
	DynamoDBStreamSchema,
} from "@aws-lambda-powertools/parser/schemas/dynamodb";
import { z } from "zod";

const logger = new Logger({ logLevel: "debug" });

const customSchema = z.object({
	id: z.string(),
});

const extendedSchema = DynamoDBStreamSchema.extend({
	Records: z.array(
		DynamoDBStreamRecord.extend({
			dynamodb: z.object({
				NewImage: DynamoDBMarshalled(customSchema),
			}),
		}),
	),
});

export const handler = async (event: unknown) => {
	logger.debug("event", { event });
	logger.debug("parsed", { parsed: extendedSchema.safeParse(event).data });
};

that outputs these logs:

{
  "level": "DEBUG",
  "message": "event",
  "timestamp": "2025-03-19T11:24:54.468Z",
  "service": "service_undefined",
  "sampling_rate": 0,
  "xray_trace_id": "1-67daa986-6d12de7ba0444d00eb00bf18",
  "event": {
      "Records": [
          {
              "eventID": "b8e6d30883a579b10278fe9f46b310e0",
              "eventName": "INSERT",
              "eventVersion": "1.1",
              "eventSource": "aws:dynamodb",
              "awsRegion": "eu-west-1",
              "dynamodb": {
                  "ApproximateCreationDateTime": 1742383494,
                  "Keys": {
                      "id": {
                          "S": "adadsad"
                      }
                  },
                  "NewImage": {
                      "id": {
                          "S": "adadsad"
                      }
                  },
                  "SequenceNumber": "100000000119433541593",
                  "SizeBytes": 18,
                  "StreamViewType": "NEW_IMAGE"
              },
              "eventSourceARN": "arn:aws:dynamodb:eu-west-1:123456789012:table/ddbstream-aamorosi-SomeTableTable-vbmbeuhw/stream/2025-03-19T11:19:41.987"
          }
        ]
    }
}
{
    "level": "DEBUG",
    "message": "parsed",
    "timestamp": "2025-03-19T11:24:54.476Z",
    "service": "service_undefined",
    "sampling_rate": 0,
    "xray_trace_id": "1-67daa986-6d12de7ba0444d00eb00bf18",
    "parsed": {
        "Records": [
            {
                "eventID": "b8e6d30883a579b10278fe9f46b310e0",
                "eventName": "INSERT",
                "eventVersion": "1.1",
                "eventSource": "aws:dynamodb",
                "awsRegion": "eu-west-1",
                "eventSourceARN": "arn:aws:dynamodb:eu-west-1:123456789012:table/ddbstream-aamorosi-SomeTableTable-vbmbeuhw/stream/2025-03-19T11:19:41.987",
                "dynamodb": {
                    "NewImage": {
                        "id": "adadsad"
                    }
                }
            }
        ]
    }
}

and I can confirm that indeed, using the example in our docs makes the other fields under Records.[*].dynamodb disappear.

I will open a PR to create/export the schema you suggested. I also have noticed another minor issue with the /helpers/dynamodb export, which is missing type hints here, and can cause TypeScript issues under certain configurations.

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Mar 19, 2025
Copy link
Contributor

This is now released under v2.17.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Mar 25, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped parser This item relates to the Parser Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants