Reputation: 323
I have a simple validator that checks if "password" = "passwordconf"
exports.RegisterUser = [
check('username').isLength({ min: 1 , max: 10}).trim().withMessage("Length 1-10"),
check('password').isLength({ min: 6 , max: 10}).trim().withMessage("Length 6-10"),
check('passwordconf').isLength({ min: 6 , max: 10}).trim().withMessage("Length 6-10"),
check('passwordconf').custom((value , { req }) => {
if (value !== req.body.password) {
throw new Error('Password confirmation is incorrect');
}
}),
sanitizeBody('*').trim().escape(),
function ( req , res ) {
//check for errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
} else {
var user = new User({
username : req.body.username,
password : req.body.password
});
var username = req.body.username;
//check if user is in DB
User.findOne({ 'username' : username})
.exec(( err , docs) => {
if (err) {
res.send('There was an error');
return err;
} else if (!docs) {
//username does not exist
user.save((err) => {
if (err) {
return next(err)
} else {
//saved it
res.send('saved a new user!')
}
})
} else {
res.send('Username exists!')
}
})}
}]
if i comment out this statement,
check('passwordconf').custom((value , { req }) => {
if (value !== req.body.password) {
throw new Error('Password confirmation is incorrect');
}
})
the code works.I copied this statement from the official docs https://express-validator.github.io/docs/custom-validators-sanitizers.html I gives me this error when saving a new user
{"errors":[{"location":"body","param":"passwordconf","value":"password1","msg":"Cannot read property 'then' of undefined"}]}
What is missing here?
Upvotes: 1
Views: 3609
Reputation: 41440
This is a known bug, and will be fixed in the next release (current one being v5.2.0).
Custom validators that return nothing fail with that error.
To work around this, you can simply return true
from your validator:
check('passwordconf').custom((value , { req }) => {
if (value !== req.body.password) {
throw new Error('Password confirmation is incorrect');
}
return true;
})
Upvotes: 1