arb / celebrate

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

Type definitions for converted data #234

Closed jasonaowen closed 2 years ago

jasonaowen commented 2 years ago

How do you recommend using request data that Celebrate/Joi has converted as part of validation? The type definitions for express (@types/express@4.17.13) define parameters as strings (see ParamsDictionary type definition). With conversions turned on (as they are by default, and as I want Celebrate & Joi to do), those type definitions no longer match the actual data, and TypeScript rejects otherwise-correct code.

Here is an example value that is not working as expected:

import { celebrate, Joi, errors, Segments } from 'celebrate';
import express from 'express';

const f = (x: number) => x + 1;

const app = express();

app.get(
  '/example/:id',
  celebrate({
    [Segments.PARAMS]: Joi.object().keys({
      id: Joi.number().integer().min(1).required(),
    }).required(),
  }),
  (req, res, next) => {
    //console.log('f(id)', f(req.params.id));

    res.json({
      params: req.params,
      'id_type': typeof req.params.id,
    });
  },
);
app.use(errors());
app.listen(3000, () => (
  console.log(`app listening at http://localhost:3000`)
));

When queried, it returns the expected data:

$ curl http://localhost:3000/example/1
{"params":{"id":1},"id_type":"number"}

$ curl http://localhost:3000/example/foo
{"statusCode":400,"error":"Bad Request","message":"Validation failed","validation":{"params":{"source":"params","keys":["id"],"message":"\"id\" must be a number"}}}

But uncommenting line 16 results in a compilation error:

TSError: ⨯ Unable to compile TypeScript:
example.ts:16:28 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

16     console.log('f(id)', f(req.params.id));
                              ~~~~~~~~~~~~~

By the way, thank you for making and maintaining Celebrate! Using it has been delightful.

arb commented 2 years ago

I'm not a TypeScript expert or even active user so my ability to help with this will be very limited. My hunch is that, since this is a run-time conversation from string to integer, the TS compiler doesn't have a way to know that req.params.id has been changed to an integer. You may have to recast the value before calling f() I realize this isn't a great solution because now you're going to have to re-cast every result coming out of celebrate which is far from ideal.

One possible less-annoying path would be to create your own middleware that you'd insert right after celebrate calls. In there, you could either construct a new object and attach it to res.locals for example and then use that in the rest of the route. That way you could type it as needed. You could also recast req.* in there as well before calling next()

Hopefully someone can weigh in with a better idea than the one I've had.

Here-in lies the problem with trying to formally type a loosely typed language 😅

stale[bot] commented 2 years 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.