express-decorators-joi

0.1.0 • Public • Published

express-decorators-joi

Defines a middleware function for express-decorators that validates request data using Joi.

Installation

$ npm install --save express-decorators-joi

Usage

import * as web from 'express-decorators';
import validate from 'express-decorators-joi';
 
@web.controller('/')
class TestController {
 
  @web.get('/test')
  @validate(Joi.object().keys({
    name: Joi.string().required(),
    birthdate: Joi.date().iso()
  }))
  testAction(request, response) {
    // request.data contains the validated (and converted) data
  }
}

It validates request.body, so you'll need body-parser installed and used. If the validation fails, the middleware calls next with a Joi ValidationError. You should probably trap this error and return an HTTP 400, e.g.:

app.use(function (err, request, response, next) {
  if (err.name === 'ValidationError') {
    response.status(400).json({error: err.message});
  }
});

That's about it.

Licence etc

ISC, do what you want. All bug reports, issues, comments, suggestions and pull requests welcome.

Package Sidebar

Install

npm i express-decorators-joi

Weekly Downloads

1

Version

0.1.0

License

ISC

Last publish

Collaborators

  • stewartml