Skip to content
This repository was archived by the owner on Apr 3, 2023. It is now read-only.

Commit 22046b4

Browse files
authored
Merge pull request #5 from cofin/main
Enhancement: Add `str` type hint and validator for `email` field
2 parents ae642fd + ddf3569 commit 22046b4

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

pydantic_openapi_schema/v3_0_3/contact.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

3-
from pydantic import AnyUrl, BaseModel, EmailStr, Extra
3+
from pydantic import AnyUrl, BaseModel, EmailStr, Extra, validator
44

55

66
class Contact(BaseModel):
@@ -18,11 +18,31 @@ class Contact(BaseModel):
1818
The URL pointing to the contact information. MUST be in the format of a URL.
1919
"""
2020

21-
email: Optional[EmailStr] = None
21+
email: Optional[Union[EmailStr, str]] = None
2222
"""
2323
The email address of the contact person/organization. MUST be in the format of an email address.
2424
"""
2525

26+
@validator("email", pre=True)
27+
def validate_email( # pylint: disable=no-self-argument
28+
cls,
29+
v: Union[EmailStr, str],
30+
) -> EmailStr:
31+
"""Validates that email is a valid email address
32+
33+
Args:
34+
v (EmailStr|str): Holds the email string to be validated
35+
36+
Raises:
37+
ValueError: Value is not a valid email address
38+
39+
Returns:
40+
_type_: CompressionBackend
41+
"""
42+
if isinstance(v, str):
43+
v = EmailStr(v)
44+
return v
45+
2646
class Config:
2747
extra = Extra.ignore
2848
schema_extra = {

pydantic_openapi_schema/v3_1_0/contact.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Optional
1+
from typing import Optional, Union
22

3-
from pydantic import AnyUrl, BaseModel, EmailStr, Extra
3+
from pydantic import AnyUrl, BaseModel, EmailStr, Extra, validator
44

55

66
class Contact(BaseModel):
@@ -25,6 +25,26 @@ class Contact(BaseModel):
2525
MUST be in the form of an email address.
2626
"""
2727

28+
@validator("email", pre=True)
29+
def validate_email( # pylint: disable=no-self-argument
30+
cls,
31+
v: Union[EmailStr, str],
32+
) -> EmailStr:
33+
"""Validates that email is a valid email address
34+
35+
Args:
36+
v (EmailStr|str): Holds the email string to be validated
37+
38+
Raises:
39+
ValueError: Value is not a valid email address
40+
41+
Returns:
42+
_type_: CompressionBackend
43+
"""
44+
if isinstance(v, str):
45+
v = EmailStr(v)
46+
return v
47+
2848
class Config:
2949
extra = Extra.ignore
3050
schema_extra = {

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ max-line-length = "120"
7171
ignored-argument-names = "args|kwargs|_|__"
7272

7373
[tool.pylint.BASIC]
74-
good-names = "_,i,e,fn"
74+
good-names = "_,i,e,fn,v"
7575

7676
[tool.coverage.run]
7777
omit = ["*/tests/*"]

0 commit comments

Comments
 (0)