Skip to content

Commit

Permalink
Adding new reference rule for variables, and few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
msingh-developer committed Dec 3, 2024
1 parent 0fabf15 commit 60eec91
Show file tree
Hide file tree
Showing 7 changed files with 382 additions and 6 deletions.
9 changes: 8 additions & 1 deletion docs/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"description": "Connector Capability not configured",
"message": "Connector Capability not configured",
"type": "error",
"recommendation": "Configure a capability in <%> connector to ensure proper operation.",
"recommendation": "Configure a capability in '%' to ensure proper operation.",
"code": "dv-er-node-002"
}
},
Expand Down Expand Up @@ -276,6 +276,13 @@
"type": "error",
"recommendation": "The '%' variable is not defined within a variable connector, which may lead to unexpected behavior. Define the variable appropriately.",
"code": "dv-er-variable-002"
},
"dv-er-variable-003": {
"description": "Referenced node in variable doesn't exist",
"message": "Referenced node in local variable doesn't exist",
"type": "error",
"recommendation": "The local variable(s) - '%' contains a node id which doesn't exist within the flow",
"code": "dv-er-variable-003"
}
},
"reference": "",
Expand Down
1 change: 1 addition & 0 deletions rules/dv-rule-node-001/NodeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class NodeRule extends LintRule {
if (
data.nodeType === "CONNECTION" &&
data.connectorId === "pingOneFormsConnector" &&
data.capabilityName === "customForm" &&
!data.properties?.form?.value
) {
this.addError("dv-er-node-004", {
Expand Down
7 changes: 4 additions & 3 deletions rules/dv-rule-node-002/DisabledNodeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class DisabledNodeRule extends LintRule {
this.addCode("dv-er-node-001", {
description: "Disabled Node Found",
message: "Disabled node % found",
type: "best-practice",
type: "error",
recommendation: "A disabled '%' node has been found. Consider removing it from the flow.",
});
this.addCode("dv-er-node-002", {
description: "Connector Capability not configured",
message: "Connector Capability not configured",
type: "error",
recommendation: "Configure a capability in <%> connector to ensure proper operation.",
recommendation: "Configure a capability in '%' to ensure proper operation.",
});

}
Expand All @@ -39,8 +39,9 @@ class DisabledNodeRule extends LintRule {
}

if (data.status === 'unconfigured') {
const connectorInstanceName = data.name.toLowerCase().includes('connector') ? data.name : data.name + ' connector';
this.addError("dv-er-node-002", {
recommendationArgs: [`${data.name}`],
recommendationArgs: [`${connectorInstanceName}`],
nodeId: data.id,
});
}
Expand Down
4 changes: 2 additions & 2 deletions rules/dv-rule-node-002/tests/DisabledNodeRule.expect.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"code": "dv-er-node-001",
"message": "Disabled node (v9172ugldd) found",
"type": "best-practice",
"type": "error",
"recommendation": "A disabled '(v9172ugldd)' node has been found. Consider removing it from the flow.",
"nodeId": "v9172ugldd"
}
Expand All @@ -34,7 +34,7 @@
{
"code": "dv-er-node-001",
"message": "Disabled node (v9172ugldd) found",
"type": "best-practice",
"type": "error",
"recommendation": "A disabled '(v9172ugldd)' node has been found. Consider removing it from the flow.",
"nodeId": "v9172ugldd"
}
Expand Down
52 changes: 52 additions & 0 deletions rules/dv-rule-variables-001/VariableRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class DVRule extends LintRule {
recommendation:
"The '%' variable is not defined within a variable connector, which may lead to unexpected behavior. Define the variable appropriately.",
});

this.addCode("dv-er-variable-003", {
description: "Referenced node in variable doesn't exist",
message: "Referenced node in local variable doesn't exist",
type: "error",
recommendation: "The local variable(s) - '%' contains a node id which doesn't exist within the flow"
});
}

runRule() {
Expand Down Expand Up @@ -53,6 +60,51 @@ class DVRule extends LintRule {
this.addError("dv-er-variable-001", { messageArgs: [v.ref], recommendationArgs: [v.ref] });
}
});


// Logic to check if local variable nodeId is from the same flow
const targetFlow = this.mainFlow;
const mainFlowNodeIdArr = targetFlow?.graphData.elements?.nodes?.map(node => node.data.id);

targetFlow?.graphData?.elements?.nodes?.forEach((node) => {
let stringVal = JSON.stringify(node.data.properties) || '';

const pattern = /\{\{(.*?)\}\}/g; // regex pattern to match all instances of {{...}}
const matches = [...stringVal.matchAll(pattern)];
const localVarArray = matches.map(match => match[1]);
const uniqueLocalVariables = [...new Set(localVarArray)];

const nodeIdPattern = /\b[a-z0-9]{10}\b/g; // regx to pick nodeIds
let localVarNodeIdArr = []
uniqueLocalVariables.forEach(v => {
if (v.includes('local.')) {
const matchesNodeId = [...v.matchAll(nodeIdPattern)];
let nodeIds = matchesNodeId.map(match => match[0])
localVarNodeIdArr = [...localVarNodeIdArr, ...nodeIds];
}
})
const uniqueNodeIds = [...new Set(localVarNodeIdArr)];

let errorIdToShow = []
uniqueNodeIds.forEach(nodeId => {
if (!mainFlowNodeIdArr.includes(nodeId)) {
errorIdToShow.push(nodeId);
}
});

if (errorIdToShow.length > 0) {
const matchingFullStrings = uniqueLocalVariables.filter(fullString =>
errorIdToShow.some(substring => fullString.includes(substring))
);

this.addError("dv-er-variable-003", {
recommendationArgs: [matchingFullStrings.join(', '), node.data.id],
nodeId: node.data.id
});
}

});

} catch (err) {
this.addError(undefined, { messageArgs: [`${err}`] });
}
Expand Down
50 changes: 50 additions & 0 deletions rules/dv-rule-variables-001/tests/ReferenceNodeRule.expect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "PingOne DaVinci Linter",
"pass": false,
"errorCount": 1,
"rulePackResults": [
{
"pass": false,
"errorCount": 1,
"lintResults": [
{
"flowName": "flow2",
"flowId": "91997bdf98466fe24b155490397f65d8",
"pass": false,
"errorCount": 1,
"errors": [
{
"code": "dv-er-variable-003",
"message": "Referenced node in local variable doesn't exist",
"type": "error",
"recommendation": "The local variable(s) - 'local.k42obrw9lp.payload.success' contains a node id which doesn't exist within the flow",
"nodeId": "8ve6xiwmj8"
}
],
"rulesApplied": [
"dv-rule-variables-001"
],
"ruleResults": [
{
"ruleId": "dv-rule-variables-001",
"ruleDescription": "Ensure that flow variables are used in flow. And check for flow variables referenced in nodes but not defined",
"pass": false,
"errorCount": 1,
"errors": [
{
"code": "dv-er-variable-003",
"message": "Referenced node in local variable doesn't exist",
"type": "error",
"recommendation": "The local variable(s) - 'local.k42obrw9lp.payload.success' contains a node id which doesn't exist within the flow",
"nodeId": "8ve6xiwmj8"
}
]
}
],
"rulesIgnored": []
}
],
"rulesIgnored": true
}
]
}
Loading

0 comments on commit 60eec91

Please sign in to comment.