koa-joi-schema
Koa middleware to validate input/output using Joi.
Last updated 2 years ago by simplyianm .
ISC · Repository · Bugs · Original npm · Tarball
$ cnpm install koa-joi-schema 
SYNC missed versions from official npm registry.

koa-joi-schema

npm version Build Status

Koa middleware to validate input using Joi.

Installing

npm i -S koa-joi-schema joi

Note: Joi is a peer dependency, so it must be installed independently.

Usage

See test.js for more examples.

const validate = require('koa-joi-schema')
const Joi = require('joi')

// Creates a validator for 'ctx.request.body'.
// Use dot notation to validate anything on the context.
const validator = validate('request.body')(Joi.object().keys({
  username: Joi.string().email().required(),
  password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required()
}))

const validationErrorHandler = (ctx, next) => {
  try {
    yield next()
  } catch (e) {
    if (!e.isJoi) throw e
    ctx.status = 400 // invalid input
    ctx.body = {
      error: 'Invalid input',
      reason: e
    }
  }
}

router.post('/users', bodyParser, validationErrorHandler, validator, usersCtrl.create)

License

ISC

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |