Messages
Browse files- App/Messages/Schema.py +2 -17
App/Messages/Schema.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
# App/Messages/Schema.py
|
| 2 |
from pydantic import BaseModel, UUID4, Field, ConfigDict, condecimal, constr, validator
|
| 3 |
from datetime import datetime
|
| 4 |
-
from typing import Optional, Dict
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
from pydantic import BaseModel, UUID4, Field, validator, ConfigDict
|
|
@@ -33,9 +33,7 @@ class MessageCreate(BaseModel):
|
|
| 33 |
|
| 34 |
|
| 35 |
class MessageResponse(BaseModel):
|
| 36 |
-
id: Optional[UUID4] =
|
| 37 |
-
default_factory=uuid.uuid4
|
| 38 |
-
) # Automatically generate UUID4 if not provided
|
| 39 |
device_id: Optional[str] = None
|
| 40 |
event: Optional[str] = None
|
| 41 |
message_id: Optional[str] = None
|
|
@@ -48,19 +46,6 @@ class MessageResponse(BaseModel):
|
|
| 48 |
created_time: Optional[datetime] = None
|
| 49 |
model_config = ConfigDict(from_attributes=True)
|
| 50 |
|
| 51 |
-
@validator("id", pre=True, always=True)
|
| 52 |
-
def validate_uuid(cls, v):
|
| 53 |
-
"""
|
| 54 |
-
Ensure all ID fields are valid UUIDs. If the input is invalid or missing,
|
| 55 |
-
generate a new UUID.
|
| 56 |
-
"""
|
| 57 |
-
if v is None or not isinstance(v, (UUID, str)):
|
| 58 |
-
return uuid4() # Generate a new UUID if not provided or invalid
|
| 59 |
-
try:
|
| 60 |
-
return UUID(v) # Validate and convert to UUID
|
| 61 |
-
except ValueError:
|
| 62 |
-
return uuid4() # Generate a new UUID if validation fails
|
| 63 |
-
|
| 64 |
# class Config:
|
| 65 |
# orm_mode = True
|
| 66 |
# allow_population_by_field_name = True
|
|
|
|
| 1 |
# App/Messages/Schema.py
|
| 2 |
from pydantic import BaseModel, UUID4, Field, ConfigDict, condecimal, constr, validator
|
| 3 |
from datetime import datetime
|
| 4 |
+
from typing import Optional, Dict, Union
|
| 5 |
import uuid
|
| 6 |
|
| 7 |
from pydantic import BaseModel, UUID4, Field, validator, ConfigDict
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
class MessageResponse(BaseModel):
|
| 36 |
+
id: Optional[Union[UUID4, str]] = None
|
|
|
|
|
|
|
| 37 |
device_id: Optional[str] = None
|
| 38 |
event: Optional[str] = None
|
| 39 |
message_id: Optional[str] = None
|
|
|
|
| 46 |
created_time: Optional[datetime] = None
|
| 47 |
model_config = ConfigDict(from_attributes=True)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# class Config:
|
| 50 |
# orm_mode = True
|
| 51 |
# allow_population_by_field_name = True
|