You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/parser.md
+66-22Lines changed: 66 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1,53 +1,56 @@
1
1
---
2
2
title: Parser (Zod)
3
3
descrition: Utility
4
+
status: new
4
5
---
5
6
6
7
7
8
???+ warning
8
9
**This utility is currently released as beta developer preview** and is intended strictly for feedback and testing purposes **and not for production workloads**. The version and all future versions tagged with the `-beta` suffix should be treated as not stable. Up until before the [General Availability release](https://github.com/aws-powertools/powertools-lambda-typescript/milestone/16) we might introduce significant breaking changes and improvements in response to customers feedback.
9
10
10
-
This utility provides data validation and parsing using [zod](https://zod.dev){target="_blank"}.
11
+
This utility provides data validation and parsing using [Zod](https://zod.dev){target="_blank"}.
11
12
Zod is a TypeScript-first schema declaration and validation library.
12
13
13
14
## Key features
14
15
15
-
* Define data schema as zod schema, then parse, validate and extract only what you want
16
-
* Built-in envelopes to unwrap and validate popular event sources payloads
16
+
* Define data schema as Zod schema, then parse, validate and extract only what you want
17
+
* Built-in envelopes to unwrap and validate popular AWS event sources payloads
17
18
* Extend and customize envelopes to fit your needs
19
+
* Safe parsing option to avoid throwing errors and custom error handling
18
20
* Available for Middy.js middleware and TypeScript method decorators
19
21
20
22
## Getting started
21
23
22
24
### Install
23
25
24
26
```bash
25
-
npm install @aws-lambda-powertools/parser zod
27
+
npm install @aws-lambda-powertools/parser zod~3
26
28
```
27
29
28
-
This utility supports zod v3.0.0 and above.
30
+
This utility supports Zod v3.x and above.
29
31
30
32
## Define schema
31
33
32
-
You can define your schema using zod:
34
+
You can define your schema using Zod:
33
35
34
36
```typescript title="schema.ts"
35
37
--8<--"docs/snippets/parser/schema.ts"
36
38
```
37
39
38
-
This is a schema for order and order items using zod.
39
-
You can create more complex schemas using zod, such as nested objects, arrays, unions, etc. see [zod documentation](https://zod.dev) for more details.
40
+
This is a schema for `Order` object using Zod.
41
+
You can create complex schemas by using nested objects, arrays, unions, and other types, see [Zod documentation](https://zod.dev) for more details.
40
42
41
43
## Parse events
42
44
43
-
You can parse inbound events using `parser` decorator or middy middleware.
45
+
You can parse inbound events using `parser` decorator or middy middleware, or [manually](#manual-parsing) using built-in envelopes and schemas.
44
46
Both are also able to parse either an object or JSON string as an input.
45
47
46
-
???+ note
47
-
The decorator and middleware will replace the event object with the parsed schema if successful. This means you might be careful when nesting other decorators that expect event to have a specific structure.
48
+
???+ warning
49
+
The decorator and middleware will replace the event object with the parsed schema if successful.
50
+
Be careful when using multiple decorators that expect event to have a specific structure, the order of evaluation for decorators is from bottom to top.
48
51
49
52
=== "Middy middleware"
50
-
```typescript hl_lines="31"
53
+
```typescript hl_lines="32"
51
54
--8<-- "docs/snippets/parser/middy.ts"
52
55
```
53
56
@@ -120,6 +123,11 @@ This can become difficult quite quickly. Parser simplifies the development throu
120
123
Envelopes can be used via envelope parameter available in middy and decorator.
121
124
Here's an example of parsing a custom schema in an event coming from EventBridge, where all you want is what's inside the detail key.
@@ -128,10 +136,7 @@ Here's an example of parsing a custom schema in an event coming from EventBridge
128
136
1. Pass `eventBridgeEnvelope` to `parser` decorator
129
137
2. `event` is parsed and replaced as `Order` object
130
138
131
-
=== "Middy middleware"
132
-
```typescript hl_lines="5 32"
133
-
--8<-- "docs/snippets/parser/envelopeMiddy.ts"
134
-
```
139
+
135
140
136
141
The envelopes are functions that take an event and the schema to parse, and return the result of the inner schema.
137
142
Depending on the envelope it can be something simple like extracting a key.
@@ -163,30 +168,69 @@ Parser comes with the following built-in envelopes:
163
168
|**vpcLatticeV2Envelope**| 1. Parses data using `VpcLatticeSchema`. <br/> 2. Parses `value` key using your schema and returns it. |
164
169
165
170
171
+
## Safe parsing
172
+
173
+
If you want to parse the event without throwing an error, use the `safeParse` option.
174
+
The handler `event` object will be replaced with `ParsedResult<Input?, Oputput?>`, for example `ParsedResult<SqsEvent, Order>`, where `SqsEvent` is the original event and `Order` is the parsed schema.
175
+
176
+
The `ParsedResult` object will have `success`, `data`, or `error` and `originalEvent` fields, depending on the outcome.
177
+
If the parsing is successful, the `data` field will contain the parsed event, otherwise you can access the `error` field and the `originalEvent` to handle the error and recover the original event.
178
+
179
+
=== "Middy middleware"
180
+
```typescript hl_lines="29 32 35 36 41"
181
+
--8<-- "docs/snippets/parser/safeParseMiddy.ts"
182
+
```
183
+
184
+
1. Use `safeParse` option to parse the event without throwing an error
185
+
2. Check if the result is successful or not and handle the error accordingly
186
+
3. Use `data` to access the parsed event
187
+
4. Use `error` to handle the error message
188
+
5. Use `originalEvent` to get the original event and recover
0 commit comments