Skip to content

Commit 0c44517

Browse files
authored
docs(parser): highlight JSONStringify helper (#3158)
1 parent 869b6fc commit 0c44517

10 files changed

+107
-13
lines changed

docs/utilities/parser.md

+23-7
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,15 @@ You can extend every built-in schema to include your own schema, and yet have al
106106
=== "Example payload"
107107

108108
```json
109-
--8<-- "examples/snippets/parser/examplePayload.json"
109+
--8<-- "examples/snippets/parser/samples/examplePayload.json"
110110
```
111111

112+
### JSON stringified payloads
113+
112114
If you want to extend a schema and transform a JSON stringified payload to an object, you can use helper function `JSONStringified`:
113115

114116
=== "AlbSchema with JSONStringified"
115-
```typescript hl_lines="12"
117+
```typescript hl_lines="11"
116118
--8<-- "examples/snippets/parser/extendAlbSchema.ts"
117119
```
118120

@@ -121,20 +123,34 @@ If you want to extend a schema and transform a JSON stringified payload to an ob
121123
=== "ALB example payload"
122124

123125
```json hl_lines="26"
124-
--8<-- "examples/snippets/parser/exampleAlbPayload.json"
126+
--8<-- "examples/snippets/parser/samples/exampleAlbPayload.json"
127+
```
128+
129+
=== "APIGatewayProxyEventV2Schema with JSONStringified"
130+
```typescript hl_lines="6"
131+
--8<--
132+
examples/snippets/parser/extendAPIGatewayProxyEventV2Schema.ts::4
133+
examples/snippets/parser/extendAPIGatewayProxyEventV2Schema.ts:6:
134+
--8<--
135+
```
136+
137+
1. This is compatible also with API Gateway REST API schemas
138+
139+
=== "API Gateway HTTP API example payload"
140+
141+
```json hl_lines="39"
142+
--8<-- "examples/snippets/parser/samples/exampleAPIGatewayProxyEventV2.json"
125143
```
126144

127145
=== "SQS Schema with JSONStringified"
128-
```typescript hl_lines="23-25 30 34"
146+
```typescript hl_lines="16"
129147
--8<-- "examples/snippets/parser/extendSqsSchema.ts"
130148
```
131149

132-
1. make sure to set your schema to the correct key in the JSON payload
133-
134150
=== "SQS example payload"
135151

136152
```json hl_lines="6 28"
137-
--8<-- "examples/snippets/parser/exampleSqsPayload.json"
153+
--8<-- "examples/snippets/parser/samples/exampleSqsPayload.json"
138154
```
139155

140156
## Envelopes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
2+
import { APIGatewayProxyEventV2Schema } from '@aws-lambda-powertools/parser/schemas/api-gatewayv2';
3+
import { z } from 'zod';
4+
5+
// biome-ignore format: we need the comment in the next line to stay there to annotate the code snippet in the docs
6+
const extendedSchema = APIGatewayProxyEventV2Schema.extend({ // (1)!
7+
body: JSONStringified(
8+
z.object({
9+
name: z.string(),
10+
age: z.number(),
11+
})
12+
),
13+
});
14+
type ExtendedAPIGatewayEvent = z.infer<typeof extendedSchema>;
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
2-
import { AlbSchema } from '@aws-lambda-powertools/parser/schemas';
2+
import { AlbSchema } from '@aws-lambda-powertools/parser/schemas/alb';
33
import { z } from 'zod';
44

55
const customSchema = z.object({
@@ -8,5 +8,7 @@ const customSchema = z.object({
88
});
99

1010
const extendedSchema = AlbSchema.extend({
11-
body: JSONStringified(customSchema), // (1)!
11+
body: JSONStringified(customSchema),
1212
});
13+
14+
type ExtendedAlbEvent = z.infer<typeof extendedSchema>;

examples/snippets/parser/extendSqsSchema.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
22
import {
33
SqsRecordSchema,
44
SqsSchema,
5-
} from '@aws-lambda-powertools/parser/schemas';
5+
} from '@aws-lambda-powertools/parser/schemas/sqs';
66
import { z } from 'zod';
77

88
const customSchema = z.object({
@@ -17,3 +17,5 @@ const extendedSchema = SqsSchema.extend({
1717
})
1818
),
1919
});
20+
21+
type ExtendedSqsEvent = z.infer<typeof extendedSchema>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "POST /lambda",
4+
"rawPath": "/lambda",
5+
"rawQueryString": "",
6+
"headers": {
7+
"accept": "*/*",
8+
"accept-encoding": "gzip, deflate",
9+
"authorization": "Bearer foo",
10+
"content-length": "0",
11+
"host": "lsw1ro4ipb.execute-api.eu-west-1.amazonaws.com",
12+
"user-agent": "HTTPie/3.2.2",
13+
"x-amzn-trace-id": "Root=1-66705bc7-2b4257df30cbee696ef2cf28",
14+
"x-forwarded-for": "15.248.3.126",
15+
"x-forwarded-port": "443",
16+
"x-forwarded-proto": "https"
17+
},
18+
"requestContext": {
19+
"accountId": "123456789012",
20+
"apiId": "lsw1ro4ipb",
21+
"authorizer": {
22+
"lambda": null
23+
},
24+
"domainName": "lsw1ro4ipb.execute-api.eu-west-1.amazonaws.com",
25+
"domainPrefix": "lsw1ro4ipb",
26+
"http": {
27+
"method": "POST",
28+
"path": "/lambda",
29+
"protocol": "HTTP/1.1",
30+
"sourceIp": "15.248.3.126",
31+
"userAgent": "HTTPie/3.2.2"
32+
},
33+
"requestId": "ZhNHJhhLjoEEPiw=",
34+
"routeKey": "POST /lambda",
35+
"stage": "$default",
36+
"time": "17/Jun/2024:15:52:39 +0000",
37+
"timeEpoch": 1718639559080
38+
},
39+
"body": "{\"name\":\"John\",\"age\":42}",
40+
"isBase64Encoded": false
41+
}

