question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Modify Request type to include typed body

See original GitHub issue

Summary(요약)

Even though there exists a validationMiddleware that calls plainToClass() and transforms the body object into a class instance, this class instance is not preserved and will need to be transformed again in the route handler.

Additional Details(추가적인 세부 사항)

What currently exists in the project in the controller is a simple cast const userData: CreateUserDto = req.body;, but that’s lying to the compiler because userData is not actually an instance of CreateUserDto, this can be noticed if the Dto has methods or custom getters or setters. Is this a real concern? Or should we ignore this? One replacement could be const userData = plainToClass(CreateUserDto, req.body);, but I was thinking that maybe we can make use of the validationMiddleware as it calls plainToClass already.

What do you think?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ljlm0402commented, May 6, 2021

@MuathAmer

Oh thank you very much. We will test and review your suggestions, but I think your suggestions are really good. If you leave a pull request, we will improve it quickly.

0reactions
MuathAmercommented, May 7, 2021

@ljlm0402 There is no reason it won’t work as far as I see. Is your concern about the type safety of validatedBody: CreateUserDto when setting skipMissingProperties = true? If so, I think that’s fine as long as the programmer realizes that some properties in the DTO (validatedBody) are optional, and this can also apply to the implementation as so:

export class CreateUserDto {
  @IsEmail()
  public email?: string;

  @IsString()
  public password?: string;
}

Notice the ? optional operator above.

Or you can have separate CreateUserDto and UpdateUserDto, and the programmer might use class-validator’s IsOptional() decorator or setting skipMissingProperties = true, both are valid options for UpdateUserDto, but at this point that’s class-validator’s knowledge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript type annotation for res.body - Stack Overflow
The key thing is to manually import Request and Response, and using a type generic ( Response<ITeamsRequest> ) you can define your own...
Read more >
Typed Express Request and Response with TypeScript
This article goes through some of the ways to add TypeScript to the ... Let's do another type where both body and query...
Read more >
How to extend the Express Request object in TypeScript
Let's learn how to extend the Request type in TypeScript to make its instances store custom data you can use at the controller...
Read more >
Extend Express's Request Object with Typescript Declaration ...
The first thing we need to do is to create a new declaration file @types > express > index.d.ts in the root of...
Read more >
Request.body - Web APIs - MDN Web Docs
The read-only body property of the Request interface contains a ReadableStream with the body contents that have been added to the request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found