Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/data/graph.py (Python)

Code before:
            properties = input_schema.get("properties", {})
            required_fields = set(input_schema.get("required", []))

            # Validate dependencies between fields
            for field_name, field_schema in properties.items():
                if "depends_on" in field_schema:
                    dependencies = field_schema["depends_on"]

                    # Check if dependent field has value in input_default
                    has_value = (
                        field_name in node.input_default
                        and node.input_default[field_name] is not None
                        and str(node.input_default[field_name]).strip() != ""
                    ) or (
                        "default" in field_schema
                        and field_schema["default"] is not None
                    )
                    must_have_value = field_name in required_fields

                    # Check for missing dependencies when dependent field is present
                    missing_deps = [
                        dep
                        for dep in dependencies
                        if dep not in node.input_default
                        or not node.input_default[dep]
                        or str(node.input_default[dep]).strip() == ""
                    ]

                    if (has_value or must_have_value) and missing_deps:
                        raise ValueError(
                            f"Node {block.name} #{node.id}: Field {field_name} requires {', '.join(missing_deps)} to be set"
                        )

                        # Check if field is required when dependencies are present
                    has_all_deps = all(
                        dep in node.input_default
                        and node.input_default[dep]
                        and str(node.input_default[dep]).strip() != ""
                        for dep in dependencies
                    )

                    if has_all_deps and not has_value:
                        raise ValueError(
                            f"Node {block.name} #{node.id}: {field_name} is required when {', '.join(dependencies)} are set"
                        )

        node_map = {v.id: v for v in self.nodes}

        def is_static_output_block(nid: str) -> bool:
            bid = node_map[nid].block_id
            b = get_block(bid)

Review comment (suggestion):
This check seems replicated in 3 places, and some of them are missing the default ("default" field) value check.
We can add a lambda / inner function like:

```
            def has_value(name):
                return (
                    name in node.input_default and
                    node.input_default[name] is not None and
                    str(node.input_default[name]).strip() != ""
                ) or (
                    name in input_schema and
                    input_schema[name].default is not None
                )           
```

Code after:

            # Get input schema properties and check dependencies
            input_schema = block.input_schema.model_fields
            required_fields = block.input_schema.get_required_fields()

            def has_value(name):
                return (
                    node is not None
                    and name in node.input_default
                    and node.input_default[name] is not None
                    and str(node.input_default[name]).strip() != ""
                ) or (name in input_schema and input_schema[name].default is not None)

            # Validate dependencies between fields
            for field_name, field_info in input_schema.items():

                # Apply input dependency validation only on run & field with depends_on
                json_schema_extra = field_info.json_schema_extra or {}
                dependencies = json_schema_extra.get("depends_on", [])
                if not for_run or not dependencies:
                    continue

                # Check if dependent field has value in input_default
                field_has_value = has_value(field_name)
                field_is_required = field_name in required_fields

                # Check for missing dependencies when dependent field is present
                missing_deps = [dep for dep in dependencies if not has_value(dep)]
                if missing_deps and (field_has_value or field_is_required):
                    raise ValueError(
                        f"Node {block.name} #{node.id}: Field `{field_name}` requires [{', '.join(missing_deps)}] to be set"
                    )

        node_map = {v.id: v for v in self.nodes}

        def is_static_output_block(nid: str) -> bool:
            bid = node_map[nid].block_id
            b = get_block(bid)
            return b.static_output if b else False

        # Links: links are connected and the connected pin data type are compatible.
        for link in self.links:
            source = (link.source_id, link.source_name)
            sink = (link.sink_id, link.sink_name)
            suffix = f"Link {source} <-> {sink}"

            for i, (node_id, name) in enumerate([source, sink]):
                node = node_map.get(node_id)
                if not node:
                    raise ValueError(
                        f"{suffix}, {node_id} is invalid node id, available nodes: {node_map.keys()}"