Skip to content

Commit b9297a3

Browse files
committed
Added condition to do the extended schema parsing only when a zod schema is used
1 parent a2053e7 commit b9297a3

File tree

3 files changed

+344
-293
lines changed

3 files changed

+344
-293
lines changed

packages/batch/src/BatchProcessor.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
SQSRecord,
66
} from 'aws-lambda';
77
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
8-
import { EventType } from './constants.js';
8+
import { EventType, SchemaType } from './constants.js';
99
import { BatchProcessingError } from './errors.js';
1010
import type {
1111
BaseRecord,
@@ -112,12 +112,11 @@ class BatchProcessor extends BasePartialBatchProcessor {
112112
record: BaseRecord
113113
): Promise<SuccessResponse | FailureResponse> {
114114
try {
115-
const parsedRecord = await this.parseRecord(
116-
record,
117-
this.eventType,
118-
this.schema
119-
);
120-
const data = this.toBatchType(parsedRecord, this.eventType);
115+
const recordToProcess =
116+
this.schema == null
117+
? record
118+
: await this.parseRecord(record, this.eventType, this.schema);
119+
const data = this.toBatchType(recordToProcess, this.eventType);
121120
const result = await this.handler(data, this.options?.context);
122121

123122
return this.successHandler(record, result);
@@ -142,39 +141,49 @@ class BatchProcessor extends BasePartialBatchProcessor {
142141
/**
143142
* Parse the record according to the schema passed.
144143
*
145-
* If the schema is not provided, it returns the record as is.
144+
* If the passed schema is already an extended schema,
145+
* it directly uses the schema to parse the record
146+
*
147+
* If the passed schema is an internal payload schema,
148+
* it checks whether it is a zod schema and
149+
* then extends the zod schema according to the passed event type for parsing
146150
*
147151
* @param record The record to be parsed
148152
* @param eventType The type of event to process
149153
* @param schema The StandardSchema to be used for parsing
150154
*/
151-
public async parseRecord(
155+
private async parseRecord(
152156
record: EventSourceDataClassTypes,
153157
eventType: keyof typeof EventType,
154-
schema?: StandardSchemaV1
158+
schema: StandardSchemaV1
155159
): Promise<SQSRecord | KinesisStreamRecord | DynamoDBRecord> {
156-
if (schema) {
157-
const { parse } = await import('@aws-lambda-powertools/parser');
158-
if (eventType === EventType.SQS) {
159-
try {
160-
return parse(record, undefined, schema) as SQSRecord;
161-
} catch (error) {
162-
const { JSONStringified } = await import(
163-
'@aws-lambda-powertools/parser/helpers'
164-
);
165-
const { SqsRecordSchema } = await import(
166-
'@aws-lambda-powertools/parser/schemas/sqs'
167-
);
168-
const extendedSchema = SqsRecordSchema.extend({
169-
// biome-ignore lint/suspicious/noExplicitAny: at least for now, we need to broaden the type because the JSONstringified helper method is not typed with StandardSchemaV1 but with ZodSchema
170-
body: JSONStringified(schema as any),
171-
});
172-
return parse(record, undefined, extendedSchema);
173-
}
160+
const { parse } = await import('@aws-lambda-powertools/parser');
161+
if (eventType === EventType.SQS) {
162+
const extendedSchemaParsing = parse(record, undefined, schema, true);
163+
if (extendedSchemaParsing.success)
164+
return extendedSchemaParsing.data as SQSRecord;
165+
if (schema['~standard'].vendor === SchemaType.Zod) {
166+
const { JSONStringified } = await import(
167+
'@aws-lambda-powertools/parser/helpers'
168+
);
169+
const { SqsRecordSchema } = await import(
170+
'@aws-lambda-powertools/parser/schemas/sqs'
171+
);
172+
const extendedSchema = SqsRecordSchema.extend({
173+
// biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema
174+
body: JSONStringified(schema as any),
175+
});
176+
return parse(record, undefined, extendedSchema);
174177
}
175-
throw new Error('Unsupported event type');
178+
console.warn(
179+
'The schema provided is not supported. Only Zod schemas are supported for extension.'
180+
);
181+
throw new Error('Unsupported schema type');
176182
}
177-
return record;
183+
console.warn(
184+
`The event type provided is not supported. Supported events: ${Object.values(EventType).join(',')}`
185+
);
186+
throw new Error('Unsupported event type');
178187
}
179188
}
180189

packages/batch/src/constants.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ const EventType = {
1717
DynamoDBStreams: 'DynamoDBStreams',
1818
} as const;
1919

20+
/**
21+
* Enum of supported schema types for the utility
22+
*/
23+
const SchemaType = {
24+
Zod: 'zod',
25+
} as const;
26+
2027
/**
2128
* Default response for the partial batch processor
2229
*/
@@ -35,4 +42,4 @@ const DATA_CLASS_MAPPING = {
3542
record as DynamoDBRecord,
3643
};
3744

38-
export { EventType, DEFAULT_RESPONSE, DATA_CLASS_MAPPING };
45+
export { EventType, SchemaType, DEFAULT_RESPONSE, DATA_CLASS_MAPPING };

0 commit comments

Comments
 (0)