Skip to content

Feature: Add phone number validator - #203

Merged
yezz123 merged 7 commits into
pydantic:mainfrom
mZbZ:feature/phone_number_validator
Aug 18, 2024
Merged

yezz123 merged 7 commits into
pydantic:mainfrom
mZbZ:feature/phone_number_validator

Conversation

@mZbZ

@mZbZ mZbZ commented Aug 16, 2024

Copy link
Copy Markdown
Contributor

Hi

This PR adds a new custom BeforeValidator annotation based phone number type instead of the concrete type that exists:
PhoneNumberValidator

Args:
    default_region (str | None): The default region code to use when parsing phone numbers without an international prefix.
        If `None` (default), the region must be supplied in the phone number as an international prefix.
    number_format (str): The format of the phone number to return. See `phonenumbers.PhoneNumberFormat` for valid values.
    supported_regions (list[str]): The supported regions. If empty, all regions are supported (default).
Returns:
    str: The formatted phone number.

This provides be an option for more composable phone number types, e.g:

from phonenumbers import PhoneNumber
from pydantic_extra_types.phone_numbers import PhoneNumberValidator
 
# Supports any number supplied with an international code
AnyNumberType = Annotated[
    Union[str, PhoneNumber],
    PhoneNumberValidator()
]
# Supports only US phone numbers
USNumberType = Annotated[
    Union[str, PhoneNumber],
    PhoneNumberValidator(supported_regions=['US'], default_region='US')
]

class SomeModel(BaseModel):
    phone_number: AnyNumberType
    us_number: USNumberType
  • Seperate type instances avoid any confusion with the class scoped variables
  • Follows type convention set with custom pydantic types being Annotation based where possible

@codecov

codecov Bot commented Aug 16, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e186814) to head (b3d12f7).
Report is 42 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #203    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           11        17     +6     
  Lines          685       999   +314     
  Branches       169       244    +75     
==========================================
+ Hits           685       999   +314     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread tests/test_phone_numbers_validator.py Outdated
Comment on lines +111 to +157
def test_json_schema() -> None:
assert Numbers.model_json_schema() == {
'title': 'Numbers',
'type': 'object',
'properties': {
'phone_number': {
'title': 'Phone Number',
'anyOf': [
{'type': 'string', 'format': 'phone'},
{'type': 'null'},
],
'default': None,
},
'na_number': {
'title': 'Na Number',
'anyOf': [
{'type': 'string', 'format': 'phone'},
{'type': 'null'},
],
'default': None,
},
'uk_number': {
'title': 'Uk Number',
'anyOf': [
{'type': 'string', 'format': 'phone'},
{'type': 'null'},
],
'default': None,
},
},
}

class SomethingElse(BaseModel):
phone_number: Number

assert SomethingElse.model_json_schema() == {
'title': 'SomethingElse',
'type': 'object',
'properties': {
'phone_number': {
'title': 'Phone Number',
'type': 'string',
'format': 'phone',
}
},
'required': ['phone_number'],
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work @mZbZ but can you migrate this part of the tests to tests/test_json_schema.py in the tests directory so we can keep the tree of tests follow a pattern

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

No problem

FYI, there is still a codecov report that is still running...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

No problem

FYI, there is still a codecov report that is still running...

somethings its get bugged and stick like this you can try another change or an empty commit and its will be fixed

@yezz123 yezz123 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@yezz123
yezz123 merged commit d409bfa into pydantic:main Aug 18, 2024
@mZbZ
mZbZ deleted the feature/phone_number_validator branch August 18, 2024 18:41
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

Successfully merging this pull request may close these issues.

2 participants