spice-js 2.7.32 → 2.7.33
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.
|
@@ -32,7 +32,8 @@ var SDate = require("sonover-date"),
|
|
|
32
32
|
_mapping_concurrency = Symbol(),
|
|
33
33
|
_skip_mapped_serialization = Symbol(),
|
|
34
34
|
_mapping_dept_exempt = Symbol(),
|
|
35
|
-
_current_path = Symbol()
|
|
35
|
+
_current_path = Symbol(),
|
|
36
|
+
_failed_json_fields = Symbol();
|
|
36
37
|
var GLOBAL_MAPPING_CONCURRENCY = Math.max(1, Number(process.env.SPICE_MAPPING_GLOBAL_CONCURRENCY) || 32);
|
|
37
38
|
var activeGlobalMappings = 0;
|
|
38
39
|
var globalMappingWaiters = [];
|
|
@@ -71,7 +72,7 @@ function _acquireGlobalMappingSlot() {
|
|
|
71
72
|
}
|
|
72
73
|
function runWithConcurrency(_x, _x2, _x3) {
|
|
73
74
|
return _runWithConcurrency.apply(this, arguments);
|
|
74
|
-
}
|
|
75
|
+
}
|
|
75
76
|
function _runWithConcurrency() {
|
|
76
77
|
_runWithConcurrency = _asyncToGenerator(function* (items, concurrency, worker) {
|
|
77
78
|
var results = new Array(items.length);
|
|
@@ -97,6 +98,19 @@ function _runWithConcurrency() {
|
|
|
97
98
|
});
|
|
98
99
|
return _runWithConcurrency.apply(this, arguments);
|
|
99
100
|
}
|
|
101
|
+
var JSON_PARSE_FAILED = Symbol("json_parse_failed");
|
|
102
|
+
function safeParseJsonField(value, fieldName, modelType) {
|
|
103
|
+
if (value == null || value === "") return undefined;
|
|
104
|
+
try {
|
|
105
|
+
return JSON.parse(value);
|
|
106
|
+
} catch (e) {
|
|
107
|
+
console.warn("SpiceModel(" + modelType + "): failed to parse field \"" + fieldName + "\" as JSON: " + e.message);
|
|
108
|
+
return JSON_PARSE_FAILED;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//const _type = Symbol("type");
|
|
113
|
+
|
|
100
114
|
var that;
|
|
101
115
|
if (!Promise.allSettled) {
|
|
102
116
|
Promise.allSettled = promises => Promise.all(promises.map((promise, i) => promise.then(value => ({
|
|
@@ -135,6 +149,7 @@ class SpiceModel {
|
|
|
135
149
|
this[_skip_cache] = ((_args4 = args) == null ? void 0 : (_args4$args = _args4.args) == null ? void 0 : _args4$args.skip_cache) || false;
|
|
136
150
|
this[_level] = ((_args5 = args) == null ? void 0 : (_args5$args = _args5.args) == null ? void 0 : _args5$args._level) || 0;
|
|
137
151
|
this[_current_path] = ((_args6 = args) == null ? void 0 : (_args6$args = _args6.args) == null ? void 0 : _args6$args._current_path) || "";
|
|
152
|
+
this[_failed_json_fields] = [];
|
|
138
153
|
this[_columns] = ((_args7 = args) == null ? void 0 : (_args7$args = _args7.args) == null ? void 0 : _args7$args._columns) || "";
|
|
139
154
|
this[_hooks] = {
|
|
140
155
|
create: {
|
|
@@ -258,13 +273,33 @@ class SpiceModel {
|
|
|
258
273
|
case Array:
|
|
259
274
|
case "array":
|
|
260
275
|
{
|
|
261
|
-
|
|
276
|
+
if (_.isArray(args.args[i])) {
|
|
277
|
+
this[i] = args.args[i];
|
|
278
|
+
} else {
|
|
279
|
+
var parsed = safeParseJsonField(args.args[i], i, args.collection || this.type);
|
|
280
|
+
if (parsed === JSON_PARSE_FAILED) {
|
|
281
|
+
this[_failed_json_fields].push(i);
|
|
282
|
+
this[i] = [];
|
|
283
|
+
} else if (parsed !== undefined) {
|
|
284
|
+
this[i] = parsed;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
262
287
|
break;
|
|
263
288
|
}
|
|
264
289
|
case Object:
|
|
265
290
|
case "object":
|
|
266
291
|
{
|
|
267
|
-
|
|
292
|
+
if (_.isObject(args.args[i])) {
|
|
293
|
+
this[i] = args.args[i];
|
|
294
|
+
} else {
|
|
295
|
+
var _parsed = safeParseJsonField(args.args[i], i, args.collection || this.type);
|
|
296
|
+
if (_parsed === JSON_PARSE_FAILED) {
|
|
297
|
+
this[_failed_json_fields].push(i);
|
|
298
|
+
this[i] = {};
|
|
299
|
+
} else if (_parsed !== undefined) {
|
|
300
|
+
this[i] = _parsed;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
268
303
|
break;
|
|
269
304
|
}
|
|
270
305
|
default:
|
|
@@ -848,6 +883,21 @@ class SpiceModel {
|
|
|
848
883
|
}
|
|
849
884
|
})();
|
|
850
885
|
}
|
|
886
|
+
get failedJsonFields() {
|
|
887
|
+
return this[_failed_json_fields] || [];
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// Refuse writes when a schema-typed object/array field contained invalid
|
|
891
|
+
// JSON, so a parse fallback can never overwrite valid stored data.
|
|
892
|
+
assertNoFailedJsonFields(operation) {
|
|
893
|
+
var failed = this[_failed_json_fields] || [];
|
|
894
|
+
if (failed.length > 0) {
|
|
895
|
+
var err = new Error(this.type + ": cannot " + operation + " because field(s) [" + failed.join(", ") + "] contained invalid JSON. Stored data was left untouched.");
|
|
896
|
+
err.status = 400;
|
|
897
|
+
err.failed_json_fields = [...failed];
|
|
898
|
+
throw err;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
851
901
|
update(args) {
|
|
852
902
|
var _this8 = this;
|
|
853
903
|
return _asyncToGenerator(function* () {
|
|
@@ -856,6 +906,7 @@ class SpiceModel {
|
|
|
856
906
|
var p = (_this8$_ctx = _this8[_ctx]) == null ? void 0 : _this8$_ctx.profiler;
|
|
857
907
|
var doUpdate = /*#__PURE__*/function () {
|
|
858
908
|
var _ref1 = _asyncToGenerator(function* () {
|
|
909
|
+
_this8.assertNoFailedJsonFields("update");
|
|
859
910
|
_this8.updated_at = new SDate().now();
|
|
860
911
|
var results = yield _this8.database.get(args.id);
|
|
861
912
|
var item_exist = yield _this8.exist(results);
|
|
@@ -926,6 +977,7 @@ class SpiceModel {
|
|
|
926
977
|
var p = (_this9$_ctx = _this9[_ctx]) == null ? void 0 : _this9$_ctx.profiler;
|
|
927
978
|
var doCreate = /*#__PURE__*/function () {
|
|
928
979
|
var _ref11 = _asyncToGenerator(function* () {
|
|
980
|
+
_this9.assertNoFailedJsonFields("create");
|
|
929
981
|
var form;
|
|
930
982
|
_this9.created_at = new SDate().now();
|
|
931
983
|
args = _.defaults(args, {
|
package/package.json
CHANGED
package/src/models/SpiceModel.js
CHANGED
|
@@ -27,7 +27,8 @@ var SDate = require("sonover-date"),
|
|
|
27
27
|
_mapping_concurrency = Symbol(),
|
|
28
28
|
_skip_mapped_serialization = Symbol(),
|
|
29
29
|
_mapping_dept_exempt = Symbol(),
|
|
30
|
-
_current_path = Symbol()
|
|
30
|
+
_current_path = Symbol(),
|
|
31
|
+
_failed_json_fields = Symbol();
|
|
31
32
|
|
|
32
33
|
const GLOBAL_MAPPING_CONCURRENCY = Math.max(
|
|
33
34
|
1,
|
|
@@ -82,6 +83,20 @@ async function runWithConcurrency(items, concurrency, worker) {
|
|
|
82
83
|
return results;
|
|
83
84
|
}
|
|
84
85
|
|
|
86
|
+
const JSON_PARSE_FAILED = Symbol("json_parse_failed");
|
|
87
|
+
|
|
88
|
+
function safeParseJsonField(value, fieldName, modelType) {
|
|
89
|
+
if (value == null || value === "") return undefined;
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(value);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.warn(
|
|
94
|
+
`SpiceModel(${modelType}): failed to parse field "${fieldName}" as JSON: ${e.message}`,
|
|
95
|
+
);
|
|
96
|
+
return JSON_PARSE_FAILED;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
85
100
|
//const _type = Symbol("type");
|
|
86
101
|
|
|
87
102
|
let that;
|
|
@@ -144,6 +159,7 @@ export default class SpiceModel {
|
|
|
144
159
|
this[_skip_cache] = args?.args?.skip_cache || false;
|
|
145
160
|
this[_level] = args?.args?._level || 0;
|
|
146
161
|
this[_current_path] = args?.args?._current_path || "";
|
|
162
|
+
this[_failed_json_fields] = [];
|
|
147
163
|
this[_columns] = args?.args?._columns || "";
|
|
148
164
|
this[_hooks] = {
|
|
149
165
|
create: {
|
|
@@ -289,18 +305,40 @@ export default class SpiceModel {
|
|
|
289
305
|
}
|
|
290
306
|
case Array:
|
|
291
307
|
case "array": {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
308
|
+
if (_.isArray(args.args[i])) {
|
|
309
|
+
this[i] = args.args[i];
|
|
310
|
+
} else {
|
|
311
|
+
const parsed = safeParseJsonField(
|
|
312
|
+
args.args[i],
|
|
313
|
+
i,
|
|
314
|
+
args.collection || this.type,
|
|
315
|
+
);
|
|
316
|
+
if (parsed === JSON_PARSE_FAILED) {
|
|
317
|
+
this[_failed_json_fields].push(i);
|
|
318
|
+
this[i] = [];
|
|
319
|
+
} else if (parsed !== undefined) {
|
|
320
|
+
this[i] = parsed;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
296
323
|
break;
|
|
297
324
|
}
|
|
298
325
|
case Object:
|
|
299
326
|
case "object": {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
327
|
+
if (_.isObject(args.args[i])) {
|
|
328
|
+
this[i] = args.args[i];
|
|
329
|
+
} else {
|
|
330
|
+
const parsed = safeParseJsonField(
|
|
331
|
+
args.args[i],
|
|
332
|
+
i,
|
|
333
|
+
args.collection || this.type,
|
|
334
|
+
);
|
|
335
|
+
if (parsed === JSON_PARSE_FAILED) {
|
|
336
|
+
this[_failed_json_fields].push(i);
|
|
337
|
+
this[i] = {};
|
|
338
|
+
} else if (parsed !== undefined) {
|
|
339
|
+
this[i] = parsed;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
304
342
|
break;
|
|
305
343
|
}
|
|
306
344
|
default: {
|
|
@@ -941,11 +979,32 @@ export default class SpiceModel {
|
|
|
941
979
|
}
|
|
942
980
|
}
|
|
943
981
|
|
|
982
|
+
get failedJsonFields() {
|
|
983
|
+
return this[_failed_json_fields] || [];
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// Refuse writes when a schema-typed object/array field contained invalid
|
|
987
|
+
// JSON, so a parse fallback can never overwrite valid stored data.
|
|
988
|
+
assertNoFailedJsonFields(operation) {
|
|
989
|
+
const failed = this[_failed_json_fields] || [];
|
|
990
|
+
if (failed.length > 0) {
|
|
991
|
+
const err = new Error(
|
|
992
|
+
`${this.type}: cannot ${operation} because field(s) [${failed.join(
|
|
993
|
+
", ",
|
|
994
|
+
)}] contained invalid JSON. Stored data was left untouched.`,
|
|
995
|
+
);
|
|
996
|
+
err.status = 400;
|
|
997
|
+
err.failed_json_fields = [...failed];
|
|
998
|
+
throw err;
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
944
1002
|
async update(args) {
|
|
945
1003
|
// Profiling: use track() for proper async context forking
|
|
946
1004
|
const p = this[_ctx]?.profiler;
|
|
947
1005
|
|
|
948
1006
|
const doUpdate = async () => {
|
|
1007
|
+
this.assertNoFailedJsonFields("update");
|
|
949
1008
|
this.updated_at = new SDate().now();
|
|
950
1009
|
let results = await this.database.get(args.id);
|
|
951
1010
|
let item_exist = await this.exist(results);
|
|
@@ -1025,6 +1084,7 @@ export default class SpiceModel {
|
|
|
1025
1084
|
const p = this[_ctx]?.profiler;
|
|
1026
1085
|
|
|
1027
1086
|
const doCreate = async () => {
|
|
1087
|
+
this.assertNoFailedJsonFields("create");
|
|
1028
1088
|
let form;
|
|
1029
1089
|
this.created_at = new SDate().now();
|
|
1030
1090
|
args = _.defaults(args, { id_prefix: this.type });
|
|
@@ -44,6 +44,18 @@ class MockDatabase {
|
|
|
44
44
|
return updated;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Mock insert operation
|
|
49
|
+
* @param {string} id - Document ID
|
|
50
|
+
* @param {Object} data - Document data
|
|
51
|
+
* @returns {Promise<Object>} Inserted document
|
|
52
|
+
*/
|
|
53
|
+
async insert(id, data, ttl) {
|
|
54
|
+
const inserted = { ...data, id };
|
|
55
|
+
this.mockData.set(id, inserted);
|
|
56
|
+
return inserted;
|
|
57
|
+
}
|
|
58
|
+
|
|
47
59
|
/**
|
|
48
60
|
* Seed mock database with test data
|
|
49
61
|
* @param {string} id - Document ID
|
|
@@ -631,3 +631,255 @@ describe('SpiceModel - Critical Fixes for Empty/Null Values', () => {
|
|
|
631
631
|
});
|
|
632
632
|
});
|
|
633
633
|
});
|
|
634
|
+
|
|
635
|
+
describe('SpiceModel - Constructor JSON parse resilience', () => {
|
|
636
|
+
let warnSpy;
|
|
637
|
+
|
|
638
|
+
beforeEach(() => {
|
|
639
|
+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
afterEach(() => {
|
|
643
|
+
warnSpy.mockRestore();
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
test('should fall back to {} for HTML in object-typed field and finish init', () => {
|
|
647
|
+
const modifiersSpy = jest.spyOn(
|
|
648
|
+
SpiceModel.prototype,
|
|
649
|
+
'createModifiersFromProps'
|
|
650
|
+
);
|
|
651
|
+
|
|
652
|
+
const model = createTestModel({
|
|
653
|
+
type: 'application',
|
|
654
|
+
props: {
|
|
655
|
+
name: { type: String },
|
|
656
|
+
metadata: { type: Object },
|
|
657
|
+
title: { type: String },
|
|
658
|
+
},
|
|
659
|
+
args: {
|
|
660
|
+
name: 'App One',
|
|
661
|
+
metadata: '<p style="color:red">broken</p>',
|
|
662
|
+
title: 'Still set',
|
|
663
|
+
},
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
expect(model.metadata).toEqual({});
|
|
667
|
+
expect(model.name).toBe('App One');
|
|
668
|
+
expect(model.title).toBe('Still set');
|
|
669
|
+
expect(modifiersSpy).toHaveBeenCalled();
|
|
670
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
671
|
+
expect.stringContaining('failed to parse field "metadata"')
|
|
672
|
+
);
|
|
673
|
+
|
|
674
|
+
modifiersSpy.mockRestore();
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
test('should fall back to [] for invalid JSON in array-typed field', () => {
|
|
678
|
+
const model = createTestModel({
|
|
679
|
+
type: 'addition',
|
|
680
|
+
props: {
|
|
681
|
+
name: { type: String },
|
|
682
|
+
tags: { type: Array },
|
|
683
|
+
},
|
|
684
|
+
args: {
|
|
685
|
+
name: 'Addition One',
|
|
686
|
+
tags: '<p style="">not json</p>',
|
|
687
|
+
},
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
expect(model.tags).toEqual([]);
|
|
691
|
+
expect(model.name).toBe('Addition One');
|
|
692
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
693
|
+
expect.stringContaining('failed to parse field "tags"')
|
|
694
|
+
);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
test('should parse valid JSON strings for object and array fields', () => {
|
|
698
|
+
const model = createTestModel({
|
|
699
|
+
type: 'application',
|
|
700
|
+
props: {
|
|
701
|
+
metadata: { type: Object },
|
|
702
|
+
tags: { type: Array },
|
|
703
|
+
},
|
|
704
|
+
args: {
|
|
705
|
+
metadata: '{"foo":"bar"}',
|
|
706
|
+
tags: '["a","b"]',
|
|
707
|
+
},
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
expect(model.metadata).toEqual({ foo: 'bar' });
|
|
711
|
+
expect(model.tags).toEqual(['a', 'b']);
|
|
712
|
+
expect(warnSpy).not.toHaveBeenCalledWith(
|
|
713
|
+
expect.stringContaining('failed to parse field')
|
|
714
|
+
);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
test('should pass through already-parsed object and array values', () => {
|
|
718
|
+
const metadata = { foo: 'bar' };
|
|
719
|
+
const tags = ['a', 'b'];
|
|
720
|
+
|
|
721
|
+
const model = createTestModel({
|
|
722
|
+
type: 'application',
|
|
723
|
+
props: {
|
|
724
|
+
metadata: { type: Object },
|
|
725
|
+
tags: { type: Array },
|
|
726
|
+
},
|
|
727
|
+
args: {
|
|
728
|
+
metadata,
|
|
729
|
+
tags,
|
|
730
|
+
},
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
expect(model.metadata).toBe(metadata);
|
|
734
|
+
expect(model.tags).toBe(tags);
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
test('should track failed fields on the model', () => {
|
|
738
|
+
const model = createTestModel({
|
|
739
|
+
type: 'application',
|
|
740
|
+
props: {
|
|
741
|
+
metadata: { type: Object },
|
|
742
|
+
tags: { type: Array },
|
|
743
|
+
},
|
|
744
|
+
args: {
|
|
745
|
+
metadata: '<p>bad</p>',
|
|
746
|
+
tags: 'also bad',
|
|
747
|
+
},
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
expect(model.failedJsonFields.sort()).toEqual(['metadata', 'tags']);
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
test('should have no failed fields when all values parse', () => {
|
|
754
|
+
const model = createTestModel({
|
|
755
|
+
type: 'application',
|
|
756
|
+
props: {
|
|
757
|
+
metadata: { type: Object },
|
|
758
|
+
},
|
|
759
|
+
args: {
|
|
760
|
+
metadata: '{"foo":"bar"}',
|
|
761
|
+
},
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
expect(model.failedJsonFields).toEqual([]);
|
|
765
|
+
});
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
describe('SpiceModel - Write protection for failed JSON fields', () => {
|
|
769
|
+
let warnSpy;
|
|
770
|
+
|
|
771
|
+
beforeEach(() => {
|
|
772
|
+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
afterEach(() => {
|
|
776
|
+
warnSpy.mockRestore();
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
test('update should reject and preserve stored data when a field failed to parse', async () => {
|
|
780
|
+
const model = createTestModel({
|
|
781
|
+
type: 'application',
|
|
782
|
+
props: {
|
|
783
|
+
name: { type: String },
|
|
784
|
+
metadata: { type: Object },
|
|
785
|
+
},
|
|
786
|
+
args: {
|
|
787
|
+
name: 'Updated Name',
|
|
788
|
+
metadata: '<p style="color:red">broken html</p>',
|
|
789
|
+
skip_serialize: true,
|
|
790
|
+
skip_hooks: true,
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
const storedDoc = {
|
|
795
|
+
id: 'application-1',
|
|
796
|
+
name: 'Original Name',
|
|
797
|
+
metadata: { important: 'data' },
|
|
798
|
+
};
|
|
799
|
+
seedDatabase(model, storedDoc);
|
|
800
|
+
jest.spyOn(model.database, 'update');
|
|
801
|
+
|
|
802
|
+
await expect(
|
|
803
|
+
model.update({ id: 'application-1', skip_serialize: true, skip_hooks: true })
|
|
804
|
+
).rejects.toThrow(/cannot update.*metadata.*invalid JSON/);
|
|
805
|
+
|
|
806
|
+
// No write must reach the database; stored data stays intact
|
|
807
|
+
expect(model.database.update).not.toHaveBeenCalled();
|
|
808
|
+
const stored = await model.database.get('application-1');
|
|
809
|
+
expect(stored.metadata).toEqual({ important: 'data' });
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
test('update rejection should carry status 400 and the failed field names', async () => {
|
|
813
|
+
const model = createTestModel({
|
|
814
|
+
type: 'application',
|
|
815
|
+
props: {
|
|
816
|
+
metadata: { type: Object },
|
|
817
|
+
},
|
|
818
|
+
args: {
|
|
819
|
+
metadata: 'not json at all',
|
|
820
|
+
skip_serialize: true,
|
|
821
|
+
skip_hooks: true,
|
|
822
|
+
},
|
|
823
|
+
});
|
|
824
|
+
seedDatabase(model, { id: 'application-1', metadata: { keep: true } });
|
|
825
|
+
|
|
826
|
+
let caught;
|
|
827
|
+
try {
|
|
828
|
+
await model.update({ id: 'application-1', skip_serialize: true, skip_hooks: true });
|
|
829
|
+
} catch (e) {
|
|
830
|
+
caught = e;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
expect(caught).toBeDefined();
|
|
834
|
+
expect(caught.status).toBe(400);
|
|
835
|
+
expect(caught.failed_json_fields).toEqual(['metadata']);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
test('create should reject when a field failed to parse', async () => {
|
|
839
|
+
const model = createTestModel({
|
|
840
|
+
type: 'application',
|
|
841
|
+
props: {
|
|
842
|
+
metadata: { type: Object },
|
|
843
|
+
},
|
|
844
|
+
args: {
|
|
845
|
+
metadata: '<div>nope</div>',
|
|
846
|
+
skip_serialize: true,
|
|
847
|
+
skip_hooks: true,
|
|
848
|
+
},
|
|
849
|
+
});
|
|
850
|
+
jest.spyOn(model.database, 'insert');
|
|
851
|
+
|
|
852
|
+
await expect(
|
|
853
|
+
model.create({ skip_serialize: true, skip_hooks: true })
|
|
854
|
+
).rejects.toThrow(/cannot create.*metadata.*invalid JSON/);
|
|
855
|
+
expect(model.database.insert).not.toHaveBeenCalled();
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
test('update should proceed normally when all fields parsed', async () => {
|
|
859
|
+
const model = createTestModel({
|
|
860
|
+
type: 'application',
|
|
861
|
+
props: {
|
|
862
|
+
name: { type: String },
|
|
863
|
+
metadata: { type: Object },
|
|
864
|
+
},
|
|
865
|
+
args: {
|
|
866
|
+
name: 'New Name',
|
|
867
|
+
metadata: '{"valid":"json"}',
|
|
868
|
+
skip_serialize: true,
|
|
869
|
+
skip_hooks: true,
|
|
870
|
+
},
|
|
871
|
+
});
|
|
872
|
+
seedDatabase(model, { id: 'application-1', name: 'Old', metadata: {} });
|
|
873
|
+
// setMonitor needs the real cache global, which test mocks don't provide
|
|
874
|
+
jest.spyOn(model, 'setMonitor').mockImplementation(() => {});
|
|
875
|
+
|
|
876
|
+
const result = await model.update({
|
|
877
|
+
id: 'application-1',
|
|
878
|
+
skip_serialize: true,
|
|
879
|
+
skip_hooks: true,
|
|
880
|
+
});
|
|
881
|
+
|
|
882
|
+
expect(result.name).toBe('New Name');
|
|
883
|
+
expect(result.metadata).toEqual({ valid: 'json' });
|
|
884
|
+
});
|
|
885
|
+
});
|