I'm trying to get started with Middy to validate input schema on AWS lambda.

The lambda function below keeps throwing this error:

Middleware must be an object containing at least one key among "before", "after", "onError"

Anyone knows what could be the issue?

I'm running

     Node v18.12.1
    "@middy/core": "^4.0.2",
    "@middy/http-error-handler": "^4.0.2",
    "@middy/http-json-body-parser": "^4.0.2",
    "@middy/validator": "^4.0.2",

here is the lambda code

import middy from "@middy/core";
import validator from "@middy/validator";
import httpErrorHandler from "@middy/http-error-handler";
import jsonBodyParser from "@middy/http-json-body-parser";


const baseHandler = (event) => {
    const { fname, lname } = event.body;
    return {
      statusCode: 200,
      headers: { "Content-Type": "text/plain" },
      body: `Hello, ${fname}-${lname}.`,
    };
  };

const inputSchema = {
    type: "object",
    properties: {
      body: {
        type: "object",
        properties: {
          fname: { type: "string" },
          lname: { type: "string" },
        },
        required: ["fname", "lname"],
      },
    },
  };

const handler = middy(baseHandler)
    .use(jsonBodyParser())
    .use(
        validator({
        inputSchema,
        })
    )
    .use(httpErrorHandler());

export { handler };

I expected that the validator throws an error when the input parameters are not provided to the function

1

There are 1 best solutions below

0
On

thanks Will Farrell

Change inputSchema to eventSchema. The name was changed in v3, and fully removed in v4.

See: https://middy.js.org/docs/upgrade/3-4

view it on GitHub