feat(validate): 递归校验引擎(校验下沉到任意深度)
childrenOf(field,value): tab 选中分支 fields / object fields / array 元素套 item → 子(字段,值)对。 validateNode 递归: 每节点跑 validateField,出错即抛(带 path),再下钻复合。 接入 prepareAddItem/prepareEditItem(替原顶层 validateField)。第一版 fail-fast, 嵌套错 message 缀 [path] 定位。默认/transform 下沉、collect-all、inline 标红留后续。
This commit is contained in:
Generated
+203
-162
File diff suppressed because it is too large
Load Diff
+19
-8
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "shrimp-server",
|
||||
"version": "0.7.2",
|
||||
"version": "0.7.3",
|
||||
"description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)",
|
||||
"main": "dist/main/index.js",
|
||||
"types": "dist/main/index.d.ts",
|
||||
@@ -24,21 +24,29 @@
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bcrypt": "^5 || ^6",
|
||||
"express": "^4 || ^5",
|
||||
"mongoose": "^6 || ^7 || ^8",
|
||||
"jsonwebtoken": "^8 || ^9",
|
||||
"bcrypt": "^5 || ^6"
|
||||
"mongoose": "^6 || ^7 || ^8"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"express": { "optional": true },
|
||||
"mongoose": { "optional": true },
|
||||
"jsonwebtoken": { "optional": true },
|
||||
"bcrypt": { "optional": true }
|
||||
"express": {
|
||||
"optional": true
|
||||
},
|
||||
"mongoose": {
|
||||
"optional": true
|
||||
},
|
||||
"jsonwebtoken": {
|
||||
"optional": true
|
||||
},
|
||||
"bcrypt": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/jsonwebtoken": "^9.0.0",
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/node": "^20.0.0",
|
||||
"bcrypt": "^5.1.1",
|
||||
"express": "^4.19.0",
|
||||
@@ -47,5 +55,8 @@
|
||||
"tsx": "^4.7.0",
|
||||
"typescript": "^5.3.0",
|
||||
"vitest": "^1.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,8 +151,7 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
||||
}
|
||||
|
||||
const fieldErr = await this.validateField(field, result[field.key], result)
|
||||
if (fieldErr) throw new ShrimpError(fieldErr)
|
||||
await this.validateNode(field, result[field.key], [field.key], result)
|
||||
|
||||
if (handler?.validate && result[field.key] !== undefined) {
|
||||
const err = await handler.validate(result[field.key], field, result)
|
||||
@@ -181,8 +180,7 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
result[field.key] = await handler.reqFilter(result[field.key], field, result)
|
||||
}
|
||||
|
||||
const fieldErr = await this.validateField(field, result[field.key], result)
|
||||
if (fieldErr) throw new ShrimpError(fieldErr)
|
||||
await this.validateNode(field, result[field.key], [field.key], result)
|
||||
|
||||
if (handler?.validate) {
|
||||
const err = await handler.validate(result[field.key], field, result)
|
||||
@@ -192,6 +190,39 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
|
||||
return result
|
||||
}
|
||||
|
||||
/** 递归校验:本节点 validateField,出错即抛(带 path);再下钻复合(tab/object/array)子节点。第一版 fail-fast */
|
||||
private async validateNode(field: IFieldConfig, value: any, path: (string | number)[], rootItem: Record<string, any>): Promise<void> {
|
||||
const err = await this.validateField(field, value, rootItem)
|
||||
if (err) throw new ShrimpError(this.errWithPath(err, path))
|
||||
for (const child of this.childrenOf(field, value)) {
|
||||
await this.validateNode(child.field, child.value, [...path, child.key], rootItem)
|
||||
}
|
||||
}
|
||||
|
||||
/** 复合类型吐出子 (字段, 值) 对:tab 选中分支的 fields / object 的 fields / array 每个元素套 item。叶子或空 → [] */
|
||||
private childrenOf(field: IFieldConfig, value: any): { key: string | number; field: IFieldConfig; value: any }[] {
|
||||
if (value == null || typeof value !== "object") return []
|
||||
if (field.type === ShrimpFieldTypes.Tab && Array.isArray(field.tabs)) {
|
||||
const branch = field.tabs.find(t => t.key === value.type)
|
||||
return branch ? branch.fields.map(f => ({ key: f.key, field: f, value: value[f.key] })) : []
|
||||
}
|
||||
if (field.type === ShrimpFieldTypes.Object && Array.isArray(field.fields)) {
|
||||
return field.fields.map(f => ({ key: f.key, field: f, value: value[f.key] }))
|
||||
}
|
||||
if (field.type === ShrimpFieldTypes.Array && field.item && Array.isArray(value)) {
|
||||
return value.map((el, i) => ({ key: i as number, field: field.item!, value: el }))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
/** 嵌套字段出错时把 path 缀进 message(顶层不缀,已有字段名),让用户知道是哪个嵌套位置 */
|
||||
private errWithPath(err: ILabel, path: (string | number)[]): ILabel {
|
||||
if (path.length <= 1 || typeof err !== "object" || err === null) return err
|
||||
const e = err as any
|
||||
const tag = `[${path.join(".")}]`
|
||||
return { ...e, params: { ...e.params, field: e.params?.field != null ? `${e.params.field} ${tag}` : tag } }
|
||||
}
|
||||
|
||||
private async processResItem(item: Record<string, any>): Promise<Record<string, any>> {
|
||||
const result = { ...item }
|
||||
for (const field of this.fields) {
|
||||
|
||||
Reference in New Issue
Block a user