validatorjs / validator.js

String validation
MIT License
23.01k stars 2.29k forks source link

`isMobilePhone` not defined? #1988

Open jianmin-chen opened 2 years ago

jianmin-chen commented 2 years ago

Describe the bug I'm running the latest version of the library and isMobilePhone is not defined. I imported the validator functions as so:

import isEmail from "validator/lib/isEmail.js";
import isMobilePhone from "validator/lib/isMobilePhone.js";

isEmail works, but using isMobilePhone returns an error. Is there something I'm not aware of? I searched node_modules/validator and isMobilePhone does not appear to be in either es/lib or lib/util.

Examples

...
phoneNumber: ValidatorError: isMobilePhone is not a function
...

Additional context Validator.js version: 13.7.0 Node.js version: 16.14.2 OS platform: Windows

mario-zancanaro commented 2 years ago

It is happening here too...

I don't have too much context from the isMobilePhone file but I guess there's tow export there. The default and a locales one.

The object returned looks like that:

{
  default: [Function: isMobilePhone],
  locales: [...]
}

@jianmin-chen if it helps, you can still use:

import validator from "validator";

validator.isMobilePhone('999...'); // false | true
0xHaku commented 2 years ago

Indeed isMobilePhone.js has two exports, default and locales. In index.js it imports the following and I think you need to do the same. https://github.com/validatorjs/validator.js/blob/cfcf9113c69b97477c409909a8729fa8efa0d595/src/index.js#L81

The same is true for all validators that use locales.

Since it is not a bug, I think it would be better to add a note in the readme about the usage of some validators.

https://github.com/validatorjs/validator.js/blob/cfcf9113c69b97477c409909a8729fa8efa0d595/README.md?plain=1#L41-L45

What do you think?

jeftran commented 1 year ago

I got the same issue with JEST, could anyone please help? image

WikiRik commented 1 year ago

I got the same issue with JEST, could anyone please help? image

This seems to be unrelated. You probably just need to install validator through npm/pnpm/yarn