Skip to content

fix(parser): parse sqs record body field as JSON and S3Schema in S3SqsEventNoificationRecordSchema #3529

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/parser/src/schemas/s3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { JSONStringified } from '../helpers.js';
import { EventBridgeSchema } from './eventbridge.js';
import { SqsRecordSchema } from './sqs.js';

Expand Down Expand Up @@ -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
* {
Expand Down
6 changes: 5 additions & 1 deletion packages/parser/tests/unit/schema/s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading