Skip to content

Commit 7e77f76

Browse files
docs(validation): Refactoring validate example
1 parent 542693e commit 7e77f76

4 files changed

+25
-16
lines changed

docs/utilities/validation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ You can also gracefully handle schema validation errors by catching `SchemaValid
6565

6666
=== "getting_started_validator_standalone_function.py"
6767

68-
```python hl_lines="3 10 16 18"
68+
```python hl_lines="5 16 17 26"
6969
--8<-- "examples/validation/src/getting_started_validator_standalone_function.py"
7070
```
7171

examples/validation/src/getting_started_validator_standalone_function.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import getting_started_validator_standalone_schema as schemas
22

3+
from aws_lambda_powertools.utilities import parameters
34
from aws_lambda_powertools.utilities.typing import LambdaContext
45
from aws_lambda_powertools.utilities.validation import SchemaValidationError, validate
56

7+
# we can get list of allowed IPs from AWS Parameter Store using Parameters Utility
8+
# See: https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/parameters/
9+
ALLOWED_IPS = parameters.get_parameter("/lambda-powertools/allowed_ips")
10+
611

712
def lambda_handler(event, context: LambdaContext) -> dict:
813
try:
@@ -11,10 +16,14 @@ def lambda_handler(event, context: LambdaContext) -> dict:
1116
# using standalone function to validate input data only
1217
validate(event=event, schema=schemas.INPUT)
1318

14-
if event.get("username") == "lambda" and event.get("password") == "powertools":
15-
user_authenticated = "Authenticated"
19+
if (
20+
event.get("user_id") == "0d44b083-8206-4a3a-aa95-5d392a99be4a"
21+
and event.get("project") == "powertools"
22+
and event.get("ip") in ALLOWED_IPS
23+
):
24+
user_authenticated = "Allowed"
1625

17-
# in this example the body can be a string because we are not validating the OUTPUT
26+
# in this example the body can be of any type because we are not validating the OUTPUT
1827
return {"body": user_authenticated, "statusCode": 200 if user_authenticated else 204}
1928
except SchemaValidationError as exception:
2029
# SchemaValidationError indicates where a data mismatch is
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
{
3-
"username": "lambda",
4-
"password": "powertools",
3+
"user_id": "0d44b083-8206-4a3a-aa95-5d392a99be4a",
4+
"project": "powertools",
55
"ip": "192.168.0.1"
66
}

examples/validation/src/getting_started_validator_standalone_schema.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
"type": "object",
55
"title": "Sample schema",
66
"description": "The root schema comprises the entire JSON document.",
7-
"examples": [{"username": "hello world", "password": "lessa", "ip": "192.168.0.1"}],
8-
"required": ["username", "password", "ip"],
7+
"examples": [{"user_id": "0d44b083-8206-4a3a-aa95-5d392a99be4a", "powertools": "lessa", "ip": "192.168.0.1"}],
8+
"required": ["user_id", "project", "ip"],
99
"properties": {
10-
"username": {
11-
"$id": "#/properties/username",
10+
"user_id": {
11+
"$id": "#/properties/user_id",
1212
"type": "string",
13-
"title": "The username",
14-
"examples": ["lambda"],
15-
"maxLength": 30,
13+
"title": "The user_id",
14+
"examples": ["0d44b083-8206-4a3a-aa95-5d392a99be4a"],
15+
"maxLength": 50,
1616
},
17-
"password": {
18-
"$id": "#/properties/password",
17+
"project": {
18+
"$id": "#/properties/project",
1919
"type": "string",
20-
"title": "The password",
20+
"title": "The project",
2121
"examples": ["powertools"],
2222
"maxLength": 30,
2323
},

0 commit comments

Comments
 (0)