|
| 1 | +import logging |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from typing import Any, Callable, Dict |
| 4 | + |
| 5 | +from pydantic import BaseModel, ValidationError |
| 6 | + |
| 7 | +from aws_lambda_powertools.middleware_factory import lambda_handler_decorator |
| 8 | +from aws_lambda_powertools.validation.schemas import DynamoStreamSchema, EventBridgeSchema |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class Envelope(ABC): |
| 14 | + def _parse_user_dict_schema(self, user_event: Dict[str, Any], inbound_schema_model: BaseModel) -> Any: |
| 15 | + logger.debug("parsing user dictionary schema") |
| 16 | + try: |
| 17 | + return inbound_schema_model(**user_event) |
| 18 | + except (ValidationError, TypeError): |
| 19 | + logger.exception("Valdation exception while extracting user custom schema") |
| 20 | + raise |
| 21 | + |
| 22 | + def _parse_user_json_string_schema(self, user_event: str, inbound_schema_model: BaseModel) -> Any: |
| 23 | + logger.debug("parsing user dictionary schema") |
| 24 | + try: |
| 25 | + return inbound_schema_model.parse_raw(user_event) |
| 26 | + except (ValidationError, TypeError): |
| 27 | + logger.exception("Valdation exception while extracting user custom schema") |
| 28 | + raise |
| 29 | + |
| 30 | + @abstractmethod |
| 31 | + def parse(self, event: Dict[str, Any], inbound_schema_model: BaseModel) -> Any: |
| 32 | + return NotImplemented |
| 33 | + |
| 34 | + |
| 35 | +class UserEnvelope(Envelope): |
| 36 | + def parse(self, event: Dict[str, Any], inbound_schema_model: BaseModel) -> Any: |
| 37 | + try: |
| 38 | + return inbound_schema_model(**event) |
| 39 | + except (ValidationError, TypeError): |
| 40 | + logger.exception("Valdation exception received from input user custom envelope event") |
| 41 | + raise |
| 42 | + |
| 43 | + |
| 44 | +class EventBridgeEnvelope(Envelope): |
| 45 | + def parse(self, event: Dict[str, Any], inbound_schema_model: BaseModel) -> Any: |
| 46 | + try: |
| 47 | + parsed_envelope = EventBridgeSchema(**event) |
| 48 | + except (ValidationError, TypeError): |
| 49 | + logger.exception("Valdation exception received from input eventbridge event") |
| 50 | + raise |
| 51 | + return self._parse_user_dict_schema(parsed_envelope.detail, inbound_schema_model) |
| 52 | + |
| 53 | + |
| 54 | +class DynamoEnvelope(Envelope): |
| 55 | + def parse(self, event: Dict[str, Any], inbound_schema_model: BaseModel) -> Any: |
| 56 | + try: |
| 57 | + parsed_envelope = DynamoStreamSchema(**event) |
| 58 | + except (ValidationError, TypeError): |
| 59 | + logger.exception("Valdation exception received from input dynamodb stream event") |
| 60 | + raise |
| 61 | + output = [] |
| 62 | + for record in parsed_envelope.Records: |
| 63 | + parsed_new_image = ( |
| 64 | + {} |
| 65 | + if not record.dynamodb.NewImage |
| 66 | + else self._parse_user_dict_schema(record.dynamodb.NewImage, inbound_schema_model) |
| 67 | + ) # noqa: E501 |
| 68 | + parsed_old_image = ( |
| 69 | + {} |
| 70 | + if not record.dynamodb.OldImage |
| 71 | + else self._parse_user_dict_schema(record.dynamodb.OldImage, inbound_schema_model) |
| 72 | + ) # noqa: E501 |
| 73 | + output.append({"new": parsed_new_image, "old": parsed_old_image}) |
| 74 | + return output |
| 75 | + |
| 76 | + |
| 77 | +@lambda_handler_decorator |
| 78 | +def validator( |
| 79 | + handler: Callable[[Dict, Any], Any], |
| 80 | + event: Dict[str, Any], |
| 81 | + context: Dict[str, Any], |
| 82 | + inbound_schema_model: BaseModel, |
| 83 | + outbound_schema_model: BaseModel, |
| 84 | + envelope: Envelope, |
| 85 | +) -> Any: |
| 86 | + """Decorator to create validation for lambda handlers events - both inbound and outbound |
| 87 | +
|
| 88 | + As Lambda follows (event, context) signature we can remove some of the boilerplate |
| 89 | + and also capture any exception any Lambda function throws or its response as metadata |
| 90 | +
|
| 91 | + Example |
| 92 | + ------- |
| 93 | + **Lambda function using validation decorator** |
| 94 | +
|
| 95 | + @validator(inbound=inbound_schema_model, outbound=outbound_schema_model) |
| 96 | + def handler(parsed_event_model, context): |
| 97 | + ... |
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + todo add |
| 102 | +
|
| 103 | + Raises |
| 104 | + ------ |
| 105 | + err |
| 106 | + TypeError or pydantic.ValidationError or any exception raised by the lambda handler itself |
| 107 | + """ |
| 108 | + lambda_handler_name = handler.__name__ |
| 109 | + logger.debug("Validating inbound schema") |
| 110 | + parsed_event_model = envelope.parse(event, inbound_schema_model) |
| 111 | + try: |
| 112 | + logger.debug(f"Calling handler {lambda_handler_name}") |
| 113 | + response = handler({"orig": event, "custom": parsed_event_model}, context) |
| 114 | + logger.debug("Received lambda handler response successfully") |
| 115 | + logger.debug(response) |
| 116 | + except Exception: |
| 117 | + logger.exception(f"Exception received from {lambda_handler_name}") |
| 118 | + raise |
| 119 | + |
| 120 | + try: |
| 121 | + logger.debug("Validating outbound response schema") |
| 122 | + outbound_schema_model(**response) |
| 123 | + except (ValidationError, TypeError): |
| 124 | + logger.exception(f"Validation exception received from {lambda_handler_name} response event") |
| 125 | + raise |
| 126 | + return response |
0 commit comments