Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nameer opened this issue Nov 16, 2023 · 3 comments
Open

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

nameer opened this issue Nov 16, 2023 · 3 comments

Comments

@nameer
Copy link

nameer commented Nov 16, 2023

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
Copy link
Member

Kludex commented Nov 16, 2023

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

@Abdelgha-4
Copy link

Abdelgha-4 commented Jun 18, 2024

I think a Field like behavior would be useful:

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

@dodumosu
Copy link

dodumosu commented Dec 5, 2024

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?

i was looking for an answer to this, and found a badly formatted example on this page. below is my personal spin on it, and it seems to work for me

from typing import Annotated, Union

from pydantic_extra_types.phone_numbers import PhoneNumber, PhoneNumberValidator

PhoneNumberType = Annotated[
    Union[str, PhoneNumber],
    PhoneNumberValidator(default_region="NG", number_format="E164"),
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants