pydantic / pydantic-extra-types

Extra Pydantic types.
MIT License
191 stars 50 forks source link

How to set region-code or format-type for PhoneNumber? #107

Open nameer opened 11 months ago

nameer commented 11 months ago

The documentation has no example to override the default-set region code or format.

I have also gone through stack overflow. The suggestions found there were not optimal.

One of the suggestions is as follows:

from pydantic import BaseModel
from pydantic_extra_types.phone_numbers import PhoneNumber

PhoneNumber.phone_format = 'E164' #'INTERNATIONAL', 'NATIONAL'

class User(BaseModel):
    name: str
    phone_number: PhoneNumber

But this will restrict the whole app to use a single format and region code. If I need another format in another place, this will be overridden.

What is the correct way of setting region code and format for a model?

Kludex commented 11 months ago

Yeah. I think we need to refactor the logic we have in place.

Abdelgha-4 commented 4 months ago

I think a Field like behavior would be useful:

class User(BaseModel):
    name: str
    phone_number: PhoneNumber(phone_format="E164", ...)