123456789101112131415161718192021222324252627282930 |
- from pydantic_settings import BaseSettings
- from pydantic import field_validator
- import re
- class AppConfig(BaseSettings):
- """应用基础配置"""
- name: str = "Smart Question Bank"
- version: str = "1.0.0"
- debug: bool = False
- @field_validator('name')
- def validate_name(cls, value):
- if not value or len(value) < 2:
- raise ValueError('应用名称不能为空且至少2个字符')
- return value
- @field_validator('version')
- def validate_version(cls, value):
- if not re.match(r'^\d+\.\d+\.\d+$', value):
- raise ValueError('版本号格式必须为x.x.x')
- return value
- def to_string(self, indent: int = 0) -> str:
- """将配置转换为字符串"""
- indent_str = ' ' * indent
- result = []
- for field_name, field_value in self.__dict__.items():
- result.append(f"{indent_str}{field_name}: {field_value}")
- return '\n'.join(result)
|