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

Feature: Add phone number validator #203

Merged
merged 7 commits into from
Aug 18, 2024

Conversation

mZbZ
Copy link
Contributor

@mZbZ mZbZ commented Aug 16, 2024

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

Copy link

codecov bot commented Aug 16, 2024

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 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
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
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
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

Copy link
Collaborator

@yezz123 yezz123 left a comment

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
20 checks passed
@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