Description
Use case
If you don't want Zod to throw errors when validation fails, use .safeParse
. This method returns an object containing either the successfully parsed data or a ZodError instance containing detailed information about the validation problems.
There are cases when customers used safeParse
in their previous implementation and expect to have this option in the powertools parser utility. Thus, we should add an option to that will determine wether we call parse
or safeParse
internally in built-in schemas and envelopes
Solution/User Experience
We can expand the ParserOptions
by adding safeParse
argument, and call it directly on decorator
import { SqsSchema } from '@aws-lambda-powertools/parser/schemas';
import { SqsEvent } from '@aws-lambda-powertools/parser/types';
export type ParserOptions<S extends ZodSchema> = {
schema: S;
envelope?: Envelope;
safeParse?: boolean;
};
class Lambda implements LambdaInterface {
@parser({ schema: SqsSchema, safeParse: true })
public async handler(
event: SqsEvent,
_context: Context
): Promise<unknown> {
return event;
}
This would also require an expansion of the envelopes to support safeParse
option.
Alternative solutions
No response
Acknowledgment
- This feature 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
Metadata
Assignees
Labels
Type
Projects
Status