masterrecord 0.3.57 → 0.3.58
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/insertManager.js +17 -11
- package/package.json +1 -1
- package/test/promise-detection-test.js +61 -0
package/insertManager.js
CHANGED
|
@@ -352,22 +352,28 @@ class InsertManager {
|
|
|
352
352
|
if (!isRelationship) {
|
|
353
353
|
const val = currentRealModel[entity];
|
|
354
354
|
if (val != null && typeof val === 'object') {
|
|
355
|
-
|
|
355
|
+
// Always reject Promises — a set() transform cannot meaningfully handle them
|
|
356
356
|
if (typeof val.then === 'function') {
|
|
357
|
+
const entityName = entityModel.__name || 'unknown';
|
|
357
358
|
this._errorModel.isValid = false;
|
|
358
359
|
this._errorModel.errors.push(
|
|
359
360
|
`Property '${entity}' on entity '${entityName}' contains a Promise. Did you forget to await an async call?`
|
|
360
361
|
);
|
|
361
|
-
} else if (
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
)
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
)
|
|
362
|
+
} else if (!currentEntity.set) {
|
|
363
|
+
// Only flag Array/Object when there is no custom set() transform,
|
|
364
|
+
// since the setter may serialize them to a scalar (e.g. JSON.stringify)
|
|
365
|
+
const entityName = entityModel.__name || 'unknown';
|
|
366
|
+
if (Array.isArray(val)) {
|
|
367
|
+
this._errorModel.isValid = false;
|
|
368
|
+
this._errorModel.errors.push(
|
|
369
|
+
`Property '${entity}' on entity '${entityName}' contains an Array, expected a scalar value`
|
|
370
|
+
);
|
|
371
|
+
} else if (!(val instanceof Date)) {
|
|
372
|
+
this._errorModel.isValid = false;
|
|
373
|
+
this._errorModel.errors.push(
|
|
374
|
+
`Property '${entity}' on entity '${entityName}' contains an Object, expected a scalar value`
|
|
375
|
+
);
|
|
376
|
+
}
|
|
371
377
|
}
|
|
372
378
|
}
|
|
373
379
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.58",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"main": "MasterRecord.js",
|
|
6
6
|
"bin": {
|
|
@@ -219,6 +219,67 @@ console.log("──────────────────────
|
|
|
219
219
|
assert(err.errors.length === 3, 'Three errors (Promise + Array + Object)');
|
|
220
220
|
}
|
|
221
221
|
|
|
222
|
+
// -----------------------------------------------------------
|
|
223
|
+
// Test 9: Array allowed when field has a custom set() transform
|
|
224
|
+
// -----------------------------------------------------------
|
|
225
|
+
console.log("\nTest 9: Array allowed with custom set() transform");
|
|
226
|
+
console.log("──────────────────────────────────────────────────");
|
|
227
|
+
{
|
|
228
|
+
const entityWithSetter = {
|
|
229
|
+
__name: 'BatchJob',
|
|
230
|
+
id: { type: 'integer', primary: true, auto: true, nullable: true },
|
|
231
|
+
name: { type: 'string', nullable: false },
|
|
232
|
+
error_log: { type: 'string', nullable: true, set: function(value) {
|
|
233
|
+
if (Array.isArray(value)) return JSON.stringify(value);
|
|
234
|
+
return value;
|
|
235
|
+
}},
|
|
236
|
+
config_snapshot: { type: 'string', nullable: true, set: function(value) {
|
|
237
|
+
if (typeof value === 'object' && value !== null) return JSON.stringify(value);
|
|
238
|
+
return value;
|
|
239
|
+
}},
|
|
240
|
+
};
|
|
241
|
+
const { mgr, err } = makeManager();
|
|
242
|
+
const realModel = {
|
|
243
|
+
name: 'job1',
|
|
244
|
+
error_log: ['err1', 'err2'],
|
|
245
|
+
config_snapshot: { retries: 3 },
|
|
246
|
+
__entity: entityWithSetter,
|
|
247
|
+
};
|
|
248
|
+
const cleanModel = { ...realModel };
|
|
249
|
+
|
|
250
|
+
mgr.validateEntity(cleanModel, realModel, entityWithSetter);
|
|
251
|
+
|
|
252
|
+
assert(err.isValid, 'Array with set() passes validation');
|
|
253
|
+
assert(err.errors.length === 0, 'No errors when set() is defined');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// -----------------------------------------------------------
|
|
257
|
+
// Test 10: Promise still rejected even when field has set()
|
|
258
|
+
// -----------------------------------------------------------
|
|
259
|
+
console.log("\nTest 10: Promise rejected even with custom set()");
|
|
260
|
+
console.log("──────────────────────────────────────────────────");
|
|
261
|
+
{
|
|
262
|
+
const entityWithSetter = {
|
|
263
|
+
__name: 'BatchJob',
|
|
264
|
+
id: { type: 'integer', primary: true, auto: true, nullable: true },
|
|
265
|
+
error_log: { type: 'string', nullable: true, set: function(value) {
|
|
266
|
+
if (Array.isArray(value)) return JSON.stringify(value);
|
|
267
|
+
return value;
|
|
268
|
+
}},
|
|
269
|
+
};
|
|
270
|
+
const { mgr, err } = makeManager();
|
|
271
|
+
const realModel = {
|
|
272
|
+
error_log: Promise.resolve(['err1']),
|
|
273
|
+
__entity: entityWithSetter,
|
|
274
|
+
};
|
|
275
|
+
const cleanModel = { ...realModel };
|
|
276
|
+
|
|
277
|
+
mgr.validateEntity(cleanModel, realModel, entityWithSetter);
|
|
278
|
+
|
|
279
|
+
assert(!err.isValid, 'Promise still rejected with set()');
|
|
280
|
+
assert(err.errors[0].includes('Promise'), 'Error mentions Promise');
|
|
281
|
+
}
|
|
282
|
+
|
|
222
283
|
// Summary
|
|
223
284
|
console.log("\n" + "=".repeat(64));
|
|
224
285
|
console.log(`Test Results: ${passed} passed, ${failed} failed`);
|