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

@akrdevtech/lib-express-joi-validation-middleware

v1.1.0

Published

Joi based validation middleware for ExpressJS

Downloads

17

Readme

Express Joi Validation Middleware

Description

Implementation of Joi middleware for ExpressJS with TS.

  • TypeScript support.
  • Specify the order in which request inputs are validated.

Quick Links

Usage

Install

npm i @akrdevtech/lib-express-joi-validation-middleware

Peer Dependency

npm i express

Example Usage (TypeScript)

Validate body,query,cookies,headers&params at once using . Each of these may be optional as well.

import * as Joi from 'joi'
import * as express from 'express'
import { RequestValidator } from '@akrdevtech/lib-express-joi-validation-middleware';
const { validateAll } = new RequestValidator({ abortEarly: false }); // parameters of constructor is optional

const app = express()

const validateAllSchema: IValidateAllSchema = {
    body: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    query: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    cookies: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    headers: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    params: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
}

app.get('/', [
  validateAll(validateAllSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

// with joi validation options
app.get('/with-joi-validation-option', [
  validateAll(validateAllSchema,{ allowUnknown:true }),
  (req, res) => { res.send(`Hello World!`) }
]);

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Example Usage (JavaScript)

const Joi = require('joi')
const app = require('express')()
const { RequestValidator } = require('@akrdevtech/lib-express-joi-validation-middleware');

const {
    validateAll,
    validateBody,
    validateCookies,
    validateHeaders,
    validateQuery,
    validateParams
} = RequestValidator;

const validateAllSchema: IValidateAllSchema = {
    body: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    query: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    cookies: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    headers: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
    params: Joi.object({
        someField: Joi.string().min(3).required(),
    }),
}
const headerSchema = Joi.object({ someField: Joi.string().required() });
const bodySchema = Joi.object({ someField: Joi.string().required() });
const querySchema= Joi.object({ someField: Joi.string().required() });
const cookieSchema= Joi.object({ someField: Joi.string().required() });
const paramSchema= Joi.object({ someField: Joi.string().required() });

app.get('/', [
  validateAll(validateAllSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

app.get('/separately', [
  validateQuery(querySchema),
  validateBody(bodySchema),
  validateCookies(cookieSchema),
  validateHeaders(headerSchema),
  validateParams(paramSchema),
  (req, res) => { res.send(`Hello World!`) }
]);

const port = 8000;
app.listen(port, () => {console.log(`⚡️ Service started : PORT → ${port}}`);

Behaviours

Validation Ordering

Validation can be performed in a specific order using standard express middleware behaviour. Pass the middleware in the desired order.

Here's an example where the order is headers, body, query:

const headerSchema = Joi.object({ someField: Joi.string().required() });
const bodySchema = Joi.object({ someField: Joi.string().required() });
const querySchema= Joi.object({ someField: Joi.string().required() });

route.get('/', [
  validateHeaders(headerSchema),
  validateBody(bodySchema),
  validateQuery(querySchema),
  routeHandler
]);

Validation Options

Validation options can be extented with Joi.ValidationOptions.

Here’s an example where the order is headers, body, query:

const bodySchema = Joi.object({ someField: Joi.string().required() });

const options = {
  abortEarly: false,
  allowUnknown: true,
}

route.get('/', [
  validateBody(bodySchema, options),
  routeHandler
]);