Skip to content

Commit fad8713

Browse files
committed
feat(batch): make parser dependencies optional with Standard Schema support
1 parent 74eab74 commit fad8713

File tree

4 files changed

+80
-44
lines changed

4 files changed

+80
-44
lines changed

package-lock.json

Lines changed: 43 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/batch/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@
8181
"nodejs"
8282
],
8383
"dependencies": {
84-
"@aws-lambda-powertools/commons": "2.26.1"
84+
"@aws-lambda-powertools/commons": "2.26.1",
85+
"@standard-schema/spec": "^1.0.0"
8586
},
8687
"devDependencies": {
88+
"@aws-lambda-powertools/parser": "2.26.1",
8789
"@aws-lambda-powertools/testing-utils": "file:../testing",
88-
"@aws-lambda-powertools/parser": "2.26.1"
90+
"zod": "^4.1.8"
8991
}
9092
}

packages/batch/src/parser.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
22
import type { StandardSchemaV1 } from '@standard-schema/spec';
3-
import type { ZodType } from 'zod';
43
import { EventType, SchemaVendor } from './constants.js';
54
import { ParsingError } from './errors.js';
65
import type {
76
BasePartialBatchProcessorParserConfig,
87
EventSourceDataClassTypes,
8+
RuntimeZodType,
99
} from './types.js';
1010

11+
/**
12+
* Type guard to check if the schema is a Zod schema
13+
*
14+
* @param schema - The schema to check
15+
*/
16+
const isZodSchema = (schema: StandardSchemaV1): schema is RuntimeZodType =>
17+
schema['~standard'].vendor === SchemaVendor.Zod;
18+
1119
/**
1220
* Extend the schema according to the event type passed.
1321
*
@@ -22,7 +30,7 @@ import type {
2230
*/
2331
const createExtendedSchema = async (options: {
2432
eventType: keyof typeof EventType;
25-
innerSchema: ZodType;
33+
innerSchema: RuntimeZodType;
2634
transformer?: BasePartialBatchProcessorParserConfig['transformer'];
2735
}) => {
2836
const { eventType, innerSchema, transformer } = options;
@@ -73,8 +81,8 @@ const createExtendedSchema = async (options: {
7381
);
7482
return DynamoDBStreamRecord.extend({
7583
dynamodb: DynamoDBStreamChangeRecordBase.extend({
76-
OldImage: schema.optional(),
77-
NewImage: schema.optional(),
84+
OldImage: schema,
85+
NewImage: schema,
7886
}),
7987
});
8088
};
@@ -131,7 +139,7 @@ const parser = async (
131139
}
132140
if (innerSchema) {
133141
// Only proceed with schema extension if it's a Zod schema
134-
if (innerSchema['~standard'].vendor !== SchemaVendor.Zod) {
142+
if (!isZodSchema(innerSchema)) {
135143
logger.error(
136144
'The schema provided is not supported. Only Zod schemas are supported for extension.'
137145
);

packages/batch/src/types.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
SQSRecord,
88
StreamRecord,
99
} from 'aws-lambda';
10-
import type { ZodType } from 'zod';
1110
import type { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
1211
import type { BatchProcessor } from './BatchProcessor.js';
1312
import type { parser } from './parser.js';
@@ -144,8 +143,12 @@ type BasePartialBatchProcessorParserConfig =
144143
schema?: never;
145144
/**
146145
* Payload-only Zod schema, mutually exclusive with `schema`
146+
*
147+
* @remarks
148+
* Only Zod schemas are supported for `innerSchema` as we rely on Zod's schema extension capabilities.
149+
* If you need to use a different Standard Schema-compatible library, use `schema` instead.
147150
*/
148-
innerSchema: ZodType;
151+
innerSchema: StandardSchemaV1;
149152
/**
150153
* Payload transformer, only available with `innerSchema`
151154
*/
@@ -239,6 +242,20 @@ type ParsedRecord<TRecord, TPayload, TOldPayload = TPayload> = TRecord extends {
239242
}
240243
: TRecord;
241244

245+
/**
246+
* Type representing a Zod schema at runtime.
247+
*
248+
* Parts of the parser integration within BatchProcessor rely on Zod for schema transformations,
249+
* however some other parts also support other Standard Schema-compatible libraries.
250+
*
251+
* To avoid forcing a direct dependency on Zod, we use `any` here, which is not ideal but necessary.
252+
*
253+
* The vendor is checked at runtime to ensure Zod is being used when required using `StandardSchemaV1['~standard'].vendor`.
254+
*
255+
* biome-ignore lint/suspicious/noExplicitAny: using `any` to avoid direct dependency on 'zod'
256+
*/
257+
type RuntimeZodType = any;
258+
242259
export type {
243260
BatchProcessingOptions,
244261
BaseRecord,
@@ -249,4 +266,5 @@ export type {
249266
PartialItemFailureResponse,
250267
BasePartialBatchProcessorParserConfig,
251268
ParsedRecord,
269+
RuntimeZodType,
252270
};

0 commit comments

Comments
 (0)