Skip to content

CourseDesign/koa-joi-validate-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Koa Joi Validate Middleware


Validate middleware generator using Joi

Easily create Koa middleware for validate with Joi.

Install

$ npm i koa-joi-validate-middleware

Usage

Import

const validateMiddleware = require('koa-joi-validate-middleware');

Create Validate Middleware

const schema = {
  // Request headers Joi validation object
  headers: Joi.obejct({
  }),

  // URL query string Joi validation object
  query: Joi.obejct({
  }),

  // URL path parameters Joi validation object
  params: Joi.obejct({
    id: Joi.string().required(),
  }).required(),

  // POST body Joi validation object
  body: Joi.obejct({
  }),
};

const validator = validateMiddleware.create(schema, errorCallback);

errorCallback

function errorCallback(ctx, error) {
}

Use Validate Middleware

router.get('/user', validator, async (ctx) => {
  const { id } = ctx.params;
  const response = await getUserInfo(id);
  ctx.body = response;
});