koa-cast-joi
v1.0.0
Published
Koa casting middleware using Joi
Downloads
6
Readme
koa-cast-joi
Koa casting middleware using Joi.
Usage
import Koa from 'koa'
import Joi from 'joi'
import cast from 'koa-cast-joi'
const app = new Koa()
const schema = Joi.object().keys({
page: Joi.number().default(1)
})
// app.use(cast({ 'request.query': schema }))
app.use(cast.query(schema))
app.use((ctx, next) => {
console.log('next:', ctx.request.query.page + 1)
return next()
})
Install
npm install koa-cast-joi joi
Note: Joi is a peer dependency, so it must be installed independently.
API
cast(schemata, [options], [callback])
Cast app.context
by given schemata and options
schemata
is map of paths to coresponding Joi schemasoptions
is passed toJoi.validate
options.context
isapp.context
by default
callback
is passed toJoi.validate
cast.STORE(schema, [options], [callback])
Bind schema to given STORE in following fashion
cast.body
binds toctx.request.body
cast.query
binds toctx.request.query
cast.headers
binds toctx.request.headers
cast.params
binds toctx.params
Related
License
MIT