# koa-field-validate

> Parsed body field validation middleware for Koa.

Latest version **0.0.3** (published 2015-09-11) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install koa-field-validate
pnpm add koa-field-validate
yarn add koa-field-validate
bun add koa-field-validate
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2015-09-11 |
| First published | 2015-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | barberdt |
| Maintainers | barberdt |
| Keywords | koa |

## Links

- npm: https://www.npmjs.com/package/koa-field-validate
- Repository: https://github.com/barberdt/koa-field-validate
- Homepage: https://github.com/barberdt/koa-field-validate#readme
- Issues: https://github.com/barberdt/koa-field-validate/issues
- npm.io page: https://npm.io/package/koa-field-validate

## Recent versions

- 0.0.3 (latest) — 2015-09-11
- 0.0.1-beta (beta) — 2015-09-08
- 0.0.2 — 2015-09-11
- 0.0.0-beta — 2015-09-08

## README

# koa-field-validate
Parsed body field validation middleware for Koa. This is a work in progress.

## Installation
```
npm install --save koa-field-validate
```

## Usage
See `example.js` in the root of this repository for a full example of usage.

The middleware provides a `validate` function on the app context, which takes a field name as an argument. The function will return an instance of a `FieldValidator` that exposes several validation methods that can be chained. Invalidations will be populated into the app context's `fieldErrors` object, where they will be keyed by field name. Handling of the existence of `this.fieldErrors` is up to the developer's discretion.

```javascript
app.use(function *() {
  // Perform validation on the required username field
  this.validate('username').required();
  this.validate('email').required().notEmpty().email();

  // Assert that this.fieldErrors does not exist, throwing 400 if it does
  this.assert(!this.fieldErrors, 400, { fields: this.fieldErrors });

  // If the assertion was true, we have no errors
  this.body = 'No errors!';
});
```

### Current Validations
This is still a very raw middleware and is in an alpha development phase. More validations will come.

- **required(message)**
    The field must exist.

- **notEmpty(message)**
    The field must not be empty.

- **email(message)**
    The field must match a valid email regex.

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