Skip to content

Commit f7f3c99

Browse files
committed
improv: test to cover custom jmespath opts
1 parent 4f1b4d7 commit f7f3c99

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

aws_lambda_powertools/utilities/validation/validator.py

-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def validator(
2424
2525
**Validate incoming event**
2626
27-
import json
2827
from aws_lambda_powertools.utilities.validation import validator
2928
3029
@validator(inbound_schema=json_schema_dict)
@@ -33,7 +32,6 @@ def handler(event, context):
3332
3433
**Validate incoming and outgoing event**
3534
36-
import json
3735
from aws_lambda_powertools.utilities.validation import validator
3836
3937
@validator(inbound_schema=json_schema_dict, outbound_schema=response_json_schema_dict)
@@ -42,7 +40,6 @@ def handler(event, context):
4240
4341
**Unwrap event before validating against actual payload - using built-in envelopes**
4442
45-
import json
4643
from aws_lambda_powertools.utilities.validation import validator, envelopes
4744
4845
@validator(inbound_schema=json_schema_dict, envelope=envelopes.API_GATEWAY_REST)
@@ -51,7 +48,6 @@ def handler(event, context):
5148
5249
**Unwrap event before validating against actual payload - using custom JMESPath expression**
5350
54-
import json
5551
from aws_lambda_powertools.utilities.validation import validator
5652
5753
@validator(inbound_schema=json_schema_dict, envelope="payload[*].my_data")
@@ -60,7 +56,6 @@ def handler(event, context):
6056
6157
**Unwrap and deserialize JSON string event before validating against actual payload - using built-in functions**
6258
63-
import json
6459
from aws_lambda_powertools.utilities.validation import validator
6560
6661
@validator(inbound_schema=json_schema_dict, envelope="Records[*].powertools_json(body)")

tests/functional/validator/test_validator.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import jmespath
12
import pytest
3+
from jmespath import functions
24

35
from aws_lambda_powertools.utilities.validation import envelopes, exceptions, validate, validator
46

@@ -32,7 +34,7 @@ def test_validate_json_string_no_envelope(schema, wrapped_event_json_string):
3234

3335
def test_validate_invalid_schema_format(raw_event):
3436
with pytest.raises(exceptions.InvalidSchemaFormatError):
35-
validate(event=raw_event, schema="")
37+
validate(event=raw_event, schema="schema.json")
3638

3739

3840
def test_validate_invalid_envelope_expression(schema, wrapped_event):
@@ -103,3 +105,19 @@ def lambda_handler(evt, context):
103105

104106
with pytest.raises(ValueError):
105107
lambda_handler(raw_event, {})
108+
109+
110+
def test_custom_jmespath_function_overrides_builtin_functions(schema, wrapped_event_json_string):
111+
class CustomFunctions(functions.Functions):
112+
@functions.signature({"types": ["string"]})
113+
def _func_echo_decoder(self, value):
114+
return value
115+
116+
jmespath_opts = {"custom_functions": CustomFunctions()}
117+
with pytest.raises(jmespath.exceptions.UnknownFunctionError, match="Unknown function: powertools_json()"):
118+
validate(
119+
event=wrapped_event_json_string,
120+
schema=schema,
121+
envelope="powertools_json(data).payload",
122+
jmespath_options=jmespath_opts,
123+
)

0 commit comments

Comments
 (0)