Shahab Hashmi
Shahab Hashmi

Reputation: 45

Express Validator

I'm using express-validator for making a Nodejs MongoDB application. I've installed the latest version of express-validator and Node but coming across "TypeError: expressValidator is not a function" as an error.

app.use(expressValidator({
    errorFormatter: function(param, msg, value){
        var namespace = param.split('.'),
        root = namespace.shift(),
        formParam = root;

        while(namespace.length){
            formParam += '[' + namespace.shift() + ']';
        } return {
            param: formParam,
            msg: msg,
            value: value
        };
    }
})); 

Upvotes: 1

Views: 147

Answers (1)

Abhishek
Abhishek

Reputation: 104

I guess you should not require express-validator as expressValidator.
According to express-validator V 6.10.0 docs you should do some thing like this

const { body, validationResult } = require("express-validator");

app.get('/signup',body('email').isEmail(), body('password').isLength({min: 5}), (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
       // handle error
    }
    // save user in DB
});
 

we can also use custom validation for our fields checkout docs for more information.

Upvotes: 1

Related Questions