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-request-modeler

v1.0.8

Published

Allows developers to check and validate incoming requests via a simple object model

Downloads

8

Readme

express-request-modeler

 

Summary

This module allows you to validate express requests without the hassle of re-writing validation code for every route in your API.

 

About

Express-request-modeler was born in mid-winter 2018 when I was doing web-APIs for the first time in my life. I got sick and tired of copy-pasting slightly different versions of the same code in to every post request, but every route was just different enough that I had no other choice. I ultimately made the precursor to this module (Express-Request-Checker; not to be confused with pastgift's npm module of the same name). That being said anyone who isn't an absolute newbie could see that my module was a horrifying mess. Now that it's mid-winter 2020 I'm coming back to writing web-APIs and have run into the same problem, so now with experience and hindsight on my side I've decided to create this improved module for me and others who need a simple and easy to implement request validator/modeler.

 

How to Use

Express-request-modeler is an Express middleware meant to be implemented at the router level. Below you can find a simple example.

const app = require('express')();
app.use(json());
const reqModel = require('express-request-modeler');

app.route('/message')
  .post(
    [
      reqModel({
        body: {
          message: {
            rcRequired: true,
            rcType: 'string'
          }
        }
      })
    ], 
    (req, res) => {
      //...push message to database...
    });

In the example above the request will never hit the (req, res) => {} function below reqModel unless there is a field called message within the body whos value is has a type of string.

 

Validation Options

Below you can find a list of different options for validation.

| Key Name | Expected Input | About | | ------------- | ------------- | ------------- | | rcRequired | <boolean>: true/false | If set to true the request MUST contain this key-value pair or the request bounces. | | rcType | <string>: string, number, boolean, object, array | If the parent key is present in the request and the parent key's value type does not match rcType the request will bounce. | | rcMatching | <string, number, boolean>: ANY VALUE | If the parent key is present in the request and its value does not match rcMatching the request will bounce. | | rcFunc | <function> | If the parent key is present in the request the function provided to rcFunc will run on the value to decide if it's valid or not. The user-provided function is expected to return true or false (if you don't your requests will start to hang). Currently doesn't work with async functions. |

 

Advanced Example

const app = require('express')();
const reqModel = require('express-request-modeler');

app.route('/message')
  .post(
    [
      reqModel({
        headers: {
          'content-type': {
            rcMatching: 'application/json',
          }
        },

        body: {
          message: {
            rcRequired: true,
            rcType: 'string',
            rcFunc: (val) => {
              if (val.length < 220) {
                return true;
              }
              return false;
            },
            rcRejectStatus: '400',
            rcRejectMessage: 'Your message is too long!',
          }
        }
      })
    ], 
    (req, res) => {
      //...push message to database...
		});

 

Planned features

  • Allow a custom reject message for every rejection type.
  • Suppress detailed rejection messages.
  • An alternative to rcFunc that can mutate the request value.
  • Allow users to change the order in which validation checks occur.
  • Allow rcFunc to be an Async function (so that it can be used for things like validating user sessions or querying a database for validation.)