type enforcement

This commit is contained in:
Klemek
2019-07-15 10:55:16 +02:00
parent e9f5c801fe
commit e699002c96
5 changed files with 158 additions and 0 deletions
+50
View File
@@ -23,6 +23,40 @@ try {
* @property {string|undefined} type
*/
const SUB_DEF = {
'text': 'string',
'icon': 'string',
'color': 'string',
'font': 'string',
'font-size': 'number',
'font-style': 'string',
'scale': 'number',
};
const NODE_DEF = {
'name': '!string',
'icon': '!string',
'x': '!number',
'y': '!number',
'color': 'string',
'scale': 'number',
'top': SUB_DEF,
'bottom': SUB_DEF,
'left': SUB_DEF,
'right': SUB_DEF
};
const LINK_DEF = {
'from': '!string',
'to': '!string',
'type': 'string',
'color': 'string',
'scale': 'number',
'size': 'number',
'top': SUB_DEF,
'bottom': SUB_DEF,
};
const DEFAULT_OPTIONS = {
'beautify': false,
'scale': 128,
@@ -230,8 +264,24 @@ module.exports = (options) => {
});
},
/**
* @param {Object<string,Node2>} nodes
* @param {Link2[]} links
*/
compute: (nodes, links) => {
Object.keys(nodes).forEach(key => {
const res = utils.isValid(nodes[key], NODE_DEF);
if (res)
throw `node '${key}' is invalid at key ${res}`;
});
links.forEach((link, i) => {
const res = utils.isValid(link, LINK_DEF);
if (res)
throw `link ${i} (${link.from}->${link.to}) is invalid at key ${res}`;
});
const bounds = self.getBounds(nodes);
const data = {'g': []};