15 lines
522 B
Python
15 lines
522 B
Python
"""跨层共享的纯文本校验(唯一实现,API 与语音模块共用)."""
|
||
|
||
from __future__ import annotations
|
||
|
||
|
||
def safe_text(value: str, maximum: int, label: str, required: bool = False) -> str:
|
||
value = value.strip()
|
||
if (
|
||
(required and not value)
|
||
or len(value) > maximum
|
||
or any(ord(c) < 32 or ord(c) == 127 or c in "<>" for c in value)
|
||
):
|
||
raise ValueError(f"{label}请填写{1 if required else 0}–{maximum}字,不含控制字符或尖括号。")
|
||
return value
|