- Newest
- Most votes
- Most comments
Extracting the specific field name from the validation error message in API Gateway's error template is not directly supported using the default error message. The validation error message provided by API Gateway does not consistently include the field name in a easily parseable format.
However, you can customize the error response to provide more structured information about the validation error. Here's an approach you can consider:
-
Use the Gateway Response Object to customize the BAD_REQUEST_BODY response.
-
In your API Gateway configuration, you can set up a custom error response for validation errors. This can be done using the GatewayResponse of type BAD_REQUEST_BODY.
-
In the response template, you can use the $context.error.validationErrorString variable, which contains the detailed validation error message.
-
While you can't easily extract the specific field name, you can provide the entire validation error message in a structured format.
Here's an example of how you might structure your custom error response:
{ "message": "Validation failed", "details": $util.escapeJavaScript($context.error.validationErrorString), "code": "INVALID_INPUT" }
This approach doesn't extract the specific field name, but it does provide the full validation error message in a structured format. The $util.escapeJavaScript() function is used to properly escape the error message for JSON.
If you absolutely need to extract the specific field name, you would likely need to implement this logic at the Lambda function level, where you have more control and can use a full-featured programming language to parse the error message.
Remember that the exact format and content of the validation error message can vary depending on the type of validation failure, so any parsing logic would need to be robust and able to handle different error message formats.
Sources
Customized response example for AWS SAM - AWS Serverless Application Model
Variables for data transformations for API Gateway - Amazon API Gateway
Relevant content
- asked 3 years ago