Add support for AWS API Gateway Basic Request Validation
Problem
This is a Feature Proposal Description AWS API GW officially supports this now - http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-request-validation.html For example, it could be set as a new property somewhere in `service.function.events.http.validation` config object. Blog post: https://aws.amazon.com/blogs/compute/how-to-remove-boilerplate-validation-logic-in-your-rest-apis-with-amazon-api-gateway-request-validation/
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Basic Request Validation in AWS API Gateway
AWS API Gateway has introduced support for method request validation, which allows for automatic validation of incoming requests against specified models. This feature is not currently implemented in the existing configuration, leading to potential issues with request handling and data integrity.
Awaiting Verification
Be the first to verify this fix
- 1
Update API Gateway Configuration
Modify the serverless configuration to include a validation property under the http event for the desired function. This will enable request validation based on the defined models.
yamlevents: - http: path: /your-endpoint method: post request: schema: application/json: type: object properties: key: type: string required: - key validation: request: validateRequestBody: true - 2
Define Request Models
Create JSON schema models that define the structure of the expected request body. This schema will be used by API Gateway to validate incoming requests.
json{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "key": { "type": "string" } }, "required": ["key"] } - 3
Deploy Changes
Deploy the updated configuration to AWS using the serverless framework or AWS CLI to apply the new request validation settings.
bashserverless deploy - 4
Test Request Validation
Send test requests to the API endpoint with both valid and invalid payloads to ensure that the request validation is functioning as expected. Monitor the responses for validation errors.
bashcurl -X POST https://your-api-endpoint/your-endpoint -H 'Content-Type: application/json' -d '{"key": "value"}' curl -X POST https://your-api-endpoint/your-endpoint -H 'Content-Type: application/json' -d '{"invalidKey": "value"}'
Validation
Confirm that valid requests are processed successfully while invalid requests return appropriate validation error messages. Check the API Gateway logs for any validation errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep