Skip to content

Commit 010d06b

Browse files
committed
Fixed the tests for DynamoDB and added a note for using innerSchema with DynamoDB
1 parent fad8713 commit 010d06b

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

docs/features/batch.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ Available transformers by event type:
358358
--8<-- "examples/snippets/batch/samples/parser_SQS.json"
359359
```
360360

361+
!!! note
362+
If `innerSchema` is used with DynamoDB streams, the schema will be applied to both the `NewImage` and the `OldImage` by default. If you want to have separate schema for both, you will need to extend the schema and use the full schema for parsing.
363+
361364
#### Using full event schema
362365

363366
For complete control over validation, extend the built-in schemas with your custom payload schema. This approach gives you full control over the entire event structure.

packages/batch/src/BatchProcessor.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';
3333
* });
3434
* ```
3535
*
36-
* **Process batch triggered by Kinesis Data Streams*
36+
* **Process batch triggered by Kinesis Data Streams**
3737
*
3838
* @example
3939
* ```typescript
@@ -113,6 +113,39 @@ import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';
113113
* });
114114
* ```
115115
*
116+
* **Process batch with inner schema validation**
117+
*
118+
* @example
119+
* ```typescript
120+
* import {
121+
* BatchProcessor,
122+
* EventType,
123+
* processPartialResponse,
124+
* } from '@aws-lambda-powertools/batch';
125+
* import { parser } from '@aws-lambda-powertools/batch/parser';
126+
* import type { SQSHandler } from 'aws-lambda';
127+
* import { z } from 'zod';
128+
*
129+
* const myItemSchema = z.object({ name: z.string(), age: z.number() });
130+
*
131+
* const processor = new BatchProcessor(EventType.SQS, {
132+
* parser,
133+
* innerSchema: myItemSchema,
134+
* });
135+
*
136+
* const recordHandler = async (record) => {
137+
* // record is now fully typed and validated
138+
* console.log(record.body.name, record.body.age);
139+
* };
140+
*
141+
* export const handler: SQSHandler = async (event, context) =>
142+
* processPartialResponse(event, recordHandler, processor, {
143+
* context,
144+
* });
145+
* ```
146+
*
147+
* Note: If `innerSchema` is used with DynamoDB streams, the schema will be applied to both the NewImage and the OldImage by default. If you want to have separate schema for both, you will need to extend the schema and use the full schema for parsing.
148+
*
116149
* @param eventType - The type of event to process (SQS, Kinesis, DynamoDB)
117150
*/
118151
class BatchProcessor extends BasePartialBatchProcessor {

packages/batch/tests/helpers/factories.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const dynamodbRecordFactory = (body: string): DynamoDBRecord => {
6161
eventVersion: '1.0',
6262
dynamodb: {
6363
Keys: { Id: { N: '101' } },
64+
OldImage: { Message: { S: body } },
6465
NewImage: { Message: { S: body } },
6566
StreamViewType: 'NEW_AND_OLD_IMAGES',
6667
SequenceNumber: seq,

packages/batch/tests/unit/parsing.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ describe('Batch processing with Parser Integration', () => {
7878
parser,
7979
schema: DynamoDBStreamRecord.extend({
8080
dynamodb: DynamoDBStreamChangeRecordBase.extend({
81-
NewImage: DynamoDBMarshalled(customSchema).optional(),
81+
NewImage: DynamoDBMarshalled(customSchema),
82+
OldImage: DynamoDBMarshalled(customSchema),
8283
}),
8384
}),
8485
},
@@ -278,7 +279,7 @@ describe('Batch processing with Parser Integration', () => {
278279
],
279280
});
280281
expect(console.debug).toHaveBeenCalledWith(
281-
'dynamodb.NewImage.Message: Invalid input: expected string, received number'
282+
'dynamodb.NewImage.Message: Invalid input: expected string, received number; dynamodb.OldImage.Message: Invalid input: expected string, received number'
282283
);
283284
});
284285
}
@@ -314,7 +315,6 @@ describe('Batch processing with Parser Integration', () => {
314315
const records = [firstRecord, secondRecord];
315316
const processor = new BatchProcessor(EventType.SQS, {
316317
parser,
317-
// @ts-expect-error - we are explicitly testing a wrong schema vendor
318318
innerSchema: unsupportedSchema,
319319
transformer: 'json',
320320
});
@@ -334,6 +334,7 @@ describe('Batch processing with Parser Integration', () => {
334334
const secondRecord = sqsRecordFactory(JSON.stringify(successPayload2));
335335
const records = [firstRecord, secondRecord];
336336

337+
// @ts-expect-error - we are explicitly testing without a schema
337338
const processor = new BatchProcessor(EventType.SQS, {
338339
parser,
339340
transformer: 'json',
@@ -363,7 +364,6 @@ describe('Batch processing with Parser Integration', () => {
363364
const records = [firstRecord, secondRecord];
364365
const processor = new BatchProcessor(EventType.SQS, {
365366
parser,
366-
// @ts-expect-error - we are explicitly testing a wrong schema vendor
367367
innerSchema: unsupportedSchema,
368368
transformer: 'json',
369369
logger,

0 commit comments

Comments
 (0)