2.0.0 • Published 3 years ago

crepecake-params v2.0.0

Weekly downloads
12
License
MIT
Repository
github
Last release
3 years ago

crepecake-params

Params schema validation middleware for Crepecake, based on Joi

Introduction

This is a Koa/crepecake middleware which aims to: 1. Provide a unified way for downstream middlewares to retrive request parameters 2. Enable parameter schema validation

Reference

Example

const Crepecake = require('crepecake')
const app = new Crepecake()
const params = require('crepecake-params')
const Joi = require('@hapi/joi')
const Router = Crepecake.Router
const route = new Router()

const getSchema = Joi.object({
  name: Joi.string().required(),
  test: Joi.boolean().default(false),
  date: Joi.date().required()
})

route.get('/', params(getSchema, { abortEarly: false }), async ctx => {
  console.log(ctx.state.params)
  return ctx.state.params
})

app.use(route)

app.listen(8888)
2.0.0

3 years ago