Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including validate-express with all npm packages installed. Try it out:

var validateExpress = require("validate-express")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

validate-express v1.0.0

Express validation library/utility using joi

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.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free