feat(oa): validate dept control form_data shapes
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1d24f1c682
commit
260a461d05
2 files changed
+216
No files matched your search
@@ -0,0 +1,118 @@
|
||||
"""OA 表单设计器:部门选择控件 form_data 校验"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
def _walk_rules(rules: Any, out: List[dict]) -> None:
|
||||
if not isinstance(rules, list):
|
||||
return
|
||||
for r in rules:
|
||||
if not isinstance(r, dict):
|
||||
continue
|
||||
out.append(r)
|
||||
children = r.get("children") or (r.get("props") or {}).get("rule") or []
|
||||
if isinstance(children, list):
|
||||
_walk_rules(children, out)
|
||||
|
||||
|
||||
def _schema_rules(schema: Any) -> List[dict]:
|
||||
if isinstance(schema, list):
|
||||
out: List[dict] = []
|
||||
_walk_rules(schema, out)
|
||||
return out
|
||||
if not isinstance(schema, dict):
|
||||
return []
|
||||
out: List[dict] = []
|
||||
_walk_rules(schema.get("rule") or schema.get("rules") or [], out)
|
||||
return out
|
||||
|
||||
|
||||
def _field_label(rule: dict) -> str:
|
||||
return str(rule.get("title") or rule.get("label") or rule.get("field") or "部门")
|
||||
|
||||
|
||||
def _is_required(rule: dict) -> bool:
|
||||
props = rule.get("props") or {}
|
||||
if rule.get("$required"):
|
||||
return True
|
||||
if rule.get("required"):
|
||||
return True
|
||||
if props.get("required"):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _is_multiple(rule: dict) -> bool:
|
||||
props = rule.get("props") or {}
|
||||
multiple = props.get("multiple")
|
||||
if multiple is None:
|
||||
return True
|
||||
return bool(multiple)
|
||||
|
||||
|
||||
def _is_valid_dept_item(item: Any) -> bool:
|
||||
if not isinstance(item, dict):
|
||||
return False
|
||||
dept_id = item.get("id")
|
||||
name = item.get("name")
|
||||
if not isinstance(dept_id, str) or not dept_id.strip():
|
||||
return False
|
||||
if not isinstance(name, str) or not name.strip():
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _validate_dept_rule(rule: dict, data: Dict[str, Any]) -> List[str]:
|
||||
field = str(rule.get("field") or "").strip()
|
||||
if not field:
|
||||
return []
|
||||
|
||||
label = _field_label(rule)
|
||||
required = _is_required(rule)
|
||||
multiple = _is_multiple(rule)
|
||||
props = rule.get("props") or {}
|
||||
max_count = props.get("max")
|
||||
value = data.get(field)
|
||||
errors: List[str] = []
|
||||
|
||||
if multiple:
|
||||
if value is None:
|
||||
if required:
|
||||
errors.append(f"「{label}」为必填部门字段")
|
||||
return errors
|
||||
if not isinstance(value, list):
|
||||
errors.append(f"「{label}」部门字段格式不正确,应为部门列表")
|
||||
return errors
|
||||
if required and len(value) == 0:
|
||||
errors.append(f"「{label}」为必填部门字段")
|
||||
return errors
|
||||
for item in value:
|
||||
if not _is_valid_dept_item(item):
|
||||
errors.append(f"「{label}」部门项格式不正确,需包含有效的 id 与 name")
|
||||
break
|
||||
if max_count is not None and isinstance(max_count, int) and len(value) > max_count:
|
||||
errors.append(f"「{label}」选择部门数量超过最大上限 {max_count} 个")
|
||||
return errors
|
||||
|
||||
if value is None or value == "":
|
||||
if required:
|
||||
errors.append(f"「{label}」为必填部门字段")
|
||||
return errors
|
||||
if isinstance(value, list):
|
||||
errors.append(f"「{label}」部门字段格式不正确,应为单个部门对象")
|
||||
return errors
|
||||
if not _is_valid_dept_item(value):
|
||||
errors.append(f"「{label}」部门字段格式不正确,需包含有效的 id 与 name")
|
||||
return errors
|
||||
|
||||
|
||||
def validate_dept_form_values(schema: Any, form_data: Optional[Dict[str, Any]]) -> List[str]:
|
||||
"""校验 form_data 中部门选择控件的值格式,返回中文错误列表(空表示通过)。"""
|
||||
data = form_data or {}
|
||||
errors: List[str] = []
|
||||
for rule in _schema_rules(schema):
|
||||
if str(rule.get("type") or "") != "dept":
|
||||
continue
|
||||
errors.extend(_validate_dept_rule(rule, data))
|
||||
return errors
|
||||
@@ -0,0 +1,98 @@
|
||||
import unittest
|
||||
|
||||
from app.services.oa_dept_select import validate_dept_form_values
|
||||
|
||||
|
||||
class TestValidateDeptFormValues(unittest.TestCase):
|
||||
def test_multiple_ok(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "dept",
|
||||
"field": "depts",
|
||||
"title": "部门",
|
||||
"$required": True,
|
||||
"props": {"multiple": True, "max": 2},
|
||||
}
|
||||
]
|
||||
}
|
||||
data = {
|
||||
"depts": [
|
||||
{"id": "d1", "name": "中科兴皖 / 研发"},
|
||||
{"id": "d2", "name": "中科兴皖 / 质量"},
|
||||
]
|
||||
}
|
||||
self.assertEqual(validate_dept_form_values(schema, data), [])
|
||||
|
||||
def test_required_empty_array(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "dept",
|
||||
"field": "depts",
|
||||
"$required": True,
|
||||
"props": {"multiple": True},
|
||||
}
|
||||
]
|
||||
}
|
||||
errs = validate_dept_form_values(schema, {"depts": []})
|
||||
self.assertTrue(any("必填" in e or "部门" in e for e in errs))
|
||||
|
||||
def test_single_must_be_object(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "dept",
|
||||
"field": "dept",
|
||||
"props": {"multiple": False},
|
||||
}
|
||||
]
|
||||
}
|
||||
errs = validate_dept_form_values(schema, {"dept": [{"id": "d1", "name": "A"}]})
|
||||
self.assertTrue(len(errs) >= 1)
|
||||
|
||||
def test_max_exceeded(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "dept",
|
||||
"field": "depts",
|
||||
"props": {"multiple": True, "max": 1},
|
||||
}
|
||||
]
|
||||
}
|
||||
data = {
|
||||
"depts": [
|
||||
{"id": "d1", "name": "A"},
|
||||
{"id": "d2", "name": "B"},
|
||||
]
|
||||
}
|
||||
errs = validate_dept_form_values(schema, data)
|
||||
self.assertTrue(any("最大" in e or "上限" in e for e in errs))
|
||||
|
||||
def test_nested_fc_row(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "fcRow",
|
||||
"children": [
|
||||
{
|
||||
"type": "dept",
|
||||
"field": "d",
|
||||
"required": True,
|
||||
"props": {"multiple": False},
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
}
|
||||
errs = validate_dept_form_values(schema, {})
|
||||
self.assertTrue(len(errs) >= 1)
|
||||
|
||||
def test_no_dept_fields_ok(self):
|
||||
schema = {"rule": [{"type": "input", "field": "t"}]}
|
||||
self.assertEqual(validate_dept_form_values(schema, {"t": "x"}), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user