koa-body-validator

1.0.1 • Public • Published

koa-body-validator

A simple koa-router body validator middleware based on Joi.

Usage

This package requires koa-body or an alternative body-parser that saves the parsed body in koas context object (ctx.request.body).

const Koa = require('koa')
const koaBody = require('koa-body')
const router = require('koa-router')()
 
const {Joi, bodySchema} = require('koa-body-validator')
 
const app = new Koa()
 
app.use(koaBody())
 
 
router.post('/', bodySchema({
   name:      Joi.string().min(5).max(128).required(),
   email:     Joi.string().email().required(),
   password:  Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required(),
   birthyear: Joi.number().integer().min(1900).max(2007),
}), async ctx => {
   try {
      ctx.validate()
      
      ctx.body = {
         name:     ctx.request.body.name,
         email:    ctx.request.body.email,
         password: ctx.request.body.password
      }
   } catch(err) {
      ctx.body = err.message
   }
})
 
 
app.use(router.routes())
app.use(router.alloweMethods())
 
app.listen(8080)

For more instructions on how to define the schema, see the detailed Joi API Reference.

Readme

Keywords

none

Package Sidebar

Install

npm i koa-body-validator

Weekly Downloads

38

Version

1.0.1

License

GPL-3.0

Unpacked Size

37.1 kB

Total Files

4

Last publish

Collaborators

  • loicnestler