koa-fluent-validation
Fluent, functional, and extendable validation for Koa 2 body, params, and query. Built on validator for base validations and filters.
Installation
yarn add koa-fluent-validation
npm i koa-fluent-validation
Requirements
- NodeJS >= 7.6
- For validating parameters, koa-router's implementation is used with
ctx.params
.
Usage
Simple App Example
const Koa = require('koa');
const bodyparser = require('koa-bodyparser');
const { validator, v, f } = require('koa-fluent-validation');
const app = new Koa();
app.use(bodyparser());
app.use(validator());
app.use(async (ctx, next) => {
try {
await next();
} catch (e) {
if (e.status === 422) {
ctx.body = ctx.validationErrors;
return;
}
}
});
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()
}
);
});
app.listen(8080);
Documentation
For documentation see the wiki!
TODO
- [] Filter Tests
- [] Automate build via Travis