Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments: How to validate Express requests using io-ts #34

Open
jiayihu opened this issue Apr 26, 2020 · 6 comments
Open

Comments: How to validate Express requests using io-ts #34

jiayihu opened this issue Apr 26, 2020 · 6 comments
Labels

Comments

@jiayihu
Copy link
Owner

@jiayihu jiayihu commented Apr 26, 2020

This issue is reserved for comments to How to validate Express requests using io-ts. Leave a comment below and it will be shown in the blog page.

@Nicky-P
Copy link

@Nicky-P Nicky-P commented Jul 28, 2020

Awesome article, thanks for the upload! I'm having some problems following the "Transform io-ts error towards a more standard error response" section. Would you mind sharing what version of io-ts you are using?

@jiayihu
Copy link
Owner Author

@jiayihu jiayihu commented Jul 28, 2020

Hi, I was using version 2.2.1. That part may be indeed tricky, you want to consume a flat collection of errors instead of a tree. Let me know if you have any question

@Nicky-P
Copy link

@Nicky-P Nicky-P commented Jul 29, 2020

Gotcha, thank you! Looks like some built in error handling was added. https://github.com/gcanti/io-ts/blob/master/Decoder.md#built-in-error-reporter

I went ahead and used this approach with io-ts version 2.2.9

import { RequestHandler } from 'express';
import * as D from 'io-ts/lib/Decoder';
import { Decoder } from 'io-ts/lib/Decoder';
import { pipe } from 'fp-ts/lib/pipeable';
import { fold } from 'fp-ts/lib/Either';
import { ParamsDictionary } from 'express-serve-static-core';

export const validator: <T, A>(decoder: Decoder<T, A>) => RequestHandler<ParamsDictionary, any, T> = (decoder) => (req, res, next) => {
  return pipe(
    decoder.decode(req.body),
    fold(
      (errors) => res.status(400).send({ code: 'BadArgument', status: 'error', error: D.draw(errors) }),
      () => next(),
    ),
  );
};

@jiayihu
Copy link
Owner Author

@jiayihu jiayihu commented Jul 29, 2020

That's cool, I'm personally in the same Italian slack channel with Giulio but he deploys new releases so often that's hard even for me to follow all the updates :D
Thanks for reporting, I may add a note later in the article

@ak5
Copy link

@ak5 ak5 commented Oct 25, 2020

When I do

return addIsland(req.body) // req.body is strictly typed

TS still thinks req.body is any - is there something I have to do to express to get the type to stay in place outside the middleware? Right now I have to do something like

return addIsland(req.body as D.TypeOf<typeof IslandDec>) // req.body is strictly typed

Does your addIsland deal with this or am I missing something?

@jiayihu
Copy link
Owner Author

@jiayihu jiayihu commented Oct 25, 2020

The T generic in const validator: <T>(decoder: Decoder<T>) => RequestHandler<ParamsDictionary, any, T> is what makes the decoded value strictly typed. In any case just check express types to understand how the generics are passed in the flow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants