-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateExternalData.js
More file actions
37 lines (30 loc) · 1.13 KB
/
validateExternalData.js
File metadata and controls
37 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Variables and option values must respect NMTOKEN rules so that
// they are compatable with XML export formats
const allowedVariableName = (value) => {
if (!/^[a-zA-Z0-9._\-:]+$/.test(value)) {
return 'Not a valid variable name. Only letters, numbers and the symbols ._-: are supported.';
}
return undefined;
};
const getUniqueAttributes = (items) => {
const uniqueSet = items.reduce(
(acc, node) => {
Object.keys(node['attributes'])
.forEach(key => acc.add(key));
return acc;
},
new Set([]),
);
return Array.from(uniqueSet);
};
const getVariableNamesFromNetwork = network =>
['nodes', 'edges'].flatMap(
entity => getUniqueAttributes(network[entity] || []),
);
const validateNames = (items = []) => {
const errors = items
.filter(item => allowedVariableName(item) !== undefined);
if (errors.length === 0) { return false; }
return `Variable name not allowed ("${errors.join('", "')}"). Only letters, numbers and the symbols ._-: are supported.`;
};
module.exports = { validateNames, getVariableNamesFromNetwork };