Skip to content

Commit 85c09f6

Browse files
committed
fix: non-mutable argument, and type annotation
1 parent b32cb8c commit 85c09f6

File tree

2 files changed

+51
-34
lines changed

2 files changed

+51
-34
lines changed

aws_lambda_powertools/utilities/validation/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Dict
2+
from typing import Any, Dict, Optional
33

44
import fastjsonschema
55
import jmespath
@@ -11,7 +11,7 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14-
def validate_data_against_schema(data: Dict, schema: Dict, formats: Dict = {}):
14+
def validate_data_against_schema(data: Dict, schema: Dict, formats: Optional[Dict] = None):
1515
"""Validate dict data against given JSON Schema
1616
1717
Parameters
@@ -20,6 +20,8 @@ def validate_data_against_schema(data: Dict, schema: Dict, formats: Dict = {}):
2020
Data set to be validated
2121
schema : Dict
2222
JSON Schema to validate against
23+
formats: Dict
24+
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool
2325
2426
Raises
2527
------

aws_lambda_powertools/utilities/validation/validator.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Callable, Dict, Union
2+
from typing import Any, Callable, Dict, Optional, Union
33

44
from ...middleware_factory import lambda_handler_decorator
55
from .base import unwrap_event_from_envelope, validate_data_against_schema
@@ -13,14 +13,35 @@ def validator(
1313
event: Union[Dict, str],
1414
context: Any,
1515
inbound_schema: Dict = None,
16-
inbound_formats: Dict = {},
16+
inbound_formats: Optional[Dict] = None,
1717
outbound_schema: Dict = None,
18-
outbound_formats: Dict = {},
18+
outbound_formats: Optional[Dict] = None,
1919
envelope: str = None,
2020
jmespath_options: Dict = None,
2121
) -> Any:
2222
"""Lambda handler decorator to validate incoming/outbound data using a JSON Schema
2323
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+
2445
Example
2546
-------
2647
@@ -80,23 +101,6 @@ def handler(event, context):
80101
def handler(event, context):
81102
return event
82103
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-
100104
Returns
101105
-------
102106
Any
@@ -127,11 +131,33 @@ def handler(event, context):
127131
return response
128132

129133

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+
):
131141
"""Standalone function to validate event data using a JSON Schema
132142
133143
Typically used when you need more control over the validation process.
134144
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+
135161
**Validate event**
136162
137163
from aws_lambda_powertools.utilities.validation import validate
@@ -180,17 +206,6 @@ def handler(event, context):
180206
validate(event=event, schema=json_schema_dict, envelope="awslogs.powertools_base64_gzip(data) | powertools_json(@).logEvents[*]")
181207
return event
182208
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-
194209
Raises
195210
------
196211
SchemaValidationError

0 commit comments

Comments
 (0)