class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

fix: Types: Can't pass an array type of locales in `@IsMobilePhone()` decorator

Open icazemier opened this issue 7 months ago • 0 comments

Description

Hi,

I dearly want to report this issue about seemingly wrong typings in @IsMobilePhone().

Your documentation states (Tag: 0.14.2):

(locale is either an array of locales (e.g ['sk-SK', 'sr-RS'])

But one can't pass an array, only a string value

While looking at: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/372a2368108145e266bab7dc18ffff3806d10ee7/types/validator/index.d.ts#L1064

Validator library does accept an array of locales.

Minimal code-snippet showcasing the problem

Does not accept array type (pre-compile time):

  class ContactInfo {

    @IsOptional()
    @IsMobilePhone(['nl-NL'])
    mobilePhone?: string
  }

Does accept hacky type (but types are not aligned):

  class ContactInfo {

    @IsOptional()
    @IsMobilePhone(['nl-NL'] as unknown as MobilePhoneLocale)
    mobilePhone?: string
  }

Expected behavior

class-validator decorator @IsMobilePhone() should ensure this also accepts an array. And ensure it is tested by the unit tests.

For example:

locale?: ValidatorJS.MobilePhoneLocale | Array<ValidatorJS.MobilePhoneLocale>

Actual behavior

  • Typing suggests it only accepts a string shows pre compile time error: Argument of type 'string[]' is not assignable to parameter of type 'MobilePhoneLocale | undefined'.

Kind regards.

icazemier avatar May 09 '25 11:05 icazemier