Compare commits

...
3 Commits
Author SHA1 Message Date
wc24 06aec7f0dc feat(required): 按类型递归判定空(tab/object/array 各自语义)
isEmptyValue(field,value) 递归: tab=没选type / object=所有子字段都空 / array=无元素或
所有元素都空(多维递归) / 标量=null|''/空数组(0、false 不算空)。required=!isEmpty 自动
对所有类型生效。validateNode: 空的复合(非必填)不下钻校验子(整个结构缺席)。
2026-06-30 16:52:08 +08:00
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 50 additions and 18 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.6",
"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"
} }
} }
+40 -11
View File
@@ -68,9 +68,27 @@ export class ShrimpTableModule<T, P extends ITableCrudProto<T> = ITableCrudProto
return ShrimpTableModule.typeHandlers.get(field.type) return ShrimpTableModule.typeHandlers.get(field.type)
} }
/** 按类型递归判定"空":tab=没选type / object=所有子字段都空 / array=无元素或所有元素都空(多维递归)/ 标量=null|'' / 空数组 */
private isEmptyValue(field: IFieldConfig, value: any): boolean {
if (value === undefined || value === null) return true
switch (field.type) {
case ShrimpFieldTypes.Tab:
return typeof value !== "object" || value.type == null
case ShrimpFieldTypes.Object:
if (typeof value !== "object" || !Array.isArray(field.fields)) return true
return field.fields.every(f => this.isEmptyValue(f, value[f.key]))
case ShrimpFieldTypes.Array:
if (!Array.isArray(value) || value.length === 0) return true
return field.item ? value.every(el => this.isEmptyValue(field.item!, el)) : false
default:
// 标量/relation/select:'' 或空数组(multiple)算空;0、false 不算空
return value === "" || (Array.isArray(value) && value.length === 0)
}
}
/** 前置验证 */ /** 前置验证 */
private async validateField(field: IFieldConfig, value: any, item: Record<string, any>): Promise<ILabel | null> { private async validateField(field: IFieldConfig, value: any, item: Record<string, any>): Promise<ILabel | null> {
const isEmpty = value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0) const isEmpty = this.isEmptyValue(field, value)
if (field.required && isEmpty) { if (field.required && isEmpty) {
return field.rules?.find(r => r.required)?.message || { t: 'shrimp.fieldRequired', params: { field: field.label } } return field.rules?.find(r => r.required)?.message || { t: 'shrimp.fieldRequired', params: { field: field.label } }
@@ -193,7 +211,11 @@ 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)
// 空的复合(走到这必是非必填)→ 不下钻校验子:整个结构缺席,里面的 required 不该触发
if (this.isEmptyValue(field, value)) return
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 +237,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 +317,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 +340,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)) {