Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/technicallyjosh/koa-fluent-validation
Fluent, functional, and extendable validation for Koa 2. Built with validator behind the scenes.
https://github.com/technicallyjosh/koa-fluent-validation
Last synced: about 1 month ago
JSON representation
Fluent, functional, and extendable validation for Koa 2. Built with validator behind the scenes.
- Host: GitHub
- URL: https://github.com/technicallyjosh/koa-fluent-validation
- Owner: technicallyjosh
- Created: 2017-07-15T19:28:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T21:04:56.000Z (about 2 years ago)
- Last Synced: 2024-04-15T08:05:05.848Z (9 months ago)
- Language: TypeScript
- Size: 1.19 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# koa-fluent-validation
[![npm](https://img.shields.io/npm/v/koa-fluent-validation.svg?style=for-the-badge)](https://npmjs.com/package/koa-fluent-validation)
[![Travis (.org)](https://img.shields.io/travis/technicallyjosh/koa-fluent-validation.svg?style=for-the-badge)](https://travis-ci.org/technicallyjosh/koa-fluent-validation)
[![David](https://img.shields.io/david/technicallyjosh/koa-fluent-validation.svg?style=for-the-badge)](https://david-dm.org/technicallyjosh/koa-fluent-validation)Fluent, functional, and extendable validation for Koa 2 body, params, and query. Built on [validator](https://github.com/chriso/validator.js/) for base validations and filters.
## Installation
```console
$ npm i koa-fluent-validation
```## Requirements
- NodeJS >= 7.6
- **For validating parameters, [koa-router](https://github.com/alexmingoia/koa-router)'s implementation is used with `ctx.params`.**## Usage
### Simple App Example
```js
const Koa = require('koa');
const bodyparser = require('koa-bodyparser');
const { validation, v, f } = require('koa-fluent-validation');const app = new Koa();
app.use(bodyparser());
app.use(validation());app.use(async (ctx, next) => {
try {
await next();
} catch (e) {
if (e.status === 422) {
ctx.body = ctx.validationErrors;
return;
}// ... some other handling here etc
}
});// simple post route
app.use(async (ctx, next) => {
if (ctx.method !== 'POST') {
ctx.throw(404);
return;
}ctx.validateBody(
{
firstName: v()
.required()
.string(),
lastName: v()
.required()
.string(),
},
{
firstName: f().trim(),
lastName: f().trim(),
},
);// your code here
});app.listen(8080);
```## Documentation
**For documentation see the [wiki](https://github.com/technicallyjosh/koa-fluent-validation/wiki)!**
## TODO
- [] Filter Tests