mm_datas 5.3.1 → 5.3.2

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/lib/model.js CHANGED
@@ -33,10 +33,12 @@
33
33
  * ② 递归遍历 properties / items 并拼接错误路径;
34
34
  * ③ additionalProperties:false 的字段清单比对(结构性,非规则判定);
35
35
  * ④ 空串补检(params 会跳过空值,改用 length / pattern / enum / type 工具)。
36
- * 所有类型、范围、长度、正则、枚举的判定均由 mm_validator 的工具执行。
36
+ * 所有类型、范围、长度、正则、枚举的判定均由 mm_validator 的工具执行
37
+ * (校验器调用层见 model_validator.js)。
37
38
  */
38
- const path = require('path');
39
39
  const { raise } = require('./errors.js');
40
+ const runner = require('./model_validator.js');
41
+ const check_mod = require('./model_check.js');
40
42
 
41
43
  const DRAFT = 'http://json-schema.org/draft-07/schema#';
42
44
  const ALLOWED_TYPES = ['string', 'number', 'boolean', 'object', 'array', 'null'];
@@ -46,63 +48,34 @@ const FORBIDDEN_TYPES = {
46
48
  float: 'number', long: 'number', double: 'number', bigint: 'number'
47
49
  };
48
50
 
49
- let V = null;
50
- let V_ERROR = null;
51
- try {
52
- V = require('mm_validator');
53
- } catch (e) {
54
- V_ERROR = e.message;
55
- }
56
-
57
- const TOOLS = {};
58
- const CALLS = { total: 0, by_tool: {} };
59
-
60
- function tool(name) {
61
- if (!V) {
62
- raise('VALIDATOR_MISSING', 'schema 校验依赖 mm_validator:模块未安装或无法解析' +
63
- (V_ERROR ? '(' + V_ERROR + ')' : ''));
64
- }
65
- if (!TOOLS[name]) {
66
- try {
67
- TOOLS[name] = require(path.join(V.TOOL_DIR, name));
68
- } catch (e) {
69
- raise('VALIDATOR_MISSING', '无法加载 mm_validator/tool/' + name + ':' + e.message);
70
- }
71
- if (!TOOLS[name] || typeof TOOLS[name].main !== 'function') {
72
- raise('VALIDATOR_MISSING', 'mm_validator/tool/' + name + ' 未导出 main');
73
- }
74
- }
75
- return TOOLS[name];
76
- }
77
-
78
- /** 调用一个 mm_validator 工具;工具当前同步写 ctx.output,若未来异步化则明确报错而非放行 */
79
- function run(name, params) {
80
- const ctx = { output: null };
81
- let ret = null;
82
- try {
83
- ret = tool(name).main(params, ctx, {});
84
- } catch (e) {
85
- raise('VALIDATOR_ERROR', 'mm_validator/' + name + ' 执行失败:' + e.message);
86
- }
87
- CALLS.total++;
88
- CALLS.by_tool[name] = (CALLS.by_tool[name] || 0) + 1;
89
- if (ret && typeof ret.then === 'function') ret.catch(function () {});
90
- if (!ctx.output) {
91
- raise('VALIDATOR_ERROR', 'mm_validator/' + name + ' 未同步返回校验结果(工具已异步化,需同步适配)');
92
- }
93
- return ctx.output;
94
- }
95
-
51
+ /**
52
+ * 深拷贝(JSON 序列化实现)
53
+ * @param {*} v 任意值
54
+ * @returns {*} 拷贝结果
55
+ */
96
56
  function clone(v) {
97
57
  return v === undefined ? undefined : JSON.parse(JSON.stringify(v));
98
58
  }
99
59
 
60
+ /**
61
+ * 三候选取值
62
+ * @param {*} a 候选 a
63
+ * @param {*} b 候选 b
64
+ * @param {*} c 候选 c
65
+ * @returns {*} 第一个非 undefined 的值
66
+ */
100
67
  function pick(a, b, c) {
101
68
  if (a !== undefined) return a;
102
69
  if (b !== undefined) return b;
103
70
  return c;
104
71
  }
