Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yujinlim/koa-tcomb-validation
tcomb validation as koa middleware
https://github.com/yujinlim/koa-tcomb-validation
Last synced: 15 days ago
JSON representation
tcomb validation as koa middleware
- Host: GitHub
- URL: https://github.com/yujinlim/koa-tcomb-validation
- Owner: yujinlim
- License: mit
- Created: 2018-04-06T06:02:32.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-20T02:09:27.000Z (over 6 years ago)
- Last Synced: 2024-11-07T18:03:56.146Z (2 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-tcomb-validation [![Build Status](https://img.shields.io/travis/yujinlim/koa-tcomb-validation.svg?style=flat-square)](https://travis-ci.org/yujinlim/koa-tcomb-validation) ![npm](https://img.shields.io/npm/dt/koa-tcomb-validation.svg?style=flat-square) ![npm](https://img.shields.io/npm/v/koa-tcomb-validation.svg?style=flat-square)
> tcomb validation as koa middleware[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fyujinlim%2Fkoa-tcomb-validation.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fyujinlim%2Fkoa-tcomb-validation?ref=badge_large)
## 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
```js
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)
```