# express-validator

 express-validator は、バリデーターおよびサニタイザー関数をラップする  
 [Express](https://tips.weseek.co.jp/5e392db0e848c600487d69d3) ミドルウェアである。


## Custom validator

 express-validator は依存関係である validator.js[^1] を通じて文字列の[バリデーション](/用語集/バリデーション)を実現しているが、  
より高度なものを実現するには [custom validator](https://express-validator.github.io/docs/custom-validators-sanitizers.html#custom-validator) を使用する必要がある。

### GROWIでの使用例

- [該当のPR](https://github.com/weseek/growi/pull/1554/files)

```javascript:src/server/routes/apiv3/notification-setting.js
  globalNotification: [
    body('triggerPath').isString().trim().not().isEmpty(),
    body('notifyToType').isString().trim().isIn(['mail', 'slack']),
    body('toEmail').trim().custom((value, { req }) => {
      return (req.body.notifyToType === 'mail') ? (!!value && value.match(/.+@.+\..+/)) : true;
    }),
    body('slackChannels').trim().custom((value, { req }) => {
      return (req.body.notifyToType === 'slack') ? !!value : true;
    }),
  ],
```

 toEmail の[バリデーション](/用語集/バリデーション)は常に実行する訳ではなく、 `notifyToType` が email の場合のみに実行する。  

---

## 関連ページ
- [/GROWI/故意の技術的負債から目を逸らさない](https://tips.weseek.co.jp/5e71da49df47e40049fa6c6f)


[^1]:文字列のみを検証およびサニタイズする[ライブラリ](https://github.com/validatorjs/validator.js)

---

## Page Navigation

- Canonical URL: https://tips.weseek.co.jp/用語集/Express/express-validator
- Permalink: https://tips.weseek.co.jp/5e38f300e848c600487d69ab
- Parent: [Express](/5e392db0e848c600487d69d3.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 0 total
- Last updated: 2020-03-18T08:25:02.138Z by deleted_at_1672138397219
- Full page listing (all children regardless of count): https://tips.weseek.co.jp/_api/v3/page-listing/children?id=5e38f300e848c600487d69ab
