From 623aa28c6e66dc9b54a2bf8de931744e9dffabff Mon Sep 17 00:00:00 2001 From: Makendran Date: Sun, 12 Jan 2025 19:32:54 +0530 Subject: [PATCH 1/4] Update README.md Have corrected the spelling of validation and also curl command. With the previous curl command, it is not getting output. The curl command which I pasted, it works. --- apigw-data-validation/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apigw-data-validation/README.md b/apigw-data-validation/README.md index f3ab16342..5b95a6406 100644 --- a/apigw-data-validation/README.md +++ b/apigw-data-validation/README.md @@ -46,19 +46,21 @@ 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://qap9jh21xk.execute-api.us-west-2.amazonaws.com/Prod/123?order=ORD12345' \ --header 'Content-Type: application/json' \ +--header 'custom-agent: MyMobileApp/1.0' \ --data-raw '{ "make":"MINI", "model":"Countryman", "year": 2010 }' ``` -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": 2010}}` ### 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://qap9jh21xk.execute-api.us-west-2.amazonaws.com/Prod/123?order=ORD12345' \ --header 'Content-Type: application/json' \ +--header 'custom-agent: MyMobileApp/1.0' \ --data-raw '{ "make":"MINI", "model":"Countryman", From 022fff3789240bb2bbdf5c78611217af54129d04 Mon Sep 17 00:00:00 2001 From: Makendran Date: Sat, 15 Feb 2025 17:46:12 +0000 Subject: [PATCH 2/4] Updated as per the request --- apigw-data-validation/README.md | 22 ++++++++++------------ apigw-data-validation/src/app.py | 26 +++++++++++++++++++------- apigw-data-validation/template.yaml | 29 +++++++++-------------------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/apigw-data-validation/README.md b/apigw-data-validation/README.md index 5b95a6406..673b47395 100644 --- a/apigw-data-validation/README.md +++ b/apigw-data-validation/README.md @@ -46,25 +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/123?order=ORD12345' \ +curl --location --request POST 'https://{api-id}.execute-api.{region}.amazonaws.com/Prod/vehicle' \ --header 'Content-Type: application/json' \ ---header 'custom-agent: MyMobileApp/1.0' \ --data-raw '{ - "make":"MINI", - "model":"Countryman", - "year": 2010 -}' + "make": "MINI", + "model": "Countryman", + "year": 2020 +} ``` -Expected response: `{"message": "Data validation 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/123?order=ORD12345' \ +curl --location --request POST 'https://{api-id}.execute-api.{region}.amazonaws.com/Prod/vehicle' \ --header 'Content-Type: application/json' \ ---header 'custom-agent: MyMobileApp/1.0' \ --data-raw '{ - "make":"MINI", - "model":"Countryman", - "year": 2002 + "make": "MINI", + "model": "Countryman", + "year": 2009 }' ``` Expected response: `{"message": "Invalid request body"}` diff --git a/apigw-data-validation/src/app.py b/apigw-data-validation/src/app.py index 4abf0f0cd..c1dfd70a5 100644 --- a/apigw-data-validation/src/app.py +++ b/apigw-data-validation/src/app.py @@ -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' + }) + } \ No newline at end of file diff --git a/apigw-data-validation/template.yaml b/apigw-data-validation/template.yaml index 632d9272e..49f2347e2 100644 --- a/apigw-data-validation/template.yaml +++ b/apigw-data-validation/template.yaml @@ -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 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 @@ -40,7 +39,7 @@ Resources: Properties: CodeUri: src/ Handler: app.lambda_handler - Runtime: python3.9 + Runtime: python3.13 Architectures: - arm64 Events: @@ -48,24 +47,14 @@ Resources: 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/" \ No newline at end of file From 1f7991a77630483c9291a634502b43758a30c882 Mon Sep 17 00:00:00 2001 From: Makendran Date: Thu, 27 Feb 2025 20:55:39 +0530 Subject: [PATCH 3/4] Update apigw-data-validation/template.yaml Co-authored-by: ellisms <114107920+ellisms@users.noreply.github.com> --- apigw-data-validation/template.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apigw-data-validation/template.yaml b/apigw-data-validation/template.yaml index 49f2347e2..6fd9fb5b5 100644 --- a/apigw-data-validation/template.yaml +++ b/apigw-data-validation/template.yaml @@ -1,6 +1,6 @@ AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 -Description: API Gateway data validation example +Description: API Gateway data validation example (uksb-1tthgi812) (tag:apigw-data-validation) Globals: Function: From cbd57ff4da600fdd6a29849504f72bd8eb1adbac Mon Sep 17 00:00:00 2001 From: Makendran Date: Thu, 27 Feb 2025 20:55:50 +0530 Subject: [PATCH 4/4] Update apigw-data-validation/README.md Co-authored-by: ellisms <114107920+ellisms@users.noreply.github.com> --- apigw-data-validation/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apigw-data-validation/README.md b/apigw-data-validation/README.md index 673b47395..a5711f587 100644 --- a/apigw-data-validation/README.md +++ b/apigw-data-validation/README.md @@ -52,7 +52,7 @@ curl --location --request POST 'https://{api-id}.execute-api.{region}.amazonaws. "make": "MINI", "model": "Countryman", "year": 2020 -} +}' ``` Expected response: `{"message": "Data validation succeded", "data": {"make": "MINI", "model": "Countryman", "year": 2020}}` ### Now enter a year less than 2010