@revisium/schema-toolkit 0.2.0

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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +184 -0
  3. package/dist/consts/index.cjs +26 -0
  4. package/dist/consts/index.cjs.map +1 -0
  5. package/dist/consts/index.d.cts +17 -0
  6. package/dist/consts/index.d.ts +17 -0
  7. package/dist/consts/index.js +23 -0
  8. package/dist/consts/index.js.map +1 -0
  9. package/dist/index.cjs +1874 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.cts +13 -0
  12. package/dist/index.d.ts +13 -0
  13. package/dist/index.js +1787 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/json-patch.types-DiJBqfxV.d.ts +28 -0
  16. package/dist/json-patch.types-lByaF-OL.d.cts +28 -0
  17. package/dist/json-string.store-O1J0j-a1.d.cts +228 -0
  18. package/dist/json-string.store-S9FXT39Q.d.ts +228 -0
  19. package/dist/json.types-46Cq-WxU.d.cts +8 -0
  20. package/dist/json.types-46Cq-WxU.d.ts +8 -0
  21. package/dist/lib/index.cjs +1346 -0
  22. package/dist/lib/index.cjs.map +1 -0
  23. package/dist/lib/index.d.cts +91 -0
  24. package/dist/lib/index.d.ts +91 -0
  25. package/dist/lib/index.js +1315 -0
  26. package/dist/lib/index.js.map +1 -0
  27. package/dist/mocks/index.cjs +92 -0
  28. package/dist/mocks/index.cjs.map +1 -0
  29. package/dist/mocks/index.d.cts +26 -0
  30. package/dist/mocks/index.d.ts +26 -0
  31. package/dist/mocks/index.js +81 -0
  32. package/dist/mocks/index.js.map +1 -0
  33. package/dist/model/index.cjs +718 -0
  34. package/dist/model/index.cjs.map +1 -0
  35. package/dist/model/index.d.cts +19 -0
  36. package/dist/model/index.d.ts +19 -0
  37. package/dist/model/index.js +697 -0
  38. package/dist/model/index.js.map +1 -0
  39. package/dist/plugins/index.cjs +122 -0
  40. package/dist/plugins/index.cjs.map +1 -0
  41. package/dist/plugins/index.d.cts +21 -0
  42. package/dist/plugins/index.d.ts +21 -0
  43. package/dist/plugins/index.js +112 -0
  44. package/dist/plugins/index.js.map +1 -0
  45. package/dist/schema.types-Q3MYTp8z.d.cts +68 -0
  46. package/dist/schema.types-Q3MYTp8z.d.ts +68 -0
  47. package/dist/types/index.cjs +15 -0
  48. package/dist/types/index.cjs.map +1 -0
  49. package/dist/types/index.d.cts +56 -0
  50. package/dist/types/index.d.ts +56 -0
  51. package/dist/types/index.js +13 -0
  52. package/dist/types/index.js.map +1 -0
  53. package/dist/validation-schemas/index.cjs +380 -0
  54. package/dist/validation-schemas/index.cjs.map +1 -0
  55. package/dist/validation-schemas/index.d.cts +32 -0
  56. package/dist/validation-schemas/index.d.ts +32 -0
  57. package/dist/validation-schemas/index.js +365 -0
  58. package/dist/validation-schemas/index.js.map +1 -0
  59. package/package.json +169 -0
