preface

After the parameter fields are defined in the DTO layer, the client passes an undefined field when calling. In this case, we need to report an error to inform the client that the field does not exist, which is not reported in Nest by default. This article will share the solution to this problem and welcome interested developers to read this article.

Scenario overview

Continuing with the project we created in the article “Building server-side Applications with NestJS” to describe this problem, we define three fields in appTo.ts, as shown in the code below.

  • id
  • title
  • name
export class AppDto {
  @MinLength(5)
  @IsString(a)publicid! :string;
  @IsString(a)publictitle! :string;
  @IsString(a)publicname! :string;
}
Copy the code

Then, we started the project, called the interface with Postman, and passed an age field, which was not defined in AppDto. After calling the interface, as shown in the picture below, the interface was successfully called, which was not what we expected, we expected it to report an error.

In Java, we define fields in the entity class, and SpringBoot can throw exceptions directly when it serializes client arguments.

The solution

When solving this problem, I searched a wave on the network, but found no suitable scheme. Finally, I asked a wave of netizens for help, and the scheme I got was to write methods in the controller layer to traverse all the keys of parameters and verify them, and then throw exceptions. I think this is the next policy, write their own method is too cumbersome, not conducive to maintenance.

Try to solve

Suddenly, someone online told me forbidUnknownValues, which opened my eyes and gave me hope.

After some searching, detailed documentation was found for it, as follows:

When you see this, the corners of your mouth go crazy, and the global pipe in main.ts always turns this configuration item on, as shown below:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe({ forbidUnknownValues: true }));
  await app.listen(3000);
}
bootstrap();

Copy the code

This thought all is well, the execution result is unsatisfactory 🌚

Problem solving

At this point, I fell into a meditation, according to the description should be this parameter. As I pondered, I saw the Whitelist and forbidNonWhitelisted fields.

  • Whitelist if set to true, the validator strips out any validator objects that do not use any of the decorator’s attributes.
  • ForbidNonWhitelisted if set to true, the validator will throw an exception rather than strip out non-whitelisted attributes.

If the forbidNonWhitelisted property is invalid, the forbidNonWhitelisted property is invalid. If the forbidNonWhitelisted property is invalid, the forbidNonWhitelisted property is invalid.

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({ whitelist: true.forbidNonWhitelisted: true}));await app.listen(3000);
}
bootstrap();

Copy the code

We continued to test the call interface using Postman, and it worked perfectly.

The sample code

For the complete code listed in this article, go to:

  • main.ts
  • AppDto.ts

Write in the last

At this point, the article is shared.

I’m an amazing programmer, a front-end developer.

If you are interested in me, please visit my personal website for further information.

  • If there are any errors in this article, please correct them in the comments section. If this article helped you, please like it and follow 😊
  • This article was first published in the magic programmer public number, without permission to prohibit reprinting 💌