fix: validate decision nodes have at least 2 children in frontend and AI builder

The frontend validate button was not checking that decision nodes with
children have at least 2 branches, so it would pass validation but the
backend publish check would reject with a 422. The AI tree validator also
only checked options count, not children count — so AI-generated trees
with 2 options pointing to the same single child would pass generation
validation but fail at publish time.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-02-26 14:06:08 -05:00
parent 2241b37d25
commit 1002f0c177
2 changed files with 17 additions and 3 deletions

View File

@@ -83,12 +83,16 @@ def validate_generated_tree(tree: dict[str, Any]) -> list[str]:
# Type-specific validation
if node_type == "decision":
options = node.get("options", [])
children = node.get("children", [])
if not isinstance(options, list) or len(options) < 2:
errors.append(
f"Decision node '{node_id}' must have at least 2 options"
)
else:
children = node.get("children", [])
if isinstance(children, list) and len(children) > 0 and len(children) < 2:
errors.append(
f"Decision node '{node_id}' must have at least 2 children (branches)"
)
if isinstance(options, list) and len(options) >= 2:
child_ids = {c.get("id") for c in children if isinstance(c, dict)}
option_ids: set[str] = set()