38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
"""手工派车日历输入,只接收可写业务字段。"""
|
|
import re
|
|
from datetime import date
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
class CreateBody(BaseModel):
|
|
model_config = ConfigDict(populate_by_name=True, extra="forbid", str_strip_whitespace=True)
|
|
|
|
dispatch_date: date = Field(..., alias="dispatchDate", description="派车日期,范围 1900-01-01 至 2100-12-31")
|
|
vehicle_id: str = Field(..., alias="vehicleId", min_length=1, max_length=64)
|
|
product_id: str = Field(..., alias="productId", min_length=1, max_length=64)
|
|
origin: str = Field(..., min_length=1, max_length=255)
|
|
destination: str = Field(..., min_length=1, max_length=255)
|
|
remark: Optional[str] = Field(default=None, max_length=500)
|
|
|
|
@field_validator("dispatch_date", mode="before")
|
|
@classmethod
|
|
def validate_date_format(cls, value):
|
|
if not isinstance(value, str) or not re.fullmatch(r"[0-9]{4}-[0-9]{2}-[0-9]{2}", value):
|
|
raise ValueError("派车日期须为 YYYY-MM-DD")
|
|
return value
|
|
|
|
|
|
@field_validator("dispatch_date")
|
|
@classmethod
|
|
def validate_date_range(cls, value: date) -> date:
|
|
# Pydantic 2.5 的 date ge/le 会生成不合法的 OpenAPI 数字范围约束。
|
|
if not date(1900, 1, 1) <= value <= date(2100, 12, 31):
|
|
raise ValueError("派车日期须在 1900-01-01 至 2100-12-31 范围内")
|
|
return value
|
|
|
|
|
|
class UpdateBody(CreateBody):
|
|
"""PUT 提交完整表单。"""
|