|
| 1 | +import logging |
| 2 | +from typing import Any, Callable, Dict, Union |
| 3 | + |
| 4 | +from ...middleware_factory import lambda_handler_decorator |
| 5 | +from .base import unwrap_event_from_envelope, validate_data_against_schema |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +@lambda_handler_decorator |
| 11 | +def validator( |
| 12 | + handler: Callable, |
| 13 | + event: Union[Dict, str], |
| 14 | + context: Any, |
| 15 | + inbound_schema: Dict = None, |
| 16 | + outbound_schema: Dict = None, |
| 17 | + envelope: str = None, |
| 18 | + jmespath_options: Dict = None, |
| 19 | +) -> Any: |
| 20 | + """Lambda handler decorator to validate incoming/outbound data using a JSON Schema |
| 21 | +
|
| 22 | + Example |
| 23 | + ------- |
| 24 | +
|
| 25 | + **Validate incoming event** |
| 26 | +
|
| 27 | + from aws_lambda_powertools.utilities.validation import validator |
| 28 | +
|
| 29 | + @validator(inbound_schema=json_schema_dict) |
| 30 | + def handler(event, context): |
| 31 | + return event |
| 32 | +
|
| 33 | + **Validate incoming and outgoing event** |
| 34 | +
|
| 35 | + from aws_lambda_powertools.utilities.validation import validator |
| 36 | +
|
| 37 | + @validator(inbound_schema=json_schema_dict, outbound_schema=response_json_schema_dict) |
| 38 | + def handler(event, context): |
| 39 | + return event |
| 40 | +
|
| 41 | + **Unwrap event before validating against actual payload - using built-in envelopes** |
| 42 | +
|
| 43 | + from aws_lambda_powertools.utilities.validation import validator, envelopes |
| 44 | +
|
| 45 | + @validator(inbound_schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST) |
| 46 | + def handler(event, context): |
| 47 | + return event |
| 48 | +
|
| 49 | + **Unwrap event before validating against actual payload - using custom JMESPath expression** |
| 50 | +
|
| 51 | + from aws_lambda_powertools.utilities.validation import validator |
| 52 | +
|
| 53 | + @validator(inbound_schema=json_schema_dict, envelope="payload[*].my_data") |
| 54 | + def handler(event, context): |
| 55 | + return event |
| 56 | +
|
| 57 | + **Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions** |
| 58 | +
|
| 59 | + from aws_lambda_powertools.utilities.validation import validator |
| 60 | +
|
| 61 | + @validator(inbound_schema=json_schema_dict, envelope="Records[*].powertools_json(body)") |
| 62 | + def handler(event, context): |
| 63 | + return event |
| 64 | +
|
| 65 | + **Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501 |
| 66 | +
|
| 67 | + from aws_lambda_powertools.utilities.validation import validator |
| 68 | +
|
| 69 | + @validator(inbound_schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))") |
| 70 | + def handler(event, context): |
| 71 | + return event |
| 72 | +
|
| 73 | + **Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501 |
| 74 | +
|
| 75 | + from aws_lambda_powertools.utilities.validation import validator |
| 76 | +
|
| 77 | + @validator(inbound_schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]") |
| 78 | + def handler(event, context): |
| 79 | + return event |
| 80 | +
|
| 81 | + Parameters |
| 82 | + ---------- |
| 83 | + handler : Callable |
| 84 | + Method to annotate on |
| 85 | + event : Dict |
| 86 | + Lambda event to be validated |
| 87 | + context : Any |
| 88 | + Lambda context object |
| 89 | + inbound_schema : Dict |
| 90 | + JSON Schema to validate incoming event |
| 91 | + outbound_schema : Dict |
| 92 | + JSON Schema to validate outbound event |
| 93 | + envelope : Dict |
| 94 | + JMESPath expression to filter data against |
| 95 | + jmespath_options : Dict |
| 96 | + Alternative JMESPath options to be included when filtering expr |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + Any |
| 101 | + Lambda handler response |
| 102 | +
|
| 103 | + Raises |
| 104 | + ------ |
| 105 | + SchemaValidationError |
| 106 | + When schema validation fails against data set |
| 107 | + InvalidSchemaFormatError |
| 108 | + When JSON schema provided is invalid |
| 109 | + InvalidEnvelopeExpressionError |
| 110 | + When JMESPath expression to unwrap event is invalid |
| 111 | + """ |
| 112 | + if envelope: |
| 113 | + event = unwrap_event_from_envelope(data=event, envelope=envelope, jmespath_options=jmespath_options) |
| 114 | + |
| 115 | + if inbound_schema: |
| 116 | + logger.debug("Validating inbound event") |
| 117 | + validate_data_against_schema(data=event, schema=inbound_schema) |
| 118 | + |
| 119 | + response = handler(event, context) |
| 120 | + |
| 121 | + if outbound_schema: |
| 122 | + logger.debug("Validating outbound event") |
| 123 | + validate_data_against_schema(data=response, schema=outbound_schema) |
| 124 | + |
| 125 | + return response |
| 126 | + |
| 127 | + |
| 128 | +def validate(event: Dict, schema: Dict = None, envelope: str = None, jmespath_options: Dict = None): |
| 129 | + """Standalone function to validate event data using a JSON Schema |
| 130 | +
|
| 131 | + Typically used when you need more control over the validation process. |
| 132 | +
|
| 133 | + **Validate event** |
| 134 | +
|
| 135 | + from aws_lambda_powertools.utilities.validation import validate |
| 136 | +
|
| 137 | + def handler(event, context): |
| 138 | + validate(event=event, schema=json_schema_dict) |
| 139 | + return event |
| 140 | +
|
| 141 | + **Unwrap event before validating against actual payload - using built-in envelopes** |
| 142 | +
|
| 143 | + from aws_lambda_powertools.utilities.validation import validate, envelopes |
| 144 | +
|
| 145 | + def handler(event, context): |
| 146 | + validate(event=event, schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST) |
| 147 | + return event |
| 148 | +
|
| 149 | + **Unwrap event before validating against actual payload - using custom JMESPath expression** |
| 150 | +
|
| 151 | + from aws_lambda_powertools.utilities.validation import validate |
| 152 | +
|
| 153 | + def handler(event, context): |
| 154 | + validate(event=event, schema=json_schema_dict, envelope="payload[*].my_data") |
| 155 | + return event |
| 156 | +
|
| 157 | + **Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions** |
| 158 | +
|
| 159 | + from aws_lambda_powertools.utilities.validation import validate |
| 160 | +
|
| 161 | + def handler(event, context): |
| 162 | + validate(event=event, schema=json_schema_dict, envelope="Records[*].powertools_json(body)") |
| 163 | + return event |
| 164 | +
|
| 165 | + **Unwrap, decode base64 and deserialize JSON string event before validating against actual payload - using built-in functions** |
| 166 | +
|
| 167 | + from aws_lambda_powertools.utilities.validation import validate |
| 168 | +
|
| 169 | + def handler(event, context): |
| 170 | + validate(event=event, schema=json_schema_dict, envelope="Records[*].kinesis.powertools_json(powertools_base64(data))") |
| 171 | + return event |
| 172 | +
|
| 173 | + **Unwrap, decompress ZIP archive and deserialize JSON string event before validating against actual payload - using built-in functions** # noqa: E501 |
| 174 | +
|
| 175 | + from aws_lambda_powertools.utilities.validation import validate |
| 176 | +
|
| 177 | + def handler(event, context): |
| 178 | + validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]") |
| 179 | + return event |
| 180 | +
|
| 181 | + Parameters |
| 182 | + ---------- |
| 183 | + event : Dict |
| 184 | + Lambda event to be validated |
| 185 | + schema : Dict |
| 186 | + JSON Schema to validate incoming event |
| 187 | + envelope : Dict |
| 188 | + JMESPath expression to filter data against |
| 189 | + jmespath_options : Dict |
| 190 | + Alternative JMESPath options to be included when filtering expr |
| 191 | +
|
| 192 | + Raises |
| 193 | + ------ |
| 194 | + SchemaValidationError |
| 195 | + When schema validation fails against data set |
| 196 | + InvalidSchemaFormatError |
| 197 | + When JSON schema provided is invalid |
| 198 | + InvalidEnvelopeExpressionError |
| 199 | + When JMESPath expression to unwrap event is invalid |
| 200 | + """ |
| 201 | + if envelope: |
| 202 | + event = unwrap_event_from_envelope(data=event, envelope=envelope, jmespath_options=jmespath_options) |
| 203 | + |
| 204 | + validate_data_against_schema(data=event, schema=schema) |
0 commit comments