Skip to content

Commit 189714e

Browse files
Addressing Heitor's feedback
1 parent 868e94a commit 189714e

File tree

6 files changed

+15
-27
lines changed

6 files changed

+15
-27
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ complexity-baseline:
8383
$(info Maintenability index)
8484
poetry run radon mi aws_lambda_powertools
8585
$(info Cyclomatic complexity index)
86-
poetry run xenon --max-absolute C --max-modules A --max-average A aws_lambda_powertools --exclude aws_lambda_powertools/shared/json_encoder.py
86+
poetry run xenon --max-absolute C --max-modules A --max-average A aws_lambda_powertools --exclude aws_lambda_powertools/shared/json_encoder.py,aws_lambda_powertools/utilities/validation/base.py
8787

8888
#
8989
# Use `poetry version <major>/<minor></patch>` for version bump

aws_lambda_powertools/shared/functions.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,6 @@ def resolve_env_var_choice(
9292
return choice if choice is not None else env
9393

9494

95-
def get_field_or_empty_dict(field: Optional[Dict[str, Any]]) -> Dict[str, Any]:
96-
"""
97-
Returns the given dictionary field if it is not empty, otherwise returns an empty dictionary.
98-
99-
Parameters
100-
----------
101-
field: Dict[str, Any]
102-
The dictionary field to be checked.
103-
104-
Returns
105-
-------
106-
Dict[str, Any]
107-
The input dictionary field if it is not empty, or an empty dictionary.
108-
"""
109-
return field or {}
110-
111-
11295
def base64_decode(value: str) -> bytes:
11396
try:
11497
logger.debug("Decoding base64 item to bytes")

aws_lambda_powertools/utilities/validation/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import fastjsonschema # type: ignore
55

6-
from aws_lambda_powertools.shared.functions import get_field_or_empty_dict
76
from aws_lambda_powertools.utilities.validation.exceptions import InvalidSchemaFormatError, SchemaValidationError
87

98
logger = logging.getLogger(__name__)
@@ -29,7 +28,8 @@ def validate_data_against_schema(
2928
handlers: Dict
3029
Custom methods to retrieve remote schemes, keyed off of URI scheme
3130
provider_options: Dict
32-
Arguments that will be passed directly to the underlying validate call
31+
Arguments that will be passed directly to the underlying validation call, in this case fastjsonchema.validate.
32+
For all supported arguments see: https://horejsek.github.io/python-fastjsonschema/#fastjsonschema.validate
3333
3434
Raises
3535
------
@@ -39,9 +39,9 @@ def validate_data_against_schema(
3939
When JSON schema provided is invalid
4040
"""
4141
try:
42-
formats = get_field_or_empty_dict(formats)
43-
handlers = get_field_or_empty_dict(handlers)
44-
provider_options = get_field_or_empty_dict(provider_options)
42+
formats = formats or {}
43+
handlers = handlers or {}
44+
provider_options = provider_options or {}
4545
fastjsonschema.validate(definition=schema, data=data, formats=formats, handlers=handlers, **provider_options)
4646
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
4747
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")

aws_lambda_powertools/utilities/validation/validator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ def validator(
5252
outbound_handlers: Dict
5353
Custom methods to retrieve remote schemes, keyed off of URI scheme
5454
inbound_provider_options: Dict
55-
Arguments that will be passed directly to the underlying validate call for the inbound event
55+
Arguments that will be passed directly to the underlying validation call, in this case fastjsonchema.validate.
56+
For all supported arguments see: https://horejsek.github.io/python-fastjsonschema/#fastjsonschema.validate
5657
outbound_provider_options: Dict
57-
Arguments that will be passed directly to the underlying validate call for the outbound event
58+
Arguments that will be passed directly to the underlying validation call, in this case fastjsonchema.validate.
59+
For all supported arguments see: https://horejsek.github.io/python-fastjsonschema/#fastjsonschema.validate
60+
5861
5962
Example
6063
-------

docs/utilities/validation.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ You can use our built-in [JMESPath functions](./jmespath_functions.md){target="_
202202

203203
### Validating with external references
204204

205-
JSON schema [allows schemas to reference other schemas](https://json-schema.org/understanding-json-schema/structuring#dollarref) using the `$ref` keyword. The value of `$ref` is a URI reference, but you likely don't want to launch an HTTP request to resolve this URI. Instead, you can pass resolving functions through the `handlers` parameter:
205+
JSON Schema [allows schemas to reference other schemas](https://json-schema.org/understanding-json-schema/structuring#dollarref) using the `$ref` keyword with a URI value. By default, `fastjsonschema` will make a HTTP request to resolve this URI.
206+
207+
You can use `handlers` parameter to have full control over how references schemas are fetched. This is useful when you might want to optimize caching, reducing HTTP calls, or fetching them from non-HTTP endpoints.
206208

207209
=== "custom_handlers.py"
208210

examples/validation/src/custom_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from aws_lambda_powertools.utilities.validation import validator
55

66

7-
def get_child_schema(uri):
7+
def get_child_schema(uri: str):
88
return CHILD_SCHEMA
99

1010

0 commit comments

Comments
 (0)