Skip to content

Commit

Permalink
fix(parser): parse sqs record body field as JSON and S3Schema in S3Sq…
Browse files Browse the repository at this point in the history
…sEventNoificationRecordSchema (#3529)

Co-authored-by: Andrea Amorosi <[email protected]>
  • Loading branch information
cyrildewit and dreamorosi authored Feb 1, 2025
1 parent 0e861e2 commit bcd4b9f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
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

0 comments on commit bcd4b9f

Please sign in to comment.