title: Using express-validator to check if input is a username or email
body: <p>I am trying to validate a login request where it accepts a username that can either be a email or username, and a password. How can I do this with Express-Validator?</p>
  
  <p>I'm reading the documentation and the way the validator works is that you have to pass in the data as the second argument. 
  I'm not sure how that's done with checking to see if it's also a username or password.</p>
  
  <pre><code>router.post("/", (req, res) =&gt; {
      const { nickname, password } = req.body;
      // Create a new object based on email or username
      const login = {};
      if (nickname.indexOf('@') === -1) {
          login.nickname = nickname;
      } else {
          login.email = nickname;
      }
      login.password = password;
      // JWT Sign...
  });
  </code></pre>
  
  <p>With the validator, I'd also like to check if it's</p>
  
  <ul>
  <li>A username or email</li>
  <li>If it's a email, make sure it's an actual email using isEmail() validator</li>
  <li>If it's a username, make sure it's more then three characters and there's an actual username.</li>
  </ul>
  
  <p><strong>Attempt</strong></p>
  
  <pre><code>router.post("/", (req, res) =&gt; {
      const { nickname, password } = req.body;
      // Create a new object based on email or username
      const login = {};
      if (nickname.indexOf('@') === -1) {
          check('nickname').isLength({ 'min': 3 });
          login.nickname = nickname;
      } else {
          check('nickname').isEmail();
          login.email = nickname;
      }
      login.password = password;
      const errors = validationResult(req);
      if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
      }
  });
  </code></pre>
  
  <p>Seemed like the validator just didn't work. </p>