Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/frontend/src/hooks/useAgentGraph.ts (TypeScript)

Code before:
          const handle = path.split(/[\/.]/)[0];
          if (
            node.data.connections.some(
              (conn) => conn.target === node.id || conn.targetHandle === handle,
            )
          ) {
            return;
          }
          console.warn("Error", error);
          errorMessage = error.message || "Invalid input";
          if (path && error.message) {
            const key = path.slice(1);
            console.log("Error", key, error.message);
            setNestedProperty(
              errors,
              key,
              error.message[0].toUpperCase() + error.message.slice(1),
            );
          } else if (error.keyword === "required") {
            const key = error.params.missingProperty;
            setNestedProperty(errors, key, "This field is required");
          }
        });
      } 
      
      Object.entries(node.data.inputSchema.properties || {}).forEach(([key, schema]) => {
        if ('depends_on' in schema) {
          const dependencies = Array.isArray(schema.depends_on) ? schema.depends_on : [schema.depends_on];
          
          // Check if dependent field is set while required fields are empty
          const hasValue = inputData[key as keyof typeof inputData] && String(inputData[key as keyof typeof inputData]).length > 0;
          const missingDependencies = dependencies.filter(dep => !inputData[dep as keyof typeof inputData] || String(inputData[dep as keyof typeof inputData]).length === 0);
          
          if (hasValue && missingDependencies.length > 0) {
            errors[key] = `Requires ${missingDependencies.join(', ')} to be set`;
            errorMessage = `Field ${key} requires ${missingDependencies.join(', ')} to be set`;
          }
          
          // Check if required fields are set but dependent field is empty
          const hasAllDependencies = dependencies.every(dep => 
            inputData[dep as keyof typeof inputData] && 
            String(inputData[dep as keyof typeof inputData]).length > 0
          );
          if (hasAllDependencies && (!inputData[key as keyof typeof inputData] || String(inputData[key as keyof typeof inputData]).length === 0)) {
            errors[key] = `This field is required when ${dependencies.join(', ')} are set`;
            errorMessage = `${key} is required when ${dependencies.join(', ')} are set`;
          }
        }
      });



Review comment (security):
Since you're doing this validation on save, it should be done in the backend. Frontend-only validation = vulnerable.

Code after:
            node.data.connections.some(
              (conn) => conn.target === node.id || conn.targetHandle === handle,
            )
          ) {
            return;
          }
          console.warn("Error", error);
          errorMessage = error.message || "Invalid input";
          if (path && error.message) {
            const key = path.slice(1);
            console.log("Error", key, error.message);
            setNestedProperty(
              errors,
              key,
              error.message[0].toUpperCase() + error.message.slice(1),
            );
          } else if (error.keyword === "required") {
            const key = error.params.missingProperty;
            setNestedProperty(errors, key, "This field is required");
          }
        });
      }

      Object.entries(node.data.inputSchema.properties || {}).forEach(
        ([key, schema]) => {
          if (schema.depends_on) {
            const dependencies = schema.depends_on;

            // Check if dependent field has value
            const hasValue =
              inputData[key] != null ||
              ("default" in schema && schema.default != null);

            const mustHaveValue = node.data.inputSchema.required?.includes(key);

            // Check for missing dependencies when dependent field is present
            const missingDependencies = dependencies.filter(
              (dep) =>
                !inputData[dep as keyof typeof inputData] ||
                String(inputData[dep as keyof typeof inputData]).trim() === "",
            );

            if ((hasValue || mustHaveValue) && missingDependencies.length > 0) {
              setNestedProperty(
                errors,
                key,
                `Requires ${missingDependencies.join(", ")} to be set`,
              );
              errorMessage = `Field ${key} requires ${missingDependencies.join(", ")} to be set`;
            }