-
Notifications
You must be signed in to change notification settings - Fork 156
feat(parser): add helper functions to transform JSON stringified payloads #2901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
503f889
add helper functions for parser
am29d c5a4a05
fix test header
am29d aa81cac
add examples to the parser docs for JSONStringified
am29d c6d2e0c
Merge branch 'main' into parser/json-stringify-helper
am29d 9ee7981
add docstrings to helper functions
am29d f5825e0
formatting
am29d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"requestContext": { | ||
"elb": { | ||
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a" | ||
} | ||
}, | ||
"httpMethod": "GET", | ||
"path": "/lambda", | ||
"queryStringParameters": { | ||
"query": "1234ABCD" | ||
}, | ||
"headers": { | ||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", | ||
"accept-encoding": "gzip", | ||
"accept-language": "en-US,en;q=0.9", | ||
"connection": "keep-alive", | ||
"host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com", | ||
"upgrade-insecure-requests": "1", | ||
"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", | ||
"x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476", | ||
"x-forwarded-for": "72.12.164.125", | ||
"x-forwarded-port": "80", | ||
"x-forwarded-proto": "http", | ||
"x-imforwards": "20" | ||
}, | ||
"body": "{\"name\":\"Walter\", \"age\": 50}", | ||
"isBase64Encoded": false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", | ||
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...", | ||
"body": "{\"name\": \"John Doe\", \"age\": 30}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"SentTimestamp": "1545082649183", | ||
"SenderId": "AIDAIENQZJOLO23YVJ4VO", | ||
"ApproximateFirstReceiveTimestamp": "1545082649185" | ||
}, | ||
"messageAttributes": { | ||
"testAttr": { | ||
"stringValue": "100", | ||
"binaryValue": "base64Str", | ||
"dataType": "Number" | ||
} | ||
}, | ||
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", | ||
"awsRegion": "us-east-2" | ||
}, | ||
{ | ||
"messageId": "2e1424d4-f796-459a-8184-9c92662be6da", | ||
"receiptHandle": "AQEBzWwaftRI0KuVm4tP+/7q1rGgNqicHq...", | ||
"body": "{\"name\": \"foo\", \"age\": 10}", | ||
"attributes": { | ||
"ApproximateReceiveCount": "1", | ||
"SentTimestamp": "1545082650636", | ||
"SenderId": "AIDAIENQZJOLO23YVJ4VO", | ||
"ApproximateFirstReceiveTimestamp": "1545082650649", | ||
"DeadLetterQueueSourceArn": "arn:aws:sqs:us-east-2:123456789012:my-queue-dead" | ||
}, | ||
"messageAttributes": {}, | ||
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3", | ||
"eventSource": "aws:sqs", | ||
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue", | ||
"awsRegion": "us-east-2" | ||
} | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers'; | ||
import { AlbSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import { z } from 'zod'; | ||
|
||
const customSchema = z.object({ | ||
name: z.string(), | ||
age: z.number(), | ||
}); | ||
|
||
const extendedSchema = AlbSchema.extend({ | ||
body: JSONStringified(customSchema), // (1)! | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { JSONStringified } from '@aws-lambda-powertools/parser/helpers'; | ||
import { | ||
SqsRecordSchema, | ||
SqsSchema, | ||
} from '@aws-lambda-powertools/parser/schemas'; | ||
import { z } from 'zod'; | ||
|
||
const customSchema = z.object({ | ||
name: z.string(), | ||
age: z.number(), | ||
}); | ||
|
||
const extendedSchema = SqsSchema.extend({ | ||
Records: z.array( | ||
SqsRecordSchema.extend({ | ||
body: JSONStringified(customSchema), // (1)! | ||
}) | ||
), | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { type ZodSchema, z } from 'zod'; | ||
|
||
/** | ||
* A helper function to parse a JSON string and validate it against a schema. | ||
* Use it for built-in schemas like `AlbSchema`, `ApiGatewaySchema`, etc. to extend them with your customer schema. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { JSONStringified } from '@aws-lambda-powertools/parser/helpers'; | ||
* import { AlbSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
* import { z } from 'zod'; | ||
* | ||
* const customSchema = z.object({ | ||
* name: z.string(), | ||
* age: z.number(), | ||
* }); | ||
* | ||
* const extendedSchema = AlbSchema.extend({ | ||
* body: JSONStringified(customSchema), | ||
* }); | ||
* | ||
* ``` | ||
* | ||
* @param schema - The schema to validate the JSON string against | ||
*/ | ||
const JSONStringified = (schema: ZodSchema) => | ||
z | ||
.string() | ||
.transform((str, ctx) => { | ||
try { | ||
return JSON.parse(str); | ||
} catch (err) { | ||
ctx.addIssue({ | ||
code: 'custom', | ||
message: 'Invalid JSON', | ||
}); | ||
} | ||
}) | ||
.pipe(schema); | ||
|
||
export { JSONStringified }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.