1.0.0 • Published 8 years ago
koa-cast-joi v1.0.0
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 joiNote: Joi is a peer dependency, so it must be installed independently.
API
cast(schemata, [options], [callback])
Cast app.context by given schemata and options
schematais map of paths to coresponding Joi schemasoptionsis passed toJoi.validateoptions.contextisapp.contextby default
callbackis passed toJoi.validate
cast.STORE(schema, [options], [callback])
Bind schema to given STORE in following fashion
cast.bodybinds toctx.request.bodycast.querybinds toctx.request.querycast.headersbinds toctx.request.headerscast.paramsbinds toctx.params
Related
License
MIT