Compare commits

...
2 Commits
Author SHA1 Message Date
wc24 172c49816e feat(filter): 嵌套 filterable 叶子 → 点路径过滤(object/tab)
collectFilterablePaths 递归收集标了 filterable 的字段(object 子/tab 分支子)连同点路径;
buildQuery 白名单从顶层字段扩成路径(点路径本就是 mongoose 嵌套查询,翻译逻辑不变)。
array 下钻、tab 判别值过滤留后续。
2026-06-30 16:01:20 +08:00
wc24 a8cd07f851 fix(validate): 错误信息不再拼开发用的 [path],回归干净字段名+i18n
校验错只发 {t:i18nKey, params:{field:label}}(不能为空走语言包 key、字段名走 label),
不把 groups.0.title 这种路径塞进可见文案。path 仍递归传递,留给将来 inline 标红结构化携带。
2026-06-30 15:17:58 +08:00
3 changed files with 29 additions and 17 deletions
+8 -5
View File
@@ -1,14 +1,14 @@
{ {
"name": "shrimp-server", "name": "shrimp-server",
"version": "0.7.2", "version": "0.7.4",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "shrimp-server", "name": "shrimp-server",
"version": "0.7.2", "version": "0.7.4",
"dependencies": { "dependencies": {
"shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.2" "shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.4"
}, },
"devDependencies": { "devDependencies": {
"@types/bcrypt": "^5.0.0", "@types/bcrypt": "^5.0.0",
@@ -3374,8 +3374,11 @@
} }
}, },
"node_modules/shrimp-server": { "node_modules/shrimp-server": {
"version": "0.7.2", "version": "0.7.4",
"resolved": "git+http://git.zeromvc.com/wc24/shrimp-server.git#6383aade980b848871390eacaf52bb53c11387cb", "resolved": "git+http://git.zeromvc.com/wc24/shrimp-server.git#a8cd07f8511487186732dfd884121f7acb77f5c0",
"dependencies": {
"shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.3"
},
"peerDependencies": { "peerDependencies": {
"bcrypt": "^5 || ^6", "bcrypt": "^5 || ^6",
"express": "^4 || ^5", "express": "^4 || ^5",
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "shrimp-server", "name": "shrimp-server",
"version": "0.7.3", "version": "0.7.5",
"description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)", "description": "Shrimp 框架后端 — 声明式字段驱动的 Node.js 管理后台框架(Express + Mongoose + JWT)",
"main": "dist/main/index.js", "main": "dist/main/index.js",
"types": "dist/main/index.d.ts", "types": "dist/main/index.d.ts",
@@ -57,6 +57,6 @@
"vitest": "^1.4.0" "vitest": "^1.4.0"
}, },
"dependencies": { "dependencies": {
"shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.2" "shrimp-server": "git+http://git.zeromvc.com/wc24/shrimp-server.git#v0.7.4"
} }
} }
+19 -10
View File
@@ -193,7 +193,9 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
/** 递归校验:本节点 validateField,出错即抛(带 path);再下钻复合(tab/object/array)子节点。第一版 fail-fast */ /** 递归校验:本节点 validateField,出错即抛(带 path);再下钻复合(tab/object/array)子节点。第一版 fail-fast */
private async validateNode(field: IFieldConfig, value: any, path: (string | number)[], rootItem: Record<string, any>): Promise<void> { private async validateNode(field: IFieldConfig, value: any, path: (string | number)[], rootItem: Record<string, any>): Promise<void> {
const err = await this.validateField(field, value, rootItem) const err = await this.validateField(field, value, rootItem)
if (err) throw new ShrimpError(this.errWithPath(err, path)) // 出错只发字段名(label) + i18n key 的 message,不把开发用的 path 拼进可见文案。
// path 仍递归往下传(供将来 inline 标红按结构化方式携带,不在这里塞进 message)。
if (err) throw new ShrimpError(err)
for (const child of this.childrenOf(field, value)) { for (const child of this.childrenOf(field, value)) {
await this.validateNode(child.field, child.value, [...path, child.key], rootItem) await this.validateNode(child.field, child.value, [...path, child.key], rootItem)
} }
@@ -215,14 +217,6 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
return [] 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>> { private async processResItem(item: Record<string, any>): Promise<Record<string, any>> {
const result = { ...item } const result = { ...item }
for (const field of this.fields) { for (const field of this.fields) {
@@ -303,6 +297,19 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
return this.excludeKeys(item, ["__v", ...this.fields.filter(f => f.hidden).map(f => f.key)]) return this.excludeKeys(item, ["__v", ...this.fields.filter(f => f.hidden).map(f => f.key)])
} }
/** 递归收集标了 filterable 的字段 → 点路径(object 子 / tab 分支子)。array 暂不下钻(后续) */
private collectFilterablePaths(fields: IFieldConfig[], base: string[], out: Map<string, IFieldConfig>): void {
for (const f of fields) {
const path = [...base, f.key]
if (f.filterable) out.set(path.join("."), f)
if (f.type === ShrimpFieldTypes.Tab && Array.isArray(f.tabs)) {
for (const t of f.tabs) this.collectFilterablePaths(t.fields, path, out)
} else if (f.type === ShrimpFieldTypes.Object && Array.isArray(f.fields)) {
this.collectFilterablePaths(f.fields, path, out)
}
}
}
private buildQuery(query?: IPageQuery & any): IShrimpQuery { private buildQuery(query?: IPageQuery & any): IShrimpQuery {
const { keyword, page, pageSize, sortBy, sortOrder, filter, ownerId } = query || {} const { keyword, page, pageSize, sortBy, sortOrder, filter, ownerId } = query || {}
const p = page || 1 const p = page || 1
@@ -313,7 +320,9 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
} }
if (filter && typeof filter === 'object') { if (filter && typeof filter === 'object') {
const filterableFields = new Map(this.fields.filter(f => f.filterable).map(f => [f.key, f])) // filterable 路径(含 object/tab 嵌套叶子,点路径如 condition.level)→ 白名单。点路径本就是 mongoose 嵌套查询,翻译逻辑不变
const filterableFields = new Map<string, IFieldConfig>()
this.collectFilterablePaths(this.fields, [], filterableFields)
const stringLikeTypes = new Set(['string', 'textarea']) const stringLikeTypes = new Set(['string', 'textarea'])
const where: Record<string, IShrimpCondition> = {} const where: Record<string, IShrimpCondition> = {}
for (const [key, value] of Object.entries(filter)) { for (const [key, value] of Object.entries(filter)) {