0

Trying to validate the request through the DTO by calling the @requestBody annotation at the controller route.

Controller Method -

@httpPost('/login')
    public async login(@request() req: Request, @response() res: Response, @requestBody() body: LoginDto): Promise<any> {
        console.log(" ~ file: auth.controller.ts:20 ~ AuthController ~ login ~ body:", body)
        return res.status(200).json(body);
    }

DTO -

export interface LoginDto {
    email: string;
    password: string;
}

Request Body -

{
  email: 'helloworld@email.com',
  password: '1234',
  confirm_password: '1234'
}

Expected Output -

~ file: auth.controller.ts:20 ~ AuthController ~ login ~ body: {
      email: 'helloworld@email.com',
      password: '1234'
    }

Actual Output -

~ file: auth.controller.ts:20 ~ AuthController ~ login ~ body: {
  email: 'helloworld@email.com',
  password: '1234',
  confirm_password: '1234'
}

The DTO is not working as expected.Not sure whats going on wrong.If anyone has experienced this before and have a solution for it, then it would be helpful. Thanks !

Rudr Thakur
  • 330
  • 3
  • 12
  • What result did you expect? What do you mean by "it's not working" – Lin Du May 20 '23 at 08:57
  • the interface of the dto doesn't have confirm_password field, so it shouldn't technically output that when I try to print the body. I hope that makes sense. I will just edit the question to include the expected output. – Rudr Thakur May 20 '23 at 10:43
  • I don't think the `inversify-express-utils` package will modify the request body at runtime to satisfy the TS interface. See this test https://github.com/inversify/inversify-express-utils/blob/v6.4.3/test/fetures/controller_inheritance.test.ts#L136 – Lin Du May 20 '23 at 14:07
  • oh thanks for the heads up! Is there any other way that I can achieve this perhaps any other package – Rudr Thakur May 21 '23 at 04:12

0 Answers0