Skip to content

Does IsMobilePhone accept an array ? #1866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Dxxxxxxww opened this issue Dec 22, 2022 · 9 comments · Fixed by #2360
Closed

Does IsMobilePhone accept an array ? #1866

Dxxxxxxww opened this issue Dec 22, 2022 · 9 comments · Fixed by #2360
Labels
type: question Questions about the usage of the library.

Comments

@Dxxxxxxww
Copy link

I was trying to...
@IsMobilePhone(['sk-SK', 'sr-RS']) mobile: string;
The problem:

it show a message which is 'type string[] can’t pass to type MobilePhoneLocale'. but in isMobilePhone.d.ts, the annotation says 'locale is either an array of locales, or one of ...'

`

  • Checks if the string is a mobile phone number (locale is either an array of locales (e.g ['sk-SK', 'sr-RS'])
  • OR one of ['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'be-BY', ...
    `

so how can i use IsMobilePhone and pass an array to param locale

@Dxxxxxxww Dxxxxxxww added the type: question Questions about the usage of the library. label Dec 22, 2022
@Dxxxxxxww
Copy link
Author

”class-validator": "^0.14.0"

@braaar
Copy link
Member

braaar commented Dec 22, 2022

Could you provide a simple reproduction of this?

This works for me:

class MyClass {
  @IsMobilePhone(['fi-FI', 'nn-NO'])
  phone: string;
}

const plain = { phone: '+4744664477' };

const instance = await plainToInstance(MyClass, plain);

const errors = await validate(instance);

console.log(errors); // []

@braaar
Copy link
Member

braaar commented Dec 22, 2022

the type MobilePhoneLocale is any, as far as I understand. So assigning a string[] to it shouldn't be a problem?

@Dxxxxxxww
Copy link
Author

Could you provide a simple reproduction of this?

This works for me:

class MyClass {
  @IsMobilePhone(['fi-FI', 'nn-NO'])
  phone: string;
}

const plain = { phone: '+4744664477' };

const instance = await plainToInstance(MyClass, plain);

const errors = await validate(instance);

console.log(errors); // []

my simple is like yours. which version are you using?

this is my simple. src/validator.ts file will show you my problem("Argument of type 'string[]' is not assignable to parameter of type 'MobilePhoneLocale'.").

simple

@braaar
Copy link
Member

braaar commented Jan 4, 2023

I was using version 0.13.2 of class-validator.

I created a new project and installed everything fresh with 0.14.0, and now I am getting the same type error, even if the validator version has not changed in class-validator 0.14.0 (It's been ^13.7.0 for a while). Curious.

Now I've started digging into it and I'm noticing that all the ValidatorJS argument types aren't resolved properly in my project that uses 0.13.2.

Like, check this out:
Screenshot 2023-01-04 at 16 28 50

Obviously typescript wouldn't complain if this type just defaulting to any. In version 0.14.0 the types are resolved correctly and typescript correctly complains when I try to put in an array.

A quick look at another decorator, IsEmail yields the same problem with an unresolved type.

Is this consistently the case? I can't seem to find something in the changes since 0.13.2 that would explain how the types didn't work correctly in 0.13.2 but started working in 0.14.0.

Could it be that @types/validator fixed something in its types which isn't visible in our package.json, but could have changed in our package-lock.json?

Have you experienced this, @NoNameProvided?

@braaar
Copy link
Member

braaar commented Jan 4, 2023

When I look at my yarn.lock file in my newly created project, I see this:

class-validator@^0.14.0:
  version "0.14.0"
  resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.14.0.tgz#40ed0ecf3c83b2a8a6a320f4edb607be0f0df159"
  integrity sha512-ct3ltplN8I9fOwUd8GrP8UQixwff129BkEtuWDKL5W45cQuLd19xqmTLu5ge78YDm/fdje6FMt0hGOhl0lii3A==
  dependencies:
    "@types/validator" "^13.7.10"
    libphonenumber-js "^1.10.14"
    validator "^13.7.0"

I suspect a change occured somewhere between version 13.7.0 and 13.7.10 of @types/validator.

@JayPe69
Copy link

JayPe69 commented Jun 9, 2023

Any news on this problem ? I got it too

@badien
Copy link

badien commented Jan 6, 2024

I think this is a bug of V0.14.0, when you use validator@^13.9.0 and it is installed latest version 13.11.0 while isMobilePhone of validator is break and not use locale as array input. Please check and fix. Thanks

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the usage of the library.
Development

Successfully merging a pull request may close this issue.

4 participants