express-validator can not validate while using multer
See original GitHub issuewhenever I submit my form with empty values the express-validator
works fine and sends the validation errors, however when I also submit the form with NOT EMPTY VALUES I get errors too, I’m really confused about what’s wrong with this code?
const {check, validationResult} = require('express-validator');
var multer = require('multer');
var upload = multer({dest: 'uploads/'});
router.post(
'/add',
[
check('title', 'title field is required').not().isEmpty(),
check('text', 'text field is required').not().isEmpty()
],
upload.single('mainImage'),
(req, res) =>{
const validationErrors = validationResult(req);
if(!validationErrors.isEmpty()){
// in every situation, only this part of code is going to be executed
return res.status(422).json({errors: validationErrors.array()});
}else{
// inserting into DB
res.send();
}
});
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Multer and express-validator creating problem in validation
But with multer this will not happen - you will be able to access body fields like description and name and then do...
Read more >Express-validator not working after multer function : r/node
I have a form with some text inputs and a file input, I handle the file upload with multer and I'm trying to...
Read more >Multer File Type Validation Tutorial with Example - positronX.io
Today we are going to learn, how to validate file types while uploading files with Multer. In this tutorial, we will understand how...
Read more >Top 5 express-validator Code Examples - Snyk
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... app.post('/posts/new', [ check('postTitle', 'Title must...
Read more >Handling File Uploads in Node.js with Express and Multer
That's everything we need on the view side. Validation and Sanitization. There is a handy middleware express-validator for validating and sanitizing data using...
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
@Alirezaies
Hi, i know its a little bit late, but since this is one of the first search results for this issue, one way to do it’s using “checkSchema” to check for basically anything in the request object.
You can do something like this:
And now you can have all your errors messages handled by express-validator, while the file will only be uploaded if it is a valid file acording to your multer fileFilter.
Bear in mind that if you’re using multer.only(field), etc, you will have to change the custom options in the validator accordingly, since .only will make the only file available at req.file, instead of at req.file[path].
by using
upload.single('mainImage')
before thecheck()
, no validation error will be produced byexpress-validator
, but if I put theupload.single('mainImage')
after thecheck()
I get validation errors on both empty and filled form.StackOverflow didn’t help at all