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-joi-validations

v0.1.0

Published

Express middleware to validate request (headers, params, query, body) using Joi

Downloads

5,359,313

Readme

express-joi-validations

Express middleware to validate request (headers, params, query, body) using Joi

Installation

npm i express-joi-validations

Usage

Out of the box

import validate from 'express-joi-validations';

router.put('/posts/:id', validate({ headers: userToken, params: postId, body: postBody }), (request, response) => {
  // Validation errors will be in request.validationErrors
  // Validation values will be in request.validationValues
});

With custom settings

import express from 'express';
import { expressJoiValidations } from 'express-joi-validations';

const app = express();
app.use(expressJoiValidations({ validationConfigs }));

Configuration

  • throwErrors (default: false): Define if errors should be thrown (suggested usage with express-async-errors)

  • overwriteRequest (default: false): Define if original request should be overwritten with validated data.

Helper methods

validateBody

import { Joi, validateBody } from 'express-joi-validations';

// POST /posts
// body: { title: "Lorem ipsum", content: "Lorem ipsum dolor sit amet" }

const postBody = Joi.object({
  title: Joi.string().required().trim(),
  content: Joi.string().required().trim().min(10),
});

router.post('/posts', validateBody(postBody), postsController.create);

validateParams

import { Joi, validateParams } from 'express-joi-validations';

// GET /posts/507f1f77bcf86cd799439011

const postId = Joi.object({
  id: Joi.string().hex().length(24),
});

router.get('/posts/:id', validateParams(postId), postsController.detail);

validateQuery

import { Joi, validateQuery } from 'express-joi-validations';

// GET /posts?page=2

const postQuery = Joi.object({
  page: Joi.optional().number().integer().min(1).default(1),
});

router.get('/posts', validateQuery(postQuery), postsController.list);

validateHeaders

import { Joi, validateHeaders } from 'express-joi-validations';
// DELETE /posts/507f1f77bcf86cd799439011
// headers: { Authorization: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" }

const userToken = Joi.object({
  authorization: Joi.string().regex(/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_.+/=]*$/),
});

router.delete('/posts/:id', validateHeaders(userToken), postsController.remove);
Chain
import { validateHeaders, validateParams, validateBody } from 'express-joi-validations';

router.put('/posts/:id', validateHeaders(userToken), validateParams(postId), validateBody(postBody), postsController.update);
Joi options

To every function, is possible to add a second parameter to specify custom Joi Options:

router.post('/posts', validateBody(postBody, { allowUnknown: true }), postsController.create);