arb / celebrate

A joi validation middleware for Express.
MIT License
1.34k stars 65 forks source link

give celebrate-middleware a more suitable name #73

Closed chrismessiah closed 6 years ago

chrismessiah commented 6 years ago

I'm looking into generating a Swagger specification from Express routes which use celebrate for input validation. Currently when celebrate is added to the router the middleware is named middleware. In order to identify that its is a celebrate-middlware I would like it to have a more suitable name such as celebrateMiddleware or similar.

chrismessiah commented 6 years ago

Here is a short before-after example to what I'm getting at when you print an express router

Say that we have this setup

const router = require('express').Router();
const userSchema = ...
const createUser = ...
router.route('/').post(
  celebrate({ body: userSchema }),
  createUser,
);
console.log(router.stack[0].route.stack);

Then the log will look like this

[ Layer {
    handle: { [Function: middleware] _schema: [Object] },
    name: 'middleware',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: { /^\/?$/i fast_star: false, fast_slash: false },
    method: 'post' },
  Layer {
    handle: [Function],
    name: '<anonymous>',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: { /^\/?$/i fast_star: false, fast_slash: false },
    method: 'post' } ]

Instead of like this

[ Layer {
    handle: { [Function: celebrateMiddleware] _schema: [Object] },
    name: 'celebrateMiddleware',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: { /^\/?$/i fast_star: false, fast_slash: false },
    method: 'post' },
  Layer {
    handle: [Function],
    name: '<anonymous>',
    params: undefined,
    path: undefined,
    keys: [],
    regexp: { /^\/?$/i fast_star: false, fast_slash: false },
    method: 'post' } ]

While this example is only with 2 middlwares it would be harder to detect which middleware is celebrate if it has the name middleware

arb commented 6 years ago

Could you check the handle value instead?

const { celebrate } = require('celebrate');
if (handle.function === celebrate) {
  console.log('this is the celebrate middleware');
}

While your fix would solve the problem, I don't think the internally used variable names should have an impact on users of the module.

chrismessiah commented 6 years ago

Ah great and you're right! Didn't think about that. In addition one can just check for the _schema object. Thanks!