1
+ from __future__ import annotations
2
+
1
3
import logging
2
- from typing import Any , Callable , Dict , Optional , Union
4
+ from typing import Any , Callable
3
5
6
+ from aws_lambda_powertools .middleware_factory import lambda_handler_decorator
4
7
from aws_lambda_powertools .utilities import jmespath_utils
5
-
6
- from ...middleware_factory import lambda_handler_decorator
7
- from .base import validate_data_against_schema
8
+ from aws_lambda_powertools .utilities .validation .base import validate_data_against_schema
8
9
9
10
logger = logging .getLogger (__name__ )
10
11
11
12
12
13
@lambda_handler_decorator
13
14
def validator (
14
15
handler : Callable ,
15
- event : Union [ Dict , str ] ,
16
+ event : dict | str ,
16
17
context : Any ,
17
- inbound_schema : Optional [ Dict ] = None ,
18
- inbound_formats : Optional [ Dict ] = None ,
19
- outbound_schema : Optional [ Dict ] = None ,
20
- outbound_formats : Optional [ Dict ] = None ,
18
+ inbound_schema : dict | None = None ,
19
+ inbound_formats : dict | None = None ,
20
+ outbound_schema : dict | None = None ,
21
+ outbound_formats : dict | None = None ,
21
22
envelope : str = "" ,
22
- jmespath_options : Optional [ Dict ] = None ,
23
+ jmespath_options : dict | None = None ,
23
24
** kwargs : Any ,
24
25
) -> Any :
25
26
"""Lambda handler decorator to validate incoming/outbound data using a JSON Schema
@@ -28,21 +29,21 @@ def validator(
28
29
----------
29
30
handler : Callable
30
31
Method to annotate on
31
- event : Dict
32
+ event : dict
32
33
Lambda event to be validated
33
34
context : Any
34
35
Lambda context object
35
- inbound_schema : Dict
36
+ inbound_schema : dict
36
37
JSON Schema to validate incoming event
37
- outbound_schema : Dict
38
+ outbound_schema : dict
38
39
JSON Schema to validate outbound event
39
- envelope : Dict
40
+ envelope : dict
40
41
JMESPath expression to filter data against
41
- jmespath_options : Dict
42
+ jmespath_options : dict
42
43
Alternative JMESPath options to be included when filtering expr
43
- inbound_formats: Dict
44
+ inbound_formats: dict
44
45
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
45
- outbound_formats: Dict
46
+ outbound_formats: dict
46
47
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
47
48
48
49
Example
@@ -140,26 +141,26 @@ def handler(event, context):
140
141
141
142
def validate (
142
143
event : Any ,
143
- schema : Dict ,
144
- formats : Optional [ Dict ] = None ,
145
- envelope : Optional [ str ] = None ,
146
- jmespath_options : Optional [ Dict ] = None ,
144
+ schema : dict ,
145
+ formats : dict | None = None ,
146
+ envelope : str | None = None ,
147
+ jmespath_options : dict | None = None ,
147
148
):
148
149
"""Standalone function to validate event data using a JSON Schema
149
150
150
151
Typically used when you need more control over the validation process.
151
152
152
153
Parameters
153
154
----------
154
- event : Dict
155
+ event : dict
155
156
Lambda event to be validated
156
- schema : Dict
157
+ schema : dict
157
158
JSON Schema to validate incoming event
158
- envelope : Dict
159
+ envelope : dict
159
160
JMESPath expression to filter data against
160
- jmespath_options : Dict
161
+ jmespath_options : dict
161
162
Alternative JMESPath options to be included when filtering expr
162
- formats: Dict
163
+ formats: dict
163
164
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
164
165
165
166
Example
0 commit comments