Skip to content

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

Open
@icazemier

Description

@icazemier

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: needs triageIssues which needs to be reproduced to be verified report.type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions