koa-proper
0.1.4 • Public • Published koa-proper
A body validation for koa, based on prop-types.
Installation
npm i --save koa-proper
Usage
import Koa from 'koa'
import proper from 'koa-proper'
const app = new Koa()
app.use(proper())
app.use(async ctx => {
const props = ctx.request.query
const types = {
username: ctx.PropTypes.string.isRequired
}
const params = ctx.proper(props, types)
ctx.body = params
})
Options
Name |
Type |
Default |
Description |
auto |
Boolean |
true |
when true , will throw http error automatically |
throw |
Function |
(ctx, error) => { ctx.throw(400, error.message) } |
error thrower |
log |
Function |
(ctx, error) => { console.warn(error.message) } |
error logger |
You can set global options, and one time options
const app = new Koa()
app.use(proper({
auto: true
}))
app.use(async ctx => {
const props = ctx.request.query
const types = {
username: ctx.PropTypes.string.isRequired
}
const params = ctx.proper(props, types, { auto: false })
if (params) {
ctx.body = params
} else {
ctx.body = 'The query is invalid'
}
})
Licences
MIT
Package Sidebar
Install
Weekly Downloads