# body-expect

> Simple package for validate request body in Koa, Express... or anywhere.

Latest version **0.4.2** (published 2019-03-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install body-expect
pnpm add body-expect
yarn add body-expect
bun add body-expect
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.2 |
| Published | 2019-03-01 |
| First published | 2019-03-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mikhail Semin |
| Maintainers | bifot |

## Links

- npm: https://www.npmjs.com/package/body-expect
- Repository: https://github.com/bifot/body-expect
- Homepage: https://github.com/bifot/body-expect#readme
- Issues: https://github.com/bifot/body-expect/issues
- npm.io page: https://npm.io/package/body-expect

## Recent versions

- 0.4.2 (latest) — 2019-03-01
- 0.4.1 — 2019-03-01
- 0.4.0 — 2019-03-01
- 0.3.0 — 2019-03-01
- 0.2.2 — 2019-03-01
- 0.2.1 — 2019-03-01
- 0.2.0 — 2019-03-01
- 0.1.0 — 2019-03-01

## README

# body-expect

Simple package for validate request body in Koa, Express... or anywhere.

## Install

```sh
$ npm i body-expect
```

## Usage

### Express

```js
const express = require('express');
const bodyParser = require('body-parser');
const expect = require('body-expect/express');

const app = express();

app.use(bodyParser.json());

app.use((req, res, next) => {
  req.params = {
    ...req.params,
    ...req.query,
    ...req.body,
  };

  next();
});

app.post(
  '/users',
  expect({
    age: {
      type: Number,
      validate: age => age >= 18,
    },
    name: String,
    hobby: {
      type: String,
      required: false,
    },
  }),
  (req, res) => {
    if (req.validationErrors && req.validationErrors.length) {
      res.status(400).json({
        error: `Bad request data: ${req.validationErrors.join(', ')}`, 
      });
      
      return;
    }
    
    // business logic here ...
  },
);

app.listen(process.env.PORT);
```

### Koa

```js
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const expect = require('body-expect/koa');

const app = new Koa();
const router = new Router();

router.post(
  '/goals',
  expect({
    text: String,
    date: Number,
  }),
  (ctx) => {
    // business logic here ...
  },
);

app.use(bodyParser());

app.use(async (ctx, next) => {
  ctx.params = {
    ...ctx.params,
    ...ctx.query,
    ...ctx.request.body,
  };

  await next();
});

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {    
    ctx.status = err.status || 500;
    ctx.body = err.message || 'Server error.';
  }
});

app.use(router.routes());

app.listen(process.env.PORT);
```

### Abstract

```js
const createSchema = require('body-expect');

const expect = createSchema({
  foo: String,
});

const errors = expect({});
```

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