@revisium/schema-toolkit 0.2.0 → 0.2.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.
Files changed (81) hide show
  1. package/dist/chunk-22K22MMD.js +112 -0
  2. package/dist/chunk-22K22MMD.js.map +1 -0
  3. package/dist/chunk-2GCG6HJO.cjs +545 -0
  4. package/dist/chunk-2GCG6HJO.cjs.map +1 -0
  5. package/dist/chunk-3E7UGCRR.js +519 -0
  6. package/dist/chunk-3E7UGCRR.js.map +1 -0
  7. package/dist/chunk-47AQWLHU.cjs +122 -0
  8. package/dist/chunk-47AQWLHU.cjs.map +1 -0
  9. package/dist/chunk-5VDDLW7U.js +9 -0
  10. package/dist/chunk-5VDDLW7U.js.map +1 -0
  11. package/dist/chunk-AORPUIKD.js +81 -0
  12. package/dist/chunk-AORPUIKD.js.map +1 -0
  13. package/dist/chunk-ASXCDIN2.js +365 -0
  14. package/dist/chunk-ASXCDIN2.js.map +1 -0
  15. package/dist/chunk-DEPJRTVT.js +3 -0
  16. package/dist/chunk-DEPJRTVT.js.map +1 -0
  17. package/dist/chunk-EJYILFAK.cjs +92 -0
  18. package/dist/chunk-EJYILFAK.cjs.map +1 -0
  19. package/dist/chunk-FD57PCAC.cjs +4 -0
  20. package/dist/chunk-FD57PCAC.cjs.map +1 -0
  21. package/dist/chunk-H7W4QNMA.js +3 -0
  22. package/dist/chunk-H7W4QNMA.js.map +1 -0
  23. package/dist/chunk-IPL2CGVA.cjs +11 -0
  24. package/dist/chunk-IPL2CGVA.cjs.map +1 -0
  25. package/dist/chunk-JLIZ7QT2.cjs +787 -0
  26. package/dist/chunk-JLIZ7QT2.cjs.map +1 -0
  27. package/dist/chunk-ODCH3PP2.cjs +4 -0
  28. package/dist/chunk-ODCH3PP2.cjs.map +1 -0
  29. package/dist/chunk-Q2UOTIMG.js +17 -0
  30. package/dist/chunk-Q2UOTIMG.js.map +1 -0
  31. package/dist/chunk-V2SSSD5X.cjs +15 -0
  32. package/dist/chunk-V2SSSD5X.cjs.map +1 -0
  33. package/dist/chunk-VGTHMWJJ.cjs +380 -0
  34. package/dist/chunk-VGTHMWJJ.cjs.map +1 -0
  35. package/dist/chunk-WCFA4226.js +757 -0
  36. package/dist/chunk-WCFA4226.js.map +1 -0
  37. package/dist/chunk-XNFSFT7T.cjs +19 -0
  38. package/dist/chunk-XNFSFT7T.cjs.map +1 -0
  39. package/dist/chunk-ZXLLJAMQ.js +13 -0
  40. package/dist/chunk-ZXLLJAMQ.js.map +1 -0
  41. package/dist/consts/index.cjs +11 -20
  42. package/dist/consts/index.cjs.map +1 -1
  43. package/dist/consts/index.js +2 -21
  44. package/dist/consts/index.js.map +1 -1
  45. package/dist/index.cjs +372 -1869
  46. package/dist/index.cjs.map +1 -1
  47. package/dist/index.d.cts +2 -2
  48. package/dist/index.d.ts +2 -2
  49. package/dist/index.js +10 -1785
  50. package/dist/index.js.map +1 -1
  51. package/dist/{json-string.store-O1J0j-a1.d.cts → json-string.store-BNF1Y93E.d.cts} +1 -0
  52. package/dist/{json-string.store-S9FXT39Q.d.ts → json-string.store-qHcWNv07.d.ts} +1 -0
  53. package/dist/lib/index.cjs +128 -1342
  54. package/dist/lib/index.cjs.map +1 -1
  55. package/dist/lib/index.d.cts +1 -1
  56. package/dist/lib/index.d.ts +1 -1
  57. package/dist/lib/index.js +5 -1313
  58. package/dist/lib/index.js.map +1 -1
  59. package/dist/mocks/index.cjs +38 -81
  60. package/dist/mocks/index.cjs.map +1 -1
  61. package/dist/mocks/index.js +2 -79
  62. package/dist/mocks/index.js.map +1 -1
  63. package/dist/model/index.cjs +99 -711
  64. package/dist/model/index.cjs.map +1 -1
  65. package/dist/model/index.d.cts +8 -4
  66. package/dist/model/index.d.ts +8 -4
  67. package/dist/model/index.js +3 -695
  68. package/dist/model/index.js.map +1 -1
  69. package/dist/plugins/index.cjs +41 -118
  70. package/dist/plugins/index.cjs.map +1 -1
  71. package/dist/plugins/index.js +2 -110
  72. package/dist/plugins/index.js.map +1 -1
  73. package/dist/types/index.cjs +9 -11
  74. package/dist/types/index.cjs.map +1 -1
  75. package/dist/types/index.js +2 -11
  76. package/dist/types/index.js.map +1 -1
  77. package/dist/validation-schemas/index.cjs +57 -373
  78. package/dist/validation-schemas/index.cjs.map +1 -1
  79. package/dist/validation-schemas/index.js +1 -363
  80. package/dist/validation-schemas/index.js.map +1 -1
  81. package/package.json +1 -1
