Description
openedon Dec 13, 2023
Summary
When using our built-in schema with zod, developers can infer the types with type SqsEvent = z.infer<typeof SqsSchema>
and use it in their Lambda handler. Another popular option is to use @types/aws-lambda
package wich has 3M downloads per week. We need to make sure that both types are compatible, so there is no confusion or unexpected behaviour.
Why is this needed?
If either @types/aws-lambda
or our schema is updated, we want to have a reliable test to verify that they are both compatible and if not to fix one them.
Which area does this relate to?
Parser
Solution
I have checked the first idea that worked:
import { z } from 'zod';
import { ALBEvent, APIGatewayProxyEvent } from 'aws-lambda';
import { AlbSchema } from '../../src/schemas/alb';
import { APIGatewayProxyEventSchema } from '../../src/schemas/apigw';
type AlbType = z.infer<typeof AlbSchema>;
type ApiGatewayType = z.infer<typeof APIGatewayProxyEventSchema>;
describe('Types compatibility', () => {
it('ALBEvent types are compatible', () => {
// These functions will fail to compile if A and B are not structurally identical
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const A = (value: AlbType): ALBEvent => value;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const B = (value: ALBEvent): AlbType => value;
});
it('ApiGatewayEvent types are compatible', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const A = (value: ApiGatewayType): APIGatewayProxyEvent => value;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const B = (value: APIGatewayProxyEvent): ApiGatewayType => value;
});
});
The compiler will fail if the structure does not match and will tell exactly which properties are not compatible.
Alternatively research more about https://github.com/fabien0102/ts-to-zod. If we can convert @types/aws-lambda
to schemas, we might directly compare the schemas to find the differences.
Acknowledgment
- This request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Assignees
Labels
Type
Projects
Status
Backlog