forked from mtusi2026/mtusi2026lab2
Add first lab code
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
class CreateChat(ContractModel):
|
||||
title: StrictStr = Field(min_length=1, max_length=100)
|
||||
member_ids: List[StrictStr] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
def validate_title(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank title")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
def validate_member_ids(cls, value):
|
||||
if len(value) != len(set(value)):
|
||||
raise ValueError("duplicate member_ids")
|
||||
return value
|
||||
|
||||
|
||||
class Chat(ContractModel):
|
||||
id: str
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class ChatPage(ContractModel):
|
||||
items: List[Chat]
|
||||
next_cursor: Optional[str]
|
||||
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
class ContractModel(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class Health(ContractModel):
|
||||
status: str
|
||||
@@ -0,0 +1,29 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
class CreateMessage(ContractModel):
|
||||
text: StrictStr = Field(min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
def validate_text(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank text")
|
||||
return value
|
||||
|
||||
|
||||
class Message(ContractModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class MessagePage(ContractModel):
|
||||
items: List[Message]
|
||||
next_cursor: Optional[str]
|
||||
@@ -0,0 +1,33 @@
|
||||
import re
|
||||
|
||||
from pydantic import Field, StrictStr, field_validator
|
||||
|
||||
from .common import ContractModel
|
||||
|
||||
|
||||
|
||||
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||
|
||||
|
||||
class CreateUser(ContractModel):
|
||||
username: StrictStr
|
||||
display_name: StrictStr = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("username")
|
||||
def validate_username(cls, value):
|
||||
if not USERNAME_RE.fullmatch(value):
|
||||
raise ValueError("invalid username")
|
||||
return value
|
||||
|
||||
@field_validator("display_name")
|
||||
def validate_display_name(cls, value):
|
||||
if not value.strip():
|
||||
raise ValueError("blank display_name")
|
||||
return value
|
||||
|
||||
|
||||
class User(ContractModel):
|
||||
id: str
|
||||
username: str
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
created_at: str
|
||||
Reference in New Issue
Block a user