question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

express-validator not producing validation errors

See original GitHub issue

I am facing error while doing latest express validator (V ^4.3.0) example. There is one basic example of login with post request. There are two parameters email and password. Below is my code.

routes.js file:

var express = require('express');  
var router  = express.Router();  
var validator = require('./validator');

router.post('/login', [sanitize('email').trim(), 
 validator.publicRouteValidate('login')], (req, res, next) => {
  console.log(req);
}  

validator.js file:

'use strict';  
const { check, validationResult } = require('express-validator/check');  
const { matchedData, sanitize } = require('express-validator/filter');

module.exports = {  
  publicRouteValidate: function (method) {  
    return (req, res, next) => {  
      switch (method) {  
        case 'login':  
          check('email').isEmail().withMessage('email must be an email')  
            .isLength({min: 109}).withMessage('minimum length should be 100 characters')  
        break;  
      }  
      var errors = validationResult(req)
      console.log(errors.mapped())
      if (!errors.isEmpty()) {
         res.status(422).json({ errors: errors.array()[0].msg })
      } else {
         res.send('login')
      }
}}}  

Now when I am doing POST request with only email and value of email is abc. So I should get the error like email must be an email. But I did not get any response. So What is the issue I do not know?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
notflipcommented, Jan 17, 2019

Could anyone briefly explain why we need to do this in 2 separate functions? So with middleware instead of allowing it to be done in one function?

0reactions
lock[bot]commented, May 31, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - express-validator not producing validation errors
Your publicRouteValidate function is only creating a validator, but it is never being called. This happens because, as documented, ...
Read more >
validationResult() - express-validator
Extracts the validation errors from a request and makes them available in a ... a boolean indicating whether this result object contains no...
Read more >
How to use the express-validation.ValidationError function in ...
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... error 所有傳入參數驗證錯誤 if (err instanceof...
Read more >
How to make input validation simple and clean in ... - Medium
Your client side validation is not enough and it may be subverted ... If there is no error , you will receive an...
Read more >
Form Data Validation in Node.js with express-validator
Custom Validation Rules and Error Messages with express-validator ... from Validator.js methods, since there is no method to check it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found