Skip to content

Commit

Permalink
Fix: data links error (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
mecm1993 authored and danielcaldas committed Oct 10, 2019
1 parent 3ef7be2 commit 36850ef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/graph/graph.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import CONST from "./graph.const";
import DEFAULT_CONFIG from "./graph.config";
import ERRORS from "../../err";

import { isDeepEqual, isEmptyObject, merge, pick, antiPick, throwErr } from "../../utils";
import { isDeepEqual, isEmptyObject, merge, pick, antiPick, throwErr, throwWarning } from "../../utils";
import { computeNodeDegree } from "./collapse.helper";

const NODE_PROPS_WHITELIST = ["id", "highlighted", "x", "y", "index", "vy", "vx"];
Expand Down Expand Up @@ -205,9 +205,10 @@ function _tagOrphanNodes(nodes, linksMatrix) {
* Some integrity validations on links and nodes structure. If some validation fails the function will
* throw an error.
* @param {Object} data - Same as {@link #initializeGraphState|data in initializeGraphState}.
* @throws can throw the following error msg:
* @throws can throw the following error or warning msg:
* INSUFFICIENT_DATA - msg if no nodes are provided
* INVALID_LINKS - if links point to nonexistent nodes
* INSUFFICIENT_LINKS - if no links are provided
* @returns {undefined}
* @memberof Graph/helper
*/
Expand All @@ -216,6 +217,11 @@ function _validateGraphData(data) {
throwErr("Graph", ERRORS.INSUFFICIENT_DATA);
}

if (!data.links || !data.links.length) {
throwWarning("Graph", ERRORS.INSUFFICIENT_LINKS);
data.links = [];
}

const n = data.links.length;

for (let i = 0; i < n; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/err.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*eslint max-len: ["error", 200]*/
export default {
GRAPH_NO_ID_PROP: "id prop not defined! id property is mandatory and it should be unique.",
INSUFFICIENT_LINKS:
"you are passing invalid data to react-d3-graph. You must include a links array in the data object you're passing down to the <Graph> component.",
INVALID_LINKS:
"you provided a invalid links data structure. Links source and target attributes must point to an existent node",
INSUFFICIENT_DATA:
Expand Down
15 changes: 14 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,17 @@ function throwErr(component, msg) {
throw Error(error);
}

export { isDeepEqual, isEmptyObject, deepClone, merge, pick, antiPick, throwErr };
/**
* Helper function for customized warning logging.
* @param {string} component - the name of the component where the warning is to be thrown.
* @param {string} msg - the message contain a more detailed explanation about the error.
* @returns {Warning} the thrown warning.
* @memberof utils
*/
function throwWarning(component, msg) {
const warning = `react-d3-graph :: ${component} :: ${msg}`;

console.warn(warning);
}

export { isDeepEqual, isEmptyObject, deepClone, merge, pick, antiPick, throwErr, throwWarning };

0 comments on commit 36850ef

Please sign in to comment.