-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.py
More file actions
64 lines (48 loc) · 1.57 KB
/
Copy pathvalidator.py
File metadata and controls
64 lines (48 loc) · 1.57 KB
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
from typing import Literal, Optional
from pydantic import BaseModel
SUPPORTED_SCHEMA_VERSIONS = {"1.0"}
class UserRegisteredEvent(BaseModel):
schema_version: str = "1.0"
event: Literal["user_registered"]
user_id: str
phone: str
entity_type: str
class OTPVerifiedEvent(BaseModel):
schema_version: str = "1.0"
event: Literal["otp_verified"]
user_id: str
phone: str
class ProfileUpdatedEvent(BaseModel):
schema_version: str = "1.0"
event: Literal["profile_updated"]
user_id: str
profile_id: str
entity_type: str
name: Optional[str] = None
email: Optional[str] = None
location: Optional[str] = None
class PasswordChangedEvent(BaseModel):
schema_version: str = "1.0"
event: Literal["password_changed"]
user_id: str
phone: str
EVENT_MODELS = {
"user_registered": UserRegisteredEvent,
"otp_verified": OTPVerifiedEvent,
"profile_updated": ProfileUpdatedEvent,
"password_changed": PasswordChangedEvent,
}
def parse_event(data: dict):
"""Validate an event dict and return a typed model instance.
Raises:
ValueError: unknown schema_version or event type
pydantic.ValidationError: payload does not match the model
"""
version = data.get("schema_version")
if version not in SUPPORTED_SCHEMA_VERSIONS:
raise ValueError(f"Unsupported schema_version: {version!r}")
event_type = data.get("event")
model_cls = EVENT_MODELS.get(event_type)
if model_cls is None:
raise ValueError(f"Unknown event type: {event_type!r}")
return model_cls(**data)