-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.py
67 lines (56 loc) · 2.22 KB
/
validators.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
from pydantic import BaseModel, field_validator, model_validator, EmailStr
class CryptoData(BaseModel):
name: str
ticker: str
price: float | int
mcap: int
volume: int
# With enough data in db, will be able to delete following variables and replace w/ functions to calculate
change_usd_24h: float
change_percent_24h: float
change_percent_7d: float
change_percent_30d: float
@classmethod
@model_validator(mode="before")
def validate_data(cls, values):
for field in ["price", "mcap", "volume", "change_usd_24h",
"change_percent_24h", "change_percent_7d", "change_percent_30d"]:
if values.get(field) is None:
raise KeyError(f"{field} is missing from the API response.")
for field in ["price", "mcap", "volume"]:
if values[field] < 0:
raise ValueError(f"{field} cannot be negative.")
return values
class StockData(BaseModel):
name: str
ticker: str
close: float
mcap: float | None # Indices return "None" though Trading View
volume: int
# With enough data in db, will be able to delete following variables and replace w/ functions to calculate
change_value_24h: float
change_percent_24h: float
change_value_weekly: float
change_percent_weekly: float
change_value_monthly: float
change_percent_monthly: float
@classmethod
@model_validator(mode="before")
def validate_data(cls, values):
for field in ["close", "change_value_24h", "change_percent_24h", "change_value_weekly",
"change_percent_weekly", "change_value_monthly", "change_percent_monthly"]:
if values.get(field) is None:
raise KeyError(f"'{field}' is missing from the API response.")
if values["close"] < 0:
raise ValueError(f"{values['ticker']} candle close value cannot be negative.")
return values
class GoogleDriveData(BaseModel):
data: list[dict]
class UserData(BaseModel):
email: EmailStr
@classmethod
@field_validator('email', mode="before")
def validate_email(cls, value):
if value is None or value == '':
raise ValueError('Email is required')
return value