Skip to content

orinak/koa-cast-joi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 schemas
  • options is passed to Joi.validate
    • options.context is app.context by default
  • callback is passed to Joi.validate

cast.STORE(schema, [options], [callback])

Bind schema to given STORE in following fashion

  • cast.body binds to ctx.request.body
  • cast.query binds to ctx.request.query
  • cast.headers binds to ctx.request.headers
  • cast.params binds to ctx.params

Related

License

MIT

About

Koa casting middleware using Joi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published