@@ -5,7 +5,7 @@ import type {
5
5
SQSRecord ,
6
6
} from 'aws-lambda' ;
7
7
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js' ;
8
- import { EventType } from './constants.js' ;
8
+ import { EventType , SchemaType } from './constants.js' ;
9
9
import { BatchProcessingError } from './errors.js' ;
10
10
import type {
11
11
BaseRecord ,
@@ -112,12 +112,11 @@ class BatchProcessor extends BasePartialBatchProcessor {
112
112
record : BaseRecord
113
113
) : Promise < SuccessResponse | FailureResponse > {
114
114
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 ) ;
121
120
const result = await this . handler ( data , this . options ?. context ) ;
122
121
123
122
return this . successHandler ( record , result ) ;
@@ -142,39 +141,49 @@ class BatchProcessor extends BasePartialBatchProcessor {
142
141
/**
143
142
* Parse the record according to the schema passed.
144
143
*
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
146
150
*
147
151
* @param record The record to be parsed
148
152
* @param eventType The type of event to process
149
153
* @param schema The StandardSchema to be used for parsing
150
154
*/
151
- public async parseRecord (
155
+ private async parseRecord (
152
156
record : EventSourceDataClassTypes ,
153
157
eventType : keyof typeof EventType ,
154
- schema ? : StandardSchemaV1
158
+ schema : StandardSchemaV1
155
159
) : 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 ) ;
174
177
}
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' ) ;
176
182
}
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' ) ;
178
187
}
179
188
}
180
189
0 commit comments