From 188d7fbb9b00c071164cccf2c643b18966e2a961 Mon Sep 17 00:00:00 2001 From: cyrildewit <16477999+cyrildewit@users.noreply.github.com> Date: Sat, 25 Jan 2025 01:32:58 +0100 Subject: [PATCH] fix(parser): parse sqs record as JSON and S3Schema in S3SqsEventNotificationRecordSchema --- packages/parser/src/schemas/s3.ts | 6 +++++- packages/parser/tests/unit/schema/s3.test.ts | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/parser/src/schemas/s3.ts b/packages/parser/src/schemas/s3.ts index 07f5a01e33..0505f635be 100644 --- a/packages/parser/src/schemas/s3.ts +++ b/packages/parser/src/schemas/s3.ts @@ -1,4 +1,5 @@ import { z } from 'zod'; +import { JSONStringified } from '../helpers.js'; import { EventBridgeSchema } from './eventbridge.js'; import { SqsRecordSchema } from './sqs.js'; @@ -170,12 +171,15 @@ const S3Schema = z.object({ }); const S3SqsEventNotificationRecordSchema = SqsRecordSchema.extend({ - body: z.string(), + body: JSONStringified(S3Schema), }); /** * Zod schema for S3 -> SQS -> Lambda event notification. * + * Each SQS record’s body field is automatically parsed from a JSON string + * and then validated as an S3 event. + * * @example * ```json * { diff --git a/packages/parser/tests/unit/schema/s3.test.ts b/packages/parser/tests/unit/schema/s3.test.ts index 600aa6a1bf..be7124dbe1 100644 --- a/packages/parser/tests/unit/schema/s3.test.ts +++ b/packages/parser/tests/unit/schema/s3.test.ts @@ -196,11 +196,15 @@ describe('Schema: S3', () => { filename: 'sqs-event', }); + const expected = structuredClone(event); + // @ts-expect-error - Modifying the expected result to account for transform + expected.Records[0].body = JSON.parse(expected.Records[0].body); + // Prepare const result = S3SqsEventNotificationSchema.parse(event); // Assess - expect(result).toStrictEqual(event); + expect(result).toStrictEqual(expected); }); it('throws if the S3 event notification SQS event is not valid', () => {