Skip to content

Commit 708c637

Browse files
committed
fix(docs): Extract validation code examples
Changes: - Extract code examples - Run isort and black - Fix python syntax errors - Update line highlights - Add make task Related to: - aws-powertools#1064
1 parent b577366 commit 708c637

9 files changed

+215
-187
lines changed

Diff for: Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,13 @@ changelog:
9090

9191
mypy:
9292
poetry run mypy --pretty aws_lambda_powertools
93+
94+
format-examples:
95+
poetry run isort docs/shared
96+
poetry run black docs/shared/*.py
97+
poetry run isort docs/examples
98+
poetry run black docs/examples/*/*/*.py
99+
100+
lint-examples:
101+
poetry run python3 -m py_compile docs/shared/*.py
102+
poetry run python3 -m py_compile docs/examples/*/*/*.py
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validator
4+
5+
6+
@validator(inbound_schema=schemas.INPUT, envelope="detail")
7+
def handler(event, context):
8+
return event
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import envelopes, validator
4+
5+
6+
@validator(inbound_schema=schemas.INPUT, envelope=envelopes.EVENTBRIDGE)
7+
def handler(event, context):
8+
return event
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import schema
2+
3+
from aws_lambda_powertools.utilities.validation import validate
4+
5+
custom_format = {
6+
"int64": True, # simply ignore it,
7+
"positive": lambda x: False if x < 0 else True,
8+
}
9+
10+
validate(event=event, schema=schemas.INPUT, formats=custom_format)
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
INPUT = {
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"definitions": {
4+
"AWSAPICallViaCloudTrail": {
5+
"properties": {
6+
"additionalEventData": {"$ref": "#/definitions/AdditionalEventData"},
7+
"awsRegion": {"type": "string"},
8+
"errorCode": {"type": "string"},
9+
"errorMessage": {"type": "string"},
10+
"eventID": {"type": "string"},
11+
"eventName": {"type": "string"},
12+
"eventSource": {"type": "string"},
13+
"eventTime": {"format": "date-time", "type": "string"},
14+
"eventType": {"type": "string"},
15+
"eventVersion": {"type": "string"},
16+
"recipientAccountId": {"type": "string"},
17+
"requestID": {"type": "string"},
18+
"requestParameters": {"$ref": "#/definitions/RequestParameters"},
19+
"resources": {"items": {"type": "object"}, "type": "array"},
20+
"responseElements": {"type": ["object", "null"]},
21+
"sourceIPAddress": {"type": "string"},
22+
"userAgent": {"type": "string"},
23+
"userIdentity": {"$ref": "#/definitions/UserIdentity"},
24+
"vpcEndpointId": {"type": "string"},
25+
"x-amazon-open-api-schema-readOnly": {"type": "boolean"},
26+
},
27+
"required": [
28+
"eventID",
29+
"awsRegion",
30+
"eventVersion",
31+
"responseElements",
32+
"sourceIPAddress",
33+
"eventSource",
34+
"requestParameters",
35+
"resources",
36+
"userAgent",
37+
"readOnly",
38+
"userIdentity",
39+
"eventType",
40+
"additionalEventData",
41+
"vpcEndpointId",
42+
"requestID",
43+
"eventTime",
44+
"eventName",
45+
"recipientAccountId",
46+
],
47+
"type": "object",
48+
},
49+
"AdditionalEventData": {
50+
"properties": {
51+
"objectRetentionInfo": {"$ref": "#/definitions/ObjectRetentionInfo"},
52+
"x-amz-id-2": {"type": "string"},
53+
},
54+
"required": ["x-amz-id-2"],
55+
"type": "object",
56+
},
57+
"Attributes": {
58+
"properties": {
59+
"creationDate": {"format": "date-time", "type": "string"},
60+
"mfaAuthenticated": {"type": "string"},
61+
},
62+
"required": ["mfaAuthenticated", "creationDate"],
63+
"type": "object",
64+
},
65+
"LegalHoldInfo": {
66+
"properties": {
67+
"isUnderLegalHold": {"type": "boolean"},
68+
"lastModifiedTime": {"format": "int64", "type": "integer"},
69+
},
70+
"type": "object",
71+
},
72+
"ObjectRetentionInfo": {
73+
"properties": {
74+
"legalHoldInfo": {"$ref": "#/definitions/LegalHoldInfo"},
75+
"retentionInfo": {"$ref": "#/definitions/RetentionInfo"},
76+
},
77+
"type": "object",
78+
},
79+
"RequestParameters": {
80+
"properties": {
81+
"bucketName": {"type": "string"},
82+
"key": {"type": "string"},
83+
"legal-hold": {"type": "string"},
84+
"retention": {"type": "string"},
85+
},
86+
"required": ["bucketName", "key"],
87+
"type": "object",
88+
},
89+
"RetentionInfo": {
90+
"properties": {
91+
"lastModifiedTime": {"format": "int64", "type": "integer"},
92+
"retainUntilMode": {"type": "string"},
93+
"retainUntilTime": {"format": "int64", "type": "integer"},
94+
},
95+
"type": "object",
96+
},
97+
"SessionContext": {
98+
"properties": {"attributes": {"$ref": "#/definitions/Attributes"}},
99+
"required": ["attributes"],
100+
"type": "object",
101+
},
102+
"UserIdentity": {
103+
"properties": {
104+
"accessKeyId": {"type": "string"},
105+
"accountId": {"type": "string"},
106+
"arn": {"type": "string"},
107+
"principalId": {"type": "string"},
108+
"sessionContext": {"$ref": "#/definitions/SessionContext"},
109+
"type": {"type": "string"},
110+
},
111+
"required": ["accessKeyId", "sessionContext", "accountId", "principalId", "type", "arn"],
112+
"type": "object",
113+
},
114+
},
115+
"properties": {
116+
"account": {"type": "string"},
117+
"detail": {"$ref": "#/definitions/AWSAPICallViaCloudTrail"},
118+
"detail-type": {"type": "string"},
119+
"id": {"type": "string"},
120+
"region": {"type": "string"},
121+
"resources": {"items": {"type": "string"}, "type": "array"},
122+
"source": {"type": "string"},
123+
"time": {"format": "date-time", "type": "string"},
124+
"version": {"type": "string"},
125+
},
126+
"required": ["detail-type", "resources", "id", "source", "time", "detail", "region", "version", "account"],
127+
"title": "AWSAPICallViaCloudTrail",
128+
"type": "object",
129+
"x-amazon-events-detail-type": "AWS API Call via CloudTrail",
130+
"x-amazon-events-source": "aws.s3",
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validator
4+
5+
6+
@validator(inbound_schema=schemas.INPUT, outbound_schema=schemas.OUTPUT)
7+
def handler(event, context):
8+
return event
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validate
4+
from aws_lambda_powertools.utilities.validation.exceptions import SchemaValidationError
5+
6+
7+
def handler(event, context):
8+
try:
9+
validate(event=event, schema=schemas.INPUT)
10+
except SchemaValidationError as e:
11+
# do something before re-raising
12+
raise
13+
14+
return event

Diff for: docs/shared/validation_basic_jsonschema.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@
3333
"examples": [{"statusCode": 200, "body": "response"}],
3434
"required": ["statusCode", "body"],
3535
"properties": {
36-
"statusCode": {"$id": "#/properties/statusCode", "type": "integer", "title": "The statusCode"},
37-
"body": {"$id": "#/properties/body", "type": "string", "title": "The response"},
36+
"statusCode": {
37+
"$id": "#/properties/statusCode",
38+
"type": "integer",
39+
"title": "The statusCode",
40+
},
41+
"body": {
42+
"$id": "#/properties/body",
43+
"type": "string",
44+
"title": "The response",
45+
},
3846
},
3947
}

0 commit comments

Comments
 (0)