No project description provided
Project description
powertools-oas-validator
Introduction
Powertools for AWS Lambda (Python) is an awesome set of tools for supercharging your lambdas. Powertools supports validating incoming requests (or event in PT lingo) against JSONSchema which is not ideal if you are using OpenAPI schemas to define your API contracts.
The Powertools OAS Validator adds a decorator that you can use with your lambda handlers and have the events validated against an OpenAPI schema instead.
Usage
Decorate your functions with @validate_request(oas_path="openapi.yaml")
and your request (and schema) will be validated on a request.
Schema validation is enabled per default but can be disabled if you already validate your schema (perhaps as part of a pipeline)
Minimal Example
from typing import Dict
from aws_lambda_powertools.event_handler import APIGatewayRestResolve, Rresponse
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.middleware import validate_request
app = APIGatewayRestResolver()
@app.post("/example")
def example() -> Response:
...
@validate_request(oas_path="openapi.yaml")
def lambda_handler(event: Dict, context: LambdaContext) -> Dict:
response = app.resolve(event, context)
return response
Extended Example
from typing import Dict
from aws_lambda_powertools.event_handler import APIGatewayRestResolve, Rresponse
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.middleware import validate_request
app = APIGatewayRestResolver()
@app.post("/example")
def example() -> Response:
...
@validate_request(
oas_path="openapi.yaml",
validate_spec=True, # default True, disable to not validate OpenAPI Schema
validate_body=True, # default True, disable to not validate event["body"]
validate_headers=True, # default True, disable to not validate event["headers"]
validate_query=True, # default True, disable to not validate event["rawQueryString"]
validate_path=True, # default True, disable to not validate path parameters
validate_cookies=True, # default True, disable to not validate cookies
)
def lambda_handler(event: Dict, context: LambdaContext) -> Dict:
response = app.resolve(event, context)
return response
Contributions
Please make a pull request and I will review it ASAP.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for powertools_oas_validator-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae2ac7a74548883caa5681356c5e1151a5a5df33ce20ff1d5bbcdf4bf68c9316 |
|
MD5 | 7773d0227e768955d82f7533fdb7d01a |
|
BLAKE2b-256 | aa9553b88f39827ee03782a7afe6ab56f2aa2a898fe615a6d4a1e6845076a6c5 |