befly 3.8.7 → 3.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/check.ts +46 -32
- package/package.json +2 -2
package/check.ts
CHANGED
|
@@ -63,6 +63,7 @@ export const checkTable = async function (): Promise<boolean> {
|
|
|
63
63
|
|
|
64
64
|
// 收集所有表文件
|
|
65
65
|
const allTableFiles: TableFileInfo[] = [];
|
|
66
|
+
let hasError = false;
|
|
66
67
|
|
|
67
68
|
// 收集项目表字段定义文件
|
|
68
69
|
for await (const file of tablesGlob.scan({
|
|
@@ -105,6 +106,7 @@ export const checkTable = async function (): Promise<boolean> {
|
|
|
105
106
|
// 1) 文件名小驼峰校验
|
|
106
107
|
if (!LOWER_CAMEL_CASE_REGEX.test(fileBaseName)) {
|
|
107
108
|
Logger.warn(`${item.typeName}表 ${fileName} 文件名必须使用小驼峰命名(例如 testCustomers.json)`);
|
|
109
|
+
hasError = true;
|
|
108
110
|
continue;
|
|
109
111
|
}
|
|
110
112
|
|
|
@@ -116,113 +118,125 @@ export const checkTable = async function (): Promise<boolean> {
|
|
|
116
118
|
for (const [colKey, fieldDef] of Object.entries(table)) {
|
|
117
119
|
if (typeof fieldDef !== 'object' || fieldDef === null || Array.isArray(fieldDef)) {
|
|
118
120
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 规则必须为对象`);
|
|
121
|
+
hasError = true;
|
|
119
122
|
continue;
|
|
120
123
|
}
|
|
121
124
|
|
|
122
125
|
// 检查是否使用了保留字段
|
|
123
126
|
if (RESERVED_FIELDS.includes(colKey as any)) {
|
|
124
127
|
Logger.warn(`${item.typeName}表 ${fileName} 文件包含保留字段 ${colKey},` + `不能在表定义中使用以下字段: ${RESERVED_FIELDS.join(', ')}`);
|
|
128
|
+
hasError = true;
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
// 直接使用字段对象
|
|
128
132
|
const field = fieldDef as FieldDefinition;
|
|
129
133
|
|
|
130
|
-
// 检查必填字段:name, type
|
|
134
|
+
// 检查必填字段:name, type
|
|
131
135
|
if (!field.name || typeof field.name !== 'string') {
|
|
132
136
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 缺少必填字段 name 或类型错误`);
|
|
137
|
+
hasError = true;
|
|
133
138
|
continue;
|
|
134
139
|
}
|
|
135
140
|
if (!field.type || typeof field.type !== 'string') {
|
|
136
141
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 缺少必填字段 type 或类型错误`);
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
if (field.min === undefined) {
|
|
140
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 缺少必填字段 min`);
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
if (field.max === undefined) {
|
|
144
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 缺少必填字段 max`);
|
|
142
|
+
hasError = true;
|
|
145
143
|
continue;
|
|
146
144
|
}
|
|
147
145
|
|
|
148
146
|
// 检查可选字段的类型
|
|
147
|
+
if (field.min !== undefined && !(field.min === null || typeof field.min === 'number')) {
|
|
148
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 min 类型错误,必须为 null 或数字`);
|
|
149
|
+
hasError = true;
|
|
150
|
+
}
|
|
151
|
+
if (field.max !== undefined && !(field.max === null || typeof field.max === 'number')) {
|
|
152
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 max 类型错误,必须为 null 或数字`);
|
|
153
|
+
hasError = true;
|
|
154
|
+
}
|
|
149
155
|
if (field.detail !== undefined && typeof field.detail !== 'string') {
|
|
150
156
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 detail 类型错误,必须为字符串`);
|
|
157
|
+
hasError = true;
|
|
151
158
|
}
|
|
152
159
|
if (field.index !== undefined && typeof field.index !== 'boolean') {
|
|
153
160
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 index 类型错误,必须为布尔值`);
|
|
161
|
+
hasError = true;
|
|
154
162
|
}
|
|
155
163
|
if (field.unique !== undefined && typeof field.unique !== 'boolean') {
|
|
156
164
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 unique 类型错误,必须为布尔值`);
|
|
165
|
+
hasError = true;
|
|
157
166
|
}
|
|
158
167
|
if (field.nullable !== undefined && typeof field.nullable !== 'boolean') {
|
|
159
168
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 nullable 类型错误,必须为布尔值`);
|
|
169
|
+
hasError = true;
|
|
160
170
|
}
|
|
161
171
|
if (field.unsigned !== undefined && typeof field.unsigned !== 'boolean') {
|
|
162
172
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 unsigned 类型错误,必须为布尔值`);
|
|
173
|
+
hasError = true;
|
|
163
174
|
}
|
|
164
175
|
if (field.regexp !== undefined && field.regexp !== null && typeof field.regexp !== 'string') {
|
|
165
176
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段 regexp 类型错误,必须为 null 或字符串`);
|
|
177
|
+
hasError = true;
|
|
166
178
|
}
|
|
167
179
|
|
|
168
|
-
const { name: fieldName, type: fieldType, min: fieldMin, max: fieldMax, default: fieldDefault
|
|
180
|
+
const { name: fieldName, type: fieldType, min: fieldMin, max: fieldMax, default: fieldDefault } = field;
|
|
169
181
|
|
|
170
|
-
//
|
|
182
|
+
// 字段名称必须为中文、数字、字母、下划线、短横线、空格
|
|
171
183
|
if (!FIELD_NAME_REGEX.test(fieldName)) {
|
|
172
184
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段名称 "${fieldName}" 格式错误,` + `必须为中文、数字、字母、下划线、短横线、空格`);
|
|
185
|
+
hasError = true;
|
|
173
186
|
}
|
|
174
187
|
|
|
175
|
-
//
|
|
188
|
+
// 字段类型必须为string,number,text,array_string,array_text之一
|
|
176
189
|
if (!FIELD_TYPES.includes(fieldType as any)) {
|
|
177
190
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 字段类型 "${fieldType}" 格式错误,` + `必须为${FIELD_TYPES.join('、')}之一`);
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// 第3/4个值:需要是 null 或 数字
|
|
181
|
-
if (!(fieldMin === null || typeof fieldMin === 'number')) {
|
|
182
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 最小值 "${fieldMin}" 格式错误,必须为null或数字`);
|
|
183
|
-
}
|
|
184
|
-
if (!(fieldMax === null || typeof fieldMax === 'number')) {
|
|
185
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 最大值 "${fieldMax}" 格式错误,必须为null或数字`);
|
|
191
|
+
hasError = true;
|
|
186
192
|
}
|
|
187
193
|
|
|
188
194
|
// 约束:当最小值与最大值均为数字时,要求最小值 <= 最大值
|
|
189
|
-
if (fieldMin !== null && fieldMax !== null) {
|
|
195
|
+
if (fieldMin !== undefined && fieldMax !== undefined && fieldMin !== null && fieldMax !== null) {
|
|
190
196
|
if (fieldMin > fieldMax) {
|
|
191
197
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 最小值 "${fieldMin}" 不能大于最大值 "${fieldMax}"`);
|
|
198
|
+
hasError = true;
|
|
192
199
|
}
|
|
193
200
|
}
|
|
194
201
|
|
|
195
|
-
//
|
|
202
|
+
// 类型联动校验 + 默认值规则
|
|
196
203
|
if (fieldType === 'text') {
|
|
197
|
-
// text:min/max
|
|
198
|
-
if (fieldMin !== null) {
|
|
199
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 的 text
|
|
204
|
+
// text:min/max 应该为 null,默认值必须为 null
|
|
205
|
+
if (fieldMin !== undefined && fieldMin !== null) {
|
|
206
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 的 text 类型最小值应为 null,当前为 "${fieldMin}"`);
|
|
207
|
+
hasError = true;
|
|
200
208
|
}
|
|
201
|
-
if (fieldMax !== null) {
|
|
202
|
-
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 的 text
|
|
209
|
+
if (fieldMax !== undefined && fieldMax !== null) {
|
|
210
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 的 text 类型最大长度应为 null,当前为 "${fieldMax}"`);
|
|
211
|
+
hasError = true;
|
|
203
212
|
}
|
|
204
|
-
if (fieldDefault !== null) {
|
|
213
|
+
if (fieldDefault !== undefined && fieldDefault !== null) {
|
|
205
214
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 为 text 类型,默认值必须为 null,当前为 "${fieldDefault}"`);
|
|
215
|
+
hasError = true;
|
|
206
216
|
}
|
|
207
|
-
} else if (fieldType === 'string' || fieldType === '
|
|
208
|
-
if (fieldMax === null || typeof fieldMax !== 'number') {
|
|
217
|
+
} else if (fieldType === 'string' || fieldType === 'array_string') {
|
|
218
|
+
if (fieldMax !== undefined && (fieldMax === null || typeof fieldMax !== 'number')) {
|
|
209
219
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 为 ${fieldType} 类型,` + `最大长度必须为数字,当前为 "${fieldMax}"`);
|
|
210
|
-
|
|
220
|
+
hasError = true;
|
|
221
|
+
} else if (fieldMax !== undefined && fieldMax > MAX_VARCHAR_LENGTH) {
|
|
211
222
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 最大长度 ${fieldMax} 越界,` + `${fieldType} 类型长度必须在 1..${MAX_VARCHAR_LENGTH} 范围内`);
|
|
223
|
+
hasError = true;
|
|
212
224
|
}
|
|
213
225
|
} else if (fieldType === 'number') {
|
|
214
226
|
// number 类型:default 如果存在,必须为 null 或 number
|
|
215
227
|
if (fieldDefault !== undefined && fieldDefault !== null && typeof fieldDefault !== 'number') {
|
|
216
228
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 为 number 类型,` + `默认值必须为数字或 null,当前为 "${fieldDefault}"`);
|
|
229
|
+
hasError = true;
|
|
217
230
|
}
|
|
218
231
|
}
|
|
219
232
|
}
|
|
220
233
|
} catch (error: any) {
|
|
221
234
|
Logger.error(`${item.typeName}表 ${fileName} 解析失败`, error);
|
|
235
|
+
hasError = true;
|
|
222
236
|
}
|
|
223
237
|
}
|
|
224
238
|
|
|
225
|
-
return
|
|
239
|
+
return !hasError;
|
|
226
240
|
} catch (error: any) {
|
|
227
241
|
Logger.error('数据表定义检查过程中出错', error);
|
|
228
242
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.9",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -69,5 +69,5 @@
|
|
|
69
69
|
"es-toolkit": "^1.41.0",
|
|
70
70
|
"pathe": "^2.0.3"
|
|
71
71
|
},
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "6e8ca2ec311fcc99e89adc35c151e9a63e7e8e0e"
|
|
73
73
|
}
|