-
Notifications
You must be signed in to change notification settings - Fork 406
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
feat: simple JSON Schema validator utility #153
feat: simple JSON Schema validator utility #153
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #153 +/- ##
===========================================
- Coverage 100.00% 99.79% -0.21%
===========================================
Files 33 39 +6
Lines 918 996 +78
Branches 77 83 +6
===========================================
+ Hits 918 994 +76
- Misses 0 1 +1
- Partials 0 1 +1
Continue to review full report at Codecov.
|
I like this a lot 🎊 |
Idea on how to handle API Gateway events and other events that might contain serialized data for the We could add a function to JMESPath named
Why prefix with powertools? The documentation for JMESPath states that "If you have a custom function that you’ve found useful, consider submitting it to jmespath.site and propose that it be added to the JMESPath language", therefore new functions could be added over time. If JMESPath adds a "json()" function and this function behaves in a slightly different way, there should be no conflict with the Powertools implementation. |
@nmoutschen @cakepietoast could you have a pass at the UX and help me answer the questions I put at the description, please? :) |
Overall the UX looks good. Quick questions:
|
Yes to the first question, and for API GW payload v1 and v2 kept the same
“body” key (luckily for us)
…On Wed, 2 Sep 2020 at 21:52, Nicolas Moutschen ***@***.***> wrote:
Overall the UX looks good. Quick questions:
- Will this also support outbound validation through the same
decorator?
- (Nitpick) Should the envelope for API_GATEWAY_REST should be v1?
Since HTTP API supports both v1 and v2.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#153 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBGM4G4G3YPVQ5IVAJLSD2PBJANCNFSM4QQ5T2KA>
.
|
Update based on discussions we had on Slack JMESPath as a project dependencyInitially started as an optional dependency, but we'll need a standard across Powertools as customers request an utility to extract as well as validate their events against JSONSchema. JMESPath is literally 24KB as it uses AST and a Lexer, so there is no impact on an additional dep, nor at runtime. It also provides the flexibility customers want to use their own JMESPath expressions to work with their events in the same way they're used with AWS CLI and other related projects -- Making it a standard. Custom function to deserialize base64 dataKinesis is a popular event sources and customers will not be able to validate JSON payloads without decoding it first - This problem also occurs when working with the event, where customers need to create a We'll create a custom JMESPath function named @nmoutschen @cakepietoast need your public sign-off to this for historical reasons :) |
If you meant |
You have it! |
This is shockingly similar to code we wrote for our APIs to handle schema validations. Question on use as a decorator: it looks like there's nothing to handle validator exceptions so a 4xx response could be returned instead of a 5xx. Is the intended use case here to use middleware to wrap the handler so those exceptions can be handled? |
@brysontyrrell This utility should support both sync and async workflows. The error handling would be different between the two. And for sync, it could be AppSync, ALB, API Gateway, etc. where the return values might vary. I think the use-case where someone would like to use the decorator over the handler is for cases where the result should be the Lambda execution failing. In async cases, this would trigger the OnFailure/DLQ/etc. flow. For API Gateway cases, I think this could be done like this: def handler(evt, ctx):
try:
validate(event=evt, schema=hello_schema, envelope="powertools_json(data).payload")
except ValidationException:
return {
"statusCode": 400,
"body": "Invalid payload"
} The ValidationException would come from validation errors, while internal errors would result in an invocation failure. |
@brysontyrrell exactly what Nicolas said - It's also a common problem so solutions aren't going to be that much different. I've just got JSON Schema validation against Kinesis Data Stream records seamlessly -- UX will look like this @validator(inbound_schema=hello_schema, envelope=envelopes.KINESIS_DATA_STREAM)
def handler_base64_event(evt, ctx):
... What's happening here:
From a JSON Schema perspective, it'll have to take into account an array of data, other than that it's pretty seamless IMHO. What do you think @jplock @nmoutschen @michaelbrewer @cakepietoast |
* develop: (57 commits) chore: bump version to 1.5.0 (#158) chore(batch): Housekeeping for recent changes (#157) docs: address readability feedbacks chore: add debug logging for sqs batch processing chore: remove middlewares module, moving decorator functionality to base and sqs docs: add detail to batch processing fix: throw exception by default if messages processing fails chore: add test for sqs_batch_processor interface fix: add sqs_batch_processor as its own method docs: simplify documentation more SQS specific focus Update for sqs_batch_processor interface chore: add sqs_batch_processor decorator to simplify interface feat(parameters): transform = "auto" (#133) chore: fix typos, docstrings and type hints (#154) chore: tiny changes for readability fix: ensure debug log event has latest ctx docs: rephrase the wording to make it more clear docs: refactor example; improve docs about creating your own processor refactor: remove references to BaseProcessor. Left BasePartialProcessor docs: add newly created Slack Channel fix: update image with correct sample ...
Co-authored-by: Tom McCarthy <[email protected]>
Issue #, if available: #95, and related to #147
Description of changes:
Checklist
validator
decorator for both inbound/outbound schemavalidate
standalone functionjmespath
an optional dependency (pip install aws-lambda-powertools[jmespath])Open questions
None
UX
Decorator
Deserializing JSON Strings before validation with powertools_json
We'll provide a function to deserialize JSON Strings that are commonly present in API Gateway events as well as any arbitrary payload. This JMESPath function will deserialize any value you pass as an argument.
Deserializing Base64 JSON Strings before validation with powertools_base64 and powertools_json
We'll provide a function to decode base64 data that can be used in tandem with our function to deserialize JSON Strings - This is typically the case for events coming from Kinesis Records, CloudWatch Logs, etc.
An example of a raw JMESPath expression to decode and deserialize JSON Strings sent to Kinesis before applying validation - The validation has to take into account an array of the data, in this case
Decompressing and deserializing GZIP Base64 JSON Strings before validation with powertools_base64_gzip
AWS services like CloudWatch Logs compress log events as ZIP archives and base64 its binary content - Similar to base64 and JSON deserialization above, we'll provide a
powertools_base64_gzip
function.An example of a CloudWatch Logs event already compressed to demonstrate this function:
Standalone function
Built-in envelopes
Breaking change checklist
RFC issue #:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.