1
1
import logging
2
- from typing import Any , Callable , Dict , Union
2
+ from typing import Any , Callable , Dict , Optional , Union
3
3
4
4
from ...middleware_factory import lambda_handler_decorator
5
5
from .base import unwrap_event_from_envelope , validate_data_against_schema
@@ -13,14 +13,35 @@ def validator(
13
13
event : Union [Dict , str ],
14
14
context : Any ,
15
15
inbound_schema : Dict = None ,
16
- inbound_formats : Dict = {} ,
16
+ inbound_formats : Optional [ Dict ] = None ,
17
17
outbound_schema : Dict = None ,
18
- outbound_formats : Dict = {} ,
18
+ outbound_formats : Optional [ Dict ] = None ,
19
19
envelope : str = None ,
20
20
jmespath_options : Dict = None ,
21
21
) -> Any :
22
22
"""Lambda handler decorator to validate incoming/outbound data using a JSON Schema
23
23
24
+ Parameters
25
+ ----------
26
+ handler : Callable
27
+ Method to annotate on
28
+ event : Dict
29
+ Lambda event to be validated
30
+ context : Any
31
+ Lambda context object
32
+ inbound_schema : Dict
33
+ JSON Schema to validate incoming event
34
+ outbound_schema : Dict
35
+ JSON Schema to validate outbound event
36
+ envelope : Dict
37
+ JMESPath expression to filter data against
38
+ jmespath_options : Dict
39
+ Alternative JMESPath options to be included when filtering expr
40
+ inbound_formats: Dict
41
+ Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
42
+ outbound_formats: Dict
43
+ Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
44
+
24
45
Example
25
46
-------
26
47
@@ -80,23 +101,6 @@ def handler(event, context):
80
101
def handler(event, context):
81
102
return event
82
103
83
- Parameters
84
- ----------
85
- handler : Callable
86
- Method to annotate on
87
- event : Dict
88
- Lambda event to be validated
89
- context : Any
90
- Lambda context object
91
- inbound_schema : Dict
92
- JSON Schema to validate incoming event
93
- outbound_schema : Dict
94
- JSON Schema to validate outbound event
95
- envelope : Dict
96
- JMESPath expression to filter data against
97
- jmespath_options : Dict
98
- Alternative JMESPath options to be included when filtering expr
99
-
100
104
Returns
101
105
-------
102
106
Any
@@ -127,11 +131,33 @@ def handler(event, context):
127
131
return response
128
132
129
133
130
- def validate (event : Dict , schema : Dict = None , formats : Dict = {}, envelope : str = None , jmespath_options : Dict = None ):
134
+ def validate (
135
+ event : Dict ,
136
+ schema : Dict = None ,
137
+ formats : Optional [Dict ] = None ,
138
+ envelope : str = None ,
139
+ jmespath_options : Dict = None ,
140
+ ):
131
141
"""Standalone function to validate event data using a JSON Schema
132
142
133
143
Typically used when you need more control over the validation process.
134
144
145
+ Parameters
146
+ ----------
147
+ event : Dict
148
+ Lambda event to be validated
149
+ schema : Dict
150
+ JSON Schema to validate incoming event
151
+ envelope : Dict
152
+ JMESPath expression to filter data against
153
+ jmespath_options : Dict
154
+ Alternative JMESPath options to be included when filtering expr
155
+ formats: Dict
156
+ Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
157
+
158
+ Example
159
+ -------
160
+
135
161
**Validate event**
136
162
137
163
from aws_lambda_powertools.utilities.validation import validate
@@ -180,17 +206,6 @@ def handler(event, context):
180
206
validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
181
207
return event
182
208
183
- Parameters
184
- ----------
185
- event : Dict
186
- Lambda event to be validated
187
- schema : Dict
188
- JSON Schema to validate incoming event
189
- envelope : Dict
190
- JMESPath expression to filter data against
191
- jmespath_options : Dict
192
- Alternative JMESPath options to be included when filtering expr
193
-
194
209
Raises
195
210
------
196
211
SchemaValidationError
0 commit comments