mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-24 13:37:13 +00:00
feat(backend): 新增模块示例和路径配置 fix(backend): 修复菜单模型名称唯一性约束 refactor(backend): 重构日志模块和路径配置 style(backend): 统一API方法命名规范 refactor(frontend): 重构API方法命名规范 fix(frontend): 修复权限校验问题 refactor: 移动定时任务测试文件到正确模块 docs: 更新模板文件路径 refactor: 优化日志记录和错误处理 fix: 修复排序默认值问题
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Optional
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
|
|
from app.core.base_schema import BaseSchema
|
|
|
|
|
|
class DemoCreateSchema(BaseModel):
|
|
"""新增模型"""
|
|
name: str = Field(..., min_length=2, max_length=50, description='名称')
|
|
status: bool = Field(True, description="是否启用(True:启用 False:禁用)")
|
|
description: Optional[str] = Field(default=None, max_length=255, description="描述")
|
|
|
|
@field_validator('name')
|
|
@classmethod
|
|
def validate_name(cls, v: str) -> str:
|
|
"""验证名称字段的格式和内容"""
|
|
# 去除首尾空格
|
|
v = v.strip()
|
|
if not v:
|
|
raise ValueError('名称不能为空')
|
|
return v
|
|
|
|
@model_validator(mode='after')
|
|
def _after_validation(self):
|
|
"""
|
|
核心业务规则校验
|
|
"""
|
|
# 长度校验:名称最小长度
|
|
if len(self.name) < 2 or len(self.name) > 50:
|
|
raise ValueError('名称长度必须在2-50个字符之间')
|
|
# 格式校验:名称只能包含字母、数字、下划线和中划线
|
|
if not self.name.isalnum() and not all(c in '-_' for c in self.name):
|
|
raise ValueError('名称只能包含字母、数字、下划线和中划线')
|
|
|
|
return self
|
|
|
|
|
|
|
|
class DemoUpdateSchema(DemoCreateSchema):
|
|
"""更新模型"""
|
|
...
|
|
|
|
|
|
class DemoOutSchema(DemoCreateSchema, BaseSchema):
|
|
"""响应模型"""
|
|
model_config = ConfigDict(from_attributes=True)
|