typestack / class-validator

Decorator-based property validation for classes.
MIT License
10.93k stars 788 forks source link

feature: Allow `IsMobilePhone` Decorator to Accept Multiple Locales #2489

Open naithagoni opened 3 months ago

naithagoni commented 3 months ago

Description

I'm currently using the validation decorators in my NestJS DTOs. However, I'm encountering limitations with the IsMobilePhone decorator, as it only accepts a single locale. This issue persists with "class-validator": "^0.14.1".

For example:

@IsMobilePhone(
  'en-IN',
  { strictMode: true },
  {
    message: 'Phone number must be a valid Indian phone number',
  }
)
phoneNumber: string

Proposed Solution

The IsMobilePhone decorator should be enhanced to accept multiple locales. This would allow for validation against phone numbers from multiple regions. For instance:

@IsMobilePhone(
  ['en-IN', 'en-US', 'de-DE'],
  { strictMode: true },
  {
    message: 'Phone number must be a valid phone number',
  }
)
phoneNumber: string

This enhancement would significantly improve the flexibility and usability of the IsMobilePhone decorator.

kurbar commented 3 months ago

The underlying validator library (that class-validator uses under the hood for IsMobilePhone) actually supports specifying multiple locales as an array but the Decorator typing does not support it.

townegr commented 2 months ago

From a previously related-issue, looks like there was some light chatter here: https://github.com/typestack/class-validator/issues/1866 but not much follow-through. Any progress on this?