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

Commit 00a73b4

Browse files
committed
FEAT: Added str to validator so that pylint does not report a linting error when passing a string instead of EmailStr.
1 parent ae642fd commit 00a73b4

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

pydantic_openapi_schema/v3_0_3/contact.py

Lines changed: 22 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,10 +18,29 @@ 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
"""
25+
@validator("email", pre=True)
26+
def validate_email( # pylint: disable=no-self-argument
27+
cls, v: Union[EmailStr, str],
28+
) -> EmailStr:
29+
"""Validates that email is a valid email address
30+
31+
Args:
32+
v (EmailStr|str): Holds the email string to be validated
33+
34+
Raises:
35+
ValueError: Value is not a valid email address
36+
37+
Returns:
38+
_type_: CompressionBackend
39+
"""
40+
if isinstance(v, str):
41+
v = EmailStr(v)
42+
return v
43+
2544

2645
class Config:
2746
extra = Extra.ignore

pydantic_openapi_schema/v3_1_0/contact.py

Lines changed: 20 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):
@@ -24,6 +24,24 @@ class Contact(BaseModel):
2424
The email address of the contact person/organization.
2525
MUST be in the form of an email address.
2626
"""
27+
@validator("email", pre=True)
28+
def validate_email( # pylint: disable=no-self-argument
29+
cls, 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
2745

2846
class Config:
2947
extra = Extra.ignore

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)