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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-express-guard

v1.0.7

Published

Zod middleware generator for type safe request validation

Downloads

2

Readme

zod-express-guard

Small package intended to use zod to make express request type-safe.



Installation

Just install with your favorite package manager:

npm i zod-express-guard

Zod, Express and @types/express are peer dependencies, so you can change versions without having to wait for me to update this library. Newer versions of npm resolve these by default, but you can do it manually like this:

npm i -D @types/express && npm i express zod

Usage

This library exposes four validation functions, and three of them are shortcuts that end up calling the first one.

All functions support async middlewares and will catch async errors.

validate

This is the main function. It receives an object with the Zod schemas for what you want to validate, together with the middleware you want to guard. If validation passes, your middleware will be called with type-safe req properties. If validation fails, next will be called containg and instance of ZodError

Example:

// routes/login.ts
import z from 'zod'
import { validate } from 'zod-express-guard'

export default validate(
  {
    body: z.object({
      login: z.string().nonempty(),
      password: z.string().nonempty()
    })
  },
  (req, res) => {
    // req.body has the type { login: string, password: string }
    // req.query and req.params have type unknown, since they were not validated
    res.status(200).json({ message: 'Validation passed' })
  }
)

You can pass up to three properties to the first parameter: body, query and params, each one validates its respective property inside req

validateBody, validateQuery and validateParams

These three functions serve as shortcuts when you only want to validate a single property inside req. This is useful for our login middleware in the previous example:

// routes/login.ts

import z from 'zod'
import { validateBody } from 'zod-express-guard'

export default validateBody(
  z.object({
    login: z.string().nonempty(),
    password: z.string().nonempty()
  }),
  (req, res) => {
    // req.body has the type { login: string, password: string }
    // req.query and req.params have type unknown, since they were not validated
    res.status(200).json({ message: 'Validation passed' })
  }
)

validateQuery and validateParams work similarly, but for req.query and req.params, respectively.

Note that, when using a shortcut function, only one of the three properties will be validated and made acessible, and the other two will have type unknown

Contributing

Environment setup

Clone this and run npm install on the root.

Making changes

Please make sure you modify the tests to reflect any changes you add.

Use npx gitmoji -c to commit, and follow existing commit patterns

Running tests

Simply run npm test after running npm install.

The package.json already specifies a pretest script that will run a clean build before testing.