CoolFace
Apppublic

mr4/knowledge-graph-preview

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
dashboard-validator.js61 linesDownload Raw Back to lib
1/**2 * Validates that the given data conforms to the expected Dashboard JSON structure.3 * Checks for the presence and correct types of `nodes` and `edges` arrays,4 * and validates that each node has `id`, `type`, `name` and each edge has `source`, `target`, `type`.5 * Returns all validation errors found, not just the first.6 */7export function validateDashboard(data) {8    const errors = [];9    if (data === null || data === undefined || typeof data !== 'object' || Array.isArray(data)) {10        errors.push('Dashboard data must be a non-null object');11        return { valid: false, errors };12    }13    const record = data;14    // Validate nodes15    if (!('nodes' in record) || !Array.isArray(record.nodes)) {16        errors.push('Dashboard data must contain a "nodes" array');17    }18    else {19        const nodes = record.nodes;20        for (let i = 0; i < nodes.length; i++) {21            const node = nodes[i];22            if (node === null || node === undefined || typeof node !== 'object' || Array.isArray(node)) {23                errors.push(`nodes[${i}] must be an object`);24                continue;25            }26            const nodeRecord = node;27            const requiredFields = ['id', 'type', 'name'];28            for (const field of requiredFields) {29                if (!(field in nodeRecord) || typeof nodeRecord[field] !== 'string') {30                    errors.push(`nodes[${i}] is missing required field "${field}"`);31                }32            }33        }34    }35    // Validate edges36    if (!('edges' in record) || !Array.isArray(record.edges)) {37        errors.push('Dashboard data must contain an "edges" array');38    }39    else {40        const edges = record.edges;41        for (let i = 0; i < edges.length; i++) {42            const edge = edges[i];43            if (edge === null || edge === undefined || typeof edge !== 'object' || Array.isArray(edge)) {44                errors.push(`edges[${i}] must be an object`);45                continue;46            }47            const edgeRecord = edge;48            const requiredFields = ['source', 'target', 'type'];49            for (const field of requiredFields) {50                if (!(field in edgeRecord) || typeof edgeRecord[field] !== 'string') {51                    errors.push(`edges[${i}] is missing required field "${field}"`);52                }53            }54        }55    }56    return {57        valid: errors.length === 0,58        errors,59    };60}61