arb / celebrate

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

Option to not call next with errors #246

Closed windward-hive closed 1 year ago

windward-hive commented 1 year ago

It would be nice if there's an option to make celebrate not call next with the errors, instead it should proceed with the request and add an errors to the request object

arb commented 1 year ago

I'm unlikely to make this change. I always envisioned celebrate as sitting in front of your business functions to validate all the inputs before you even get started on handling the request. I'm curious what you'd want to do with the result errors?

You could wrap celebrate with your own function and inject a "fake" next to get a handle to the errors.

const { celebrate, Joi, errors, Segments } = require('celebrate');

const c = (schema) => (req, res, next) => {
  celebrate(schema)(req, res, (errors) => {
     console.log(errors);
  });

  next();
});

// and then you'd use it like
app.post('/sign-up', c({[Segments.BODY]: { name: Joi.string() }));

Something like that would give you direct access to the error object celebrate spits out.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.