# bodychecker

> A middleware for express.js to check the request body.

Latest version **1.0.3** (published 2018-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install bodychecker
pnpm add bodychecker
yarn add bodychecker
bun add bodychecker
```

## 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.3 |
| Published | 2018-01-21 |
| First published | 2017-08-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | MZI |
| Maintainers | mzi |
| Keywords | express, middleware |

## Links

- npm: https://www.npmjs.com/package/bodychecker
- Repository: https://github.com/MZIchenjl/bodychecker
- Homepage: https://github.com/MZIchenjl/bodychecker#readme
- Issues: https://github.com/MZIchenjl/bodychecker/issues
- npm.io page: https://npm.io/package/bodychecker

## Dependencies (1)

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

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2018-01-21
- 1.0.0 — 2018-01-19
- 0.9.3 — 2017-12-06
- 0.9.2 — 2017-12-06
- 0.9.0 — 2017-12-06
- 0.1.1 — 2017-08-20
- 0.1.0 — 2017-08-15

## README

# Bodychecker

[![npm version](https://img.shields.io/npm/v/bodychecker.svg)](https://www.npmjs.com/package/bodychecker)
[![travis-ci](https://travis-ci.org/MZIchenjl/bodychecker.svg?branch=master)](https://travis-ci.org/MZIchenjl/bodychecker)

Bodychecker is a express middleware for handling `req.body`, which is used to validate the value in the body like the React Proptypes way.

# Installation

```bash
npm install --save bodychecker
```

# Usage 

Bodychecker add result "`req.$bcResult`" to the express RequestHandler. 

example:

```javascript
var express = require('express')
var bodychecker  = require('bodychecker')

var app = express()

app.post('/post/new', bodychecker({
    title: bodychecker.string.isRequired,
    author: bodychecker.string,
    time: bodychecker.number,
    content: bodychecker.string.isRequired
}), function (req, res, next) {
    // req.$bcResult is the result of the bodycheck, which will be null if all the fields are valid
    // place your awesome code
})
```

# API

All supported types:

* `any` (_optional_ `isRequired`) any type, e.g `'abc',123,undefind`

* `array` (_optional_ `isRequired`) array type, e.g `[1,2,3...]`

* `object` (_optional_ `isRequired`) object type, e.g `{a:1}`

* `bool` (_optional_ `isRequired`) boolean type, e.g `true, false`

* `number` (_optional_ `isRequired`) number type, e.g `3.14159265`

* `string` (_optional_ `isRequired`) string type, e.g `Hello world!`

* `oneof` (_optional_ `isRequired`) `array` argument required, e.g `oneof(['a', 'b'])`

* `oneoftype` (_optional_ `isRequired`) `array` of `function` argument required, e.g `oneoftype([bodychecker.string,bodychecker.number])`

* `arrayof` (_optional_ `isRequired`) `function` argument required, e.g `arrayof(bodychecker.string)`

* `objectof` (_optional_ `isRequired`) `function` argument required, e.g `objectof(bodychecker.string)`

* `shapeof` (_optional_ `isRequired`) `obeject` of  `function` argument required, e.g `shapeof({ title: bodychecker.string })`

Object "req.$bcResult"

```javascript
{
    message: 'message', // the default error message generated by bodychecker
    type: 'invalid', // the error type 'empty', 'invalid'
    fieldpath: fieldpath, // the path of the error fieldvalue, e.g 'people.0.name'
    fieldvalue: fieldvalue // the error fieldvalue
}
```

__Custom checker__

Otherwise, You can also add custom checker.

```javascript
/**
 * customChecker
 * @param {object} body - the req.body object
 * @param {string} fieldname - the field name
 * @return {object|null} - the result
 */
function customChecker(body, fieldname) {
    // make sure this function can return with result or null
}
```

# LICENSE

MIT

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