npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

middy-kneel-before-zod

v1.1.2

Published

An input and output validator middleware using Zod for Middy

Readme

Kneel Before Zod

An input and output validator middleware for zod using middy

This middleware automatically validates the inputs and output of events in AWS Lambda. It currently handles input in the request body, query string parameters, and path parameters, and output in the response body.

If an incoming event fails validation a BadRequest error is raised. If an outgoing response fails validation a InternalServerError error is raised. Optional user callbacks can be provided if you would prefer to raise a custom error instead. This middleware can be used in combination with httpErrorHandler to automatically return this response to the user.

When using http request body input, it is recommended that a body parser middleware (like http-json-body-parser) is used to parse the string input into the correct object type. Make sure you put it in the middy .use chain before this middleware.

Similarly, if using a body output with an API Gateway event, you'll need to make sure that the output is stringified before being sent out. A middleware like http-response-serializer works nicely for this. It also needs to be included in the middy .use chain before this middleware. (Because the after actions are processed in the opposite order of the before actions.)

Configuration

The middleware is configured by passing in a configuration object. All of the properties on the configuration object are optional. The properties are:

Four schema types: (zod schemas created from z.object())

  • inputBodySchema
  • inputPathParametersSchema
  • inputQueryStringParametersSchema
  • outputBodySchema

And two callback handler functions:

  • inputErrorHandler(error: z.ZodError): void
  • outputErrorHandler(error: z.ZodError): void

Sample usage

import middy from '@middy/core';
import middyJsonBodyParser from '@middy/http-json-body-parser';
import httpResponseSerializer from '@middy/http-response-serializer';
import { middyZodValidator } from 'middy-zod-validator';
import { z } from 'zod';

const handler = middy((event, context) => {
    return {};
});

const personSchema = z.object({
    firstName: z.string(),
    lastName: z.string(),
    age: z.number(),
});

const responseSchema = z.object({
    message: string,
});

handler
    .use(middyJsonBodyParser())
    .use(httpErrorHandler())
    .use(
        httpResponseSerializer({
            serializers: [
                {
                    regex: /^application\/json$/,
                    serializer: ({ body }) => JSON.stringify(body),
                },
            ],
            default: 'application/json',
        }),
    )
    .use(
        middyZodValidator({
            inputBodySchema: person,
            outputBodySchema: responseSchema,
        }),
    );

// The types to use in your event code would be something like:
// type Person = z.infer<typeof personSchema>;
// type Response = z.infer<typeof responseSchema>;

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.