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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dozyhermit/nemo-zod-validators

v2.0.3

Published

A collection of middleware validators made with zod and @rescale/nemo for Next.js

Downloads

25

Readme

nemo-zod-validators

@rescale/nemo and zod saved my life when working with Next.js, but writing the same validation functions over and over was driving me crazy.

So, this just makes adding validation even easier.

Install

npm i @dozyhermit/nemo-zod-validators

Upgrade

If your project uses older versions of @rescale/nemo or zod, please upgrade your project schemas and middleware before upgrading @dozyhermit/nemo-zod-validators.

See:

  • @rescale/nemo: https://nemo.zanreal.com/docs
  • zod: https://zod.dev

Then you can upgrade @dozyhermit/nemo-zod-validators to the latest version.

Note: If you find that your schemas aren't working as robustly as before, double check you have correctly migrated your zod schemas.

Usage

In your middleware.ts Next.js project file:

import { createNEMO } from '@rescale/nemo';
import { z } from 'zod';
import { validatePath } from '@dozyhermit/nemo-zod-validators';

const schema = z.object({
  world: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

const middlewares = {
  '/api/hello/:world': [validatePath<SchemaType>({ schema })],
};

export const middleware = createNEMO(middlewares);

Functions

validateGeneric

Validates data against a zod schema using safeParse. This is the base for all the schema based validators in this package.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validateGeneric<SchemaType>({ data: { hello: 'World' }, schema });

Creating Your Own Validator

This couldn't be simpler, but the only thing you have to remember is how to prevent validateGeneric from executing immediately.

For example, let's create a validateCustom validator:

type ValidateCustom = ValidateSchema;

export const validateCustom = <T>({ schema }: ValidateCustom) => {
  return async (request: NextRequest, _event: NemoEvent) =>
    validateGeneric<T>({
      data: request.body as T,
      schema,
    });
};

In the above, we return a function because when we use it like below:

const middlewares = {
  '/api/hello/world': [validateCustom<SchemaType>({ schema })],
};

it means validateGeneric is not immediately executed when the api starts; it will only ever run when we make a request to /api/hello/world.

validateBody

Validates the request.json() function inside a NextRequest request using zod.safeParse.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validateBody<SchemaType>({ schema });

validateCookies

Validates the request.cookies object inside a NextRequest request using zod.safeParse.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validateCookies<SchemaType>({ schema });

validateEquals

Validates a local variable against a variable inside a NextRequest request using the === operator.

The return type can be either Response or NextResponse.next().

Example

const value = process.env.MY_ENV_VAR;
const transform = (request) => request.headers.get('MY_HEADER');

validateEquals<string | null>({ value, transform });

validateHeaders

Validates the request.headers tuple inside a NextRequest request using zod.safeParse.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validateHeaders<SchemaType>({ schema });

validateParams

Deprecated. Please use validatePath instead.

validateParams and validatePath are exactly the same function, but it has been renamed to be more appropriate.

Feel free to keep using it, as it likely won't ever be removed.

validatePath

Validates the params object provided by NemoEvent using zod.safeParse.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validatePath<SchemaType>({ schema });

validateQuery

Validates request.nextUrl.searchParams (URLSearchParams) inside a NextRequest request using zod.safeParse.

The return type can be either Response or NextResponse.next().

Example

const schema = z.object({
  hello: z.string().regex(/[0-9]+/, { error: 'Invalid string' }),
});

type SchemaType = z.infer<typeof Schema>;

validateQuery<SchemaType>({ schema });