import { isString , isPlainObject } from '@antv/util' ; import { GraphData , TreeGraphData , NodeConfig , EdgeConfig , ComboConfig , ITEM_TYPE } from '../types' ; import { traverseTree } from './graphic' ; param data return export const dataValidation = ( data ? ) : boolean => { if ( ! data ) { console . error ( 'G6 Error Tips: the data must be defined' ) ; return false ; } const { nodes , edges , combos = [ ] } = data ; if ( ! nodes && ! edges ) { let validated = true ; traverseTree ( data as TreeGraphData , param => { if ( ! isString ( param . id ) ) { validated = false ; return false ; } return true ; } ) ; return validated ; } const nonNode = ( ( nodes as NodeConfig [ ] ) || [ ] ) . find ( node => ! isString ( node . id ) ) ; if ( nonNode ) { console . warn ( `G6 Warning Tips: missing 'id' property, or %c${ nonNode . id }%c is not a string.` , 'font-size: 20px; color: red;' , '' ) ; return false ; } const nodeIds = ( ( nodes as NodeConfig [ ] ) || [ ] ) . map ( node => node . id ) ; const comboIds = ( combos as ComboConfig [ ] ) ?. map ( combo => combo . id ) ; const ids = [ ... nodeIds , ... comboIds ] ; const nonEdges = ( ( edges as EdgeConfig [ ] ) || [ ] ) . find ( edge => ! ids . includes ( edge . source ) || ! ids . includes ( edge . target ) ) ; if ( nonEdges ) { console . warn ( `G6 Warning Tips: The source %c${ nonEdges . source }%c or the target %c${ nonEdges . target }%c of the edge do not exist in the nodes or combos.` , 'font-size: 20px; color: red;' , '' , 'font-size: 20px; color: red;' , '' ) ; return false ; } return true ; } ; param type param data return export const singleDataValidation = ( type , data ) : boolean => { if ( type === 'node' || type === 'combo' ) { if ( data . id && ! isString ( data . id ) ) { console . warn ( `G6 Warning Tips: missing 'id' property, or the 'id' %c${ data . id }%c is not a string.` , 'font-size: 20px; color: red;' , '' ) ; return false ; } } else if ( type === 'edge' ) { if ( ! ( data as EdgeConfig ) . source || ! ( data as EdgeConfig ) . target ) { console . warn ( `G6 Warning Tips: missing 'source' or 'target' for the edge.` ) ; return false ; } } return true ; } ;