diff --git a/apigw-data-validation/README.md b/apigw-data-validation/README.md index f3ab16342..a5711f587 100644 --- a/apigw-data-validation/README.md +++ b/apigw-data-validation/README.md @@ -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"}` 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..6fd9fb5b5 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 (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 @@ -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