package/dist/index.js ADDED
@@ -0,0 +1,1787 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { EventEmitter } from 'events';
3
+
4
+ // src/types/schema.types.ts
5
+ var JsonSchemaTypeName = /* @__PURE__ */ ((JsonSchemaTypeName2) => {
6
+ JsonSchemaTypeName2["String"] = "string";
7
+ JsonSchemaTypeName2["Number"] = "number";
8
+ JsonSchemaTypeName2["Boolean"] = "boolean";
9
+ JsonSchemaTypeName2["Object"] = "object";
10
+ JsonSchemaTypeName2["Array"] = "array";
11
+ return JsonSchemaTypeName2;
12
+ })(JsonSchemaTypeName || {});
13
+
14
+ // src/plugins/row-id.schema.ts
15
+ var rowIdSchema = {
16
+ type: "string" /* String */,
17
+ default: "",
18
+ readOnly: true
19
+ };
20
+
21
+ // src/plugins/row-version-id.schema.ts
22
+ var rowVersionIdSchema = {
23
+ type: "string" /* String */,
24
+ default: "",
25
+ readOnly: true
26
+ };
27
+
28
+ // src/plugins/row-created-id.schema.ts
29
+ var rowCreatedIdSchema = {
30
+ type: "string" /* String */,
31
+ default: "",
32
+ readOnly: true
33
+ };
34
+
35
+ // src/plugins/row-created-at.schema.ts
36
+ var rowCreatedAtSchema = {
37
+ type: "string" /* String */,
38
+ default: "",
39
+ readOnly: true
40
+ };
41
+
42
+ // src/plugins/row-published-at.schema.ts
43
+ var rowPublishedAtSchema = {
44
+ type: "string" /* String */,
45
+ default: ""
46
+ };
47
+
48
+ // src/plugins/row-updated-at.schema.ts
49
+ var rowUpdatedAtSchema = {
50
+ type: "string" /* String */,
51
+ default: "",
52
+ readOnly: true
53
+ };
54
+
55
+ // src/plugins/row-hash.schema.ts
56
+ var rowHashSchema = {
57
+ type: "string" /* String */,
58
+ default: "",
59
+ readOnly: true
60
+ };
61
+
62
+ // src/plugins/row-schema-hash.schema.ts
63
+ var rowSchemaHashSchema = {
64
+ type: "string" /* String */,
65
+ default: "",
66
+ readOnly: true
67
+ };
68
+
69
+ // src/plugins/file-schema.ts
70
+ var fileSchema = {
71
+ type: "object" /* Object */,
72
+ properties: {
73
+ status: { type: "string" /* String */, default: "", readOnly: true },
74
+ fileId: { type: "string" /* String */, default: "", readOnly: true },
75
+ url: { type: "string" /* String */, default: "", readOnly: true },
76
+ fileName: { type: "string" /* String */, default: "" },
77
+ hash: {
78
+ type: "string" /* String */,
79
+ default: "",
80
+ readOnly: true
81
+ },
82
+ extension: {
83
+ type: "string" /* String */,
84
+ default: "",
85
+ readOnly: true
86
+ },
87
+ mimeType: {
88
+ type: "string" /* String */,
89
+ default: "",
90
+ readOnly: true
91
+ },
92
+ size: {
93
+ type: "number" /* Number */,
94
+ default: 0,
95
+ readOnly: true
96
+ },
97
+ width: {
98
+ type: "number" /* Number */,
99
+ default: 0,
100
+ readOnly: true
101
+ },
102
+ height: {
103
+ type: "number" /* Number */,
104
+ default: 0,
105
+ readOnly: true
106
+ }
107
+ },
108
+ required: [
109
+ "status",
110
+ "fileId",
111
+ "url",
112
+ "fileName",
113
+ "hash",
114
+ "extension",
115
+ "mimeType",
116
+ "size",
117
+ "width",
118
+ "height"
119
+ ],
120
+ additionalProperties: false
121
+ };
122
+
123
+ // src/mocks/schema.mocks.ts
124
+ var getReplacePatch = ({
125
+ path,
126
+ value
127
+ }) => ({
128
+ op: "replace",
129
+ path,
130
+ value
131
+ });
132
+ var getRemovePatch = ({
133
+ path
134
+ }) => ({
135
+ op: "remove",
136
+ path
137
+ });
138
+ var getAddPatch = ({
139
+ path,
140
+ value
141
+ }) => ({
142
+ op: "add",
143
+ path,
144
+ value
145
+ });
146
+ var getMovePatch = ({
147
+ from,
148
+ path
149
+ }) => ({
150
+ op: "move",
151
+ from,
152
+ path
153
+ });
154
+ var getStringSchema = (params = {}) => {
155
+ const schema = {
156
+ type: "string" /* String */,
157
+ default: params.default ?? ""
158
+ };
159
+ if (params.foreignKey) {
160
+ schema.foreignKey = params.foreignKey;
161
+ }
162
+ if (params.readOnly) {
163
+ schema.readOnly = params.readOnly;
164
+ }
165
+ return schema;
166
+ };
167
+ var getNumberSchema = (defaultValue = 0, readOnly) => {
168
+ const schema = {
169
+ type: "number" /* Number */,
170
+ default: defaultValue
171
+ };
172
+ if (readOnly) {
173
+ schema.readOnly = readOnly;
174
+ }
175
+ return schema;
176
+ };
177
+ var getBooleanSchema = (defaultValue = false, readOnly) => {
178
+ const schema = {
179
+ type: "boolean" /* Boolean */,
180
+ default: defaultValue
181
+ };
182
+ if (readOnly) {
183
+ schema.readOnly = readOnly;
184
+ }
185
+ return schema;
186
+ };
187
+ var getObjectSchema = (properties) => ({
188
+ type: "object" /* Object */,
189
+ additionalProperties: false,
190
+ required: Object.keys(properties).sort((a, b) => a.localeCompare(b)),
191
+ properties
192
+ });
193
+ var getArraySchema = (items) => ({
194
+ type: "array" /* Array */,
195
+ items
196
+ });
197
+ var getRefSchema = ($ref) => ({
198
+ $ref
199
+ });
200
+
201
+ // src/consts/system-schema-ids.ts
202
+ var SystemSchemaIds = /* @__PURE__ */ ((SystemSchemaIds2) => {
203
+ SystemSchemaIds2["RowId"] = "urn:jsonschema:io:revisium:row-id-schema:1.0.0";
204
+ SystemSchemaIds2["RowCreatedId"] = "urn:jsonschema:io:revisium:row-created-id-schema:1.0.0";
205
+ SystemSchemaIds2["RowVersionId"] = "urn:jsonschema:io:revisium:row-version-id-schema:1.0.0";
206
+ SystemSchemaIds2["RowCreatedAt"] = "urn:jsonschema:io:revisium:row-created-at-schema:1.0.0";
207
+ SystemSchemaIds2["RowPublishedAt"] = "urn:jsonschema:io:revisium:row-published-at-schema:1.0.0";
208
+ SystemSchemaIds2["RowUpdatedAt"] = "urn:jsonschema:io:revisium:row-updated-at-schema:1.0.0";
209
+ SystemSchemaIds2["RowHash"] = "urn:jsonschema:io:revisium:row-hash-schema:1.0.0";
210
+ SystemSchemaIds2["RowSchemaHash"] = "urn:jsonschema:io:revisium:row-schema-hash-schema:1.0.0";
211
+ SystemSchemaIds2["File"] = "urn:jsonschema:io:revisium:file-schema:1.0.0";
212
+ return SystemSchemaIds2;
213
+ })(SystemSchemaIds || {});
214
+
215
+ // src/consts/index.ts
216
+ var CustomSchemeKeywords = /* @__PURE__ */ ((CustomSchemeKeywords2) => {
217
+ CustomSchemeKeywords2["ForeignKey"] = "foreignKey";
218
+ return CustomSchemeKeywords2;
219
+ })(CustomSchemeKeywords || {});
220
+
221
+ // src/lib/addSharedFieldsFromStore.ts
222
+ var addSharedFieldsFromState = (schema, state) => {
223
+ if (state.title) {
224
+ schema.title = state.title;
225
+ }
226
+ if (state.description) {
227
+ schema.description = state.description;
228
+ }
229
+ if (state.deprecated) {
230
+ schema.deprecated = state.deprecated;
231
+ }
232
+ return schema;
233
+ };
234
+
235
+ // src/model/schema/json-string.store.ts
236
+ var JsonStringStore = class extends EventEmitter {
237
+ constructor(nodeId = nanoid()) {
238
+ super();
239
+ this.nodeId = nodeId;
240
+ }
241
+ type = "string" /* String */;
242
+ $ref = "";
243
+ name = "";
244
+ parent = null;
245
+ default = "";
246
+ readOnly;
247
+ title;
248
+ description;
249
+ deprecated;
250
+ foreignKey;
251
+ pattern;
252
+ enum;
253
+ format;
254
+ contentMediaType;
255
+ valuesMap = /* @__PURE__ */ new Map();
256
+ registerValue(value) {
257
+ const length = this.getOrCreateValues(value.rowId).push(value);
258
+ return length - 1;
259
+ }
260
+ getValue(rowId, index = 0) {
261
+ return this.getOrCreateValues(rowId)[index];
262
+ }
263
+ getPlainSchema(options) {
264
+ if (this.$ref && options?.skip$Ref !== true) {
265
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
266
+ }
267
+ return addSharedFieldsFromState(
268
+ {
269
+ type: this.type,
270
+ default: this.default,
271
+ ...this.foreignKey ? { foreignKey: this.foreignKey } : {},
272
+ ...this.readOnly ? { readOnly: this.readOnly } : {},
273
+ ...this.pattern ? { pattern: this.pattern } : {},
274
+ ...this.enum ? { enum: this.enum } : {},
275
+ ...this.format ? { format: this.format } : {},
276
+ ...this.contentMediaType ? { contentMediaType: this.contentMediaType } : {}
277
+ },
278
+ this
279
+ );
280
+ }
281
+ getOrCreateValues(rowId) {
282
+ let values = this.valuesMap.get(rowId);
283
+ if (!values) {
284
+ values = [];
285
+ this.valuesMap.set(rowId, values);
286
+ }
287
+ return values;
288
+ }
289
+ };
290
+ var JsonNumberStore = class extends EventEmitter {
291
+ constructor(nodeId = nanoid()) {
292
+ super();
293
+ this.nodeId = nodeId;
294
+ }
295
+ type = "number" /* Number */;
296
+ $ref = "";
297
+ name = "";
298
+ parent = null;
299
+ default = 0;
300
+ readOnly;
301
+ title;
302
+ description;
303
+ deprecated;
304
+ valuesMap = /* @__PURE__ */ new Map();
305
+ registerValue(value) {
306
+ const length = this.getOrCreateValues(value.rowId).push(value);
307
+ return length - 1;
308
+ }
309
+ getValue(rowId, index = 0) {
310
+ return this.getOrCreateValues(rowId)[index];
311
+ }
312
+ getPlainSchema(options) {
313
+ if (this.$ref && options?.skip$Ref !== true) {
314
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
315
+ }
316
+ return addSharedFieldsFromState(
317
+ {
318
+ type: this.type,
319
+ default: this.default,
320
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
321
+ },
322
+ this
323
+ );
324
+ }
325
+ getOrCreateValues(rowId) {
326
+ let values = this.valuesMap.get(rowId);
327
+ if (!values) {
328
+ values = [];
329
+ this.valuesMap.set(rowId, values);
330
+ }
331
+ return values;
332
+ }
333
+ };
334
+ var JsonBooleanStore = class extends EventEmitter {
335
+ constructor(nodeId = nanoid()) {
336
+ super();
337
+ this.nodeId = nodeId;
338
+ }
339
+ type = "boolean" /* Boolean */;
340
+ $ref = "";
341
+ name = "";
342
+ parent = null;
343
+ default = false;
344
+ readOnly;
345
+ title;
346
+ description;
347
+ deprecated;
348
+ valuesMap = /* @__PURE__ */ new Map();
349
+ registerValue(value) {
350
+ const length = this.getOrCreateValues(value.rowId).push(value);
351
+ return length - 1;
352
+ }
353
+ getValue(rowId, index = 0) {
354
+ return this.getOrCreateValues(rowId)[index];
355
+ }
356
+ getPlainSchema(options) {
357
+ if (this.$ref && options?.skip$Ref !== true) {
358
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
359
+ }
360
+ return addSharedFieldsFromState(
361
+ {
362
+ type: this.type,
363
+ default: this.default,
364
+ ...this.readOnly ? { readOnly: this.readOnly } : {}
365
+ },
366
+ this
367
+ );
368
+ }
369
+ getOrCreateValues(rowId) {
370
+ let values = this.valuesMap.get(rowId);
371
+ if (!values) {
372
+ values = [];
373
+ this.valuesMap.set(rowId, values);
374
+ }
375
+ return values;
376
+ }
377
+ };
378
+ var JsonObjectStore = class {
379
+ constructor(nodeId = nanoid()) {
380
+ this.nodeId = nodeId;
381
+ }
382
+ type = "object" /* Object */;
383
+ $ref = "";
384
+ name = "";
385
+ parent = null;
386
+ default = {};
387
+ title;
388
+ description;
389
+ deprecated;
390
+ additionalProperties = false;
391
+ required = [];
392
+ properties = {};
393
+ valuesMap = /* @__PURE__ */ new Map();
394
+ get empty() {
395
+ return Object.keys(this.properties).length === 0;
396
+ }
397
+ registerValue(value) {
398
+ const length = this.getOrCreateValues(value.rowId).push(value);
399
+ return length - 1;
400
+ }
401
+ getValue(rowId, index = 0) {
402
+ return this.getOrCreateValues(rowId)[index];
403
+ }
404
+ addPropertyWithStore(name, store) {
405
+ if (this.properties[name] || this.required.includes(name)) {
406
+ throw new Error("this name already exists");
407
+ }
408
+ store.parent = this;
409
+ store.name = name;
410
+ this.required.push(name);
411
+ this.required.sort((a, b) => a.localeCompare(b));
412
+ this.properties[name] = store;
413
+ this.default[name] = store.default;
414
+ const event = { name, property: store };
415
+ for (const value of this.iterValues()) {
416
+ value.addProperty(event);
417
+ }
418
+ return store;
419
+ }
420
+ migratePropertyWithStore(name, store) {
421
+ const item = this.properties[name];
422
+ if (!item) {
423
+ throw new Error("this name does not exist");
424
+ }
425
+ item.parent = null;
426
+ store.parent = this;
427
+ store.name = name;
428
+ this.properties[name] = store;
429
+ this.default[name] = store.default;
430
+ const event = {
431
+ name,
432
+ property: store,
433
+ previousProperty: item
434
+ };
435
+ for (const value of this.iterValues()) {
436
+ value.migrateProperty(event);
437
+ }
438
+ return store;
439
+ }
440
+ changeName(fromName, toName) {
441
+ const item = this.properties[fromName];
442
+ if (!item) {
443
+ throw new Error("this fromName does not exist");
444
+ }
445
+ delete this.properties[fromName];
446
+ delete this.default[fromName];
447
+ const foundRequiredIndex = this.required.findIndex(
448
+ (required) => required === fromName
449
+ );
450
+ if (foundRequiredIndex !== -1) {
451
+ this.required.splice(foundRequiredIndex, 1);
452
+ }
453
+ if (!this.required.includes(toName)) {
454
+ this.required.push(toName);
455
+ this.required.sort((a, b) => a.localeCompare(b));
456
+ }
457
+ this.properties[toName] = item;
458
+ this.default[toName] = item.default;
459
+ const event = {
460
+ fromName,
461
+ toName,
462
+ property: item
463
+ };
464
+ for (const value of this.iterValues()) {
465
+ value.changeName(event);
466
+ }
467
+ }
468
+ removeProperty(name) {
469
+ const item = this.properties[name];
470
+ if (!item) {
471
+ throw new Error("this name does not exist");
472
+ }
473
+ item.parent = null;
474
+ item.name = "";
475
+ delete this.properties[name];
476
+ delete this.default[name];
477
+ const foundRequiredIndex = this.required.findIndex(
478
+ (required) => required === name
479
+ );
480
+ if (foundRequiredIndex !== -1) {
481
+ this.required.splice(foundRequiredIndex, 1);
482
+ }
483
+ const event = {
484
+ name,
485
+ property: item
486
+ };
487
+ for (const value of this.iterValues()) {
488
+ value.removeProperty(event);
489
+ }
490
+ }
491
+ getProperty(name) {
492
+ return this.properties[name];
493
+ }
494
+ getPlainSchema(options) {
495
+ if (this.$ref && options?.skip$Ref !== true) {
496
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
497
+ }
498
+ return addSharedFieldsFromState(
499
+ {
500
+ type: this.type,
501
+ additionalProperties: this.additionalProperties,
502
+ required: this.required,
503
+ properties: Object.entries(this.properties).reduce((result, [name, store]) => {
504
+ result[name] = store.getPlainSchema(options);
505
+ return result;
506
+ }, {})
507
+ },
508
+ this
509
+ );
510
+ }
511
+ getOrCreateValues(rowId) {
512
+ let values = this.valuesMap.get(rowId);
513
+ if (!values) {
514
+ values = [];
515
+ this.valuesMap.set(rowId, values);
516
+ }
517
+ return values;
518
+ }
519
+ *iterValues() {
520
+ for (const values of this.valuesMap.values()) {
521
+ for (const value of values) {
522
+ yield value;
523
+ }
524
+ }
525
+ }
526
+ };
527
+ var JsonArrayStore = class {
528
+ constructor(_items, nodeId = nanoid()) {
529
+ this._items = _items;
530
+ this.nodeId = nodeId;
531
+ this.items.parent = this;
532
+ }
533
+ type = "array" /* Array */;
534
+ $ref = "";
535
+ name = "";
536
+ parent = null;
537
+ default = [];
538
+ title;
539
+ description;
540
+ deprecated;
541
+ valuesMap = /* @__PURE__ */ new Map();
542
+ get items() {
543
+ return this._items;
544
+ }
545
+ registerValue(value) {
546
+ const length = this.getOrCreateValues(value.rowId).push(value);
547
+ return length - 1;
548
+ }
549
+ getValue(rowId, index = 0) {
550
+ return this.getOrCreateValues(rowId)[index];
551
+ }
552
+ migrateItems(items) {
553
+ const previousItems = this._items;
554
+ previousItems.parent = null;
555
+ this._items = items;
556
+ this._items.parent = this;
557
+ const event = { items, previousItems };
558
+ for (const value of this.iterValues()) {
559
+ value.migrateItems(event);
560
+ }
561
+ }
562
+ replaceItems(items) {
563
+ const previousItems = this._items;
564
+ previousItems.parent = null;
565
+ this._items = items;
566
+ this._items.parent = this;
567
+ const event = { items, previousItems };
568
+ for (const value of this.iterValues()) {
569
+ value.replaceItems(event);
570
+ }
571
+ }
572
+ getPlainSchema(options) {
573
+ if (this.$ref && options?.skip$Ref !== true) {
574
+ return addSharedFieldsFromState({ $ref: this.$ref }, this);
575
+ }
576
+ return addSharedFieldsFromState(
577
+ {
578
+ type: this.type,
579
+ items: this.items.getPlainSchema(options)
580
+ },
581
+ this
582
+ );
583
+ }
584
+ getOrCreateValues(rowId) {
585
+ let values = this.valuesMap.get(rowId);
586
+ if (!values) {
587
+ values = [];
588
+ this.valuesMap.set(rowId, values);
589
+ }
590
+ return values;
591
+ }
592
+ *iterValues() {
593
+ for (const values of this.valuesMap.values()) {
594
+ for (const value of values) {
595
+ yield value;
596
+ }
597
+ }
598
+ }
599
+ };
600
+
601
+ // src/model/value/json-string-value.store.ts
602
+ var JsonStringValueStore = class {
603
+ constructor(schema, rowId, value = null) {
604
+ this.schema = schema;
605
+ this.rowId = rowId;
606
+ this.value = value;
607
+ this.index = this.schema.registerValue(this);
608
+ }
609
+ type = "string" /* String */;
610
+ index;
611
+ parent = null;
612
+ get foreignKey() {
613
+ return this.schema.foreignKey;
614
+ }
615
+ getPlainValue() {
616
+ return this.value ?? this.schema.default;
617
+ }
618
+ };
619
+
620
+ // src/model/value/json-number-value.store.ts
621
+ var JsonNumberValueStore = class {
622
+ constructor(schema, rowId, value = null) {
623
+ this.schema = schema;
624
+ this.rowId = rowId;
625
+ this.value = value;
626
+ this.index = this.schema.registerValue(this);
627
+ }
628
+ type = "number" /* Number */;
629
+ index;
630
+ parent = null;
631
+ getPlainValue() {
632
+ return this.value ?? this.schema.default;
633
+ }
634
+ };
635
+
636
+ // src/model/value/json-boolean-value.store.ts
637
+ var JsonBooleanValueStore = class {
638
+ constructor(schema, rowId, value = null) {
639
+ this.schema = schema;
640
+ this.rowId = rowId;
641
+ this.value = value;
642
+ this.index = this.schema.registerValue(this);
643
+ }
644
+ type = "boolean" /* Boolean */;
645
+ index;
646
+ parent = null;
647
+ getPlainValue() {
648
+ return this.value ?? this.schema.default;
649
+ }
650
+ };
651
+
652
+ // src/model/value/value-transformation.ts
653
+ var equel = (value) => value;
654
+ var fromNumberToString = (value, defaultValue = "") => value.toString() || defaultValue;
655
+ var fromBooleanToString = (value, defaultValue = "") => value.toString() || defaultValue;
656
+ var fromStringToBoolean = (value, defaultValue = false) => {
657
+ if (!value) {
658
+ return defaultValue;
659
+ }
660
+ if (value.toLowerCase() === "false") {
661
+ return false;
662
+ }
663
+ return true;
664
+ };
665
+ var fromStringToNumber = (value, defaultValue = 0) => {
666
+ const number = Number.parseFloat(value);
667
+ if (Number.isNaN(number)) {
668
+ return defaultValue;
669
+ }
670
+ return number;
671
+ };
672
+ var fromBooleanToNumber = (value) => {
673
+ return Number(value);
674
+ };
675
+ var fromNumberToBoolean = (value) => {
676
+ return Boolean(value);
677
+ };
678
+ var toArrayTransformation = (transformation) => (value) => {
679
+ const result = transformation(value);
680
+ return [result];
681
+ };
682
+ var fromArrayTransformation = (transformation) => (value) => {
683
+ if (Array.isArray(value) && value.length) {
684
+ return transformation(value[0]);
685
+ }
686
+ return void 0;
687
+ };
688
+ var replaceTransformationsMapper = [
689
+ {
690
+ fromType: "number" /* Number */,
691
+ toType: "string" /* String */,
692
+ transformation: fromNumberToString
693
+ },
694
+ {
695
+ fromType: "string" /* String */,
696
+ toType: "number" /* Number */,
697
+ transformation: fromStringToNumber
698
+ },
699
+ {
700
+ fromType: "boolean" /* Boolean */,
701
+ toType: "string" /* String */,
702
+ transformation: fromBooleanToString
703
+ },
704
+ {
705
+ fromType: "string" /* String */,
706
+ toType: "boolean" /* Boolean */,
707
+ transformation: fromStringToBoolean
708
+ },
709
+ {
710
+ fromType: "boolean" /* Boolean */,
711
+ toType: "number" /* Number */,
712
+ transformation: fromBooleanToNumber
713
+ },
714
+ {
715
+ fromType: "number" /* Number */,
716
+ toType: "boolean" /* Boolean */,
717
+ transformation: fromNumberToBoolean
718
+ }
719
+ ];
720
+ var getTransformation = (from, to) => {
721
+ if (to instanceof JsonArrayStore) {
722
+ const transformation = findTransformation(from.type, to.items.type);
723
+ if (!transformation) {
724
+ return;
725
+ }
726
+ return toArrayTransformation(transformation);
727
+ } else if (from instanceof JsonArrayStore) {
728
+ const transformation = findTransformation(from.items.type, to.type);
729
+ if (!transformation) {
730
+ return;
731
+ }
732
+ return fromArrayTransformation(transformation);
733
+ }
734
+ return findTransformation(from.type, to.type);
735
+ };
736
+ var findTransformation = (from, to) => {
737
+ if (from === to) {
738
+ return equel;
739
+ }
740
+ for (const item of replaceTransformationsMapper) {
741
+ if (item.fromType === from && item.toType === to) {
742
+ return item.transformation;
743
+ }
744
+ }
745
+ return void 0;
746
+ };
747
+
748
+ // src/model/value/json-array-value.store.ts
749
+ var JsonArrayValueStore = class {
750
+ constructor(schema, rowId, value) {
751
+ this.schema = schema;
752
+ this.rowId = rowId;
753
+ this.value = value;
754
+ this.index = this.schema.registerValue(this);
755
+ this.init();
756
+ }
757
+ type = "array" /* Array */;
758
+ index;
759
+ parent = null;
760
+ getPlainValue() {
761
+ return this.value.map((item) => item.getPlainValue());
762
+ }
763
+ migrateItems(event) {
764
+ const transformation = getTransformation(event.previousItems, event.items);
765
+ this.value = this.value.map((valueItem) => {
766
+ const rawValue = transformation ? transformation(
767
+ valueItem.getPlainValue(),
768
+ event.items.default
769
+ ) : event.items.default;
770
+ return createJsonValueStore(event.items, this.rowId, rawValue);
771
+ });
772
+ }
773
+ replaceItems(event) {
774
+ this.value = this.value.map(() => {
775
+ const rawValue = this.getReplacedValue(event);
776
+ return createJsonValueStore(event.items, this.rowId, rawValue);
777
+ });
778
+ }
779
+ getReplacedValue(event) {
780
+ const previousValue = event.items.getValue(this.rowId);
781
+ if (previousValue) {
782
+ return previousValue.getPlainValue();
783
+ }
784
+ return event.items.default;
785
+ }
786
+ init() {
787
+ for (const value of this.value) {
788
+ value.parent = this;
789
+ }
790
+ }
791
+ };
792
+
793
+ // src/lib/createJsonValueStore.ts
794
+ var createJsonValueStore = (schema, rowId, rawValue) => {
795
+ if (schema.type === "object" /* Object */) {
796
+ return createJsonObjectValueStore(schema, rowId, rawValue);
797
+ } else if (schema.type === "array" /* Array */) {
798
+ return createJsonArrayValueStore(schema, rowId, rawValue);
799
+ } else {
800
+ return createPrimitiveValueStore(schema, rowId, rawValue);
801
+ }
802
+ };
803
+ var createJsonObjectValueStore = (schema, rowId, rawValue) => {
804
+ const value = Object.entries(rawValue).reduce(
805
+ (reduceValue, [key, itemValue]) => {
806
+ const itemSchema = schema.getProperty(key);
807
+ if (itemSchema === void 0 || itemValue === void 0) {
808
+ throw new Error("Invalid item");
809
+ }
810
+ reduceValue[key] = createJsonValueStore(itemSchema, rowId, itemValue);
811
+ return reduceValue;
812
+ },
813
+ {}
814
+ );
815
+ return new JsonObjectValueStore(schema, rowId, value);
816
+ };
817
+ var createJsonArrayValueStore = (schema, rowId, rawValue) => {
818
+ const value = rawValue.map(
819
+ (value2) => createJsonValueStore(schema.items, rowId, value2)
820
+ );
821
+ return new JsonArrayValueStore(schema, rowId, value);
822
+ };
823
+ var createPrimitiveValueStore = (schema, rowId, rawValue) => {
824
+ if (schema.type === "string" /* String */) {
825
+ return new JsonStringValueStore(schema, rowId, rawValue);
826
+ } else if (schema.type === "number" /* Number */) {
827
+ return new JsonNumberValueStore(schema, rowId, rawValue);
828
+ } else if (schema.type === "boolean" /* Boolean */) {
829
+ return new JsonBooleanValueStore(schema, rowId, rawValue);
830
+ } else {
831
+ throw new Error("this type is not allowed");
832
+ }
833
+ };
834
+
835
+ // src/model/value/json-object-value.store.ts
836
+ var JsonObjectValueStore = class {
837
+ constructor(schema, rowId, value) {
838
+ this.schema = schema;
839
+ this.rowId = rowId;
840
+ this.value = value;
841
+ this.index = this.schema.registerValue(this);
842
+ this.init();
843
+ }
844
+ type = "object" /* Object */;
845
+ index;
846
+ parent = null;
847
+ getPlainValue() {
848
+ return Object.entries(this.value).reduce(
849
+ (result, [name, store]) => {
850
+ result[name] = store.getPlainValue();
851
+ return result;
852
+ },
853
+ {}
854
+ );
855
+ }
856
+ migrateProperty(event) {
857
+ const rawValue = this.getMigratedValue(event);
858
+ this.value[event.name] = createJsonValueStore(
859
+ event.property,
860
+ this.rowId,
861
+ rawValue
862
+ );
863
+ }
864
+ addProperty(event) {
865
+ const rawValue = this.getAddedValue(event);
866
+ this.value[event.name] = createJsonValueStore(
867
+ event.property,
868
+ this.rowId,
869
+ rawValue
870
+ );
871
+ }
872
+ removeProperty(event) {
873
+ delete this.value[event.name];
874
+ }
875
+ changeName(event) {
876
+ const itemValue = this.value[event.fromName];
877
+ if (itemValue !== void 0) {
878
+ delete this.value[event.fromName];
879
+ this.value[event.toName] = itemValue;
880
+ }
881
+ }
882
+ getAddedValue(event) {
883
+ const previousValue = event.property.getValue(this.rowId, this.index);
884
+ if (previousValue) {
885
+ return previousValue.getPlainValue();
886
+ }
887
+ return event.property.default;
888
+ }
889
+ getMigratedValue(event) {
890
+ const transformation = getTransformation(
891
+ event.previousProperty,
892
+ event.property
893
+ );
894
+ const valueStore = this.value[event.name];
895
+ if (transformation && valueStore) {
896
+ return transformation(
897
+ valueStore.getPlainValue(),
898
+ event.property.default
899
+ );
900
+ }
901
+ return event.property.default;
902
+ }
903
+ init() {
904
+ for (const value of Object.values(this.value)) {
905
+ value.parent = this;
906
+ }
907
+ }
908
+ };
909
+
910
+ // src/lib/createJsonSchemaStore.ts
911
+ var createJsonSchemaStore = (schema, refs = {}) => {
912
+ if ("$ref" in schema) {
913
+ const refSchema = refs[schema.$ref];
914
+ if (!refSchema) {
915
+ throw new Error(`Not found schema for $ref="${schema.$ref}"`);
916
+ }
917
+ const refStore = createJsonSchemaStore(refSchema, refs);
918
+ saveSharedFields(refStore, schema);
919
+ refStore.$ref = schema.$ref;
920
+ return refStore;
921
+ } else if (schema.type === "object" /* Object */) {
922
+ const objectStore = createJsonObjectSchemaStore(schema, refs);
923
+ saveSharedFields(objectStore, schema);
924
+ return objectStore;
925
+ } else if (schema.type === "array" /* Array */) {
926
+ const itemsStore = createJsonSchemaStore(schema.items, refs);
927
+ const arrayStore = new JsonArrayStore(itemsStore);
928
+ saveSharedFields(arrayStore, schema);
929
+ return arrayStore;
930
+ } else {
931
+ const primitivesStore = createPrimitiveStoreBySchema(schema);
932
+ saveSharedFields(primitivesStore, schema);
933
+ primitivesStore.readOnly = schema.readOnly;
934
+ return primitivesStore;
935
+ }
936
+ };
937
+ var createJsonObjectSchemaStore = (value, refs) => {
938
+ const store = new JsonObjectStore();
939
+ for (const requiredField of value.required) {
940
+ if (!value.properties[requiredField]) {
941
+ throw new Error(
942
+ `Not found required field "${requiredField}" in "properties"`
943
+ );
944
+ }
945
+ }
946
+ Object.entries(value.properties).forEach(([name, item]) => {
947
+ store.addPropertyWithStore(name, createJsonSchemaStore(item, refs));
948
+ });
949
+ return store;
950
+ };
951
+ var createPrimitiveStoreBySchema = (schema) => {
952
+ if (schema.type === "string" /* String */) {
953
+ const stringStore = new JsonStringStore();
954
+ stringStore.foreignKey = schema.foreignKey;
955
+ stringStore.format = schema.format;
956
+ stringStore.enum = schema.enum;
957
+ stringStore.contentMediaType = schema.contentMediaType;
958
+ stringStore.pattern = schema.pattern;
959
+ return stringStore;
960
+ } else if (schema.type === "number" /* Number */) {
961
+ return new JsonNumberStore();
962
+ } else if (schema.type === "boolean" /* Boolean */) {
963
+ return new JsonBooleanStore();
964
+ } else {
965
+ throw new Error("this type is not allowed");
966
+ }
967
+ };
968
+ var saveSharedFields = (store, schema) => {
969
+ store.title = schema.title;
970
+ store.description = schema.description;
971
+ store.deprecated = schema.deprecated;
972
+ };
973
+
974
+ // src/lib/getJsonSchemaStoreByPath.ts
975
+ var getJsonSchemaStoreByPath = (store, path) => {
976
+ if (path === "") {
977
+ return store;
978
+ }
979
+ if (path === "/") {
980
+ throw new Error(
981
+ 'invalid root path, need to use path="" instead of path="/"'
982
+ );
983
+ }
984
+ const tokens = path.split("/");
985
+ tokens.shift();
986
+ let currentStore = store;
987
+ let currentToken = tokens.shift();
988
+ let currentPath = "";
989
+ while (currentToken) {
990
+ if (currentStore.type === "object" /* Object */) {
991
+ if (currentToken !== "properties") {
992
+ throw new Error(
993
+ `Expected "${currentPath}/properties/*" instead of ${currentPath}/${currentToken}/*`
994
+ );
995
+ }
996
+ currentPath = `${currentPath}/${currentToken}`;
997
+ currentToken = tokens.shift();
998
+ if (!currentToken) {
999
+ throw new Error(`Expected property name after "${currentPath}"`);
1000
+ }
1001
+ const foundCurrentStore = currentStore.getProperty(currentToken);
1002
+ if (!foundCurrentStore) {
1003
+ throw new Error(`Not found "${currentToken}" in "${currentPath}"`);
1004
+ }
1005
+ currentStore = foundCurrentStore;
1006
+ currentPath = `${currentPath}/${currentToken}`;
1007
+ currentToken = tokens.shift();
1008
+ } else if (currentStore.type === "array" /* Array */) {
1009
+ if (currentToken !== "items") {
1010
+ throw new Error(
1011
+ `Expected "${currentPath}/items/*" instead of ${currentPath}/${currentToken}/*`
1012
+ );
1013
+ }
1014
+ currentPath = `${currentPath}/${currentToken}`;
1015
+ currentStore = currentStore.items;
1016
+ currentToken = tokens.shift();
1017
+ } else {
1018
+ throw new Error(`Unexpected "${currentToken}" in "${currentPath}"`);
1019
+ }
1020
+ }
1021
+ return currentStore;
1022
+ };
1023
+
1024
+ // src/lib/getParentForPath.ts
1025
+ var getParentForPath = (path) => {
1026
+ if (path === "" || path === "/") {
1027
+ throw new Error("Invalid path");
1028
+ }
1029
+ const tokens = path.split("/");
1030
+ tokens.shift();
1031
+ let currentToken = tokens.shift();
1032
+ let parentPath = "";
1033
+ let field = "";
1034
+ while (currentToken) {
1035
+ if (currentToken === "properties") {
1036
+ currentToken = tokens.shift();
1037
+ if (!currentToken) {
1038
+ throw new Error("Invalid path");
1039
+ }
1040
+ field = currentToken;
1041
+ currentToken = tokens.shift();
1042
+ if (currentToken) {
1043
+ parentPath = `${parentPath}/properties/${field}`;
1044
+ }
1045
+ } else if (currentToken === "items") {
1046
+ field = currentToken;
1047
+ currentToken = tokens.shift();
1048
+ if (currentToken && !["items", "properties"].includes(currentToken)) {
1049
+ throw new Error("Invalid path");
1050
+ } else if (currentToken) {
1051
+ parentPath = `${parentPath}/items`;
1052
+ }
1053
+ } else {
1054
+ throw new Error("Invalid path");
1055
+ }
1056
+ }
1057
+ return {
1058
+ parentPath,
1059
+ field
1060
+ };
1061
+ };
1062
+
1063
+ // src/lib/validateJsonFieldName.ts
1064
+ var maxLength = 64;
1065
+ var VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE = `It must contain between 1 and ${maxLength} characters, start with a letter or underscore (_), cannot start with two underscores (__), and can only include letters (a-z, A-Z), numbers (0-9), hyphens (-), and underscores (_).`;
1066
+ var validPattern = /^(?!__)[a-zA-Z_][a-zA-Z0-9-_]*$/;
1067
+ var validateJsonFieldName = (id) => {
1068
+ const isInvalid = id.length < 1 || id.length > maxLength || !validPattern.test(id);
1069
+ return !isInvalid;
1070
+ };
1071
+
1072
+ // src/lib/applyPatches.ts
1073
+ var applyReplacePatch = (store, patch, refs = {}) => {
1074
+ const patchStore = createJsonSchemaStore(patch.value, refs);
1075
+ const foundStore = getJsonSchemaStoreByPath(store, patch.path);
1076
+ const parent = foundStore.parent;
1077
+ if (!parent) {
1078
+ return patchStore;
1079
+ }
1080
+ if (parent.type === "object" /* Object */) {
1081
+ parent.migratePropertyWithStore(foundStore.name, patchStore);
1082
+ } else if (parent.type === "array" /* Array */) {
1083
+ parent.migrateItems(patchStore);
1084
+ } else {
1085
+ throw new Error("Invalid parent");
1086
+ }
1087
+ return store;
1088
+ };
1089
+ var applyRemovePatch = (rootStore, patch) => {
1090
+ const foundStore = getJsonSchemaStoreByPath(rootStore, patch.path);
1091
+ const parent = foundStore.parent;
1092
+ if (!parent) {
1093
+ throw new Error("Parent does not exist");
1094
+ }
1095
+ if (parent.type !== "object" /* Object */) {
1096
+ throw new Error("Cannot remove from non-object");
1097
+ }
1098
+ parent.removeProperty(foundStore.name);
1099
+ };
1100
+ var applyAddPatch = (rootStore, patch, refs = {}) => {
1101
+ const patchStore = createJsonSchemaStore(patch.value, refs);
1102
+ const { parentPath, field } = getParentForPath(patch.path);
1103
+ const foundParent = getJsonSchemaStoreByPath(rootStore, parentPath);
1104
+ if (!foundParent) {
1105
+ throw new Error("Parent does not exist");
1106
+ }
1107
+ if (foundParent.type !== "object" /* Object */) {
1108
+ throw new Error("Cannot add to non-object");
1109
+ }
1110
+ if (foundParent.getProperty(field)) {
1111
+ throw new Error(`Field "${field}" already exists in parent`);
1112
+ }
1113
+ foundParent.addPropertyWithStore(field, patchStore);
1114
+ };
1115
+ var applyMovePatch = (store, patch) => {
1116
+ const { parentPath: fromParentPath, field: fromField } = getParentForPath(
1117
+ patch.from
1118
+ );
1119
+ const { parentPath: toParentPath, field: toField } = getParentForPath(
1120
+ patch.path
1121
+ );
1122
+ const foundFromParent = getJsonSchemaStoreByPath(store, fromParentPath);
1123
+ const foundToParent = getJsonSchemaStoreByPath(store, toParentPath);
1124
+ const isValidToField = validateJsonFieldName(toField);
1125
+ if (!isValidToField) {
1126
+ throw new Error(
1127
+ `Invalid name: ${toField}. ${VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE}`
1128
+ );
1129
+ }
1130
+ if (!foundFromParent || !foundToParent) {
1131
+ throw new Error("Cannot move from or to non-existent parent");
1132
+ }
1133
+ if (foundFromParent.type !== "object" /* Object */) {
1134
+ throw new Error("Cannot move from non-object parent");
1135
+ }
1136
+ const foundFromField = getJsonSchemaStoreByPath(store, patch.from);
1137
+ const isMovedPropertyInSameParentPatch = foundFromParent === foundToParent && foundFromParent.type === "object" /* Object */ && foundFromParent.getProperty(fromField);
1138
+ if (isMovedPropertyInSameParentPatch) {
1139
+ return foundFromParent.changeName(fromField, toField);
1140
+ }
1141
+ if (foundToParent.type === "object" /* Object */) {
1142
+ foundFromParent.removeProperty(fromField);
1143
+ if (foundToParent.getProperty(toField)) {
1144
+ foundToParent.removeProperty(toField);
1145
+ }
1146
+ foundToParent.addPropertyWithStore(toField, foundFromField);
1147
+ return;
1148
+ }
1149
+ if (foundToParent.type === "array" /* Array */) {
1150
+ foundFromParent.removeProperty(fromField);
1151
+ foundToParent.replaceItems(foundFromField);
1152
+ return;
1153
+ }
1154
+ throw new Error('Invalid type of "to" parent');
1155
+ };
1156
+
1157
+ // src/lib/getDBJsonPathByJsonSchemaStore.ts
1158
+ var getDBJsonPathByJsonSchemaStore = (store) => {
1159
+ let node = store;
1160
+ let path = "";
1161
+ while (node.parent) {
1162
+ if (node.parent.type === "object" /* Object */) {
1163
+ path = `.${node.name}${path}`;
1164
+ } else if (node.parent.type === "array" /* Array */) {
1165
+ path = `[*]${path}`;
1166
+ }
1167
+ node = node.parent;
1168
+ }
1169
+ if (!path) {
1170
+ return "$";
1171
+ }
1172
+ return `$${path}`;
1173
+ };
1174
+
1175
+ // src/lib/getPathByStore.ts
1176
+ var getPathByStore = (store) => {
1177
+ let node = store;
1178
+ let path = "";
1179
+ while (node.parent) {
1180
+ if (node.parent.type === "object" /* Object */) {
1181
+ path = `/properties/${node.name}${path}`;
1182
+ } else if (node.parent.type === "array" /* Array */) {
1183
+ path = `/items${path}`;
1184
+ }
1185
+ node = node.parent;
1186
+ }
1187
+ if (!path) {
1188
+ return "/";
1189
+ }
1190
+ return `${path}`;
1191
+ };
1192
+
1193
+ // src/lib/traverseStore.ts
1194
+ var traverseStore = (store, callback) => {
1195
+ callback(store);
1196
+ if (store.type === "object" /* Object */) {
1197
+ Object.values(store.properties).forEach((item) => {
1198
+ traverseStore(item, callback);
1199
+ });
1200
+ } else if (store.type === "array" /* Array */) {
1201
+ traverseStore(store.items, callback);
1202
+ }
1203
+ };
1204
+
1205
+ // src/lib/getForeignKeyPatchesFromSchema.ts
1206
+ var getForeignKeyPatchesFromSchema = (store, options) => {
1207
+ const stores = [];
1208
+ traverseStore(store, (item) => {
1209
+ if (item.type === "string" /* String */ && item.foreignKey === options.tableId) {
1210
+ item.foreignKey = options.nextTableId;
1211
+ const patch = {
1212
+ op: "replace",
1213
+ path: getPathByStore(item),
1214
+ value: item.getPlainSchema()
1215
+ };
1216
+ stores.push(patch);
1217
+ }
1218
+ });
1219
+ return stores;
1220
+ };
1221
+
1222
+ // src/lib/getForeignKeysFromSchema.ts
1223
+ var getForeignKeysFromSchema = (store) => {
1224
+ const foreignKeys = /* @__PURE__ */ new Set();
1225
+ traverseStore(store, (item) => {
1226
+ if (item.type === "string" /* String */ && item.foreignKey) {
1227
+ foreignKeys.add(item.foreignKey);
1228
+ }
1229
+ });
1230
+ return [...foreignKeys].sort((a, b) => a.localeCompare(b));
1231
+ };
1232
+
1233
+ // src/lib/traverseValue.ts
1234
+ var traverseValue = (store, callback) => {
1235
+ callback(store);
1236
+ if (store.type === "object" /* Object */) {
1237
+ Object.values(store.value).forEach((item) => {
1238
+ traverseValue(item, callback);
1239
+ });
1240
+ } else if (store.type === "array" /* Array */) {
1241
+ store.value.forEach((itemValue) => {
1242
+ traverseValue(itemValue, callback);
1243
+ });
1244
+ }
1245
+ };
1246
+
1247
+ // src/lib/getForeignKeysFromValue.ts
1248
+ var getForeignKeysFromValue = (value) => {
1249
+ const foreignKeys = /* @__PURE__ */ new Map();
1250
+ traverseValue(value, (item) => {
1251
+ if (item.type === "string" /* String */ && item.foreignKey) {
1252
+ let tableForeignKey = foreignKeys.get(item.foreignKey);
1253
+ if (!tableForeignKey) {
1254
+ tableForeignKey = /* @__PURE__ */ new Set();
1255
+ foreignKeys.set(item.foreignKey, tableForeignKey);
1256
+ }
1257
+ tableForeignKey.add(item.getPlainValue());
1258
+ }
1259
+ });
1260
+ return [...foreignKeys].map(([tableId, rowIds]) => ({
1261
+ tableId,
1262
+ rowIds: [...rowIds].sort((a, b) => a.localeCompare(b))
1263
+ }));
1264
+ };
1265
+
1266
+ // src/lib/getInvalidFieldNamesInSchema.ts
1267
+ var getInvalidFieldNamesInSchema = (schema, refs = {}) => {
1268
+ const schemaStore = createJsonSchemaStore(schema, refs);
1269
+ const invalidFields = [];
1270
+ traverseStore(schemaStore, (item) => {
1271
+ if (item.parent?.type === "object" /* Object */) {
1272
+ if (!validateJsonFieldName(item.name)) {
1273
+ invalidFields.push(item);
1274
+ }
1275
+ }
1276
+ });
1277
+ return invalidFields;
1278
+ };
1279
+
1280
+ // src/lib/getJsonValueByPath.ts
1281
+ var getJsonValueStoreByPath = (root, path) => {
1282
+ if (!path) {
1283
+ return root;
1284
+ }
1285
+ const segments = getSegments(path);
1286
+ let current = root;
1287
+ for (const seg of segments) {
1288
+ if (current instanceof JsonObjectValueStore) {
1289
+ const next = current.value[String(seg)];
1290
+ if (!next) {
1291
+ throw new Error(`Path not found at segment "${seg}"`);
1292
+ }
1293
+ current = next;
1294
+ } else if (current instanceof JsonArrayValueStore) {
1295
+ if (typeof seg !== "number") {
1296
+ throw new Error(`Invalid array index "${seg}"`);
1297
+ }
1298
+ const next = current.value[seg];
1299
+ if (!next) {
1300
+ throw new Error(`Path not found at segment "${seg}"`);
1301
+ }
1302
+ current = next;
1303
+ } else {
1304
+ throw new Error(`Cannot navigate into primitive at segment "${seg}"`);
1305
+ }
1306
+ }
1307
+ return current;
1308
+ };
1309
+ var regex = /([^.[\]]+)|\[(\d+)]/g;
1310
+ var getSegments = (path) => {
1311
+ const segments = [];
1312
+ let match;
1313
+ while (match = regex.exec(path)) {
1314
+ if (match[1] !== void 0) {
1315
+ segments.push(match[1]);
1316
+ } else if (match[2] !== void 0) {
1317
+ segments.push(Number(match[2]));
1318
+ }
1319
+ }
1320
+ return segments;
1321
+ };
1322
+
1323
+ // src/lib/replaceForeignKeyValue.ts
1324
+ var replaceForeignKeyValue = (options) => {
1325
+ let wasUpdated = false;
1326
+ traverseValue(options.valueStore, (item) => {
1327
+ if (item.type === "string" /* String */ && item.foreignKey === options.foreignKey && item.value === options.value) {
1328
+ item.value = options.nextValue;
1329
+ wasUpdated = true;
1330
+ }
1331
+ });
1332
+ return wasUpdated;
1333
+ };
1334
+
1335
+ // src/lib/resolveRefs.ts
1336
+ var pluginRefs = {
1337
+ ["urn:jsonschema:io:revisium:row-id-schema:1.0.0" /* RowId */]: rowIdSchema,
1338
+ ["urn:jsonschema:io:revisium:row-version-id-schema:1.0.0" /* RowVersionId */]: rowVersionIdSchema,
1339
+ ["urn:jsonschema:io:revisium:row-created-id-schema:1.0.0" /* RowCreatedId */]: rowCreatedIdSchema,
1340
+ ["urn:jsonschema:io:revisium:row-created-at-schema:1.0.0" /* RowCreatedAt */]: rowCreatedAtSchema,
1341
+ ["urn:jsonschema:io:revisium:row-published-at-schema:1.0.0" /* RowPublishedAt */]: rowPublishedAtSchema,
1342
+ ["urn:jsonschema:io:revisium:row-updated-at-schema:1.0.0" /* RowUpdatedAt */]: rowUpdatedAtSchema,
1343
+ ["urn:jsonschema:io:revisium:row-hash-schema:1.0.0" /* RowHash */]: rowHashSchema,
1344
+ ["urn:jsonschema:io:revisium:row-schema-hash-schema:1.0.0" /* RowSchemaHash */]: rowSchemaHashSchema,
1345
+ ["urn:jsonschema:io:revisium:file-schema:1.0.0" /* File */]: fileSchema
1346
+ };
1347
+ var resolveRefs = (schema) => {
1348
+ const store = createJsonSchemaStore(schema, pluginRefs);
1349
+ return store.getPlainSchema({ skip$Ref: true });
1350
+ };
1351
+
1352
+ // src/lib/schema-table.ts
1353
+ var SchemaTable = class {
1354
+ constructor(schema, refs = {}) {
1355
+ this.refs = refs;
1356
+ this.store = createJsonSchemaStore(schema, refs);
1357
+ }
1358
+ rows = /* @__PURE__ */ new Map();
1359
+ store;
1360
+ applyPatches(patches) {
1361
+ patches.forEach((patch) => {
1362
+ switch (patch.op) {
1363
+ case "replace": {
1364
+ const nextStore = applyReplacePatch(this.store, patch, this.refs);
1365
+ if (nextStore !== this.store) {
1366
+ this.migrateRows(nextStore);
1367
+ }
1368
+ break;
1369
+ }
1370
+ case "remove": {
1371
+ applyRemovePatch(this.store, patch);
1372
+ break;
1373
+ }
1374
+ case "add": {
1375
+ applyAddPatch(this.store, patch, this.refs);
1376
+ break;
1377
+ }
1378
+ case "move": {
1379
+ applyMovePatch(this.store, patch);
1380
+ break;
1381
+ }
1382
+ default:
1383
+ throw new Error(`Unsupported patch operation`);
1384
+ }
1385
+ });
1386
+ }
1387
+ getSchema() {
1388
+ return this.store.getPlainSchema();
1389
+ }
1390
+ addRow(rowId, data) {
1391
+ const row = createJsonValueStore(this.store, rowId, data);
1392
+ this.rows.set(rowId, row);
1393
+ }
1394
+ getRow(id) {
1395
+ const row = this.rows.get(id);
1396
+ if (!row) {
1397
+ throw new Error("Invalid id");
1398
+ }
1399
+ return row.getPlainValue();
1400
+ }
1401
+ getRows() {
1402
+ return [...this.rows].map(([id, data]) => ({
1403
+ id,
1404
+ data: data.getPlainValue()
1405
+ }));
1406
+ }
1407
+ migrateRows(nextStore) {
1408
+ const transformation = getTransformation(this.store, nextStore);
1409
+ if (transformation) {
1410
+ for (const [rowId, row] of this.rows) {
1411
+ const rawNextValue = transformation(
1412
+ row.getPlainValue(),
1413
+ nextStore.default
1414
+ );
1415
+ const nextRow = createJsonValueStore(nextStore, rowId, rawNextValue);
1416
+ this.rows.set(rowId, nextRow);
1417
+ }
1418
+ }
1419
+ this.store = nextStore;
1420
+ }
1421
+ };
1422
+
1423
+ // src/validation-schemas/shared-fields.ts
1424
+ var sharedFields = {
1425
+ deprecated: {
1426
+ type: "boolean"
1427
+ },
1428
+ title: {
1429
+ type: "string"
1430
+ },
1431
+ description: {
1432
+ type: "string"
1433
+ }
1434
+ };
1435
+
1436
+ // src/validation-schemas/meta-schema.ts
1437
+ var refMetaSchema = {
1438
+ type: "object",
1439
+ properties: {
1440
+ ...sharedFields,
1441
+ $ref: {
1442
+ type: "string"
1443
+ }
1444
+ },
1445
+ additionalProperties: false,
1446
+ required: ["$ref"]
1447
+ };
1448
+ var baseStringFields = {
1449
+ type: {
1450
+ const: "string"
1451
+ },
1452
+ default: {
1453
+ type: "string"
1454
+ },
1455
+ readOnly: {
1456
+ type: "boolean"
1457
+ },
1458
+ pattern: {
1459
+ type: "string",
1460
+ format: "regex"
1461
+ },
1462
+ enum: {
1463
+ type: "array",
1464
+ items: { type: "string" },
1465
+ minItems: 1,
1466
+ uniqueItems: true
1467
+ },
1468
+ format: {
1469
+ type: "string",
1470
+ enum: ["date-time", "date", "time", "email", "regex"]
1471
+ },
1472
+ contentMediaType: {
1473
+ type: "string",
1474
+ enum: [
1475
+ "text/plain",
1476
+ "text/markdown",
1477
+ "text/html",
1478
+ "application/json",
1479
+ "application/schema+json",
1480
+ "application/yaml"
1481
+ ]
1482
+ },
1483
+ ...sharedFields
1484
+ };
1485
+ var stringMetaSchema = {
1486
+ type: "object",
1487
+ properties: {
1488
+ ...baseStringFields,
1489
+ foreignKey: {
1490
+ type: "string"
1491
+ }
1492
+ },
1493
+ additionalProperties: false,
1494
+ required: ["type", "default"]
1495
+ };
1496
+ var noForeignKeyStringMetaSchema = {
1497
+ type: "object",
1498
+ properties: {
1499
+ ...baseStringFields
1500
+ },
1501
+ additionalProperties: false,
1502
+ required: ["type", "default"]
1503
+ };
1504
+ var numberMetaSchema = {
1505
+ type: "object",
1506
+ properties: {
1507
+ type: {
1508
+ const: "number"
1509
+ },
1510
+ default: {
1511
+ type: "number"
1512
+ },
1513
+ readOnly: {
1514
+ type: "boolean"
1515
+ },
1516
+ ...sharedFields
1517
+ },
1518
+ additionalProperties: false,
1519
+ required: ["type", "default"]
1520
+ };
1521
+ var booleanMetaSchema = {
1522
+ type: "object",
1523
+ properties: {
1524
+ type: {
1525
+ const: "boolean"
1526
+ },
1527
+ default: {
1528
+ type: "boolean"
1529
+ },
1530
+ readOnly: {
1531
+ type: "boolean"
1532
+ },
1533
+ ...sharedFields
1534
+ },
1535
+ additionalProperties: false,
1536
+ required: ["type", "default"]
1537
+ };
1538
+ var objectMetaSchema = {
1539
+ type: "object",
1540
+ properties: {
1541
+ type: {
1542
+ const: "object"
1543
+ },
1544
+ ...sharedFields,
1545
+ properties: {
1546
+ type: "object",
1547
+ additionalProperties: { $dynamicRef: "#meta" },
1548
+ default: {}
1549
+ },
1550
+ additionalProperties: { const: false },
1551
+ required: { $ref: "#/$defs/stringArray" }
1552
+ },
1553
+ additionalProperties: false,
1554
+ required: ["type", "properties", "additionalProperties", "required"]
1555
+ };
1556
+ var arrayMetaSchema = {
1557
+ type: "object",
1558
+ properties: {
1559
+ type: {
1560
+ const: "array"
1561
+ },
1562
+ ...sharedFields,
1563
+ items: {
1564
+ oneOf: [
1565
+ { $ref: "#/$defs/refSchema" },
1566
+ { $ref: "#/$defs/objectSchema" },
1567
+ { $ref: "#/$defs/arraySchema" },
1568
+ { $ref: "#/$defs/stringSchema" },
1569
+ { $ref: "#/$defs/numberSchema" },
1570
+ { $ref: "#/$defs/booleanSchema" }
1571
+ ]
1572
+ }
1573
+ },
1574
+ additionalProperties: false,
1575
+ required: ["type", "items"]
1576
+ };
1577
+ var metaSchema = {
1578
+ $id: "meta-schema.json",
1579
+ type: "object",
1580
+ $dynamicAnchor: "meta",
1581
+ oneOf: [
1582
+ { $ref: "#/$defs/refSchema" },
1583
+ { $ref: "#/$defs/objectSchema" },
1584
+ { $ref: "#/$defs/arraySchema" },
1585
+ { $ref: "#/$defs/stringSchema" },
1586
+ { $ref: "#/$defs/numberSchema" },
1587
+ { $ref: "#/$defs/booleanSchema" }
1588
+ ],
1589
+ $defs: {
1590
+ stringArray: {
1591
+ type: "array",
1592
+ items: { type: "string" },
1593
+ uniqueItems: true,
1594
+ default: []
1595
+ },
1596
+ refSchema: refMetaSchema,
1597
+ objectSchema: objectMetaSchema,
1598
+ stringSchema: stringMetaSchema,
1599
+ numberSchema: numberMetaSchema,
1600
+ booleanSchema: booleanMetaSchema,
1601
+ arraySchema: arrayMetaSchema
1602
+ }
1603
+ };
1604
+ var notForeignKeyMetaSchema = {
1605
+ type: "object",
1606
+ $dynamicAnchor: "meta",
1607
+ oneOf: [
1608
+ { $ref: "#/$defs/refSchema" },
1609
+ { $ref: "#/$defs/objectSchema" },
1610
+ { $ref: "#/$defs/arraySchema" },
1611
+ { $ref: "#/$defs/stringSchema" },
1612
+ { $ref: "#/$defs/numberSchema" },
1613
+ { $ref: "#/$defs/booleanSchema" }
1614
+ ],
1615
+ $defs: {
1616
+ stringArray: {
1617
+ type: "array",
1618
+ items: { type: "string" },
1619
+ uniqueItems: true,
1620
+ default: []
1621
+ },
1622
+ refSchema: refMetaSchema,
1623
+ objectSchema: objectMetaSchema,
1624
+ stringSchema: noForeignKeyStringMetaSchema,
1625
+ numberSchema: numberMetaSchema,
1626
+ booleanSchema: booleanMetaSchema,
1627
+ arraySchema: arrayMetaSchema
1628
+ }
1629
+ };
1630
+
1631
+ // src/validation-schemas/json-patch-schema.ts
1632
+ var jsonPatchSchema = {
1633
+ $id: "json-patch-schema.json",
1634
+ definitions: {
1635
+ path: {
1636
+ description: "A JSON Pointer path.",
1637
+ type: "string"
1638
+ },
1639
+ objectSchema: objectMetaSchema,
1640
+ stringSchema: stringMetaSchema,
1641
+ numberSchema: numberMetaSchema,
1642
+ booleanSchema: booleanMetaSchema,
1643
+ arraySchema: arrayMetaSchema
1644
+ },
1645
+ minItems: 1,
1646
+ items: {
1647
+ oneOf: [
1648
+ {
1649
+ type: "object",
1650
+ additionalProperties: false,
1651
+ required: ["value", "op", "path"],
1652
+ properties: {
1653
+ path: {
1654
+ $ref: "#/definitions/path"
1655
+ },
1656
+ op: {
1657
+ description: "The operation to perform.",
1658
+ type: "string",
1659
+ enum: ["add", "replace"]
1660
+ },
1661
+ value: {
1662
+ $ref: "meta-schema.json",
1663
+ description: "The value to add, replace or test."
1664
+ }
1665
+ }
1666
+ },
1667
+ {
1668
+ type: "object",
1669
+ additionalProperties: false,
1670
+ required: ["op", "path"],
1671
+ properties: {
1672
+ path: {
1673
+ $ref: "#/definitions/path"
1674
+ },
1675
+ op: {
1676
+ description: "The operation to perform.",
1677
+ type: "string",
1678
+ enum: ["remove"]
1679
+ }
1680
+ }
1681
+ },
1682
+ {
1683
+ type: "object",
1684
+ additionalProperties: false,
1685
+ required: ["from", "op", "path"],
1686
+ properties: {
1687
+ path: {
1688
+ $ref: "#/definitions/path"
1689
+ },
1690
+ op: {
1691
+ description: "The operation to perform.",
1692
+ type: "string",
1693
+ enum: ["move", "copy"]
1694
+ },
1695
+ from: {
1696
+ $ref: "#/definitions/path",
1697
+ description: "A JSON Pointer path pointing to the location to move/copy from."
1698
+ }
1699
+ }
1700
+ }
1701
+ ]
1702
+ },
1703
+ title: "JSON schema for JSONPatch files",
1704
+ type: "array"
1705
+ };
1706
+
1707
+ // src/validation-schemas/history-patches-schema.ts
1708
+ var historyPatchesSchema = {
1709
+ $id: "history-patches-schema.json",
1710
+ type: "array",
1711
+ minItems: 1,
1712
+ items: {
1713
+ type: "object",
1714
+ properties: {
1715
+ patches: {
1716
+ $ref: "json-patch-schema.json"
1717
+ },
1718
+ hash: {
1719
+ type: "string"
1720
+ }
1721
+ },
1722
+ required: ["patches", "hash"]
1723
+ }
1724
+ };
1725
+
1726
+ // src/validation-schemas/table-migrations-schema.ts
1727
+ var tableMigrationsSchema = {
1728
+ $id: "table-migrations-schema.json",
1729
+ oneOf: [
1730
+ { $ref: "#/definitions/InitMigration" },
1731
+ { $ref: "#/definitions/UpdateMigration" },
1732
+ { $ref: "#/definitions/RenameMigration" },
1733
+ { $ref: "#/definitions/RemoveMigration" }
1734
+ ],
1735
+ definitions: {
1736
+ InitMigration: {
1737
+ type: "object",
1738
+ additionalProperties: false,
1739
+ required: ["changeType", "tableId", "hash", "id", "schema"],
1740
+ properties: {
1741
+ changeType: { type: "string", const: "init" },
1742
+ tableId: { type: "string" },
1743
+ hash: { type: "string" },
1744
+ id: { type: "string" },
1745
+ schema: { $ref: "meta-schema.json" }
1746
+ }
1747
+ },
1748
+ UpdateMigration: {
1749
+ type: "object",
1750
+ additionalProperties: false,
1751
+ required: ["changeType", "tableId", "hash", "id", "patches"],
1752
+ properties: {
1753
+ changeType: { type: "string", const: "update" },
1754
+ tableId: { type: "string" },
1755
+ hash: { type: "string" },
1756
+ id: { type: "string" },
1757
+ patches: { $ref: "json-patch-schema.json" }
1758
+ }
1759
+ },
1760
+ RenameMigration: {
1761
+ type: "object",
1762
+ additionalProperties: false,
1763
+ required: ["changeType", "id", "tableId", "nextTableId"],
1764
+ properties: {
1765
+ changeType: { type: "string", const: "rename" },
1766
+ id: { type: "string" },
1767
+ tableId: { type: "string" },
1768
+ nextTableId: { type: "string" }
1769
+ }
1770
+ },
1771
+ RemoveMigration: {
1772
+ type: "object",
1773
+ additionalProperties: false,
1774
+ required: ["changeType", "id", "tableId"],
1775
+ properties: {
1776
+ changeType: { type: "string", const: "remove" },
1777
+ id: { type: "string" },
1778
+ tableId: { type: "string" }
1779
+ }
1780
+ }
1781
+ },
1782
+ title: "JSON Schema for a Single Migration"
1783
+ };
1784
+
1785
+ export { CustomSchemeKeywords, JsonArrayStore, JsonArrayValueStore, JsonBooleanStore, JsonBooleanValueStore, JsonNumberStore, JsonNumberValueStore, JsonObjectStore, JsonObjectValueStore, JsonSchemaTypeName, JsonStringStore, JsonStringValueStore, SchemaTable, SystemSchemaIds, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, addSharedFieldsFromState, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, arrayMetaSchema, baseStringFields, booleanMetaSchema, createJsonArrayValueStore, createJsonObjectSchemaStore, createJsonObjectValueStore, createJsonSchemaStore, createJsonValueStore, createPrimitiveStoreBySchema, createPrimitiveValueStore, equel, fileSchema, fromArrayTransformation, fromBooleanToNumber, fromBooleanToString, fromNumberToBoolean, fromNumberToString, fromStringToBoolean, fromStringToNumber, getAddPatch, getArraySchema, getBooleanSchema, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getMovePatch, getNumberSchema, getObjectSchema, getParentForPath, getPathByStore, getRefSchema, getRemovePatch, getReplacePatch, getStringSchema, getTransformation, historyPatchesSchema, jsonPatchSchema, metaSchema, noForeignKeyStringMetaSchema, notForeignKeyMetaSchema, numberMetaSchema, objectMetaSchema, pluginRefs, refMetaSchema, replaceForeignKeyValue, resolveRefs, rowCreatedAtSchema, rowCreatedIdSchema, rowHashSchema, rowIdSchema, rowPublishedAtSchema, rowSchemaHashSchema, rowUpdatedAtSchema, rowVersionIdSchema, saveSharedFields, sharedFields, stringMetaSchema, tableMigrationsSchema, toArrayTransformation, traverseStore, traverseValue, validateJsonFieldName };
1786
+ //# sourceMappingURL=index.js.map
1787
+ //# sourceMappingURL=index.js.map