0.2.0 • Published 8 years ago

koa-joi-schema v0.2.0

Weekly downloads
2
License
ISC
Repository
github
Last release
8 years ago

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