-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.py
87 lines (70 loc) · 2.54 KB
/
schemas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import re
from pydantic import BaseModel, EmailStr, field_validator
from config.validators_exception import ValidationError
class SignUpOrLogin(BaseModel):
email: EmailStr
password: str
@field_validator("email")
def email_validator(
cls,
value: str
) -> str | ValueError:
"""
Check Input Email
"""
if not value.endswith("@gmail.com") and not value.endswith("@ukr.net"):
raise ValidationError(f"Expected: @gmail.com or @ukr.net but got {value}")
else:
return value
@field_validator("password")
def password_validator(
cls,
value: str
) -> str | ValueError:
"""
Check Input Password
"""
if len(value) < 8 or len(value) > 16:
raise ValidationError("Password must contain between 8 and 16 characters long")
if not re.search(r'[A-Z]', value):
raise ValidationError("Password must contain at least one uppercase letter")
if not re.search(r'[a-z]', value):
raise ValidationError("Password must contain at least one lowercase letter")
if not re.search(r'[0-9]', value):
raise ValidationError("Password must contain at least one digit")
if not re.search(r'[!@#$%^&*(),.?":{}|<>_]', value):
raise ValidationError("Password must contain at least one special character")
return value
class PostAdd(BaseModel):
text: str
@field_validator("text")
def text_validator(
cls,
value: str
) -> str | ValueError:
"""
Check Input Text
"""
# If Payload Large than 1 MB
if len(value.encode('utf-8')) > 1 * 1024 * 1024:
raise ValidationError('Payload Too Large')
# Only allow letters, numbers, spaces, and common punctuation
if not re.match("^[a-zA-Z0-9\s.,!?]*$", value):
raise ValidationError("Text Contains Invalid Characters")
# Minimum 5 Words
if len(value.split()) < 5:
raise ValidationError("Text must contain at least 5 words")
return value
class PostDelete(BaseModel):
post_id: int
@field_validator("post_id", mode="before") # Mode Before -> Call Validator Before Pydantic Format Data
def post_id_validator(
cls,
value: str
) -> int | ValidationError:
"""
Check Input Post Id
"""
if not isinstance(value, int):
raise ValidationError("Post Id Can be Only Integer")
return value