mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-23 13:13:09 +00:00
feat: 优化表单提交逻辑和添加表单验证
refactor: 重构后端模型验证逻辑 fix: 修复资源文件路径处理和下载服务 style: 调整前端样式和布局 perf: 优化AI错误处理和提示信息 docs: 更新上传组件提示信息 test: 添加表单提交测试用例 chore: 更新依赖和配置文件
This commit is contained in:
@@ -28,6 +28,36 @@ class MenuCreateSchema(BaseModel):
|
||||
parent_id: Optional[int] = Field(default=None, ge=1, description="父菜单ID")
|
||||
description: Optional[str] = Field(default=None, max_length=255, description="描述")
|
||||
|
||||
@model_validator(mode='before')
|
||||
@classmethod
|
||||
def _normalize(cls, values):
|
||||
if isinstance(values, dict):
|
||||
# 字符串去空格
|
||||
for k in ["name", "icon", "permission", "route_name", "route_path", "component_path", "redirect", "title", "description"]:
|
||||
if k in values and isinstance(values[k], str):
|
||||
values[k] = values[k].strip() or None if values[k].strip() == "" else values[k].strip()
|
||||
# 布尔兼容
|
||||
for k in ["status", "keep_alive", "hidden", "always_show", "affix"]:
|
||||
if k in values and isinstance(values[k], str):
|
||||
values[k] = values[k].strip().lower() in {"true", "1", "yes", "y"}
|
||||
# 父ID转整型
|
||||
if "parent_id" in values and isinstance(values["parent_id"], str):
|
||||
try:
|
||||
values["parent_id"] = int(values["parent_id"].strip())
|
||||
except Exception:
|
||||
pass
|
||||
# 路由名/路径规范
|
||||
import re
|
||||
if "route_name" in values and isinstance(values["route_name"], str):
|
||||
rn = values["route_name"]
|
||||
if rn and not re.match(r"^[A-Za-z][A-Za-z0-9_.-]{1,99}$", rn):
|
||||
raise ValueError("路由名称需字母开头,仅含字母/数字/_ . -")
|
||||
if "route_path" in values and isinstance(values["route_path"], str):
|
||||
rp = values["route_path"]
|
||||
if rp and not rp.startswith("/"):
|
||||
raise ValueError("路由路径需以 / 开头")
|
||||
return values
|
||||
|
||||
@model_validator(mode='after')
|
||||
def validate_fields(self):
|
||||
return menu_request_validator(self)
|
||||
|
||||
Reference in New Issue
Block a user