1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

express-validatorを使用した際の空Objectの判定

Posted at

express-validatorがチェックで使用しているvalidator.jsにはObjectが空か({}か)のチェック処理はない。
このため、custom定義を行い、自力でチェック処理を実装する必要がある。その際のメモ。

空Objectの判定

let check = [param("param1", "error message").custom((value, { req, location, path }) => {
  if (typeof value !== 'object') return false;  // 型がObjectでなければエラー
  return Object.keys(value).length !== 0;       // keyの一覧(配列)を取得、長さが0であればエラー
})];

チェック自体はよくあるやり方だと思うが、忘れないようにメモメモ。。。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?