|
7 | 7 | from pydantic import BaseModel
|
8 | 8 | from pydantic.error_wrappers import ValidationError
|
9 | 9 |
|
10 |
| -from aws_lambda_powertools.utilities.validation import DynamoDBEnvelope, EventBridgeEnvelope, UserEnvelope, validator |
| 10 | +from aws_lambda_powertools.utilities.validation import ( |
| 11 | + DynamoDBEnvelope, |
| 12 | + EventBridgeEnvelope, |
| 13 | + SqsEnvelope, |
| 14 | + UserEnvelope, |
| 15 | + validator |
| 16 | +) |
11 | 17 |
|
12 | 18 |
|
13 | 19 | class OutboundSchema(BaseModel):
|
@@ -60,6 +66,7 @@ class MyMessage(BaseModel):
|
60 | 66 | @validator(inbound_schema_model=MyMessage, outbound_schema_model=OutboundSchema, envelope=DynamoDBEnvelope())
|
61 | 67 | def dynamodb_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Optional[Any]]:
|
62 | 68 | assert event["custom"]
|
| 69 | + assert len(event["custom"]) == 3 |
63 | 70 | # first record
|
64 | 71 | assert not event["custom"][0]["old"]
|
65 | 72 | assert event["custom"][0]["new"].message == "hello"
|
@@ -169,3 +176,95 @@ def test_eventbridge_fail_inbound_validation():
|
169 | 176 | event = {"greeting": "hello"}
|
170 | 177 | with pytest.raises(ValidationError):
|
171 | 178 | eventbridge_handler(event, LambdaContext())
|
| 179 | + |
| 180 | + |
| 181 | +sqs_event_attribs = { |
| 182 | + "test4": { |
| 183 | + "stringValue": "dfgdfgfd", |
| 184 | + "stringListValues": [], |
| 185 | + "binaryListValues": [], |
| 186 | + "dataType": "String.custom_type", |
| 187 | + }, |
| 188 | + "test5": {"stringValue": "a,b,c,d", "stringListValues": [], "binaryListValues": [], "dataType": "String"}, |
| 189 | + "tes6": {"stringValue": "112.1", "stringListValues": [], "binaryListValues": [], "dataType": "Number.mytype"}, |
| 190 | + "test2": {"stringValue": "111", "stringListValues": [], "binaryListValues": [], "dataType": "Number"}, |
| 191 | + "test3": {"binaryValue": "w5NNNcOXXXU=", "stringListValues": [], "binaryListValues": [], "dataType": "Binary"}, |
| 192 | + "test": {"stringValue": "gfgf", "stringListValues": [], "binaryListValues": [], "dataType": "String"}, |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +@validator(inbound_schema_model=MyMessage, outbound_schema_model=OutboundSchema, envelope=SqsEnvelope()) |
| 197 | +def sqs_json_body_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Optional[Any]]: |
| 198 | + assert event["custom"] |
| 199 | + assert len(event["custom"]) == 1 |
| 200 | + assert event["orig"] |
| 201 | + assert event["custom"][0]["body"].message == "hello" |
| 202 | + assert event["custom"][0]["body"].messageId == 8 |
| 203 | + assert len(event["custom"][0]["attributes"]) == len(sqs_event_attribs) |
| 204 | + return {"response_code": 200, "message": "working"} |
| 205 | + |
| 206 | + |
| 207 | +def test_sqs_ok_json_string_body_validation(): |
| 208 | + event = { |
| 209 | + "Records": [ |
| 210 | + { |
| 211 | + "messageId": "1743e893-cc24-1234-88f8-f80c37dcd923", |
| 212 | + "receiptHandle": "AKhXK7azPaZHY0zjmTsdfsdfdsfOgcVob", |
| 213 | + "body": '{"message": "hello", "messageId": 8}', |
| 214 | + "attributes": { |
| 215 | + "ApproximateReceiveCount": "1", |
| 216 | + "SentTimestamp": "1598117108660", |
| 217 | + "SenderId": "43434dsdfd:sdfds", |
| 218 | + "ApproximateFirstReceiveTimestamp": "1598117108667", |
| 219 | + }, |
| 220 | + "messageAttributes": sqs_event_attribs, |
| 221 | + "md5OfBody": "4db76498a982d84c188927c585076a6c", |
| 222 | + "md5OfMessageAttributes": "7186428dc148b402947274e0bb41e7ee", |
| 223 | + "eventSource": "aws:sqs", |
| 224 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:mytest", |
| 225 | + "awsRegion": "us-west-1", |
| 226 | + } |
| 227 | + ] |
| 228 | + } |
| 229 | + sqs_json_body_handler(event, LambdaContext()) |
| 230 | + |
| 231 | + |
| 232 | +@validator(inbound_schema_model=str, outbound_schema_model=OutboundSchema, envelope=SqsEnvelope()) |
| 233 | +def sqs_string_body_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Optional[Any]]: |
| 234 | + assert event["custom"] |
| 235 | + assert len(event["custom"]) == 1 |
| 236 | + assert event["orig"] |
| 237 | + assert event["custom"][0]["body"] == "hello how are you" |
| 238 | + assert len(event["custom"][0]["attributes"]) == len(sqs_event_attribs) |
| 239 | + return {"response_code": 200, "message": "working"} |
| 240 | + |
| 241 | + |
| 242 | +def test_sqs_ok_json_string_validation(): |
| 243 | + event = { |
| 244 | + "Records": [ |
| 245 | + { |
| 246 | + "messageId": "1743e893-cc24-1234-88f8-f80c37dcd923", |
| 247 | + "receiptHandle": "AKhXK7azPaZHY0zjmTsdfsdfdsfOgcVob", |
| 248 | + "body": "hello how are you", |
| 249 | + "attributes": { |
| 250 | + "ApproximateReceiveCount": "1", |
| 251 | + "SentTimestamp": "1598117108660", |
| 252 | + "SenderId": "43434dsdfd:sdfds", |
| 253 | + "ApproximateFirstReceiveTimestamp": "1598117108667", |
| 254 | + }, |
| 255 | + "messageAttributes": sqs_event_attribs, |
| 256 | + "md5OfBody": "4db76498a982d84c188927c585076a6c", |
| 257 | + "md5OfMessageAttributes": "7186428dc148b402947274e0bb41e7ee", |
| 258 | + "eventSource": "aws:sqs", |
| 259 | + "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:mytest", |
| 260 | + "awsRegion": "us-west-1", |
| 261 | + } |
| 262 | + ] |
| 263 | + } |
| 264 | + sqs_string_body_handler(event, LambdaContext()) |
| 265 | + |
| 266 | + |
| 267 | +def test_sqs_fail_inbound_validation(): |
| 268 | + event = {"greeting": "hello"} |
| 269 | + with pytest.raises(ValidationError): |
| 270 | + sqs_string_body_handler(event, LambdaContext()) |
0 commit comments