Validator.js: Can not Validate a nullable field

Created on 18 Jan 2021  路  4Comments  路  Source: validatorjs/validator.js

There is no way to pass isEmail or isMobilePhone validation with a blank value.

If there is a value of mobile number 2, and I want to check if the entry is a valid mobile number.
I can check that with isMobilePhone validator. But the problem is that the field is not mandatory and if the user does not enter anything, it's currently showing that it is an invalid mobile number.

Same thing with isEmail too.

馃悰 bug

Most helpful comment

This is express-validator a library built on top of validator.js. They are similar but have different aims.

All 4 comments

this is the correct behavior, null is not an email or mobile number. you should handle your form validation yourself this package is to validate strings.

I found a way to solve this with the optional function, from the library itself.

body('mobilenumber2').isMobilePhone().withMessage('Invalid phone number.').optional({
        nullable: true
    })

It would be great if you could mention the optional function inside the documentation.

This is express-validator a library built on top of validator.js. They are similar but have different aims.

@fedeci thanks for pointing out that for @bimalmithranpb.
For now, I am going to close this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philfreo picture philfreo  路  3Comments

mfbx9da4 picture mfbx9da4  路  4Comments

AtomicBorg picture AtomicBorg  路  3Comments

Maxreglez picture Maxreglez  路  3Comments

IOAyman picture IOAyman  路  4Comments