-
Notifications
You must be signed in to change notification settings - Fork 156
docs(parser): add docs for parser utility #1835
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 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
236fd8a
WIP: parser
am29d a676988
fix test imports
am29d 967e70c
remove unnecessary exports
am29d 2576273
add custom validation
am29d e97c729
remove unnecessary export
am29d 6ee1d43
add warning
am29d 81e7ad1
remove duplicate imports
am29d b74e20a
add types and error handlig
am29d 6f25caf
remove comment from annotations
am29d 3d8c9d0
minor changes
am29d 67cac21
revert merge changes
am29d 17076b2
merged package-lock
am29d 97a22a4
Merge branch 'feat/parser' into feat/parser-docs
am29d a4095f7
Update docs/utilities/parser.md
am29d cb36ca5
Update docs/utilities/parser.md
am29d 8bede3e
adjust imports to new implementation
am29d 0caf05a
add safeParse
am29d 55aeb06
fixed line highlight
am29d 664a12e
typo
am29d ef47459
revert index.md, add private scope to snippets packagef
am29d 12ca395
Update docs/utilities/parser.md
am29d 115a612
add parser to main, fixed zod install command
am29d ad16304
fix callout indent
am29d 40ac071
fix tooltip
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
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
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,35 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { z } from 'zod'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderSchema }) | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public async handler(event: Order, _context: Context): Promise<void> { | ||
// event is now typed as Order | ||
for (const item of event.items) { | ||
logger.info('Processing item', { item }); | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
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,36 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
am29d marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderSchema, envelope: EventBridgeEnvelope }) // (1)! | ||
public async handler(event: Order, _context: Context): Promise<void> { | ||
// event is now typed as Order | ||
for (const item of event.items) { | ||
logger.info('Processing item', item); // (2)! | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
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,37 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { parser } from '@aws-lambda-powertools/parser/middleware'; | ||
import { z } from 'zod'; | ||
import middy from '@middy/core'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
const lambdaHandler = async ( | ||
event: Order, | ||
_context: Context | ||
): Promise<void> => { | ||
for (const item of event.items) { | ||
// item is parsed as OrderItem | ||
logger.info('Processing item', { item }); | ||
} | ||
}; | ||
|
||
export const handler = middy(lambdaHandler).use( | ||
parser({ schema: orderSchema, envelope: EventBridgeEnvelope }) | ||
); |
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,21 @@ | ||
{ | ||
"version": "0", | ||
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718", | ||
"detail-type": "OrderPurchased", | ||
"source": "OrderService", | ||
"account": "111122223333", | ||
"time": "2020-10-22T18:43:48Z", | ||
"region": "us-west-1", | ||
"resources": ["some_additional"], | ||
"detail": { | ||
"id": 10876546789, | ||
"description": "My order", | ||
"items": [ | ||
{ | ||
"id": 1015938732, | ||
"quantity": 1, | ||
"description": "item xpto" | ||
} | ||
] | ||
} | ||
} |
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,40 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; | ||
import { parser } from '@aws-lambda-powertools/parser'; | ||
import { z } from 'zod'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
const orderEventSchema = EventBridgeSchema.extend({ | ||
detail: orderSchema, // (1)! | ||
}); | ||
|
||
type OrderEvent = z.infer<typeof orderEventSchema>; | ||
|
||
class Lambda implements LambdaInterface { | ||
@parser({ schema: orderEventSchema }) // (2)! | ||
public async handler(event: OrderEvent, _context: Context): Promise<void> { | ||
for (const item of event.detail.items) { | ||
// process OrderItem | ||
logger.info('Processing item', { item }); // (3)! | ||
} | ||
} | ||
} | ||
|
||
const myFunction = new Lambda(); | ||
export const handler = myFunction.handler.bind(myFunction); |
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,33 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
type Order = z.infer<typeof orderSchema>; | ||
|
||
export const handler = async ( | ||
event: EventBridgeEvent, | ||
_context: Context | ||
): Promise<void> => { | ||
const parsedEvent = EventBridgeSchema.parse(event); // (1)! | ||
logger.info('Parsed event', parsedEvent); | ||
|
||
const orders: Order = EventBridgeEnvelope.parse(event, orderSchema); // (2)! | ||
logger.info('Parsed orders', orders); | ||
}; |
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,35 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { z } from 'zod'; | ||
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes'; | ||
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas'; | ||
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
export const handler = async ( | ||
event: EventBridgeEvent, | ||
_context: Context | ||
): Promise<void> => { | ||
const parsedEvent = EventBridgeSchema.safeParse(event); // (1)! | ||
parsedEvent.success | ||
? logger.info('Event parsed successfully', parsedEvent.data) | ||
: logger.error('Event parsing failed', parsedEvent.error); | ||
const parsedEvenlope = EventBridgeEnvelope.safeParse(event, orderSchema); // (2)! | ||
parsedEvenlope.success | ||
? logger.info('Event envelope parsed successfully', parsedEvenlope.data) | ||
: logger.error('Event envelope parsing failed', parsedEvenlope.error); | ||
}; |
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,36 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import { parser } from '@aws-lambda-powertools/parser/middleware'; | ||
import { z } from 'zod'; | ||
import middy from '@middy/core'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const logger = new Logger(); | ||
|
||
const orderSchema = z.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array( | ||
z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}) | ||
), | ||
optionalField: z.string().optional(), | ||
}); | ||
|
||
type Order = z.infer<typeof orderSchema>; | ||
|
||
const lambdaHandler = async ( | ||
event: Order, | ||
_context: Context | ||
): Promise<void> => { | ||
for (const item of event.items) { | ||
// item is parsed as OrderItem | ||
logger.info('Processing item', { item }); | ||
} | ||
}; | ||
|
||
export const handler = middy(lambdaHandler).use( | ||
parser({ schema: orderSchema }) | ||
); |
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,21 @@ | ||
import { z } from 'zod'; | ||
|
||
const orderItemSchema = z.object({ | ||
id: z.number().positive(), | ||
quantity: z.number(), | ||
description: z.string(), | ||
}); | ||
|
||
export const orderSchema = z | ||
.object({ | ||
id: z.number().positive(), | ||
description: z.string(), | ||
items: z.array(orderItemSchema).refine((items) => items.length > 0, { | ||
message: 'Order must have at least one item', // (1)! | ||
}), | ||
optionalField: z.string().optional(), | ||
}) | ||
.refine((order) => order.id > 100 && order.items.length > 100, { | ||
message: | ||
'All orders with more than 100 items must have an id greater than 100', // (2)! | ||
}); |
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.