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

express-yup

v1.0.0

Published

`express-yup` is a super light-weight Express [middleware](https://expressjs.com/en/guide/using-middleware.html#using-middleware) to easily validate the Express [Request object](https://expressjs.com/en/4x/api.html#req) against a [Yup](https://www.npmjs.c

Downloads

119

Readme

Express Yup

express-yup is a super light-weight Express middleware to easily validate the Express Request object against a Yup schema.

Yup is a leaner alternative to Joi, that also supports client-side validation and is the reason why this middleware was created in the first place - to easily share schemas between the client- and server-side.

If Joi is more your thing, then rather use the excellent express-validation library.

Installation

express-yup has two peer-dependencies that you need to ensure is installed first: express and yup.

Install from NPM:

npm install express yup # or yarn add express yup

If you are using TypeScript, you might want to install their type definition files as well:

npm install -D @types/express @types/yup # or yarn add -D @types/express @types/yup

and then install express-yup (it comes with its TypeScript types):

npm install express-yup # or yarn add express-yup

Usage

Add as middleware to your Express app as a whole if you want to validate all routes against a specific schema, or add individually to each route (more likely):

import express, { NextFunction, Request, Response } from 'express'
import bodyParser from 'body-parser'
import * as yup from 'yup'
import { validate } from 'express-yup'

const app = express()

// Add whatever other middleware you need for your Express app
app.use(bodyParser.json())

// Example global `express-yup` validate middleware
const authHeaderSchema = yup.object().shape({
  headers: yup.object().shape({
    authorization: yup
      .string()
      .matches(/^Bearer\s\w+$/)
      .required(),
  }),
})

app.use(validate(authHeaderSchema))

// Example route-level middleware
const routeSchema = yup.object().shape({
  // notice that we are setting our schema to validate the `body` object here
  body: yup.object().shape({
    hello: yup.string().required(),
    bye: yup.string().required(),
  }),
})

app.post(
  '/some-route',
  validate(routeSchema),
  (req: Request, res: Response) => {
    // `hello` and `bye` guaranteed to be string properties on `req.body`
    const { hello, bye } = req.body

    // Do what you need from here
  }
)

// Global error middleware
app.use((error: Error, req: Request, res: Response, next: NextFunction) => {
  if (error instanceof yup.ValidationError) {
    res.status(400).json({ message: error.message }) // status code is 400 by default
    return
  }

  res.status(500).json({ message: 'Internal Server Error' })
})

Licence

MIT