JSPM

koa-tcomb-validation

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q30306F
  • License MIT

tcombjs as koa middleware

Package Exports

  • koa-tcomb-validation

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-tcomb-validation) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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)