Parser: Why does S3SqsEventNotificationRecordSchema not use S3Schema as type for body property? #3449
-
Hi, I am implementing a Lambda handler that will consume a SQS queue filled with S3 event notifications. I expected that Here's a copy of the official schemas for easy reference source: const S3SqsEventNotificationSchema = z.object({
Records: z.array(S3SqsEventNotificationRecordSchema),
});
const S3SqsEventNotificationRecordSchema = SqsRecordSchema.extend({
body: z.string(),
}); I wonder if this design decision was made on purpose? I have solved it by creating a custom version of const extendedS3SqsEventNotificationSchema = S3SqsEventNotificationSchema.extend({
Records: z.array(SqsRecordSchema.extend({
body: JSONStringified(S3Schema),
})),
});
type ExtendedS3SqsEventNotification = z.infer<typeof extendedS3SqsEventNotificationSchema>; Full code example for reference:```ts import type { SQSHandler } from 'aws-lambda'; import middy from '@middy/core'; import { Logger } from '@aws-lambda-powertools/logger'; import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware'; import { parser } from '@aws-lambda-powertools/parser/middleware'; import { S3Schema, S3SqsEventNotificationSchema, SqsRecordSchema, } from '@aws-lambda-powertools/parser/schemas'; import { JSONStringified } from '@aws-lambda-powertools/parser/helpers'; import { BatchProcessor, EventType, processPartialResponse } from '@aws-lambda-powertools/batch'; import { z } from 'zod';const processor = new BatchProcessor(EventType.SQS); const logger = new Logger({ const extendedS3SqsEventNotificationSchema = S3SqsEventNotificationSchema.extend({ type ExtendedS3SqsEventNotification = z.infer; const recordHandler = async (record: ExtendedS3SqsEventNotification['Records'][number]): Promise => { const lambdaSqsHandler: SQSHandler = async (event, context) => export const handler = middy(lambdaSqsHandler)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @cyrildewit - apologies for the late reply. The If we defined the schema like this: const S3SqsEventNotificationRecordSchema = SqsRecordSchema.extend({
body: S3Schema,
}); The parsing would always fail since When it comes to generic SQS events we can't make the assumption of the If you'd like us to pre-parse the |
Beta Was this translation helpful? Give feedback.
Hi @dreamorosi,
First of all, please do not apologize for something that you shouldn't ;). There's no agreement between us and definitely no obligation. I am grateful for your eloborate response and would like to take this moment to show my appreciation for this project in general! It's definitely a game changer! I just started with implementing Lambda's using TypeScript. I did a quite extensive research on the topic and can conclude that these utilities lay a foundation with the best practices that allows me to focus on the things that matter. Well done!
Back to the topic, the reason why I first wanted to reach out within the discussions section is to make sure that I fully understand wh…