# express-request-modeler

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

Latest version **1.0.8** (published 2020-12-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-request-modeler
pnpm add express-request-modeler
yarn add express-request-modeler
bun add express-request-modeler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2020-12-15 |
| First published | 2020-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | David J. Davydov |
| Maintainers | daviddavydovtech |
| Keywords | validate, validator, checker, model, modeler, express, request, api, API, middleware, middle-ware, node |

## Links

- npm: https://www.npmjs.com/package/express-request-modeler
- Repository: https://github.com/DavidDavydovTech/request-checker
- Homepage: https://github.com/DavidDavydovTech/request-checker#readme
- Issues: https://github.com/DavidDavydovTech/request-checker/issues
- npm.io page: https://npm.io/package/express-request-modeler

## Dependencies (1)

- [lodash.get](https://npm.io/package/lodash.get.md) ^4.4.2

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.0.8 (latest) — 2020-12-15
- 1.0.7 — 2020-12-15
- 1.0.6 — 2020-12-14
- 1.0.5 — 2020-12-14
- 1.0.4 — 2020-12-14
- 1.0.3 — 2020-12-14

## README

# express-request-modeler

&nbsp;
## Summary

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

&nbsp;
## 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](https://www.npmjs.com/package/express-request-checker)). 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.

&nbsp;
## 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.

```js
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`. 

&nbsp;
## 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. |

&nbsp;
## Advanced Example
```js
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...
		});
```

&nbsp;
## 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.)

---
_Source: https://npm.io/package/express-request-modeler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
