Reputation: 21
I'm validating the payload of a request via a model - see serverless.yml extract below:
functions:
authorizer:
handler: src/authorization/authorizer.general
activity:
handler: src/resources/activity.submit
events:
- http:
path: /tenant/{tenant}/activities
method: POST
cors: true
authorizer: ${self:custom.authorizer.general}
request:
schema:
application/json: ${file(models/activity.json)
All works as expected, but the validation response for a payload that does not match the schema is:
{
"message": "Invalid request body"
}
I've tried adding a response template but get a warning that 'response' will be removed (this is a lambda proxy integration, not a lambda integration).
How do I get API Gateway to give a detailed validation error message response in this scenario (i.e. a Lambda Proxy integration)?
Any help would be much appreciated as I can't find anything relevant online.
Upvotes: 1
Views: 880
Reputation: 21
I found a solution to this by adding a Gateway Response to override the default Bad Request Body response. Unfortunately, as of Serverless 1.50.0 this is not directly supported (see github issue
But was able to add the Gateway Response via a Serverless resource i.e. Cloudformation (see above link):
resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:provider.stage}-${self:service}
GatewayResponseResourceNotFound:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
RestApiId:
Ref: 'ApiGatewayRestApi'
ResponseType: BAD_REQUEST_BODY
"StatusCode" : "422"
ResponseTemplates:
application/json: "{\"message\": \"$context.error.message\", \"error\": \"$context.error.validationErrorString\"}"
Note: I was not able to find a way to indicate which property failed validation, but at least the validation message is a little more detailed
Upvotes: 1