File size: 2,442 Bytes
fd8cdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * Validates that the given data conforms to the expected Dashboard JSON structure.
 * Checks for the presence and correct types of `nodes` and `edges` arrays,
 * and validates that each node has `id`, `type`, `name` and each edge has `source`, `target`, `type`.
 * Returns all validation errors found, not just the first.
 */
export function validateDashboard(data) {
    const errors = [];
    if (data === null || data === undefined || typeof data !== 'object' || Array.isArray(data)) {
        errors.push('Dashboard data must be a non-null object');
        return { valid: false, errors };
    }
    const record = data;
    // Validate nodes
    if (!('nodes' in record) || !Array.isArray(record.nodes)) {
        errors.push('Dashboard data must contain a "nodes" array');
    }
    else {
        const nodes = record.nodes;
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            if (node === null || node === undefined || typeof node !== 'object' || Array.isArray(node)) {
                errors.push(`nodes[${i}] must be an object`);
                continue;
            }
            const nodeRecord = node;
            const requiredFields = ['id', 'type', 'name'];
            for (const field of requiredFields) {
                if (!(field in nodeRecord) || typeof nodeRecord[field] !== 'string') {
                    errors.push(`nodes[${i}] is missing required field "${field}"`);
                }
            }
        }
    }
    // Validate edges
    if (!('edges' in record) || !Array.isArray(record.edges)) {
        errors.push('Dashboard data must contain an "edges" array');
    }
    else {
        const edges = record.edges;
        for (let i = 0; i < edges.length; i++) {
            const edge = edges[i];
            if (edge === null || edge === undefined || typeof edge !== 'object' || Array.isArray(edge)) {
                errors.push(`edges[${i}] must be an object`);
                continue;
            }
            const edgeRecord = edge;
            const requiredFields = ['source', 'target', 'type'];
            for (const field of requiredFields) {
                if (!(field in edgeRecord) || typeof edgeRecord[field] !== 'string') {
                    errors.push(`edges[${i}] is missing required field "${field}"`);
                }
            }
        }
    }
    return {
        valid: errors.length === 0,
        errors,
    };
}