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

node-body-validator

v1.0.4

Published

Powerful promise based validator module to efficiently validate node body params

Downloads

6

Readme

Node body validator

Makes parameters validation in Node.js a breeze. With promise chaining, you validate incoming requests straight away, data flows between controllers and models and errored requests are efficiently handled.

As an exemple, let's validate a login request.

// /controllers/userController

const RequestValidator = require('node-body-validator');
const handleError = require('../lib/handleError');
const userModel = require('../models/userModel');

const reqValidator = new RequestValidator('form');

exports.register = function (req, res) {
    reqValidator.validate(req, [
        { name: 'email', type: 'string', validator: v => v.pattern(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,6}$/i), failMsg: 'email must be an email string' },
        { name: 'password', type: 'string', validator: v => v.minLength(8).maxLength(50), failMsg: 'password must be comprised between 8 and 50 chars' }
    ])
    then(
        post => userModel.create(post.email, post.password)
        .then(([id, token]) => res.status(201).json({ id, token }));
    )
    .catch(err => handleError(err, res));
};

Simple, expressive and beautifully powerful, right? Data flows thoughout your app.

Convinced? Let's explore all the possibilities in the next section.

Installation & usage

The module requires at least Node V6, appart from this, its install procedure is boringly conventional.

npm install --save node-body-validator

This validator is the pure Node.js version of the express-body-validator. The only difference between the them is that node-body-validator handles raw request object whereas with Express.js, bodyParser has taken care of the parsing.

Hence, this validator takes two parameters on instanciation:

  • contentType that can be either form for application/x-www-form-urlencoded or json for application/json (you can also pass full value if you whish).
  • maxBodySize is the maximum authorized size of the body in bytes. It defaults to 1MB. Promise is rejected if request is bigger than that.
const RequestValidator = require('node-body-validator');
const reqValidator = new RequestValidator('json', 10e6); // JSON encoding with body up to 10MB

Apart from that, the API is exactly the same as the other module. Check out the docs there.

Contributing

There's sure room for improvement, so feel free to hack around and submit PRs! Please just follow the style of the existing code, which is Airbnb's style with minor modifications.

To maintain things clear and visual, please follow the git commit template.