typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.89k stars 786 forks source link

Does IsMobilePhone accept an array ? #1866

Closed Dxxxxxxww closed 8 months ago

Dxxxxxxww commented 1 year ago

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 ...'

`

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

Dxxxxxxww commented 1 year ago

”class-validator": "^0.14.0"

braaar commented 1 year ago

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 commented 1 year ago

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

Dxxxxxxww commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Any news on this problem ? I got it too

badien commented 8 months ago

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

github-actions[bot] commented 7 months ago

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.