From faca62c4fdd372653e01e8c4cb52541edb9080ce Mon Sep 17 00:00:00 2001 From: hasansezertasan Date: Wed, 8 Nov 2023 12:52:51 +0300 Subject: [PATCH 1/6] Added __get_pydantic_json_schema__ method with format='tel' --- pydantic_extra_types/phone_numbers.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pydantic_extra_types/phone_numbers.py b/pydantic_extra_types/phone_numbers.py index 4c25e452..ab8edf52 100644 --- a/pydantic_extra_types/phone_numbers.py +++ b/pydantic_extra_types/phone_numbers.py @@ -8,7 +8,8 @@ from typing import Any, Callable, ClassVar, Generator -from pydantic import GetCoreSchemaHandler +from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler +from pydantic.json_schema import JsonSchemaValue from pydantic_core import PydanticCustomError, core_schema try: @@ -41,6 +42,15 @@ class PhoneNumber(str): max_length: int = 64 """The maximum length of the phone number.""" + + @classmethod + def __get_pydantic_json_schema__( + cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler + ) -> JsonSchemaValue: + field_schema: dict[str, Any] = {} + field_schema.update(type='string', format='tel') + return field_schema + @classmethod def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: return core_schema.general_after_validator_function( @@ -57,4 +67,4 @@ def _validate(cls, phone_number: str, _: core_schema.ValidationInfo) -> str: if not phonenumbers.is_valid_number(parsed_number): raise PydanticCustomError('value_error', 'value is not a valid phone number') - return phonenumbers.format_number(parsed_number, getattr(phonenumbers.PhoneNumberFormat, cls.phone_format)) + return phonenumbers.format_number(parsed_number, getattr(phonenumbers.PhoneNumberFormat, cls.phone_format)) \ No newline at end of file From ec8a49068bd4ddce80defe7f8686287f6e2a480f Mon Sep 17 00:00:00 2001 From: hasansezertasan Date: Thu, 9 Nov 2023 11:54:12 +0300 Subject: [PATCH 2/6] Formatted. --- pydantic_extra_types/phone_numbers.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pydantic_extra_types/phone_numbers.py b/pydantic_extra_types/phone_numbers.py index ab8edf52..1b119ae0 100644 --- a/pydantic_extra_types/phone_numbers.py +++ b/pydantic_extra_types/phone_numbers.py @@ -9,7 +9,6 @@ from typing import Any, Callable, ClassVar, Generator from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler -from pydantic.json_schema import JsonSchemaValue from pydantic_core import PydanticCustomError, core_schema try: @@ -42,14 +41,13 @@ class PhoneNumber(str): max_length: int = 64 """The maximum length of the phone number.""" - @classmethod def __get_pydantic_json_schema__( - cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler - ) -> JsonSchemaValue: - field_schema: dict[str, Any] = {} - field_schema.update(type='string', format='tel') - return field_schema + cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler + ) -> dict[str, Any]: + json_schema = handler(schema) + json_schema.update({'type': 'string', 'format': 'tel'}) + return json_schema @classmethod def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: @@ -67,4 +65,4 @@ def _validate(cls, phone_number: str, _: core_schema.ValidationInfo) -> str: if not phonenumbers.is_valid_number(parsed_number): raise PydanticCustomError('value_error', 'value is not a valid phone number') - return phonenumbers.format_number(parsed_number, getattr(phonenumbers.PhoneNumberFormat, cls.phone_format)) \ No newline at end of file + return phonenumbers.format_number(parsed_number, getattr(phonenumbers.PhoneNumberFormat, cls.phone_format)) From 470a6da247cec130f4ab684acde701f601e0a5ff Mon Sep 17 00:00:00 2001 From: hasansezertasan Date: Thu, 9 Nov 2023 12:00:30 +0300 Subject: [PATCH 3/6] Added 'test_json_schema' --- tests/test_phone_numbers.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_phone_numbers.py b/tests/test_phone_numbers.py index 62fce284..3c228fc1 100644 --- a/tests/test_phone_numbers.py +++ b/tests/test_phone_numbers.py @@ -50,3 +50,19 @@ def test_parse_error() -> None: def test_parsed_but_not_a_valid_number() -> None: with pytest.raises(ValidationError, match='value is not a valid phone number'): Something(phone_number='+1 555-1212') + +def test_json_schema() -> None: + assert Something.model_json_schema() == { + 'title': 'Something', + 'type': 'object', + 'properties': { + 'phone_number': { + 'title': 'Phone Number', + 'type': 'string', + 'format': 'tel', + 'minLength': 7, + 'maxLength': 64, + } + }, + 'required': ['phone_number'], + } From 8c8db1c072cc38b9c7147e28d099d2e3c3d2ee16 Mon Sep 17 00:00:00 2001 From: hasansezertasan Date: Thu, 9 Nov 2023 12:02:49 +0300 Subject: [PATCH 4/6] Reformatted. --- tests/test_phone_numbers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_phone_numbers.py b/tests/test_phone_numbers.py index 3c228fc1..8ae39fd6 100644 --- a/tests/test_phone_numbers.py +++ b/tests/test_phone_numbers.py @@ -51,6 +51,7 @@ def test_parsed_but_not_a_valid_number() -> None: with pytest.raises(ValidationError, match='value is not a valid phone number'): Something(phone_number='+1 555-1212') + def test_json_schema() -> None: assert Something.model_json_schema() == { 'title': 'Something', From bf5cd87b1f3ff73a3a86790b2ca6307c287f0008 Mon Sep 17 00:00:00 2001 From: hasansezertasan Date: Sat, 11 Nov 2023 11:56:47 +0300 Subject: [PATCH 5/6] Change format value from 'tel' to 'phone' --- pydantic_extra_types/phone_numbers.py | 2 +- tests/test_phone_numbers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pydantic_extra_types/phone_numbers.py b/pydantic_extra_types/phone_numbers.py index 1b119ae0..f399dc00 100644 --- a/pydantic_extra_types/phone_numbers.py +++ b/pydantic_extra_types/phone_numbers.py @@ -46,7 +46,7 @@ def __get_pydantic_json_schema__( cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler ) -> dict[str, Any]: json_schema = handler(schema) - json_schema.update({'type': 'string', 'format': 'tel'}) + json_schema.update({'type': 'string', 'format': 'phone'}) return json_schema @classmethod diff --git a/tests/test_phone_numbers.py b/tests/test_phone_numbers.py index 8ae39fd6..1d3a1051 100644 --- a/tests/test_phone_numbers.py +++ b/tests/test_phone_numbers.py @@ -60,7 +60,7 @@ def test_json_schema() -> None: 'phone_number': { 'title': 'Phone Number', 'type': 'string', - 'format': 'tel', + 'format': 'phone', 'minLength': 7, 'maxLength': 64, } From ca9fc007f2b24e43794d88bf449577ac829858a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Sezer=20Ta=C5=9Fan?= <13135006+hasansezertasan@users.noreply.github.com> Date: Sat, 11 Nov 2023 16:47:58 +0300 Subject: [PATCH 6/6] Update pydantic_extra_types/phone_numbers.py Co-authored-by: Marcelo Trylesinski --- pydantic_extra_types/phone_numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydantic_extra_types/phone_numbers.py b/pydantic_extra_types/phone_numbers.py index f399dc00..f776cdc4 100644 --- a/pydantic_extra_types/phone_numbers.py +++ b/pydantic_extra_types/phone_numbers.py @@ -46,7 +46,7 @@ def __get_pydantic_json_schema__( cls, schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler ) -> dict[str, Any]: json_schema = handler(schema) - json_schema.update({'type': 'string', 'format': 'phone'}) + json_schema.update({'format': 'phone'}) return json_schema @classmethod