Skip to content

Commit aa81cac

Browse files
committed
add examples to the parser docs for JSONStringified
1 parent c5a4a05 commit aa81cac

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

docs/core/event-handler/api-gateway.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ This is the sample infrastructure for API Gateway and Lambda Function URLs we ar
4040
=== "API Gateway SAM Template"
4141

4242
```yaml title="AWS Serverless Application Model (SAM) example"
43-
--8<-- "examples/snippets/event-handler/rest/templates/template.yaml"
43+
[//]: # ( --8<-- "examples/snippets/event-handler/rest/templates/template.yaml")
4444
```
4545

4646
=== "Lambda Function URL SAM Template"
4747

4848
```yaml title="AWS Serverless Application Model (SAM) example"
49-
--8<-- "examples/event_handler_lambda_function_url/sam/template.yaml"
49+
[//]: # ( --8<-- "examples/event_handler_lambda_function_url/sam/template.yaml")
5050
```
5151

5252
<!-- remove line below while editing this doc & put it back until the doc has reached its first draft -->

docs/utilities/parser.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ You can extend every built-in schema to include your own schema, and yet have al
109109
--8<-- "examples/snippets/parser/examplePayload.json"
110110
```
111111

112+
If you want to extend a schema and transform a JSON stringified payload to an object, you can use helper function `JSONStringified`:
113+
114+
=== "AlbSchema with JSONStringified"
115+
```typescript hl_lines="12"
116+
--8<-- "examples/snippets/parser/extendAlbSchema.ts"
117+
```
118+
119+
1. Extend built-in `AlbSchema` using JSONStringified function to transform your payload
120+
121+
=== "Alb exmaple payload"
122+
123+
```json hl_lines="26"
124+
--8<-- "examples/snippets/parser/exampleAlbPayload.json"
125+
```
126+
127+
=== "SQS Schema with JSONStringified"
128+
```typescript hl_lines="23-25 30 34"
129+
--8<-- "examples/snippets/parser/extendSqsSchema.ts"
130+
```
131+
132+
1. make sure to set your schema to the correct key in the JSON payload
133+
134+
=== "SQS exmaple payload"
135+
136+
```json hl_lines="6 28"
137+
--8<-- "examples/snippets/parser/exampleSqsPayload.json"
138+
```
139+
112140
## Envelopes
113141

114142
When trying to parse your payload you might encounter the following situations:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"requestContext": {
3+
"elb": {
4+
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a"
5+
}
6+
},
7+
"httpMethod": "GET",
8+
"path": "/lambda",
9+
"queryStringParameters": {
10+
"query": "1234ABCD"
11+
},
12+
"headers": {
13+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
14+
"accept-encoding": "gzip",
15+
"accept-language": "en-US,en;q=0.9",
16+
"connection": "keep-alive",
17+
"host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com",
18+
"upgrade-insecure-requests": "1",
19+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
20+
"x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476",
21+
"x-forwarded-for": "72.12.164.125",
22+
"x-forwarded-port": "80",
23+
"x-forwarded-proto": "http",
24+
"x-imforwards": "20"
25+
},
26+
"body": "{\"name\":\"Walter\", \"age\": 50}",
27+
"isBase64Encoded": false
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
5+
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
6+
"body": "{\"name\": \"John Doe\", \"age\": 30}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1545082649183",
10+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
11+
"ApproximateFirstReceiveTimestamp": "1545082649185"
12+
},
13+
"messageAttributes": {
14+
"testAttr": {
15+
"stringValue": "100",
16+
"binaryValue": "base64Str",
17+
"dataType": "Number"
18+
}
19+
},
20+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
21+
"eventSource": "aws:sqs",
22+
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
23+
"awsRegion": "us-east-2"
24+
},
25+
{
26+
"messageId": "2e1424d4-f796-459a-8184-9c92662be6da",
27+
"receiptHandle": "AQEBzWwaftRI0KuVm4tP+/7q1rGgNqicHq...",
28+
"body": "{\"name\": \"foo\", \"age\": 10}",
29+
"attributes": {
30+
"ApproximateReceiveCount": "1",
31+
"SentTimestamp": "1545082650636",
32+
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
33+
"ApproximateFirstReceiveTimestamp": "1545082650649",
34+
"DeadLetterQueueSourceArn": "arn:aws:sqs:us-east-2:123456789012:my-queue-dead"
35+
},
36+
"messageAttributes": {},
37+
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
38+
"eventSource": "aws:sqs",
39+
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
40+
"awsRegion": "us-east-2"
41+
}
42+
]
43+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
2+
import { AlbSchema } from '@aws-lambda-powertools/parser/schemas';
3+
import { z } from 'zod';
4+
5+
const customSchema = z.object({
6+
name: z.string(),
7+
age: z.number(),
8+
});
9+
10+
const extendedSchema = AlbSchema.extend({
11+
body: JSONStringified(customSchema), // (1)!
12+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
2+
import {
3+
SqsRecordSchema,
4+
SqsSchema,
5+
} from '@aws-lambda-powertools/parser/schemas';
6+
import { z } from 'zod';
7+
8+
const customSchema = z.object({
9+
name: z.string(),
10+
age: z.number(),
11+
});
12+
13+
const extendedSchema = SqsSchema.extend({
14+
Records: z.array(
15+
SqsRecordSchema.extend({
16+
body: JSONStringified(customSchema), // (1)!
17+
})
18+
),
19+
});

0 commit comments

Comments
 (0)