packages/parser/src/helpers.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import { type ZodTypeAny, z } from 'zod';
2+
/**
3+
* @typedef {import('../schemas/alb').AlbSchema} AlbSchema
4+
*/
25

36
/**
47
* A helper function to parse a JSON string and validate it against a schema.
5-
* Use it for built-in schemas like `AlbSchema`, `ApiGatewaySchema`, etc. to extend them with your customer schema.
8+
*
9+
* Use it for built-in schemas like `AlbSchema`, `ApiGatewaySchema`, etc. that have some fields that are JSON stringified
10+
* and extend them with your custom schema.
11+
*
12+
* For example, if you have an event with a JSON stringified body similar to the following:
13+
*
14+
* ```json
15+
* {
16+
* // ... other fields
17+
* "body": "{\"name\": \"John\", \"age\": 30}",
18+
* "isBase64Encoded": false,
19+
* }
20+
* ```
21+
*
22+
* You can extend any built-in schema with your custom schema using the `JSONStringified` helper function.
623
*
724
* @example
825
* ```typescript
926
* import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
10-
* import { AlbSchema } from '@aws-lambda-powertools/parser/schemas';
27+
* import { AlbSchema } from '@aws-lambda-powertools/parser/schemas/alb';
1128
* import { z } from 'zod';
1229
*
1330
* const customSchema = z.object({
@@ -19,6 +36,7 @@ import { type ZodTypeAny, z } from 'zod';
1936
* body: JSONStringified(customSchema),
2037
* });
2138
*
39+
* type ExtendedAlbEvent = z.infer<typeof extendedSchema>;
2240
* ```
2341
*
2442
* @param schema - The schema to validate the JSON string against

packages/parser/typedoc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"./src/middleware/parser.ts",
66
"./src/types/index.ts",
77
"./src/envelopes/index.ts",
8-
"./src/schemas/index.ts"
8+
"./src/schemas/index.ts",
9+
"./src/helpers.ts"
910
],
1011
"readme": "README.md"
1112
}

0 commit comments

Comments
 (0)