Skip to content

Feature request: extend API Gateway schemas with custom schema in body rather than string #2844

Closed
@julbrs

Description

@julbrs

Expected Behavior

I expect the parsing event document to work: https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parser/#parse-events

Current Behavior

When I implement such code, I ended with the error:

ParseError: Failed to parse schema.

Because no envelope was set.

Code snippet

My failing code:

import { newNetsuiteUserSchema } from '@/packages/core/model/netsuite';

import middy from '@middy/core';
import { parser } from '@aws-lambda-powertools/parser/middleware';
import { APIGatewayProxyEventV2Schema } from '@aws-lambda-powertools/parser/schemas';
import { z } from 'zod';

export const baseHandler = async (event: z.infer<typeof newNetsuiteUserSchema>) => {
  return {
    statusCode: 200,
    body: JSON.stringify(event),
  };
};

export const handler = middy(baseHandler).use(parser({ schema: newNetsuiteUserSchema }));

with:

export const newNetsuiteUserSchema = z.object({
  firstName: z.string().min(1),
  lastName: z.string().min(1),
  email: z.string().email().min(1),
  role: z.nativeEnum(UserRoles),
  custentity_lba_customer_center_access: z.string().min(1),
});

Steps to Reproduce

I am using SST (sst.dev) to build my project.

Possible Solution

I think it's mandatory to specify the envelope like that:

import { newNetsuiteUserSchema } from '@/packages/core/model/netsuite';

import middy from '@middy/core';
import { parser } from '@aws-lambda-powertools/parser/middleware';
import { APIGatewayProxyEventV2Schema } from '@aws-lambda-powertools/parser/schemas';
import { z } from 'zod';
import { ApiGatewayV2Envelope } from '@aws-lambda-powertools/parser/envelopes';

export const baseHandler = async (event: z.infer<typeof newNetsuiteUserSchema>) => {
  return {
    statusCode: 200,
    body: JSON.stringify(event),
  };
};

export const handler = middy(baseHandler).use(
  parser({ schema: newNetsuiteUserSchema, envelope: ApiGatewayV2Envelope })
);

Powertools for AWS Lambda (TypeScript) version

latest (2.5.0-beta)

AWS Lambda function runtime

20.x

Packaging format used

npm

Execution logs

},
     {
       "expected": "'Admin' | 'User'",
       "received": "undefined",
       "code": "invalid_type",
       "path": [
         "role"
       ],
       "message": "Required"
     },
     {
       "code": "invalid_type",
       "expected": "string",
       "received": "undefined",
       "path": [
         "custentity_lba_customer_center_access"
       ],
       "message": "Required"
     }
   ].
       at parse (/Users/julien/work/lab/customer-center/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@aws-lambda-powertools/parser/lib/esm/parser.js:41:15)
       at before (/Users/julien/work/lab/customer-center/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@aws-lambda-powertools/parser/lib/esm/middleware/parser.js:33:25)
       at runMiddlewares (/Users/julien/work/lab/customer-center/node_modules/.pnpm/@[email protected]/node_modules/@middy/core/index.js:230:23)
       at runRequest (/Users/julien/work/lab/customer-center/node_modules/.pnpm/@[email protected]/node_modules/@middy/core/index.js:154:11)
       at middyHandler (/Users/julien/work/lab/customer-center/node_modules/.pnpm/@[email protected]/node_modules/@middy/core/index.js:43:12)
       at file:///Users/julien/work/lab/customer-center/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]__aws-c_gdj3g64c23iicrgcjjtaychpzi/node_modules/sst/support/nodejs-runtime/index.mjs:148:22
       at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilitynot-a-bugNew and existing bug reports incorrectly submitted as bugparserThis item relates to the Parser Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions