From 36850efe7cf4723758bee7321e8ea8eb1a64decc Mon Sep 17 00:00:00 2001 From: Manuel Cepeda Date: Thu, 10 Oct 2019 08:40:35 -0400 Subject: [PATCH] Fix: data links error (#246) --- src/components/graph/graph.helper.js | 10 ++++++++-- src/err.js | 2 ++ src/utils.js | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/components/graph/graph.helper.js b/src/components/graph/graph.helper.js index cc917f235..9f2b752e4 100644 --- a/src/components/graph/graph.helper.js +++ b/src/components/graph/graph.helper.js @@ -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"]; @@ -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 */ @@ -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++) { diff --git a/src/err.js b/src/err.js index 179254cdd..ede17f6ec 100644 --- a/src/err.js +++ b/src/err.js @@ -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 component.", INVALID_LINKS: "you provided a invalid links data structure. Links source and target attributes must point to an existent node", INSUFFICIENT_DATA: diff --git a/src/utils.js b/src/utils.js index 1d6d2f7c7..4cb28e09c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 };