0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

classvalidatorが正常に働くために

Posted at

とりまバリデーションパイプをグローバルにかけるのを忘れずに

main.ts
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe({
    whitelist: true,
    forbidNonWhitelisted: true,
    transform: true,
  }));
  await app.listen(3001);
whitelist
trueなら、デコレータのついてないプロパティ削除。デコレータの必要ないプロパティが消されないよう、いらなくても@Allowをつけよう。
forbidNonWhitelisted
tureで上記の設定で消されるプロパティを、削除せずエラーを吐くようにする
transform
trueで受け取ったDTOを型注釈にかかれてる型にへんかんする。ただし、仮にプロパティが食い違っていても、無理くり変換してしまうので注意。

whitelistforbidNonWhitelistedがtrueじゃないと、余計なプロパティをはじけない。
これらがなくても、プロパティの型が間違っている場合は指摘してくれるが、例外に対処できない。サーバーが固まってしまうかもしれないので、例外にも対処できるようにしておこう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?