koa-tcomb-validation

1.0.1 • Public • Published

koa-tcomb-validation Build Status npm npm

tcomb validation as koa middleware

FOSSA Status

Description

A koa middleware that checks body/query request matches with tcomb type, if not valid, return standard 400 http code, or can be intercepted via koa global error handling.

API

validate(type, [location])

type

Type: Function
a type defined with the tcomb library

location

Type: String
Default: body
request property to compare with type object, eg body/query

Usage

const Koa = require('koa')
const bodyParser = require('koa-bodyparser')
const validate = require('koa-tcomb-validation')
const Router = require('koa-router')
 
const app = new Koa()
const router = new Router()
 
const body = t.struct({
  x: t.Number,
  y: t.Number
})
 
// request body will be check
router.post('/', validate(body, 'body'), async (ctx, next) => {
  ctx.status = 200
})
 
app.use(bodyParser())
 
// global middlewares
app.use(async (ctx, next) => {
  // the parsed body will store in ctx.request.body
  // if nothing was parsed, body will be an empty object {}
  ctx.body = ctx.request.body
  await next()
})
 
app.use(router.routes())
  .use(router.allowedMethods())
 
app.listen(3000)

Package Sidebar

Install

npm i koa-tcomb-validation

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

26.2 kB

Total Files

7

Last publish

Collaborators

  • yujinlim