Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions apigw-data-validation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,23 @@ After the application is deployed try the following scenarios.

### Create a new vehicle entering valid data:
```
curl --location --request POST 'https://qap9jh21xk.execute-api.us-west-2.amazonaws.com/Prod/' \
curl --location --request POST 'https://{api-id}.execute-api.{region}.amazonaws.com/Prod/vehicle' \
--header 'Content-Type: application/json' \
--data-raw '{
"make":"MINI",
"model":"Countryman",
"year": 2010
"make": "MINI",
"model": "Countryman",
"year": 2020
}'
```
Expected response: `{"message": "Data vbalidation succeded", "data": {"make": "MINI", "model": "Countryman", "year": 2010}}`
Expected response: `{"message": "Data validation succeded", "data": {"make": "MINI", "model": "Countryman", "year": 2020}}`
### Now enter a year less than 2010
```
curl --location --request POST 'https://qap9jh21xk.execute-api.us-west-2.amazonaws.com/Prod/' \
curl --location --request POST 'https://{api-id}.execute-api.{region}.amazonaws.com/Prod/vehicle' \
--header 'Content-Type: application/json' \
--data-raw '{
"make":"MINI",
"model":"Countryman",
"year": 2002
"make": "MINI",
"model": "Countryman",
"year": 2009
}'
```
Expected response: `{"message": "Invalid request body"}`
Expand Down
26 changes: 19 additions & 7 deletions apigw-data-validation/src/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import json

def lambda_handler(event, context):
return {
"statusCode": 200,
"body": json.dumps({
"message": "Data validation succeded",
"data": json.loads(event["body"])
}),
}
try:
# Parse the incoming JSON body
body = json.loads(event['body'])

return {
'statusCode': 200,
'body': json.dumps({
'message': 'Data validation succeeded',
'data': body
})
}
except Exception as e:
return {
'statusCode': 400,
'body': json.dumps({
'message': 'Invalid request body'
})
}
29 changes: 9 additions & 20 deletions apigw-data-validation/template.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: API Gateway data validation (uksb-1tthgi812) (tag:apigw-data-validation)
Description: API Gateway data validation example (uksb-1tthgi812) (tag:apigw-data-validation)

Globals:
Function:
Timeout: 3

Resources:

# REST API Configuration. Creates models available for validation at each endpoint.
# REST API Configuration
MainApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Models:
Vehicle: # Data model for vehicles to be validated against
Vehicle:
type: object
required:
- make
Expand All @@ -40,32 +39,22 @@ Resources:
Properties:
CodeUri: src/
Handler: app.lambda_handler
Runtime: python3.9
Runtime: python3.13
Architectures:
- arm64
Events:
Process:
Type: Api
Properties:
RestApiId: !Ref MainApi
Path: /{id}
Path: /vehicle
Method: post
RequestParameters:
- method.request.querystring.order:
Required: true
Caching: true
# - method.request.path.id:
# Required: true
- method.request.header.custom-agent:
Required: true
RequestModel:
Model: Vehicle # Links available model
Required: true # requires validation
ValidateBody: true #Validates the request body.
ValidateParameters: true #Validates the request header
Model: Vehicle
Required: true
ValidateBody: true


Outputs:
ProcessApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Description: "API Gateway endpoint URL for Prod stage"
Value: !Sub "https://${MainApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"