express-validator not producing validation errors
See original GitHub issueI 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:
- Created 6 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
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.