Bug: APIGatewayProxyEventSchema incorrect schemas #2461
Description
Expected Behavior
Valid API Gateway Proxy request (REST API) with no query string parameters and no body should pass validation when using Powertools Parser and validating against the entire APIGatewayProxyEventSchema
Current Behavior
Valid API Gateway Proxy request (REST API) with no query string parameters and no body is failling validation when using Powertools Parser and validating against the entire APIGatewayProxyEventSchema
Code snippet
import type { Context } from 'aws-lambda';
import type { APIGatewayProxyEvent } from '@aws-lambda-powertools/parser/types';
import { parser } from '@aws-lambda-powertools/parser/middleware';
import { APIGatewayProxyEventSchema } from '@aws-lambda-powertools/parser/schemas';
import middy from '@middy/core';
import { Logger } from '@aws-lambda-powertools/logger';
const logger = new Logger();
const lambdaHandler = async (event: APIGatewayProxyEvent, _context: Context): Promise<void> => {
logger.info('Event', { event });
};
export const handler = middy(lambdaHandler).use(
parser({ schema: APIGatewayProxyEventSchema })
);
Steps to Reproduce
- Create a Lambda handler that uses Powertools Parser middy middleware and passes the default APIGatewayProxyEventSchema from Powertools Parser package as the schema to validate
- Configure the Lambda as a proxy integration endpoint from API Gateway v1 (REST API)
- Hit the endpoint with any valid request that doesn't contain query string parameters or a request body
- Observe Parser/Zod error for invalid fields: queryStringParameters, multiValueQueryStringParameters, and body
Possible Solution
Change the schema to make body, queryStringParameters, and multiValueQueryStringParameters nullable.
This seems to be consistent with @types/aws-lambda and the AWS docs.
These are also the values I have been getting in practice when making actual requests to Lambda and inspecting the event.
I'm happy to submit a PR with these changes if this solution seems reasonable.
Powertools for AWS Lambda (TypeScript) version
2.1.0-beta
AWS Lambda function runtime
18.x
Packaging format used
npm
Execution logs
The parser error
[
{
"code": "invalid_type",
"expected": "object",
"received": "null",
"path": [
"queryStringParameters"
],
"message": "Expected object, received null"
},
{
"code": "invalid_type",
"expected": "object",
"received": "null",
"path": [
"multiValueQueryStringParameters"
],
"message": "Expected object, received null"
},
{
"code": "invalid_type",
"expected": "string",
"received": "null",
"path": [
"body"
],
"message": "Expected string, received null"
}
]
Metadata
Assignees
Labels
Type
Projects
Status
Shipped
Activity