Skip to content

Commit 4dc8c47

Browse files
committed
improv: add validate functional tests
1 parent 3f45c1d commit 4dc8c47

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

aws_lambda_powertools/utilities/validation/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
Simple validator to enforce incoming/outgoing event conforms with JSON Schema
33
"""
44

5-
from .exceptions import InvalidEnvelopeExpressionError, InvalidSchemaError, SchemaValidationError
5+
from .exceptions import InvalidEnvelopeExpressionError, InvalidSchemaFormatError, SchemaValidationError
66
from .validator import validate, validator
77

88
__all__ = [
99
"validate",
1010
"validator",
11-
"InvalidSchemaError",
11+
"InvalidSchemaFormatError",
1212
"SchemaValidationError",
1313
"InvalidEnvelopeExpressionError",
1414
]

aws_lambda_powertools/utilities/validation/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import jmespath
66
from jmespath.exceptions import LexerError
77

8-
from .exceptions import InvalidEnvelopeExpressionError, InvalidSchemaError, SchemaValidationError
8+
from .exceptions import InvalidEnvelopeExpressionError, InvalidSchemaFormatError, SchemaValidationError
99
from .jmespath_functions import PowertoolsFunctions
1010

1111
logger = logging.getLogger(__name__)
@@ -15,10 +15,10 @@ def validate_data_against_schema(data: Dict, schema: Dict):
1515
try:
1616
fastjsonschema.validate(definition=schema, data=data)
1717
except fastjsonschema.JsonSchemaException as e:
18-
message = f"Failed inbound validation. Error: {e.message}, Path: {e.path}, Data: {e.value}" # noqa: B306, E501
18+
message = f"Failed schema validation. Error: {e.message}, Path: {e.path}, Data: {e.value}" # noqa: B306, E501
1919
raise SchemaValidationError(message)
20-
except TypeError as e:
21-
raise InvalidSchemaError(e)
20+
except (TypeError, AttributeError) as e:
21+
raise InvalidSchemaFormatError(f"Schema received: {schema}. Error: {e}")
2222

2323

2424
def unwrap_event_from_envelope(data: Dict, envelope: str, jmespath_options: Dict):
@@ -28,5 +28,5 @@ def unwrap_event_from_envelope(data: Dict, envelope: str, jmespath_options: Dict
2828
try:
2929
logger.debug(f"Envelope detected: {envelope}. JMESPath options: {jmespath_options}")
3030
return jmespath.search(envelope, data, options=jmespath.Options(**jmespath_options))
31-
except LexerError as e:
31+
except (LexerError, TypeError) as e:
3232
raise InvalidEnvelopeExpressionError(e)

aws_lambda_powertools/utilities/validation/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class SchemaValidationError(Exception):
44
pass
55

66

7-
class InvalidSchemaError(Exception):
7+
class InvalidSchemaFormatError(Exception):
88
"""When JSON Schema is in invalid format"""
99

1010
pass

tests/functional/validator/test_validator.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from aws_lambda_powertools.utilities.validation import validate
1+
import pytest
2+
3+
from aws_lambda_powertools.utilities.validation import exceptions, validate
24

35

46
def test_validate_raw_event(schema, raw_event):
57
validate(event=raw_event, schema=schema)
68

79

8-
def test_validate_raw_envelope(schema, wrapped_event):
10+
def test_validate_wrapped_event_raw_envelope(schema, wrapped_event):
911
validate(event=wrapped_event, schema=schema, envelope="data.payload")
1012

1113

@@ -17,20 +19,31 @@ def test_validate_base64_string_envelope(schema, wrapped_event_base64_json_strin
1719
validate(event=wrapped_event_base64_json_string, schema=schema, envelope="powertools_json(powertools_base64(data))")
1820

1921

22+
def test_validate_event_does_not_conform_with_schema(schema):
23+
with pytest.raises(exceptions.SchemaValidationError):
24+
validate(event={"message": "hello_world"}, schema=schema)
25+
26+
2027
def test_validate_json_string_no_envelope(schema, wrapped_event_json_string):
21-
raise NotImplementedError()
28+
# WHEN data key contains a JSON String
29+
with pytest.raises(exceptions.SchemaValidationError, match=".*data must be object"):
30+
validate(event=wrapped_event_json_string, schema=schema, envelope="data.payload")
2231

2332

24-
def test_invalid_schema_format_exception():
25-
raise NotImplementedError()
33+
def test_validate_invalid_schema_format(raw_event):
34+
with pytest.raises(exceptions.InvalidSchemaFormatError):
35+
validate(event=raw_event, schema="")
2636

2737

28-
def test_invalid_envelope_expression():
29-
raise NotImplementedError()
38+
def test_validate_invalid_envelope_expression(schema, wrapped_event):
39+
with pytest.raises(exceptions.InvalidEnvelopeExpressionError):
40+
validate(event=wrapped_event, schema=schema, envelope=True)
3041

3142

32-
def test_invalid_event():
33-
raise NotImplementedError()
43+
def test_validate_invalid_event(schema):
44+
b64_event = "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9="
45+
with pytest.raises(exceptions.SchemaValidationError):
46+
validate(event=b64_event, schema=schema)
3447

3548

3649
def test_apigateway_http_envelope():

0 commit comments

Comments
 (0)