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

validate-express

v1.0.0

Published

Express validation library/utility using joi

Downloads

3

Readme

NPM version Build Status Dependency Status DevDependencies Status

validate-express

A thin middleware for express applications that handles validation.

The code is pretty minimal and only depends on the joi package for the schema definition and validation logic.

After successful validation, the request object is modified to contain the validated/updated values.

Installing

npm install --save validate-express

Example


var express    = require('express'),
    bodyParser = require('body-parser'),
    validate   = require('validate-express'),
    joi        = require('joi');

var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

var validation = {
    login : {
        post: {
            body: {
                identification: joi.string().required(),
                password      : joi.string().required()
            }
        }
    },
    search: {
        post: {
            body: {
                q    : joi.string().required(),
                to   : joi.date().default(function () {
                    return new Date();
                }, 'to date'),
                limit: joi.number().max(20).min(10).default(10)
            },
            before: {
                // demonstrates providing a custom transform that is called prior to validation
                to: function (value) {
                    return value === 'now' ? new Date() : value;
                }
            },
            after : {
                // demonstrates providing a custom transform that is called after successful validation
                q: function (value) {
                    return value.toUpperCase();
                }
            }
        }
    },
    me    : {
        get: {
            headers: {
                apikey      : joi.string().required(),
                'user-agent': joi.string().required().regex(/chrome/)
            }
        }
    }
};

app.post('/login', validate(validation.login.post), function (req, res) {
    // ...
});

app.post('/search', validate(validation.search.post), function (req, res) {
    // ...
    // NOTE - req.body will contain the validated/udpated values
    // e.g. if the user did not provide 'to' or 'limit' fields in the request, they now exist
    // ALSO, if the user specified to==='now', then a new Date() will be created via the transform
});

app.get('/me', validate(validation.me.get), function (req, res) {
    // ...
});

// NOTE: don't forget to provide error middleware
app.use(function (err, req, res, next) {
    if (err.name === 'ValidationError') {
        res.status(err.status).json(err);
    }
});

License

validate-express is released under the MIT license.