@@ -0,0 +1,787 @@
1
+ 'use strict';
2
+
3
+ var nanoid = require('nanoid');
4
+ var events = require('events');
5
+
6
+ // src/lib/addSharedFieldsFromStore.ts
7
+ var addSharedFieldsFromState = (schema, state) => {
8
+ if (state.title) {
9
+ schema.title = state.title;
10
+ }
11
+ if (state.description) {
12
+ schema.description = state.description;
13
+ }
14
+ if (state.deprecated) {
15
+ schema.deprecated = state.deprecated;
16
+ }
17
+ return schema;
18
+ };
19
+ var JsonStringStore = class extends events.EventEmitter {
20
+ constructor(nodeId = nanoid.nanoid()) {
21
+ super();
22
+ this.nodeId = nodeId;
23
+ }
24
+ type = "string" /* String */;
25
+ $ref = "";
26
+ name = "";
27
+ parent = null;
28
+ default = "";
29
+ readOnly;
30
+ title;
31
+ description;
32
+ deprecated;
33
+ foreignKey;
34
+ pattern;
35
+ enum;
36
+ format;
37
+ contentMediaType;
38
+ valuesMap = /* @__PURE__ */ new Map();
39
+ registerValue(value) {
40
+ const length = this.getOrCreateValues(value.rowId).push(value);
41
+ return length - 1;
42
+ }
43
+ getValue(rowId, index = 0) {
44
+ return this.getOrCreateValues(rowId)[index];
45
+ }
46
+ getPlainSchema(options) {
47
+ if (this.$ref && options?.skip$Ref !== true) {
48
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
49
+ }
50
+ return addSharedFieldsFromState(
51
+ {
52
+ type: this.type,
53
+ default: this.default,
54
+ ...this.foreignKey ? { foreignKey: this.foreignKey } : {},
55
+ ...this.readOnly ? { readOnly: this.readOnly } : {},
56
+ ...this.pattern ? { pattern: this.pattern } : {},
57
+ ...this.enum ? { enum: this.enum } : {},
58
+ ...this.format ? { format: this.format } : {},
59
+ ...this.contentMediaType ? { contentMediaType: this.contentMediaType } : {}
60
+ },
61
+ this
62
+ );
63
+ }
64
+ getOrCreateValues(rowId) {
65
+ let values = this.valuesMap.get(rowId);
66
+ if (!values) {
67
+ values = [];
68
+ this.valuesMap.set(rowId, values);
69
+ }
70
+ return values;
71
+ }
72
+ };
73
+ var JsonNumberStore = class extends events.EventEmitter {
74
+ constructor(nodeId = nanoid.nanoid()) {
75
+ super();
76
+ this.nodeId = nodeId;
77
+ }
78
+ type = "number" /* Number */;
79
+ $ref = "";
80
+ name = "";
81
+ parent = null;
82
+ default = 0;
83
+ readOnly;
84
+ title;
85
+ description;
86
+ deprecated;
87
+ valuesMap = /* @__PURE__ */ new Map();
88
+ registerValue(value) {
89
+ const length = this.getOrCreateValues(value.rowId).push(value);
90
+ return length - 1;
91
+ }
92
+ getValue(rowId, index = 0) {
93
+ return this.getOrCreateValues(rowId)[index];
94
+ }
95
+ getPlainSchema(options) {
96
+ if (this.$ref && options?.skip$Ref !== true) {
97
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
98
+ }
99
+ return addSharedFieldsFromState(
100
+ {
101
+ type: this.type,
102
+ default: this.default,
103
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
104
+ },
105
+ this
106
+ );
107
+ }
108
+ getOrCreateValues(rowId) {
109
+ let values = this.valuesMap.get(rowId);
110
+ if (!values) {
111
+ values = [];
112
+ this.valuesMap.set(rowId, values);
113
+ }
114
+ return values;
115
+ }
116
+ };
117
+ var JsonBooleanStore = class extends events.EventEmitter {
118
+ constructor(nodeId = nanoid.nanoid()) {
119
+ super();
120
+ this.nodeId = nodeId;
121
+ }
122
+ type = "boolean" /* Boolean */;
123
+ $ref = "";
124
+ name = "";
125
+ parent = null;
126
+ default = false;
127
+ readOnly;
128
+ title;
129
+ description;
130
+ deprecated;
131
+ valuesMap = /* @__PURE__ */ new Map();
132
+ registerValue(value) {
133
+ const length = this.getOrCreateValues(value.rowId).push(value);
134
+ return length - 1;
135
+ }
136
+ getValue(rowId, index = 0) {
137
+ return this.getOrCreateValues(rowId)[index];
138
+ }
139
+ getPlainSchema(options) {
140
+ if (this.$ref && options?.skip$Ref !== true) {
141
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
142
+ }
143
+ return addSharedFieldsFromState(
144
+ {
145
+ type: this.type,
146
+ default: this.default,
147
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
148
+ },
149
+ this
150
+ );
151
+ }
152
+ getOrCreateValues(rowId) {
153
+ let values = this.valuesMap.get(rowId);
154
+ if (!values) {
155
+ values = [];
156
+ this.valuesMap.set(rowId, values);
157
+ }
158
+ return values;
159
+ }
160
+ };
161
+ var JsonObjectStore = class {
162
+ constructor(nodeId = nanoid.nanoid()) {
163
+ this.nodeId = nodeId;
164
+ }
165
+ type = "object" /* Object */;
166
+ $ref = "";
167
+ name = "";
168
+ parent = null;
169
+ default = {};
170
+ title;
171
+ description;
172
+ deprecated;
173
+ additionalProperties = false;
174
+ required = [];
175
+ properties = {};
176
+ valuesMap = /* @__PURE__ */ new Map();
177
+ get empty() {
178
+ return Object.keys(this.properties).length === 0;
179
+ }
180
+ registerValue(value) {
181
+ const length = this.getOrCreateValues(value.rowId).push(value);
182
+ return length - 1;
183
+ }
184
+ getValue(rowId, index = 0) {
185
+ return this.getOrCreateValues(rowId)[index];
186
+ }
187
+ addPropertyWithStore(name, store) {
188
+ if (this.properties[name] || this.required.includes(name)) {
189
+ throw new Error("this name already exists");
190
+ }
191
+ store.parent = this;
192
+ store.name = name;
193
+ this.required.push(name);
194
+ this.required.sort((a, b) => a.localeCompare(b));
195
+ this.properties[name] = store;
196
+ this.default[name] = store.default;
197
+ const event = { name, property: store };
198
+ for (const value of this.iterValues()) {
199
+ value.addProperty(event);
200
+ }
201
+ return store;
202
+ }
203
+ migratePropertyWithStore(name, store) {
204
+ const item = this.properties[name];
205
+ if (!item) {
206
+ throw new Error("this name does not exist");
207
+ }
208
+ item.parent = null;
209
+ store.parent = this;
210
+ store.name = name;
211
+ this.properties[name] = store;
212
+ this.default[name] = store.default;
213
+ const event = {
214
+ name,
215
+ property: store,
216
+ previousProperty: item
217
+ };
218
+ for (const value of this.iterValues()) {
219
+ value.migrateProperty(event);
220
+ }
221
+ return store;
222
+ }
223
+ changeName(fromName, toName) {
224
+ const item = this.properties[fromName];
225
+ if (!item) {
226
+ throw new Error("this fromName does not exist");
227
+ }
228
+ delete this.properties[fromName];
229
+ delete this.default[fromName];
230
+ const foundRequiredIndex = this.required.findIndex(
231
+ (required) => required === fromName
232
+ );
233
+ if (foundRequiredIndex !== -1) {
234
+ this.required.splice(foundRequiredIndex, 1);
235
+ }
236
+ if (!this.required.includes(toName)) {
237
+ this.required.push(toName);
238
+ this.required.sort((a, b) => a.localeCompare(b));
239
+ }
240
+ this.properties[toName] = item;
241
+ this.default[toName] = item.default;
242
+ const event = {
243
+ fromName,
244
+ toName,
245
+ property: item
246
+ };
247
+ for (const value of this.iterValues()) {
248
+ value.changeName(event);
249
+ }
250
+ }
251
+ removeProperty(name) {
252
+ const item = this.properties[name];
253
+ if (!item) {
254
+ throw new Error("this name does not exist");
255
+ }
256
+ item.parent = null;
257
+ item.name = "";
258
+ delete this.properties[name];
259
+ delete this.default[name];
260
+ const foundRequiredIndex = this.required.findIndex(
261
+ (required) => required === name
262
+ );
263
+ if (foundRequiredIndex !== -1) {
264
+ this.required.splice(foundRequiredIndex, 1);
265
+ }
266
+ const event = {
267
+ name,
268
+ property: item
269
+ };
270
+ for (const value of this.iterValues()) {
271
+ value.removeProperty(event);
272
+ }
273
+ }
274
+ getProperty(name) {
275
+ return this.properties[name];
276
+ }
277
+ getPlainSchema(options) {
278
+ if (this.$ref && options?.skip$Ref !== true) {
279
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
280
+ }
281
+ return addSharedFieldsFromState(
282
+ {
283
+ type: this.type,
284
+ additionalProperties: this.additionalProperties,
285
+ required: this.required,
286
+ properties: Object.entries(this.properties).reduce((result, [name, store]) => {
287
+ result[name] = store.getPlainSchema(options);
288
+ return result;
289
+ }, {})
290
+ },
291
+ this
292
+ );
293
+ }
294
+ getOrCreateValues(rowId) {
295
+ let values = this.valuesMap.get(rowId);
296
+ if (!values) {
297
+ values = [];
298
+ this.valuesMap.set(rowId, values);
299
+ }
300
+ return values;
301
+ }
302
+ *iterValues() {
303
+ for (const values of this.valuesMap.values()) {
304
+ for (const value of values) {
305
+ yield value;
306
+ }
307
+ }
308
+ }
309
+ };
310
+ var JsonArrayStore = class {
311
+ constructor(_items, nodeId = nanoid.nanoid()) {
312
+ this._items = _items;
313
+ this.nodeId = nodeId;
314
+ this.items.parent = this;
315
+ }
316
+ type = "array" /* Array */;
317
+ $ref = "";
318
+ name = "";
319
+ parent = null;
320
+ default = [];
321
+ title;
322
+ description;
323
+ deprecated;
324
+ valuesMap = /* @__PURE__ */ new Map();
325
+ get items() {
326
+ return this._items;
327
+ }
328
+ registerValue(value) {
329
+ const length = this.getOrCreateValues(value.rowId).push(value);
330
+ return length - 1;
331
+ }
332
+ getValue(rowId, index = 0) {
333
+ return this.getOrCreateValues(rowId)[index];
334
+ }
335
+ migrateItems(items) {
336
+ const previousItems = this._items;
337
+ previousItems.parent = null;
338
+ this._items = items;
339
+ this._items.parent = this;
340
+ const event = { items, previousItems };
341
+ for (const value of this.iterValues()) {
342
+ value.migrateItems(event);
343
+ }
344
+ }
345
+ replaceItems(items) {
346
+ const previousItems = this._items;
347
+ previousItems.parent = null;
348
+ this._items = items;
349
+ this._items.parent = this;
350
+ const event = { items, previousItems };
351
+ for (const value of this.iterValues()) {
352
+ value.replaceItems(event);
353
+ }
354
+ }
355
+ getPlainSchema(options) {
356
+ if (this.$ref && options?.skip$Ref !== true) {
357
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
358
+ }
359
+ return addSharedFieldsFromState(
360
+ {
361
+ type: this.type,
362
+ items: this.items.getPlainSchema(options)
363
+ },
364
+ this
365
+ );
366
+ }
367
+ getOrCreateValues(rowId) {
368
+ let values = this.valuesMap.get(rowId);
369
+ if (!values) {
370
+ values = [];
371
+ this.valuesMap.set(rowId, values);
372
+ }
373
+ return values;
374
+ }
375
+ *iterValues() {
376
+ for (const values of this.valuesMap.values()) {
377
+ for (const value of values) {
378
+ yield value;
379
+ }
380
+ }
381
+ }
382
+ };
383
+
384
+ // src/model/value/json-string-value.store.ts
385
+ var JsonStringValueStore = class {
386
+ constructor(schema, rowId, value = null) {
387
+ this.schema = schema;
388
+ this.rowId = rowId;
389
+ this.value = value;
390
+ this.index = this.schema.registerValue(this);
391
+ }
392
+ type = "string" /* String */;
393
+ index;
394
+ parent = null;
395
+ get foreignKey() {
396
+ return this.schema.foreignKey;
397
+ }
398
+ getPlainValue() {
399
+ return this.value ?? this.schema.default;
400
+ }
401
+ };
402
+
403
+ // src/model/value/json-number-value.store.ts
404
+ var JsonNumberValueStore = class {
405
+ constructor(schema, rowId, value = null) {
406
+ this.schema = schema;
407
+ this.rowId = rowId;
408
+ this.value = value;
409
+ this.index = this.schema.registerValue(this);
410
+ }
411
+ type = "number" /* Number */;
412
+ index;
413
+ parent = null;
414
+ getPlainValue() {
415
+ return this.value ?? this.schema.default;
416
+ }
417
+ };
418
+
419
+ // src/model/value/json-boolean-value.store.ts
420
+ var JsonBooleanValueStore = class {
421
+ constructor(schema, rowId, value = null) {
422
+ this.schema = schema;
423
+ this.rowId = rowId;
424
+ this.value = value;
425
+ this.index = this.schema.registerValue(this);
426
+ }
427
+ type = "boolean" /* Boolean */;
428
+ index;
429
+ parent = null;
430
+ getPlainValue() {
431
+ return this.value ?? this.schema.default;
432
+ }
433
+ };
434
+
435
+ // src/model/value/value-transformation.ts
436
+ var equal = (value) => value;
437
+ var fromNumberToString = (value, defaultValue = "") => value.toString() || defaultValue;
438
+ var fromBooleanToString = (value, defaultValue = "") => value.toString() || defaultValue;
439
+ var fromStringToBoolean = (value, defaultValue = false) => {
440
+ if (!value) {
441
+ return defaultValue;
442
+ }
443
+ if (value.toLowerCase() === "false") {
444
+ return false;
445
+ }
446
+ return true;
447
+ };
448
+ var fromStringToNumber = (value, defaultValue = 0) => {
449
+ const number = Number.parseFloat(value);
450
+ if (Number.isNaN(number)) {
451
+ return defaultValue;
452
+ }
453
+ return number;
454
+ };
455
+ var fromBooleanToNumber = (value) => {
456
+ return Number(value);
457
+ };
458
+ var fromNumberToBoolean = (value) => {
459
+ return Boolean(value);
460
+ };
461
+ var toArrayTransformation = (transformation) => (value) => {
462
+ const result = transformation(value);
463
+ return [result];
464
+ };
465
+ var fromArrayTransformation = (transformation) => (value) => {
466
+ if (Array.isArray(value) && value.length) {
467
+ return transformation(value[0]);
468
+ }
469
+ return void 0;
470
+ };
471
+ var fromObjectToPrimitive = (_value, defaultValue) => {
472
+ return defaultValue;
473
+ };
474
+ var fromPrimitiveToObject = (_value, defaultValue) => {
475
+ return defaultValue;
476
+ };
477
+ var fromObjectToArray = (_value, defaultValue) => {
478
+ return defaultValue;
479
+ };
480
+ var fromArrayToObject = (_value, defaultValue) => {
481
+ return defaultValue;
482
+ };
483
+ var replaceTransformationsMapper = [
484
+ {
485
+ fromType: "number" /* Number */,
486
+ toType: "string" /* String */,
487
+ transformation: fromNumberToString
488
+ },
489
+ {
490
+ fromType: "string" /* String */,
491
+ toType: "number" /* Number */,
492
+ transformation: fromStringToNumber
493
+ },
494
+ {
495
+ fromType: "boolean" /* Boolean */,
496
+ toType: "string" /* String */,
497
+ transformation: fromBooleanToString
498
+ },
499
+ {
500
+ fromType: "string" /* String */,
501
+ toType: "boolean" /* Boolean */,
502
+ transformation: fromStringToBoolean
503
+ },
504
+ {
505
+ fromType: "boolean" /* Boolean */,
506
+ toType: "number" /* Number */,
507
+ transformation: fromBooleanToNumber
508
+ },
509
+ {
510
+ fromType: "number" /* Number */,
511
+ toType: "boolean" /* Boolean */,
512
+ transformation: fromNumberToBoolean
513
+ }
514
+ ];
515
+ var getTransformation = (from, to) => {
516
+ if (from instanceof JsonObjectStore && !(to instanceof JsonObjectStore) && !(to instanceof JsonArrayStore)) {
517
+ return fromObjectToPrimitive;
518
+ }
519
+ if (!(from instanceof JsonObjectStore) && !(from instanceof JsonArrayStore) && to instanceof JsonObjectStore) {
520
+ return fromPrimitiveToObject;
521
+ }
522
+ if (from instanceof JsonObjectStore && to instanceof JsonArrayStore) {
523
+ return fromObjectToArray;
524
+ }
525
+ if (from instanceof JsonArrayStore && to instanceof JsonObjectStore) {
526
+ return fromArrayToObject;
527
+ }
528
+ if (from instanceof JsonArrayStore && to instanceof JsonArrayStore) {
529
+ const itemTransformation = findTransformation(
530
+ from.items.type,
531
+ to.items.type
532
+ );
533
+ if (itemTransformation && itemTransformation !== equal) {
534
+ return (value, defaultValue) => {
535
+ if (!Array.isArray(value)) {
536
+ return defaultValue;
537
+ }
538
+ return value.map((item) => {
539
+ const result = itemTransformation(item, to.items.default);
540
+ return result !== void 0 ? result : to.items.default;
541
+ });
542
+ };
543
+ }
544
+ if (from.items.type !== to.items.type) {
545
+ return (_value, defaultValue) => defaultValue;
546
+ }
547
+ return equal;
548
+ }
549
+ if (to instanceof JsonArrayStore) {
550
+ const transformation = findTransformation(from.type, to.items.type);
551
+ if (!transformation) {
552
+ return;
553
+ }
554
+ return toArrayTransformation(transformation);
555
+ }
556
+ if (from instanceof JsonArrayStore) {
557
+ const transformation = findTransformation(from.items.type, to.type);
558
+ if (!transformation) {
559
+ return;
560
+ }
561
+ return fromArrayTransformation(transformation);
562
+ }
563
+ return findTransformation(from.type, to.type);
564
+ };
565
+ var findTransformation = (from, to) => {
566
+ if (from === to) {
567
+ return equal;
568
+ }
569
+ for (const item of replaceTransformationsMapper) {
570
+ if (item.fromType === from && item.toType === to) {
571
+ return item.transformation;
572
+ }
573
+ }
574
+ return void 0;
575
+ };
576
+
577
+ // src/model/value/json-object-value.store.ts
578
+ var JsonObjectValueStore = class {
579
+ constructor(schema, rowId, value) {
580
+ this.schema = schema;
581
+ this.rowId = rowId;
582
+ this.value = value;
583
+ this.index = this.schema.registerValue(this);
584
+ this.init();
585
+ }
586
+ type = "object" /* Object */;
587
+ index;
588
+ parent = null;
589
+ getPlainValue() {
590
+ return Object.entries(this.value).reduce(
591
+ (result, [name, store]) => {
592
+ result[name] = store.getPlainValue();
593
+ return result;
594
+ },
595
+ {}
596
+ );
597
+ }
598
+ migrateProperty(event) {
599
+ const rawValue = this.getMigratedValue(event);
600
+ this.value[event.name] = createJsonValueStore(
601
+ event.property,
602
+ this.rowId,
603
+ rawValue
604
+ );
605
+ }
606
+ addProperty(event) {
607
+ const rawValue = this.getAddedValue(event);
608
+ this.value[event.name] = createJsonValueStore(
609
+ event.property,
610
+ this.rowId,
611
+ rawValue
612
+ );
613
+ }
614
+ removeProperty(event) {
615
+ delete this.value[event.name];
616
+ }
617
+ changeName(event) {
618
+ const itemValue = this.value[event.fromName];
619
+ if (itemValue !== void 0) {
620
+ delete this.value[event.fromName];
621
+ this.value[event.toName] = itemValue;
622
+ }
623
+ }
624
+ getAddedValue(event) {
625
+ const ancestorValue = this.findValueAtSchemaLevel(event.property);
626
+ if (ancestorValue !== void 0) {
627
+ return ancestorValue;
628
+ }
629
+ const previousValue = event.property.getValue(this.rowId, this.index);
630
+ if (previousValue) {
631
+ return previousValue.getPlainValue();
632
+ }
633
+ return event.property.default;
634
+ }
635
+ findValueAtSchemaLevel(targetSchema) {
636
+ let current = this.parent;
637
+ while (current) {
638
+ if (current.type === "object" /* Object */) {
639
+ for (const [, fieldValue] of Object.entries(current.value)) {
640
+ if (fieldValue.schema === targetSchema) {
641
+ return fieldValue.getPlainValue();
642
+ }
643
+ }
644
+ }
645
+ current = current.parent;
646
+ }
647
+ return void 0;
648
+ }
649
+ getMigratedValue(event) {
650
+ const transformation = getTransformation(
651
+ event.previousProperty,
652
+ event.property
653
+ );
654
+ const valueStore = this.value[event.name];
655
+ if (transformation && valueStore) {
656
+ return transformation(
657
+ valueStore.getPlainValue(),
658
+ event.property.default
659
+ );
660
+ }
661
+ return event.property.default;
662
+ }
663
+ init() {
664
+ for (const value of Object.values(this.value)) {
665
+ value.parent = this;
666
+ }
667
+ }
668
+ };
669
+
670
+ // src/lib/createJsonValueStore.ts
671
+ var createJsonValueStore = (schema, rowId, rawValue) => {
672
+ if (schema.type === "object" /* Object */) {
673
+ return createJsonObjectValueStore(schema, rowId, rawValue);
674
+ } else if (schema.type === "array" /* Array */) {
675
+ return createJsonArrayValueStore(schema, rowId, rawValue);
676
+ } else {
677
+ return createPrimitiveValueStore(schema, rowId, rawValue);
678
+ }
679
+ };
680
+ var createJsonObjectValueStore = (schema, rowId, rawValue) => {
681
+ const value = Object.entries(rawValue).reduce(
682
+ (reduceValue, [key, itemValue]) => {
683
+ const itemSchema = schema.getProperty(key);
684
+ if (itemSchema === void 0 || itemValue === void 0) {
685
+ throw new Error("Invalid item");
686
+ }
687
+ reduceValue[key] = createJsonValueStore(itemSchema, rowId, itemValue);
688
+ return reduceValue;
689
+ },
690
+ {}
691
+ );
692
+ return new JsonObjectValueStore(schema, rowId, value);
693
+ };
694
+ var createJsonArrayValueStore = (schema, rowId, rawValue) => {
695
+ const value = rawValue.map(
696
+ (value2) => createJsonValueStore(schema.items, rowId, value2)
697
+ );
698
+ return new JsonArrayValueStore(schema, rowId, value);
699
+ };
700
+ var createPrimitiveValueStore = (schema, rowId, rawValue) => {
701
+ if (schema.type === "string" /* String */) {
702
+ return new JsonStringValueStore(schema, rowId, rawValue);
703
+ } else if (schema.type === "number" /* Number */) {
704
+ return new JsonNumberValueStore(schema, rowId, rawValue);
705
+ } else if (schema.type === "boolean" /* Boolean */) {
706
+ return new JsonBooleanValueStore(schema, rowId, rawValue);
707
+ } else {
708
+ throw new Error("this type is not allowed");
709
+ }
710
+ };
711
+
712
+ // src/model/value/json-array-value.store.ts
713
+ var JsonArrayValueStore = class {
714
+ constructor(schema, rowId, value) {
715
+ this.schema = schema;
716
+ this.rowId = rowId;
717
+ this.value = value;
718
+ this.index = this.schema.registerValue(this);
719
+ this.init();
720
+ }
721
+ type = "array" /* Array */;
722
+ index;
723
+ parent = null;
724
+ getPlainValue() {
725
+ return this.value.map((item) => item.getPlainValue());
726
+ }
727
+ migrateItems(event) {
728
+ const transformation = getTransformation(event.previousItems, event.items);
729
+ this.value = this.value.map((valueItem) => {
730
+ const rawValue = transformation ? transformation(
731
+ valueItem.getPlainValue(),
732
+ event.items.default
733
+ ) : event.items.default;
734
+ return createJsonValueStore(event.items, this.rowId, rawValue);
735
+ });
736
+ }
737
+ replaceItems(event) {
738
+ this.value = this.value.map(() => {
739
+ const rawValue = this.getReplacedValue(event);
740
+ return createJsonValueStore(event.items, this.rowId, rawValue);
741
+ });
742
+ }
743
+ getReplacedValue(event) {
744
+ const previousValue = event.items.getValue(this.rowId);
745
+ if (previousValue) {
746
+ return previousValue.getPlainValue();
747
+ }
748
+ return event.items.default;
749
+ }
750
+ init() {
751
+ for (const value of this.value) {
752
+ value.parent = this;
753
+ }
754
+ }
755
+ };
756
+
757
+ exports.JsonArrayStore = JsonArrayStore;
758
+ exports.JsonArrayValueStore = JsonArrayValueStore;
759
+ exports.JsonBooleanStore = JsonBooleanStore;
760
+ exports.JsonBooleanValueStore = JsonBooleanValueStore;
761
+ exports.JsonNumberStore = JsonNumberStore;
762
+ exports.JsonNumberValueStore = JsonNumberValueStore;
763
+ exports.JsonObjectStore = JsonObjectStore;
764
+ exports.JsonObjectValueStore = JsonObjectValueStore;
765
+ exports.JsonStringStore = JsonStringStore;
766
+ exports.JsonStringValueStore = JsonStringValueStore;
767
+ exports.addSharedFieldsFromState = addSharedFieldsFromState;
768
+ exports.createJsonArrayValueStore = createJsonArrayValueStore;
769
+ exports.createJsonObjectValueStore = createJsonObjectValueStore;
770
+ exports.createJsonValueStore = createJsonValueStore;
771
+ exports.createPrimitiveValueStore = createPrimitiveValueStore;
772
+ exports.equal = equal;
773
+ exports.fromArrayToObject = fromArrayToObject;
774
+ exports.fromArrayTransformation = fromArrayTransformation;
775
+ exports.fromBooleanToNumber = fromBooleanToNumber;
776
+ exports.fromBooleanToString = fromBooleanToString;
777
+ exports.fromNumberToBoolean = fromNumberToBoolean;
778
+ exports.fromNumberToString = fromNumberToString;
779
+ exports.fromObjectToArray = fromObjectToArray;
780
+ exports.fromObjectToPrimitive = fromObjectToPrimitive;
781
+ exports.fromPrimitiveToObject = fromPrimitiveToObject;
782
+ exports.fromStringToBoolean = fromStringToBoolean;
783
+ exports.fromStringToNumber = fromStringToNumber;
784
+ exports.getTransformation = getTransformation;
785
+ exports.toArrayTransformation = toArrayTransformation;
786
+ //# sourceMappingURL=chunk-JLIZ7QT2.cjs.map
787
+ //# sourceMappingURL=chunk-JLIZ7QT2.cjs.map