Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parameter exists using express-validator

I am using express-validator for validation.

I have some parameters which are not mandatory like name.

I want to validate the parameter only if was exists, else no need to validate.

I have got a solution like

if(parameter){
   req.assert('name',"Valid name is required!").isName();
}

Is there any better solution than this?

like image 772
Vishnu Avatar asked Dec 06 '22 00:12

Vishnu


1 Answers

Latest version of express-validator solved this issue

req.assert('name',"Valid name is required!").optional().isName();
like image 105
Vishnu Avatar answered Dec 29 '22 09:12

Vishnu