Skip to content

Feature request: add safeParse option #2204

Closed
@am29d

Description

@am29d

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

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilityparserThis item relates to the Parser Utility

Type

No type

Projects

Status

Shipped

Relationships

None yet

Development

No branches or pull requests

Issue actions