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-requirements

v1.0.2

Published

An Express middleware to validate the data coming into your NodeJS API.

Downloads

13

Readme

express-requirements Circle CI

Build Status Coverage Status

An express.js middleware to validate whatever comes into your NodeJS API. It works with node-validator.

Installation

npm install express-requirements

Usage

Our project

Let's admit that we have the following files tree :

|-- app.js
|-- routes
    |-- route.js
    |-- route.req.js

What is route.req.js ?

This file contains all of the requirements for one or multiple route(s). It is up to you.

You can rename this file as long as you keep the .req.js extension. Here it is an example of requirements file :

module.exports = {
    // Here you give a name to your route requirements
    my_route: {
        // Param firstName is required and must be alpha
        firstName: {
            required: true,
            isAlpha: true
        },
        // ...
    }
}

How to apply my requirements ?

First of all, let's create a basic NodeJS server like that :

// app.js
var express = require('express');
var app = express();

var bodyParser = require('body-parser');
var requirements = require('express-requirements'); // Require the module

app.use(bodyParser.json());

// Give the requirements root folder to the module
// Usually, you can use the routes root folder
app.use(requirements.use(__dirname + '/routes'));

app.use('/', require('./routes/route'));

app.listen(8888);

Finally, go to your route.js file to create a very basic route :

// route.js
var express = require('express');
var router = express.Router();
var requirements = require('express-requirements');

router.get('/test', requirements.validate('route.my_route'), function(req, res, next) {
  return res.status(200).json({
    success: true
  });
});

That's it ! Now, everytime the /test route will be call, express-requirements will validate fields according to the .req.js file.

What if I have another files tree ?

Let's change our files tree a little :

|-- app.js
|-- routes
    |-- route.js
    |-- requirements
        |-- route
            |-- main.req.js

The only thing which is going to change is the route.js file :

// route.js
var express = require('express');
// ...

router.get('/test', requirements.validate('requirements.route.main.my_route'), function(req, res, next) {
  // ...
});

When you specify the requirements file to use, the element located after the last point is always the name of your route inside the .req.js file.

In a nutshell: you can organize your files tree as you want.

Syntax

Basic

my_route: {
    firstName: {
        required: true, // Default error message will be 'missing_firstName_parameter'
        isAlpha: true, // Default error message will be 'bad_request'
        // ...
    }
}

Custom error message/code

my_route: {
    firstName: {
        required: {
            errorMessage: 'You must enter a firstName'
            // Default code is 400
        },
        isAlpha: {
            errorMessage: 'Only alpha in your %@', // Use %@ to retrieve the property name (here it is 'firstName')
            errorCode: 403
        },
        // ...
    }
}

Scopes

my_route: {
    // Only checks into the headers scope
    _headers: {
        'Authorization': { required: true }
        'Content-Type': { required: true }
    },
    // Only checks into the params scope
    _params: {
        id: { required: true, notEmpty: true }
    },
    // Only checks into the body scope
    _body: {
        username: { required: true, notEmpty: true }
    }
}

Inheritance/Override

my_route: {
    firstName: { required: true, notEmpty: true, isAlpha: true },
    lastName: {
        required: true,
        isAlpha: { errorMessage: '%@ must be alpha' }
    }
},

other_route: {
    firstName: { _inheritFrom: 'my_route' }, // Now firstName has 'notEmpty' and 'isAlpha' as requirements
    lastName: {
        _inheritFrom: 'my_route',
        isAlpha: {  errorMessage: 'Error message overriden for %@' }, // You can override any requirement component ...
        notEmpty: true // ... and even add new one
    }
}

Note that for any inheritance, required is never included. You have to add it to the inherited route by yourself.

Validator parameters

my_route: {
    firstName: { required: true, isAlpha: true },
    phoneNumber: {
        matches: {
            _parameter: '[0-9]{3}\\-[0-9]{3}\\-[0-9]{4}', // Use _parameter to pass any parameter to a validator
            errorMessage: 'wrong_%@_format'
        }
    }
}

Validators

You can use all the validators which compose node-validator. You also have these :

isArray

isArray: true
isArray: {
    notEmpty: { errorMessage: 'error', errorCode: 400 }
    content: { // Check the content of the array
        isAlpha: { errorMessage: 'must_be_alpha' },
        // You can add other validators for the array content
    }
    
    // Usual parameters
    errorMessage: 'error',
    errorCode: 400
}

Don't use any other validator than notEmpty, it won't work.

notEmpty

notEmpty: true
notEmpty: {
    errorMessage: 'error',
    errorCode: 400
}

What's next ?

  • [ ] Posibility to add your own validators
  • [ ] Add unit tests for the custom options