Applying rules to nodes

Hi, currently evaluating GoJS as a solution for a diagram-type webapp.

Is there a way to apply rules to nodes? For example:
Node 1 cannot connect to node types of “Server”, but it can connect to nodes with type “user”
Node 2 can connect to both a server node and a user node, but cannot connect to a database node
Node 3 can connect to all types of node

How would one go about assigning a type to a node and how would one implement rules upon types?

Thank you

Yes, this is called linking validation. There’s a fairly comprehensive discussion at: GoJS Validation -- Northwoods Software

You can assign a “type” to your Node by assigning some property on the node’s data object in the model. If you named that property “type”, then the value would be available as the someNode.data.type. The example at GoJS Validation -- Northwoods Software uses the “color” property to only allow linking between nodes of the same color.

Awesome, thank you.

Hey again. I’m trying it out today, but with the below code (edited from the color example you linked) every time I drag an arrow from a node (doesn’t matter which node) it magnets to a random point on the diagram and won’t drag to another node. Am I applying the type correctly by assigning it in the array or do I have to assign the type property in the node template?

        myDiagram.linkTemplate =
          $(go.Link,
            { curve: go.Link.Bezier, relinkableFrom: true, relinkableTo: true },
            $(go.Shape, { strokeWidth: 2 },
              new go.Binding("stroke", "fromNode", function(n) { return n.data.type; })
                  .ofObject()),
            $(go.Shape, { toArrow: "Standard", stroke: null},
              new go.Binding("fill", "fromNode", function(n) { return n.data.type; })
                  .ofObject())
          );

        function correctType(fromnode, fromport, tonode, toport) {
          if (fromnode.data.type == "server" && tonode.data.type == "database"){
            return true;
          }
          else{
            return false;
          }
        }

        myDiagram.toolManager.linkingTool.linkValidation = correctType;
        myDiagram.toolManager.relinkingTool.linkValidation = correctType;

        var nodeDataArray = [
          { key: "server", type: "server", color: "red" },
          { key: "database", type: "database", color: "blue" },
          { key: "user", type: "user", color: "green" },
        ];

Make sure that fromnode and tonode are non-null?

The data in the model looks good, but the Bindings on Shape.stroke and Shape.fill aren’t going to work because “server” and “database” are not valid CSS color strings.

Got it working - was a problem with my conditions in the if statement. Changed bindings back too.

        function correctType(fromnode, fromport, tonode, toport) {
          return ((fromnode.data.type == "server") && (tonode.data.type=="database"));
        }

Thanks for your help.

I just changed your validation predicate to always return either true or false.