refactor(backend): update employee schema
- schemas/employee.py: update employee data schemas
This commit is contained in:
@@ -5,6 +5,16 @@ from datetime import date, datetime
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field
|
||||
|
||||
|
||||
def _parse_optional_date(value: str | None, label: str) -> date | None:
|
||||
if not value:
|
||||
return None
|
||||
|
||||
try:
|
||||
return datetime.strptime(value, "%Y-%m-%d").date()
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"{label}格式必须为 YYYY-MM-DD。") from exc
|
||||
|
||||
|
||||
class EmployeeHistoryRead(BaseModel):
|
||||
action: str
|
||||
owner: str
|
||||
@@ -96,10 +106,10 @@ class EmployeeCreate(BaseModel):
|
||||
role_codes: list[str] = Field(default_factory=lambda: ["user"])
|
||||
|
||||
def parsed_birth_date(self) -> date | None:
|
||||
return datetime.strptime(self.birth_date, "%Y-%m-%d").date() if self.birth_date else None
|
||||
return _parse_optional_date(self.birth_date, "出生日期")
|
||||
|
||||
def parsed_join_date(self) -> date | None:
|
||||
return datetime.strptime(self.join_date, "%Y-%m-%d").date() if self.join_date else None
|
||||
return _parse_optional_date(self.join_date, "入职日期")
|
||||
|
||||
|
||||
class EmployeeUpdate(BaseModel):
|
||||
@@ -118,7 +128,7 @@ class EmployeeUpdate(BaseModel):
|
||||
password: str | None = Field(default=None, min_length=5, max_length=128)
|
||||
|
||||
def parsed_birth_date(self) -> date | None:
|
||||
return datetime.strptime(self.birth_date, "%Y-%m-%d").date() if self.birth_date else None
|
||||
return _parse_optional_date(self.birth_date, "出生日期")
|
||||
|
||||
def parsed_join_date(self) -> date | None:
|
||||
return datetime.strptime(self.join_date, "%Y-%m-%d").date() if self.join_date else None
|
||||
return _parse_optional_date(self.join_date, "入职日期")
|
||||
|
||||
Reference in New Issue
Block a user