105
72
 
73
+ /**
74
+ * 拼接校验错误路径
75
+ * @param {string} prefix 前缀
76
+ * @param {string} seg 片段
77
+ * @returns {string} 完整路径
78
+ */
106
79
  function joinPath(prefix, seg) {
107
80
  return prefix ? prefix + '.' + seg : seg;
108
81
  }
@@ -111,18 +84,18 @@ function joinPath(prefix, seg) {
111
84
  function assertTypes(def, loc) {
112
85
  if (!def || typeof def !== 'object') return;
113
86
  if (def.type !== undefined) {
114
- const list = Array.isArray(def.type) ? def.type : [def.type];
87
+ let list = Array.isArray(def.type) ? def.type : [def.type];
115
88
  for (let i = 0; i < list.length; i++) {
116
- const t = list[i];
89
+ let t = list[i];
117
90
  if (ALLOWED_TYPES.indexOf(t) === -1) {
118
- const hint = FORBIDDEN_TYPES[t] ? ',请改用 "' + FORBIDDEN_TYPES[t] + '"' : '';
91
+ let hint = FORBIDDEN_TYPES[t] ? ',请改用 "' + FORBIDDEN_TYPES[t] + '"' : '';
119
92
  raise('SCHEMA_TYPE_UNSUPPORTED', loc + ' 使用了不支持的类型 "' + t + '"' + hint +
120
93
  ';标准 JSON Schema 允许的类型仅:' + ALLOWED_TYPES.join(' / '));
121
94
  }
122
95
  }
123
96
  }
124
97
  if (def.properties) {
125
- for (const f in def.properties) assertTypes(def.properties[f], loc + '.properties.' + f);
98
+ for (let f in def.properties) assertTypes(def.properties[f], loc + '.properties.' + f);
126
99
  }
127
100
  if (def.items) assertTypes(def.items, loc + '.items');
128
101
  if (def.additionalProperties && typeof def.additionalProperties === 'object') {
@@ -138,26 +111,26 @@ function toStandard(raw) {
138
111
 
139
112
  // 简写:['name', 'level'] 或 [{ name:'name', type:'string' }]
140
113
  if (Array.isArray(raw)) {
141
- const props = {};
114
+ let props = {};
142
115
  for (let i = 0; i < raw.length; i++) {
143
- const it = raw[i];
116
+ let it = raw[i];
144
117
  if (typeof it === 'string') props[it] = {};
145
118
  else if (it && it.name) {
146
- const d = Object.assign({}, it);
119
+ let d = Object.assign({}, it);
147
120
  delete d.name;
148
121
  props[it.name] = d;
149
122
  }
150
123
  }
151
- const js = { $schema: DRAFT, type: 'object', properties: props, required: [], additionalProperties: false };
124
+ let js = { $schema: DRAFT, type: 'object', properties: props, required: [], additionalProperties: false };
152
125
  assertTypes(js, 'schema');
153
126
  return js;
154
127
  }
155
128
 
156
129
  if (typeof raw !== 'object') return null;
157
130
 
158
- const isStandard = raw.type === 'object' || !!raw.properties || !!raw.$schema || Array.isArray(raw.required);
159
- if (isStandard) {
160
- const js = clone(raw);
131
+ let is_standard = raw.type === 'object' || !!raw.properties || !!raw.$schema || Array.isArray(raw.required);
132
+ if (is_standard) {
133
+ let js = clone(raw);
161
134
  js.$schema = js.$schema || DRAFT;
162
135
  js.type = 'object';
163
136
  js.properties = js.properties || {};
@@ -167,25 +140,8 @@ function toStandard(raw) {
167
140
  return js;
168
141
  }
169
142
 
170
- // 兼容旧的扁平字段表:自动升级为标准 JSON Schema(required:true → 顶层 required 数组)
171
- const props = {};
172
- const required = [];
173
- for (const f in raw) {
174
- const src = typeof raw[f] === 'string' ? { type: raw[f] } : Object.assign({}, raw[f] || {});
175
- if (src.required === true || src.not_null === true) required.push(f);
176
- delete src.required;
177
- delete src.not_null;
178
- delete src.validator; // 自定义校验函数不属于标准 JSON Schema
179
- // 旧扁平格式的私有别名统一翻译成标准关键词,保证 schema() 返回的是标准文档
180
- if (src.min !== undefined) { src.minimum = src.min; delete src.min; }
181
- if (src.max !== undefined) { src.maximum = src.max; delete src.max; }
182
- if (src.min_length !== undefined) { src.minLength = src.min_length; delete src.min_length; }
183
- if (src.max_length !== undefined) { src.maxLength = src.max_length; delete src.max_length; }
184
- props[f] = src;
185
- }
186
- const js = { $schema: DRAFT, type: 'object', properties: props, required: required, additionalProperties: false };
187
- assertTypes(js, 'schema');
188
- return js;
143
+ // 非空但非标准的定义直接报错,不再猜测旧扁平格式
144
+ raise('SCHEMA_VALIDATION', 'schema 必须是标准 JSON Schema(type:object + properties),不支持旧扁平字段表');
189
145
  }
190
146
 
191
147
  /** 字段定义:保留标准关键词,补齐模块自身的扩展元数据 */
@@ -194,28 +150,28 @@ function normalizeField(raw) {
194
150
  if (typeof def === 'string') def = { type: def };
195
151
  else if (Array.isArray(def)) def = {};
196
152
  def = def || {};
197
- const item = Object.assign({}, def);
153
+ let item = Object.assign({}, def);
198
154
  item.primary = !!def.primary;
199
155
  item.auto = !!def.auto;
200
156
  item.description = def.description || '';
201
157
  item.snapshot = def.snapshot || null;
202
158
  item.ref = def.ref
203
159
  ? (typeof def.ref === 'string'
204
- ? { model: def.ref, key: null, mode: 'live', onMissing: 'error' }
205
- : Object.assign({ model: '', key: null, mode: 'live', onMissing: 'error' }, def.ref))
160
+ ? { model: def.ref, key: null, mode: 'live', on_missing: 'error' }
161
+ : Object.assign({ model: '', key: null, mode: 'live', on_missing: 'error' }, def.ref))
206
162
  : null;
207
163
  return item;
208
164
  }
209
165
 
210
166
  /** 标准文档的 properties → 内部扁平字段表(供 default / primary / ref / verify 使用) */
211
167
  function fieldsOf(js, props, required) {
212
- const out = {};
168
+ let out = {};
213
169
  if (!js) return out;
214
- const src = props || js.properties || {};
215
- const req = required || js.required || [];
216
- for (const f in src) {
217
- const raw = typeof src[f] === 'string' ? { type: src[f] } : Object.assign({}, src[f] || {});
218
- const item = normalizeField(raw);
170
+ let src = props || js.properties || {};
171
+ let req = required || js.required || [];
172
+ for (let f in src) {
173
+ let raw = typeof src[f] === 'string' ? { type: src[f] } : Object.assign({}, src[f] || {});
174
+ let item = normalizeField(raw);
219
175
  if (req.indexOf(f) !== -1) item.required = true;
220
176
  out[f] = item;
221
177
  }
@@ -224,16 +180,26 @@ function fieldsOf(js, props, required) {
224
180
 
225
181
  /** 嵌套 object 的 properties 转成一层内部字段表 */
226
182
  function childFields(def) {
227
- const req = Array.isArray(def.required) ? def.required : [];
183
+ let req = Array.isArray(def.required) ? def.required : [];
228
184
  return fieldsOf({ required: req }, def.properties || {}, req);
229
185
  }
230
186
 
187
+ /**
188
+ * 把用户 schema 归一成内部扁平字段表
189
+ * @param {*} raw 用户定义
190
+ * @returns {object} 扁平字段表
191
+ */
231
192
  function normalizeSchema(raw) {
232
193
  return fieldsOf(toStandard(raw));
233
194
  }
234
195
 
196
+ /**
197
+ * 找出主键字段名
198
+ * @param {object} schema 扁平字段表
199
+ * @returns {string} 主键字段名
200
+ */
235
201
  function findPrimary(schema) {
236
- for (const f in schema) if (schema[f].primary) return f;
202
+ for (let f in schema) if (schema[f].primary) return f;
237
203
  if (schema.id) return 'id';
238
204
  if (schema._id) return '_id';
239
205
  return 'id';
@@ -241,7 +207,7 @@ function findPrimary(schema) {
241
207
 
242
208
  /** 运行时读写标准文档里的某个字段定义(供 DataManager.field / ref 使用) */
243
209
  function setField(jsRaw, field, def) {
244
- const js = toStandard(jsRaw) || {
210
+ let js = toStandard(jsRaw) || {
245
211
  $schema: DRAFT, type: 'object', properties: {}, required: [], additionalProperties: false
246
212
  };
247
213
  js.properties[field] = def === undefined ? {} : def;
@@ -249,8 +215,14 @@ function setField(jsRaw, field, def) {
249
215
  return js;
250
216
  }
251
217
 
218
+ /**
219
+ * 读标准文档里的某个字段定义
220
+ * @param {*} jsRaw 标准文档
221
+ * @param {string} field 字段名
222
+ * @returns {*} 字段定义
223
+ */
252
224
  function getField(jsRaw, field) {
253
- const js = toStandard(jsRaw);
225
+ let js = toStandard(jsRaw);
254
226
  return js ? js.properties[field] : undefined;
255
227
  }
256
228
 
@@ -262,25 +234,25 @@ function normalize(name, def) {
262
234
  let kind = def.kind || (def.static ? 'static' : 'dynamic');
263
235
  if (['static', 'dynamic', 'link'].indexOf(kind) === -1) kind = 'dynamic';
264
236
 
265
- const jsonSchema = toStandard(def.schema || def.columns || def.fields || {});
266
- let schema = fieldsOf(jsonSchema);
267
- const store = def.store || {};
268
- const key = def.key || store.key || findPrimary(schema);
237
+ let json_schema = toStandard(def.schema || def.columns || def.fields || {});
238
+ let schema = fieldsOf(json_schema);
239
+ let store = def.store || {};
240
+ let key = def.key || store.key || findPrimary(schema);
269
241
  // 主键若未在 properties 中声明,自动补一项:避免 additionalProperties:false
270
242
  // 在 set/patch 校验「整行最终形态」时把自增主键误判为未知字段。
271
- if (jsonSchema && !jsonSchema.properties[key]) {
272
- jsonSchema.properties[key] = { auto: true };
273
- schema = fieldsOf(jsonSchema);
243
+ if (json_schema && !json_schema.properties[key]) {
244
+ json_schema.properties[key] = { auto: true };
245
+ schema = fieldsOf(json_schema);
274
246
  }
275
- const schemaless = Object.keys(schema).length === 0;
276
- const keyDef = schema[key];
277
- const auto = keyDef ? !!keyDef.auto : true;
278
- const readonly = def.readonly === undefined ? (kind === 'static') : !!def.readonly;
279
- const additionalProperties = jsonSchema ? jsonSchema.additionalProperties !== false : true;
280
-
281
- const relations = [];
282
- for (const field in schema) {
283
- const ref = schema[field].ref;
247
+ let schemaless = Object.keys(schema).length === 0;
248
+ let key_def = schema[key];
249
+ let auto = key_def ? !!key_def.auto : true;
250
+ let readonly = def.readonly === undefined ? (kind === 'static') : !!def.readonly;
251
+ let additionalProperties = json_schema ? json_schema.additionalProperties !== false : true;
252
+
253
+ let relations = [];
254
+ for (let field in schema) {
255
+ let ref = schema[field].ref;
284
256
  if (!ref) continue;
285
257
  relations.push({
286
258
  name: ref.as || ref.model || field,
@@ -290,12 +262,12 @@ function normalize(name, def) {
290
262
  foreign: ref.key || null,
291
263
  select: ref.select || null,
292
264
  mode: ref.mode || 'live',
293
- onMissing: ref.onMissing || 'error'
265
+ on_missing: ref.on_missing || 'error'
294
266
  });
295
267
  }
296
- const declared = def.relations || [];
268
+ let declared = def.relations || [];
297
269
  for (let i = 0; i < declared.length; i++) {
298
- const r = declared[i];
270
+ let r = declared[i];
299
271
  relations.push({
300
272
  name: r.name || r.as || ('rel_' + i),
301
273
  type: r.type || 'one-to-many',
@@ -325,7 +297,7 @@ function normalize(name, def) {
325
297
  shard: def.shard || store.shard || null,
326
298
  indexes: def.indexes || store.indexes || [],
327
299
  schema: schema, // 内部扁平字段表(元数据 + 标准关键词)
328
- jsonSchema: jsonSchema, // 用户定义的原始标准 JSON Schema 文档
300
+ json_schema: json_schema, // 用户定义的原始标准 JSON Schema 文档
329
301
  relations: relations,
330
302
  seed: def.seed || null,
331
303
  source: def.source || null,
@@ -335,195 +307,48 @@ function normalize(name, def) {
335
307
  };
336
308
  }
337
309
 
338
- /** 标准关键词 → mm_validator/tool/params 的单字段 rules */
339
- function ruleOf(def, value) {
340
- const r = {};
341
- if (!def || typeof def !== 'object') return r;
342
- if (def.required === true) r.required = true;
343
- const t = Array.isArray(def.type) ? def.type[0] : def.type;
344
- // null 无法由 params 的 typeof 判定(typeof null === 'object'),故不生成类型规则
345
- if (t && t !== 'null') r.type = t;
346
- const en = def.enum || (def.const !== undefined ? [def.const] : null);
347
- if (en) r.enum = en;
348
- if (def.pattern) r.pattern = def.pattern;
349
-
350
- if (def.minimum !== undefined) r.min = def.minimum;
351
- if (def.maximum !== undefined) r.max = def.maximum;
352
- // exclusive* 按非严格边界处理(mm_validator 无 exclusive 语义)
353
- if (def.exclusiveMinimum !== undefined && r.min === undefined) r.min = def.exclusiveMinimum;
354
- if (def.exclusiveMaximum !== undefined && r.max === undefined) r.max = def.exclusiveMaximum;
355
-
356
- const minLen = pick(def.minLength, def.minItems, def.min_length);
357
- const maxLen = pick(def.maxLength, def.maxItems, def.max_length);
358
- if (minLen !== undefined) r.min_length = minLen;
359
- if (maxLen !== undefined) r.max_length = maxLen;
360
-
361
- // 旧扁平格式的 min/max 别名:按运行时值类型落到数值范围或长度
362
- if (def.min !== undefined) {
363
- if (typeof value === 'number') { if (r.min === undefined) r.min = def.min; }
364
- else if (r.min_length === undefined) r.min_length = def.min;
365
- }
366
- if (def.max !== undefined) {
367
- if (typeof value === 'number') { if (r.max === undefined) r.max = def.max; }
368
- else if (r.max_length === undefined) r.max_length = def.max;
369
- }
370
- return r;
371
- }
372
-
373
- /** params 会跳过空串,改用 mm_validator 的单项工具补检,避免约束被静默弱化 */
374
- function checkEmpty(fullPath, field, value, def, errs) {
375
- const minLen = pick(def.minLength, def.minItems, def.min_length, def.min);
376
- const maxLen = pick(def.maxLength, def.maxItems, def.max_length, def.max);
377
- if (minLen !== undefined || maxLen !== undefined) {
378
- const out = run('length', { field: field, value: value, min: minLen, max: maxLen });
379
- if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
380
- }
381
- const en = def.enum || (def.const !== undefined ? [def.const] : null);
382
- if (en) {
383
- const out = run('enum', { field: field, value: value, options: en });
384
- if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
385
- }
386
- if (def.pattern) {
387
- const out = run('pattern', { field: field, value: value, pattern: def.pattern });
388
- if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
389
- }
390
- const t = Array.isArray(def.type) ? def.type[0] : def.type;
391
- if (t && t !== 'null') {
392
- const out = run('type', { field: field, value: value, expected_type: t });
393
- if (out && out.errors) for (let i = 0; i < out.errors.length; i++) errs.push(fullPath + ': ' + String(out.errors[i]));
394
- }
395
- }
396
-
397
- function collectParamErrors(out, prefix, errs) {
398
- if (!out || !out.errors) return;
399
- for (const f in out.errors) {
400
- const list = out.errors[f] || [];
401
- for (let i = 0; i < list.length; i++) {
402
- errs.push(joinPath(prefix, f) + ': ' + String(list[i]));
403
- }
404
- }
405
- }
406
-
407
- /** 一层 object 的校验:字段清单比对 + 一次 params 批量调用 + 嵌套递归 */
408
- function validateLevel(fields, obj, prefix, errs, noExtra) {
409
- if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return;
410
-
411
- if (noExtra) {
412
- for (const k in obj) {
413
- if (!Object.prototype.hasOwnProperty.call(fields, k)) {
414
- errs.push(joinPath(prefix, k) + ': 未在 schema 中声明(additionalProperties: false)');
415
- }
416
- }
417
- }
418
-
419
- const rules = {};
420
- const emptyFields = [];
421
- for (const f in fields) {
422
- const r = ruleOf(fields[f], obj[f]);
423
- if (Object.keys(r).length) rules[f] = r;
424
- if (obj[f] === '') emptyFields.push(f);
425
- }
426
- if (Object.keys(rules).length) {
427
- collectParamErrors(run('params', { input: obj, rules: rules }), prefix, errs);
428
- }
429
- for (let i = 0; i < emptyFields.length; i++) {
430
- const f = emptyFields[i];
431
- checkEmpty(joinPath(prefix, f), f, obj[f], fields[f], errs);
432
- }
433
-
434
- for (const f in fields) {
435
- const def = fields[f];
436
- const v = obj[f];
437
- if (v === undefined || v === null) continue;
438
- const p = joinPath(prefix, f);
439
- if (def.properties && typeof v === 'object' && !Array.isArray(v)) {
440
- validateLevel(childFields(def), v, p, errs, def.additionalProperties === false);
441
- }
442
- if (def.items && Array.isArray(v)) {
443
- for (let i = 0; i < v.length; i++) {
444
- const elem = v[i];
445
- const ep = p + '[' + i + ']';
446
- const idef = def.items;
447
- if (idef.properties && elem && typeof elem === 'object' && !Array.isArray(elem)) {
448
- validateLevel(childFields(idef), elem, ep, errs, idef.additionalProperties === false);
449
- } else {
450
- const r = ruleOf(idef, elem);
451
- if (Object.keys(r).length) {
452
- const out = run('params', { input: { _: elem }, rules: { _: r } });
453
- if (out && out.errors && out.errors._) {
454
- for (let j = 0; j < out.errors._.length; j++) {
455
- errs.push(ep + ': ' + String(out.errors._[j]).replace(/^_\s*/, ''));
456
- }
457
- }
458
- }
459
- if (elem === '') checkEmpty(ep, '_', elem, idef, errs);
460
- }
461
- }
462
- }
463
- }
464
- }
465
-
466
- /** 校验数据体:规则判定全部由 mm_validator 执行,返回错误数组或 null */
467
- function validate(model, body) {
468
- if (!model || model.schemaless || !model.jsonSchema) return null;
469
- const errs = [];
470
- validateLevel(model.schema, body || {}, '', errs, model.additionalProperties === false);
471
- return errs.length ? errs : null;
472
- }
473
-
474
- /** 只校验、不写入 */
475
- function check(model, row) {
476
- const errors = validate(model, row) || [];
477
- return { ok: errors.length === 0, errors: errors };
478
- }
479
-
480
- /** 当前生效的校验器信息 */
481
- function validatorInfo() {
482
- let version = null;
483
- if (V) {
484
- try { version = require(path.join(V.TOOL_DIR, '..', 'package.json')).version; } catch (e) { /* ignore */ }
485
- }
486
- return {
487
- package: 'mm_validator',
488
- version: version,
489
- loaded: !!V,
490
- tool_dir: V ? V.TOOL_DIR : null,
491
- available_tools: V && typeof V.listTools === 'function' ? V.listTools() : [],
492
- engine_tools: Object.keys(CALLS.by_tool),
493
- schema_draft: DRAFT,
494
- allowed_types: ALLOWED_TYPES.slice(),
495
- forbidden_types: Object.keys(FORBIDDEN_TYPES),
496
- calls: CALLS.total,
497
- calls_by_tool: Object.assign({}, CALLS.by_tool),
498
- error: V_ERROR
499
- };
500
- }
501
-
502
310
  /** 填充 schema 默认值(default 是标准 JSON Schema 关键词) */
503
311
  function fillDefaults(model, body) {
504
- const out = {};
505
- const data = body || {};
506
- for (const k in data) out[k] = clone(data[k]);
507
- for (const f in model.schema) {
508
- const def = model.schema[f];
312
+ let out = {};
313
+ let data = body || {};
314
+ for (let k in data) out[k] = clone(data[k]);
315
+ for (let f in model.schema) {
316
+ let def = model.schema[f];
509
317
  if (def.default !== undefined && out[f] === undefined) out[f] = clone(def.default);
510
318
  }
511
319
  return out;
512
320
  }
513
321
 
514
- /** 按 fields 投影 */
322
+ /**
323
+ * 按 fields 投影记录字段
324
+ * @param {object} row 记录
325
+ * @param {Array} fields 字段名列表
326
+ * @param {string} [key] 主键字段名(优先保留)
327
+ * @returns {object} 投影结果
328
+ */
515
329
  function project(row, fields, key) {
516
330
  if (!fields || !fields.length) return clone(row);
517
- const out = {};
331
+ let out = {};
518
332
  if (key && row[key] !== undefined) out[key] = row[key];
519
333
  for (let i = 0; i < fields.length; i++) {
520
- const f = fields[i];
334
+ let f = fields[i];
521
335
  if (row[f] !== undefined) out[f] = clone(row[f]);
522
336
  }
523
337
  return out;
524
338
  }
525
339
 
526
- module.exports = {
340
+ /**
341
+ * 当前生效的校验器信息(委托 model_validator)
342
+ * @returns {object} 校验器信息
343
+ */
344
+ function validatorInfo() {
345
+ return runner.validatorInfo();
346
+ }
347
+
348
+ // 用 Object.assign 原地填充默认导出对象而非重新赋值:
349
+ // model_check.js 在循环加载期捕获的是赋值前的默认导出对象,
350
+ // 重新赋值会使两者身份分离,导致其持有的引用永远为空
351
+ Object.assign(module.exports, {
527
352
  DRAFT: DRAFT,
528
353
  ALLOWED_TYPES: ALLOWED_TYPES,
529
354
  normalize: normalize,
@@ -531,10 +356,11 @@ module.exports = {
531
356
  toStandard: toStandard,
532
357
  setField: setField,
533
358
  getField: getField,
534
- validate: validate,
535
- check: check,
359
+ childFields: childFields,
360
+ validate: check_mod.validate,
361
+ check: check_mod.check,
536
362
  validatorInfo: validatorInfo,
537
363
  fillDefaults: fillDefaults,
538
364
  project: project,
539
365
  clone: clone
540
- };
366
+ });