Coder Social home page Coder Social logo

koa-joi-schema's Introduction

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 co = require('co')  // Koa 2 no longer auto-wraps with co
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 = co.wrap(function* (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

koa-joi-schema's People

Contributors

macalinao avatar azhang avatar

Stargazers

Void avatar Matthew avatar Paul Day avatar  avatar Anthony avatar Jeremy Meyer avatar

Watchers

James Cloos avatar  avatar

Forkers

azhang

koa-joi-schema's Issues

Expose Joi

I recommend you expose Joi from the module to avoid version conflicts with some validations passing in the middleware but not passing somewhere else in the application. See how koa-joi-router does it here. You won't be forcing any version of Joi on the user of this module, but you also give them an easy option rather than having to keep track of what version this module uses over the course of upgrades.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.