Love this approach to validation but wondering how I would compare the value of one field to another with it? For example password and confirmpassword. Any help would be appreciated.
const{body}=require('express-validator');app.post('/user',body('passwordConfirmation').custom((value,{req})=>{if(value!==req.body.password){thrownewError('Password confirmation does not match password');}// Indicates the success of this synchronous custom validatorreturntrue;}),(req,res)=>{// Handle the request});
Thank you, it worked beautifully. I'm guessing you can use a similar approach to checking for 'unique' emails/usernames by requiring the model in the validator class, using the value to search for existing emails/usernames in the database and if one is returned, to throw an error?
Love this approach to validation but wondering how I would compare the value of one field to another with it? For example password and confirmpassword. Any help would be appreciated.
You can use a custom rule
See example from the docs:
Thank you, it worked beautifully. I'm guessing you can use a similar approach to checking for 'unique' emails/usernames by requiring the model in the validator class, using the value to search for existing emails/usernames in the database and if one is returned, to throw an error?
yes