You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.7 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from pydantic import BaseModel, Field, field_validator
from app.api.v1.module_energy.variable.schema import VariableItemSchema
class TemplateChapterRuleSchema(BaseModel):
"""模板章节规则配置项。"""
id: int | None = Field(default=None, ge=1, description="章节规则标注ID存在则更新不传则新增")
chapter_name: str = Field(..., min_length=1, max_length=128, description="章节名称")
chapter_order: int = Field(default=999, ge=0, description="章节排序")
rule_id: int = Field(..., ge=1, description="关联规则ID")
description: str | None = Field(default=None, max_length=500, description="描述")
@field_validator("chapter_name")
@classmethod
def validate_chapter_name(cls, value: str) -> str:
value = value.strip()
if not value:
raise ValueError("章节名称不能为空")
return value
class TemplateConfigSaveSchema(BaseModel):
"""模板章节规则和变量关联统一保存模型。"""
template_id: int = Field(..., ge=1, description="模板ID")
chapters: list[TemplateChapterRuleSchema] = Field(default_factory=list, description="章节规则标注列表")
variables: list[VariableItemSchema] = Field(default_factory=list, description="变量关联列表")
class TemplateSingleChapterConfigSaveSchema(TemplateConfigSaveSchema):
"""单个章节标注和变量关联保存模型。"""
@field_validator("chapters")
@classmethod
def validate_single_chapter(cls, value: list[TemplateChapterRuleSchema]) -> list[TemplateChapterRuleSchema]:
if len(value) != 1:
raise ValueError("chapters 只能传入一条章节规则标注")
return value