feat(web): 增加表单数据校验
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import {type Connection, type Edge, getOutgoers, type Node} from '@xyflow/react'
|
||||
import {find, isEmpty, isEqual, lpad, toStr} from 'licia'
|
||||
import {find, has, isEmpty, isEqual, lpad, toStr} from 'licia'
|
||||
import NodeRegistry from './NodeRegistry.tsx'
|
||||
|
||||
export class CheckError extends Error {
|
||||
readonly id: string
|
||||
@@ -63,6 +64,8 @@ export const checkAddConnection: (connection: Connection, nodes: Node[], edges:
|
||||
|
||||
export const atLeastOneNode = () => new CheckError(300, '至少包含一个节点')
|
||||
export const hasUnfinishedNode = (nodeId: string) => new CheckError(301, `存在尚未配置完成的节点: ${nodeId}`)
|
||||
export const nodeTypeNotFound = () => new CheckError(302, '节点类型不存在')
|
||||
export const nodeError = (nodeId: string, reason?: string) => new CheckError(303, reason ?? `节点配置存在错误:${nodeId}`)
|
||||
|
||||
// @ts-ignore
|
||||
export const checkSave: (nodes: Node[], edges: Edge[], data: any) => void = (nodes, edges, data) => {
|
||||
@@ -71,8 +74,21 @@ export const checkSave: (nodes: Node[], edges: Edge[], data: any) => void = (nod
|
||||
}
|
||||
|
||||
for (let node of nodes) {
|
||||
if (!data[node.id] || !data[node.id]?.finished) {
|
||||
throw hasUnfinishedNode(node.id)
|
||||
let nodeId = node.id
|
||||
if (!has(data, nodeId) || !data[nodeId]?.finished) {
|
||||
throw hasUnfinishedNode(nodeId)
|
||||
}
|
||||
|
||||
if (!has(node, 'type')) {
|
||||
throw nodeTypeNotFound()
|
||||
}
|
||||
let nodeType = node.type!
|
||||
let nodeDefine = NodeRegistry[nodeType]
|
||||
for (let checker of nodeDefine.checkers) {
|
||||
let checkResult = checker(nodeId, nodes, edges, data)
|
||||
if (checkResult.error) {
|
||||
throw nodeError(nodeId, checkResult.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user