0.0.9 • Published 7 years ago

circe-checker v0.0.9

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

circe-checker 请求参数验证

支持queryparamsbody的参数

安装

NPM

使用

import * as Koa from 'koa'
import * as Router from 'koa-router'
import * as checker from 'circe-checker'

const app = new Koa()
const router = new Router()

app.use(checker.init())
app.use(checker.onError((err, ctx) => {
  // err.message
}))

router.get('/users/:userId/posts', checker({
  userId: (ctx) => ctx.checkParam('userId').toNumber(),
  sortBy: (ctx) => ctx.checkQuery('sortBy', false, 'createdAt')
  // someData: (ctx) => ctx.checkBody('key')
}), async (ctx) => {
  const {userId, sortBy} = ctx.vals
  // do something...
})

app.use(router.routes())
app.use(router.allowedMethods())

参数

checker.init() 参数

  • getQuery?: (ctx: Koa.Context) => any
  • getParams?: (ctx: Koa.Context) => any
  • getBody?: (ctx: Koa.Context) => any

checker.onError() 参数

(err: ValidationError, ctx: Koa.Context) => void

checker() 参数

支持的参数验证函数

查看源文件

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago