Koa casting middleware using Joi.
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()
})
npm install koa-cast-joi joi
Note: Joi is a peer dependency, so it must be installed independently.
Cast app.context
by given schemata and options
-
schemata
is map of paths to coresponding Joi schemas -
options
is passed toJoi.validate
-
options.context
isapp.context
by default
-
-
callback
is passed toJoi.validate
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
MIT