Skip to content
A koa2 middleware to validate the request with Joi
Branch: master
Clone or download
Latest commit 294a99f Jan 25, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Better error handling Nov 25, 2017
test
.eslintrc.json
.gitignore test(coverage): add test coverage with overalls Aug 31, 2017
.npmignore doc(npm): add npm ignore file Jul 25, 2017
.travis.yml chore(travis): whitelist greenkeeper branches Nov 21, 2018
CHANGELOG.md
LICENSE
README.md docs(readme): add Greenkeeper badge Nov 21, 2018
index.js feat(validator): fix the bad import Jul 25, 2017
package-lock.json
package.json chore(package): update ava to version 1.0.1 Dec 15, 2018

README.md

koa2-validation

npm Build Status Coverage Status Greenkeeper badge

KoaJs Slack

koa2-validation is a koa2 middleware to validate the request with Joi. Support body, params, query for Now.
Inspired by express-validation.

Usage

Install with npm:

npm i -S koa2-validation

Then, you can use koa2-valition to configure the validation schemas in routes. The example below is to define three validations about user.
file: test/lib/server.js

const http = require('http');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const router = require('koa-router')();
const validate = require('koa2-validation'); // 1. import the koa2-validation

const user = require('./user');


router.post('/users', validate(user.v.addUser), user.addUser);  // 3. setup the validate middleware
router.get('/users/:id', validate(user.v.getUserInfo), user.getUserInfo);
router.get('/users', validate(user.v.getUserList), user.getUserList);

const app = new Koa();

// error handler
app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.status || err.code;
    ctx.body = {
      success: false,
      message: err.message,
    };
  }
});
app.use(bodyParser()); // bodyParser should be before the routers
app.use(router.routes());

const server = http.createServer(app.callback());
module.exports = server;

You still need to define the validation schema in your controllers, as follows:
file: test/lib/user.js

const _ = require('lodash');
const Joi = require('joi');

const v = {};
exports.v = v;

const users = [{
  id: '001',
  name: 'dennis1',
  age: 18,
}, {
  id: '002',
  name: 'dennis2',
  age: 20,
}];

// 2. define the validation schema
v.addUser = {
  body: {
    id: Joi.string().required(),
    name: Joi.string(),
    age: Joi.number(),
  },
};
exports.addUser = async (ctx) => {
  const user = ctx.request.body;
  users.push(user);
  ctx.body = { success: true, data: users };
};

The validation schema is followed by Joi. You can define more effective schemas based on joi docs.

Error handler

When bad request, koa2-validation has catched the error, and throw a standard Error instance, which has an attr status 400.

app.use(async (ctx, next) => {
  try {
    await next();
  } catch (err) {
    ctx.status = err.status || err.code;
    ctx.body = {
      success: false,
      message: err.message,
    };
  }
});

Example

In the test foler, I made a demo about user management. You can get how to use koa2-validation from it. If you have some questions, you can post an issue.

You can’t perform that action at this time.