@revisium/schema-toolkit 0.5.0 → 0.10.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 (72) hide show
  1. package/README.md +34 -156
  2. package/dist/{chunk-7LKKRGZG.js → chunk-4BIANIHT.js} +226 -129
  3. package/dist/chunk-4BIANIHT.js.map +1 -0
  4. package/dist/{chunk-V2SSSD5X.cjs → chunk-B22UU3JK.cjs} +2 -2
  5. package/dist/chunk-B22UU3JK.cjs.map +1 -0
  6. package/dist/{chunk-VGTHMWJJ.cjs → chunk-ECWBPKDQ.cjs} +15 -2
  7. package/dist/chunk-ECWBPKDQ.cjs.map +1 -0
  8. package/dist/{chunk-ASXCDIN2.js → chunk-EIDMKNH4.js} +15 -3
  9. package/dist/chunk-EIDMKNH4.js.map +1 -0
  10. package/dist/chunk-GF3XXYOS.cjs +13 -0
  11. package/dist/chunk-GF3XXYOS.cjs.map +1 -0
  12. package/dist/{chunk-WCFA4226.js → chunk-HY4VE7H2.js} +11 -5
  13. package/dist/chunk-HY4VE7H2.js.map +1 -0
  14. package/dist/chunk-KJCURW4D.js +11 -0
  15. package/dist/chunk-KJCURW4D.js.map +1 -0
  16. package/dist/{chunk-ZXLLJAMQ.js → chunk-L5MLJEC7.js} +2 -2
  17. package/dist/chunk-L5MLJEC7.js.map +1 -0
  18. package/dist/chunk-SBJFTOWW.js +3 -0
  19. package/dist/{chunk-DEPJRTVT.js.map → chunk-SBJFTOWW.js.map} +1 -1
  20. package/dist/{chunk-JLIZ7QT2.cjs → chunk-TMWPWGUN.cjs} +11 -5
  21. package/dist/chunk-TMWPWGUN.cjs.map +1 -0
  22. package/dist/{chunk-EJAKPKHS.cjs → chunk-UJBE43WI.cjs} +234 -136
  23. package/dist/chunk-UJBE43WI.cjs.map +1 -0
  24. package/dist/chunk-XAFOMEOK.cjs +4 -0
  25. package/dist/{chunk-FD57PCAC.cjs.map → chunk-XAFOMEOK.cjs.map} +1 -1
  26. package/dist/index.cjs +94 -81
  27. package/dist/index.d.cts +6 -5
  28. package/dist/index.d.ts +6 -5
  29. package/dist/index.js +6 -5
  30. package/dist/{json-patch.types-DiJBqfxV.d.ts → json-patch.types-CYt9TCu3.d.ts} +1 -1
  31. package/dist/{json-patch.types-lByaF-OL.d.cts → json-patch.types-DUcWbQRY.d.cts} +1 -1
  32. package/dist/{json-string.store-BNF1Y93E.d.cts → json-string.store-CcZ197KO.d.cts} +4 -1
  33. package/dist/{json-string.store-qHcWNv07.d.ts → json-string.store-DckDcMVN.d.ts} +4 -1
  34. package/dist/lib/index.cjs +45 -40
  35. package/dist/lib/index.d.cts +7 -4
  36. package/dist/lib/index.d.ts +7 -4
  37. package/dist/lib/index.js +4 -3
  38. package/dist/mocks/index.cjs +1 -1
  39. package/dist/mocks/index.d.cts +2 -2
  40. package/dist/mocks/index.d.ts +2 -2
  41. package/dist/mocks/index.js +1 -1
  42. package/dist/model/index.cjs +26 -26
  43. package/dist/model/index.d.cts +3 -3
  44. package/dist/model/index.d.ts +3 -3
  45. package/dist/model/index.js +2 -2
  46. package/dist/plugins/index.cjs +1 -1
  47. package/dist/plugins/index.d.cts +9 -1
  48. package/dist/plugins/index.d.ts +9 -1
  49. package/dist/plugins/index.js +1 -1
  50. package/dist/{schema.types-Q3MYTp8z.d.cts → schema.types-CLHMJmkJ.d.cts} +8 -1
  51. package/dist/{schema.types-Q3MYTp8z.d.ts → schema.types-CLHMJmkJ.d.ts} +8 -1
  52. package/dist/types/index.cjs +8 -3
  53. package/dist/types/index.d.cts +5 -4
  54. package/dist/types/index.d.ts +5 -4
  55. package/dist/types/index.js +3 -2
  56. package/dist/validation-schemas/index.cjs +19 -15
  57. package/dist/validation-schemas/index.d.cts +2 -1
  58. package/dist/validation-schemas/index.d.ts +2 -1
  59. package/dist/validation-schemas/index.js +1 -1
  60. package/dist/value-diff.types-BRoI_kGE.d.cts +13 -0
  61. package/dist/value-diff.types-BRoI_kGE.d.ts +13 -0
  62. package/package.json +2 -2
  63. package/dist/chunk-7LKKRGZG.js.map +0 -1
  64. package/dist/chunk-ASXCDIN2.js.map +0 -1
  65. package/dist/chunk-DEPJRTVT.js +0 -3
  66. package/dist/chunk-EJAKPKHS.cjs.map +0 -1
  67. package/dist/chunk-FD57PCAC.cjs +0 -4
  68. package/dist/chunk-JLIZ7QT2.cjs.map +0 -1
  69. package/dist/chunk-V2SSSD5X.cjs.map +0 -1
  70. package/dist/chunk-VGTHMWJJ.cjs.map +0 -1
  71. package/dist/chunk-WCFA4226.js.map +0 -1
  72. package/dist/chunk-ZXLLJAMQ.js.map +0 -1
package/README.md CHANGED
@@ -4,181 +4,59 @@
4
4
 
5
5
  [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=revisium_schema-toolkit&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=revisium_schema-toolkit)
6
6
  [![codecov](https://codecov.io/gh/revisium/schema-toolkit/graph/badge.svg?token=OLFXI79CN3)](https://codecov.io/gh/revisium/schema-toolkit)
7
- [![GitHub License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/revisium/prisma-pg-json/blob/master/LICENSE)
7
+ [![GitHub License](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/revisium/schema-toolkit/blob/master/LICENSE)
8
8
  [![GitHub Release](https://img.shields.io/github/v/release/revisium/schema-toolkit)](https://github.com/revisium/schema-toolkit/releases)
9
9
 
10
- **Universal TypeScript toolkit for JSON Schema definitions used across the Revisium ecosystem.**
10
+ Framework-agnostic TypeScript types, system schemas, runtime stores, and utilities for working with JSON Schema in [Revisium](https://revisium.io) projects.
11
11
 
12
12
  </div>
13
13
 
14
- ## Overview
15
-
16
- `@revisium/schema-toolkit` provides framework-agnostic TypeScript types, system schemas, runtime stores, utilities, and testing helpers for working with JSON Schema in Revisium projects. This package contains shared code used by `revisium-core`, `revisium-endpoint`, and `revisium-admin`.
17
-
18
- ## Features
19
-
20
- - 🎯 **TypeScript Types** - Complete type definitions for JSON Schema, JSON Patch, JSON Value Patch, and migrations
21
- - 🔧 **System Schemas** - Pre-defined schemas for Revisium system fields (row ID, timestamps, file handling, etc.)
22
- - 🏪 **Runtime Stores** - Schema and value store classes for runtime manipulation
23
- - 🛠️ **Utilities** - 18+ utility functions for schema operations, patches, foreign keys, and more
24
- - 🧪 **Test Utilities** - Helper functions for generating mock schemas in tests
25
- - ✅ **Validation Schemas** - Ajv-compatible validation schemas (optional)
26
- - 🌐 **Framework Agnostic** - No dependencies on backend/frontend frameworks (only `nanoid` for ID generation)
27
- - 📦 **Tree-shakeable** - Use only what you need with ES modules
28
-
29
14
  ## Installation
30
15
 
31
16
  ```bash
32
17
  npm install @revisium/schema-toolkit
33
18
  ```
34
19
 
35
- ## Usage
20
+ ## API
36
21
 
37
- ### TypeScript Types
22
+ ### Schema
38
23
 
39
- ```typescript
40
- import {
41
- JsonSchema,
42
- JsonObjectSchema,
43
- JsonSchemaTypeName,
44
- } from '@revisium/schema-toolkit/types';
24
+ | Function | Description |
25
+ |----------|-------------|
26
+ | `createJsonSchemaStore` | Create runtime schema store |
27
+ | `getJsonSchemaStoreByPath` | Navigate schema by path |
28
+ | `applyPatches` | Apply JSON Patch operations to schema |
29
+ | `resolveRefs` | Resolve $ref to inline schemas |
30
+ | `validateJsonFieldName` | Validate field name format |
31
+ | `getInvalidFieldNamesInSchema` | Find invalid field names in schema |
45
32
 
46
- const userSchema: JsonObjectSchema = {
47
- type: JsonSchemaTypeName.Object,
48
- properties: {
49
- name: { type: JsonSchemaTypeName.String, default: '' },
50
- age: { type: JsonSchemaTypeName.Number, default: 0 },
51
- },
52
- };
53
- ```
33
+ ### Value
54
34
 
55
- ### System Schemas
56
-
57
- ```typescript
58
- import {
59
- rowIdSchema,
60
- rowCreatedAtSchema,
61
- fileSchema,
62
- } from '@revisium/schema-toolkit/plugins';
63
- ```
64
-
65
- ### Model Stores
66
-
67
- ```typescript
68
- import {
69
- createJsonSchemaStore,
70
- createJsonValueStore,
71
- } from '@revisium/schema-toolkit/lib';
72
-
73
- // Create schema store
74
- const schemaStore = createJsonSchemaStore({
75
- type: 'object',
76
- properties: {
77
- name: { type: 'string', default: '' },
78
- },
79
- });
80
-
81
- // Create value store
82
- const valueStore = createJsonValueStore(
83
- schemaStore,
84
- 'row-id-123',
85
- { name: 'John' }
86
- );
87
- ```
35
+ | Function | Description |
36
+ |----------|-------------|
37
+ | `createJsonValueStore` | Create runtime value store |
38
+ | `getJsonValueByPath` | Navigate value by path |
39
+ | `computeValueDiff` | Compute field-level diff between two values |
40
+ | `traverseValue` | Traverse value tree |
88
41
 
89
- ### Utilities
42
+ ### Foreign Keys
90
43
 
91
- ```typescript
92
- import {
93
- applyPatches,
94
- getForeignKeysFromSchema,
95
- resolveRefs,
96
- } from '@revisium/schema-toolkit/lib';
97
-
98
- // Apply JSON patches to schema
99
- const updatedSchema = applyPatches(schema, patches);
100
-
101
- // Get foreign keys from schema
102
- const foreignKeys = getForeignKeysFromSchema(schemaStore);
103
-
104
- // Resolve $ref in schemas (endpoint feature)
105
- const resolved = resolveRefs(schemaWithRefs);
106
- ```
107
-
108
- ### Test Utilities
109
-
110
- ```typescript
111
- import {
112
- getStringSchema,
113
- getObjectSchema,
114
- getRefSchema,
115
- } from '@revisium/schema-toolkit/mocks';
116
-
117
- const mockSchema = getObjectSchema({
118
- name: getStringSchema(),
119
- userId: getStringSchema({ foreignKey: 'User' }),
120
- avatar: getRefSchema('File'),
121
- });
122
- ```
44
+ | Function | Description |
45
+ |----------|-------------|
46
+ | `getForeignKeysFromSchema` | Extract foreign keys from schema |
47
+ | `getForeignKeysFromValue` | Extract foreign key values from data |
48
+ | `getForeignKeyPatchesFromSchema` | Get patches for foreign key changes |
49
+ | `replaceForeignKeyValue` | Replace foreign key references |
123
50
 
124
- ## API Reference
51
+ ### Path Utils
125
52
 
126
- ### Entry Points
127
-
128
- - `@revisium/schema-toolkit` - All exports (convenience)
129
- - `@revisium/schema-toolkit/types` - TypeScript types only
130
- - `@revisium/schema-toolkit/plugins` - System schemas
131
- - `@revisium/schema-toolkit/mocks` - Test utilities
132
- - `@revisium/schema-toolkit/consts` - Constants (CustomSchemeKeywords, SystemSchemaIds)
133
- - `@revisium/schema-toolkit/model` - Schema and value stores
134
- - `@revisium/schema-toolkit/lib` - Utility functions
135
- - `@revisium/schema-toolkit/validation-schemas` - Ajv validation schemas
136
-
137
- ### Key Utilities
138
-
139
- **Schema Operations:**
140
- - `createJsonSchemaStore(schema)` - Create runtime schema store
141
- - `getJsonSchemaStoreByPath(store, path)` - Navigate schema by JSON pointer
142
- - `applyPatches(schema, patches)` - Apply JSON Patch operations
143
-
144
- **Value Operations:**
145
- - `createJsonValueStore(schema, rowId, data)` - Create runtime value store
146
- - `getJsonValueStoreByPath(store, path)` - Navigate value by JSON pointer
147
-
148
- **Foreign Keys:**
149
- - `getForeignKeysFromSchema(store)` - Extract foreign keys from schema
150
- - `getForeignKeysFromValue(store)` - Extract foreign key values from data
151
- - `replaceForeignKeyValue(store, fromId, toId)` - Replace foreign key references
152
-
153
- **Other:**
154
- - `resolveRefs(schema)` - Resolve $ref to inline schemas
155
- - `validateJsonFieldName(name)` - Validate field name format
156
-
157
- ## Development
158
-
159
- ```bash
160
- # Install dependencies
161
- npm install
162
-
163
- # Build
164
- npm run build
165
-
166
- # Run tests
167
- npm test
168
-
169
- # Run tests with coverage
170
- npm run test:cov
171
-
172
- # Lint
173
- npm run lint:ci
174
-
175
- # Format
176
- npm run format
177
-
178
- # Type check
179
- npm run tsc
180
- ```
53
+ | Function | Description |
54
+ |----------|-------------|
55
+ | `parsePath` | Parse dot-notation path to segments |
56
+ | `getParentForPath` | Get parent path |
57
+ | `getPathByStore` | Get path from store |
58
+ | `deepEqual` | Deep equality comparison |
181
59
 
182
60
  ## License
183
61
 
184
- MIT
62
+ MIT
@@ -1,5 +1,5 @@
1
1
  import { fileSchema, rowSchemaHashSchema, rowHashSchema, rowUpdatedAtSchema, rowPublishedAtSchema, rowCreatedAtSchema, rowCreatedIdSchema, rowVersionIdSchema, rowIdSchema } from './chunk-LLHQWDAR.js';
2
- import { JsonArrayStore, JsonObjectStore, JsonStringStore, JsonNumberStore, JsonBooleanStore, JsonObjectValueStore, JsonArrayValueStore, createJsonValueStore, getTransformation } from './chunk-WCFA4226.js';
2
+ import { JsonArrayStore, JsonObjectStore, JsonStringStore, JsonNumberStore, JsonBooleanStore, JsonObjectValueStore, JsonArrayValueStore, createJsonValueStore, getTransformation } from './chunk-HY4VE7H2.js';
3
3
 
4
4
  // src/lib/createJsonSchemaStore.ts
5
5
  var createJsonSchemaStore = (schema, refs = {}) => {
@@ -50,11 +50,16 @@ var createPrimitiveStoreBySchema = (schema) => {
50
50
  stringStore.enum = schema.enum;
51
51
  stringStore.contentMediaType = schema.contentMediaType;
52
52
  stringStore.pattern = schema.pattern;
53
+ stringStore["x-formula"] = schema["x-formula"];
53
54
  return stringStore;
54
55
  } else if (schema.type === "number" /* Number */) {
55
- return new JsonNumberStore();
56
+ const numberStore = new JsonNumberStore();
57
+ numberStore["x-formula"] = schema["x-formula"];
58
+ return numberStore;
56
59
  } else if (schema.type === "boolean" /* Boolean */) {
57
- return new JsonBooleanStore();
60
+ const booleanStore = new JsonBooleanStore();
61
+ booleanStore["x-formula"] = schema["x-formula"];
62
+ return booleanStore;
58
63
  } else {
59
64
  throw new Error("this type is not allowed");
60
65
  }
@@ -248,129 +253,6 @@ var applyMovePatch = (store, patch) => {
248
253
  throw new Error('Invalid type of "to" parent');
249
254
  };
250
255
 
251
- // src/lib/getDBJsonPathByJsonSchemaStore.ts
252
- var getDBJsonPathByJsonSchemaStore = (store) => {
253
- let node = store;
254
- let path = "";
255
- while (node.parent) {
256
- if (node.parent.type === "object" /* Object */) {
257
- path = `.${node.name}${path}`;
258
- } else if (node.parent.type === "array" /* Array */) {
259
- path = `[*]${path}`;
260
- }
261
- node = node.parent;
262
- }
263
- if (!path) {
264
- return "$";
265
- }
266
- return `$${path}`;
267
- };
268
-
269
- // src/lib/getPathByStore.ts
270
- var getPathByStore = (store) => {
271
- let node = store;
272
- let path = "";
273
- while (node.parent) {
274
- if (node.parent.type === "object" /* Object */) {
275
- path = `/properties/${node.name}${path}`;
276
- } else if (node.parent.type === "array" /* Array */) {
277
- path = `/items${path}`;
278
- }
279
- node = node.parent;
280
- }
281
- if (!path) {
282
- return "/";
283
- }
284
- return `${path}`;
285
- };
286
-
287
- // src/lib/traverseStore.ts
288
- var traverseStore = (store, callback) => {
289
- callback(store);
290
- if (store.type === "object" /* Object */) {
291
- Object.values(store.properties).forEach((item) => {
292
- traverseStore(item, callback);
293
- });
294
- } else if (store.type === "array" /* Array */) {
295
- traverseStore(store.items, callback);
296
- }
297
- };
298
-
299
- // src/lib/getForeignKeyPatchesFromSchema.ts
300
- var getForeignKeyPatchesFromSchema = (store, options) => {
301
- const stores = [];
302
- traverseStore(store, (item) => {
303
- if (item.type === "string" /* String */ && item.foreignKey === options.tableId) {
304
- item.foreignKey = options.nextTableId;
305
- const patch = {
306
- op: "replace",
307
- path: getPathByStore(item),
308
- value: item.getPlainSchema()
309
- };
310
- stores.push(patch);
311
- }
312
- });
313
- return stores;
314
- };
315
-
316
- // src/lib/getForeignKeysFromSchema.ts
317
- var getForeignKeysFromSchema = (store) => {
318
- const foreignKeys = /* @__PURE__ */ new Set();
319
- traverseStore(store, (item) => {
320
- if (item.type === "string" /* String */ && item.foreignKey) {
321
- foreignKeys.add(item.foreignKey);
322
- }
323
- });
324
- return [...foreignKeys].sort((a, b) => a.localeCompare(b));
325
- };
326
-
327
- // src/lib/traverseValue.ts
328
- var traverseValue = (store, callback) => {
329
- callback(store);
330
- if (store.type === "object" /* Object */) {
331
- Object.values(store.value).forEach((item) => {
332
- traverseValue(item, callback);
333
- });
334
- } else if (store.type === "array" /* Array */) {
335
- store.value.forEach((itemValue) => {
336
- traverseValue(itemValue, callback);
337
- });
338
- }
339
- };
340
-
341
- // src/lib/getForeignKeysFromValue.ts
342
- var getForeignKeysFromValue = (value) => {
343
- const foreignKeys = /* @__PURE__ */ new Map();
344
- traverseValue(value, (item) => {
345
- if (item.type === "string" /* String */ && item.foreignKey) {
346
- let tableForeignKey = foreignKeys.get(item.foreignKey);
347
- if (!tableForeignKey) {
348
- tableForeignKey = /* @__PURE__ */ new Set();
349
- foreignKeys.set(item.foreignKey, tableForeignKey);
350
- }
351
- tableForeignKey.add(item.getPlainValue());
352
- }
353
- });
354
- return [...foreignKeys].map(([tableId, rowIds]) => ({
355
- tableId,
356
- rowIds: [...rowIds].sort((a, b) => a.localeCompare(b))
357
- }));
358
- };
359
-
360
- // src/lib/getInvalidFieldNamesInSchema.ts
361
- var getInvalidFieldNamesInSchema = (schema, refs = {}) => {
362
- const schemaStore = createJsonSchemaStore(schema, refs);
363
- const invalidFields = [];
364
- traverseStore(schemaStore, (item) => {
365
- if (item.parent?.type === "object" /* Object */) {
366
- if (!validateJsonFieldName(item.name)) {
367
- invalidFields.push(item);
368
- }
369
- }
370
- });
371
- return invalidFields;
372
- };
373
-
374
256
  // src/lib/json-path-utils.ts
375
257
  function parsePathSegments(path) {
376
258
  const segments = [];
@@ -595,6 +477,221 @@ function convertSchemaPathToJsonPath(schemaPath) {
595
477
  return result;
596
478
  }
597
479
 
480
+ // src/lib/computeValueDiff.ts
481
+ function computeValueDiff(fromValue, toValue) {
482
+ if (fromValue === null && toValue === null) {
483
+ return [];
484
+ }
485
+ if (fromValue === null && toValue !== null) {
486
+ return [createChange("", null, toValue, "ADDED" /* Added */)];
487
+ }
488
+ if (fromValue !== null && toValue === null) {
489
+ return [createChange("", fromValue, null, "REMOVED" /* Removed */)];
490
+ }
491
+ return computeDiff(fromValue, toValue, "");
492
+ }
493
+ function computeDiff(fromValue, toValue, path) {
494
+ if (deepEqual(fromValue, toValue)) {
495
+ return [];
496
+ }
497
+ if (isPrimitive(fromValue) || isPrimitive(toValue)) {
498
+ return [createChange(path, fromValue, toValue, "MODIFIED" /* Modified */)];
499
+ }
500
+ if (hasTypeChanged(fromValue, toValue)) {
501
+ return [createChange(path, fromValue, toValue, "MODIFIED" /* Modified */)];
502
+ }
503
+ if (Array.isArray(fromValue) && Array.isArray(toValue)) {
504
+ return computeArrayDiff(fromValue, toValue, path);
505
+ }
506
+ return computeObjectDiff(
507
+ fromValue,
508
+ toValue,
509
+ path
510
+ );
511
+ }
512
+ function computeObjectDiff(fromObj, toObj, basePath) {
513
+ const changes = [];
514
+ const fromKeys = Object.keys(fromObj);
515
+ const toKeys = new Set(Object.keys(toObj));
516
+ for (const key of fromKeys) {
517
+ const fieldPath = joinPath(basePath, key);
518
+ if (!toKeys.has(key)) {
519
+ changes.push(
520
+ createChange(fieldPath, fromObj[key], null, "REMOVED" /* Removed */)
521
+ );
522
+ } else {
523
+ changes.push(...computeDiff(fromObj[key], toObj[key], fieldPath));
524
+ }
525
+ }
526
+ for (const key of toKeys) {
527
+ if (!fromKeys.includes(key)) {
528
+ const fieldPath = joinPath(basePath, key);
529
+ changes.push(
530
+ createChange(fieldPath, null, toObj[key], "ADDED" /* Added */)
531
+ );
532
+ }
533
+ }
534
+ return changes;
535
+ }
536
+ function computeArrayDiff(fromArr, toArr, basePath) {
537
+ const changes = [];
538
+ const maxLen = Math.max(fromArr.length, toArr.length);
539
+ for (let i = 0; i < maxLen; i++) {
540
+ const fieldPath = joinPath(basePath, String(i));
541
+ const hasFrom = i < fromArr.length;
542
+ const hasTo = i < toArr.length;
543
+ if (hasFrom && hasTo) {
544
+ changes.push(...computeDiff(fromArr[i], toArr[i], fieldPath));
545
+ } else if (hasFrom) {
546
+ changes.push(
547
+ createChange(fieldPath, fromArr[i], null, "REMOVED" /* Removed */)
548
+ );
549
+ } else {
550
+ changes.push(
551
+ createChange(fieldPath, null, toArr[i], "ADDED" /* Added */)
552
+ );
553
+ }
554
+ }
555
+ return changes;
556
+ }
557
+ function isPrimitive(value) {
558
+ return typeof value === "string" || typeof value === "number" || typeof value === "boolean";
559
+ }
560
+ function hasTypeChanged(fromValue, toValue) {
561
+ const fromIsArray = Array.isArray(fromValue);
562
+ const toIsArray = Array.isArray(toValue);
563
+ return fromIsArray !== toIsArray;
564
+ }
565
+ function joinPath(base, segment) {
566
+ return base ? `${base}.${segment}` : segment;
567
+ }
568
+ function createChange(path, oldValue, newValue, changeType) {
569
+ return { path, oldValue, newValue, changeType };
570
+ }
571
+
572
+ // src/lib/getDBJsonPathByJsonSchemaStore.ts
573
+ var getDBJsonPathByJsonSchemaStore = (store) => {
574
+ let node = store;
575
+ let path = "";
576
+ while (node.parent) {
577
+ if (node.parent.type === "object" /* Object */) {
578
+ path = `.${node.name}${path}`;
579
+ } else if (node.parent.type === "array" /* Array */) {
580
+ path = `[*]${path}`;
581
+ }
582
+ node = node.parent;
583
+ }
584
+ if (!path) {
585
+ return "$";
586
+ }
587
+ return `$${path}`;
588
+ };
589
+
590
+ // src/lib/getPathByStore.ts
591
+ var getPathByStore = (store) => {
592
+ let node = store;
593
+ let path = "";
594
+ while (node.parent) {
595
+ if (node.parent.type === "object" /* Object */) {
596
+ path = `/properties/${node.name}${path}`;
597
+ } else if (node.parent.type === "array" /* Array */) {
598
+ path = `/items${path}`;
599
+ }
600
+ node = node.parent;
601
+ }
602
+ if (!path) {
603
+ return "/";
604
+ }
605
+ return `${path}`;
606
+ };
607
+
608
+ // src/lib/traverseStore.ts
609
+ var traverseStore = (store, callback) => {
610
+ callback(store);
611
+ if (store.type === "object" /* Object */) {
612
+ Object.values(store.properties).forEach((item) => {
613
+ traverseStore(item, callback);
614
+ });
615
+ } else if (store.type === "array" /* Array */) {
616
+ traverseStore(store.items, callback);
617
+ }
618
+ };
619
+
620
+ // src/lib/getForeignKeyPatchesFromSchema.ts
621
+ var getForeignKeyPatchesFromSchema = (store, options) => {
622
+ const stores = [];
623
+ traverseStore(store, (item) => {
624
+ if (item.type === "string" /* String */ && item.foreignKey === options.tableId) {
625
+ item.foreignKey = options.nextTableId;
626
+ const patch = {
627
+ op: "replace",
628
+ path: getPathByStore(item),
629
+ value: item.getPlainSchema()
630
+ };
631
+ stores.push(patch);
632
+ }
633
+ });
634
+ return stores;
635
+ };
636
+
637
+ // src/lib/getForeignKeysFromSchema.ts
638
+ var getForeignKeysFromSchema = (store) => {
639
+ const foreignKeys = /* @__PURE__ */ new Set();
640
+ traverseStore(store, (item) => {
641
+ if (item.type === "string" /* String */ && item.foreignKey) {
642
+ foreignKeys.add(item.foreignKey);
643
+ }
644
+ });
645
+ return [...foreignKeys].sort((a, b) => a.localeCompare(b));
646
+ };
647
+
648
+ // src/lib/traverseValue.ts
649
+ var traverseValue = (store, callback) => {
650
+ callback(store);
651
+ if (store.type === "object" /* Object */) {
652
+ Object.values(store.value).forEach((item) => {
653
+ traverseValue(item, callback);
654
+ });
655
+ } else if (store.type === "array" /* Array */) {
656
+ store.value.forEach((itemValue) => {
657
+ traverseValue(itemValue, callback);
658
+ });
659
+ }
660
+ };
661
+
662
+ // src/lib/getForeignKeysFromValue.ts
663
+ var getForeignKeysFromValue = (value) => {
664
+ const foreignKeys = /* @__PURE__ */ new Map();
665
+ traverseValue(value, (item) => {
666
+ if (item.type === "string" /* String */ && item.foreignKey) {
667
+ let tableForeignKey = foreignKeys.get(item.foreignKey);
668
+ if (!tableForeignKey) {
669
+ tableForeignKey = /* @__PURE__ */ new Set();
670
+ foreignKeys.set(item.foreignKey, tableForeignKey);
671
+ }
672
+ tableForeignKey.add(item.getPlainValue());
673
+ }
674
+ });
675
+ return [...foreignKeys].map(([tableId, rowIds]) => ({
676
+ tableId,
677
+ rowIds: [...rowIds].sort((a, b) => a.localeCompare(b))
678
+ }));
679
+ };
680
+
681
+ // src/lib/getInvalidFieldNamesInSchema.ts
682
+ var getInvalidFieldNamesInSchema = (schema, refs = {}) => {
683
+ const schemaStore = createJsonSchemaStore(schema, refs);
684
+ const invalidFields = [];
685
+ traverseStore(schemaStore, (item) => {
686
+ if (item.parent?.type === "object" /* Object */) {
687
+ if (!validateJsonFieldName(item.name)) {
688
+ invalidFields.push(item);
689
+ }
690
+ }
691
+ });
692
+ return invalidFields;
693
+ };
694
+
598
695
  // src/lib/getJsonValueByPath.ts
599
696
  var getJsonValueStoreByPath = (root, path) => {
600
697
  if (!path) {
@@ -725,6 +822,6 @@ var SchemaTable = class {
725
822
  }
726
823
  };
727
824
 
728
- export { SchemaTable, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, convertJsonPathToSchemaPath, convertSchemaPathToJsonPath, createJsonObjectSchemaStore, createJsonSchemaStore, createPrimitiveStoreBySchema, deepEqual, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getParentForPath, getPathByStore, getValueByPath, hasPath, parsePath, pluginRefs, replaceForeignKeyValue, resolveRefs, saveSharedFields, setValueByPath, traverseStore, traverseValue, validateJsonFieldName };
729
- //# sourceMappingURL=chunk-7LKKRGZG.js.map
730
- //# sourceMappingURL=chunk-7LKKRGZG.js.map
825
+ export { SchemaTable, VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE, applyAddPatch, applyMovePatch, applyRemovePatch, applyReplacePatch, computeValueDiff, convertJsonPathToSchemaPath, convertSchemaPathToJsonPath, createJsonObjectSchemaStore, createJsonSchemaStore, createPrimitiveStoreBySchema, deepEqual, getDBJsonPathByJsonSchemaStore, getForeignKeyPatchesFromSchema, getForeignKeysFromSchema, getForeignKeysFromValue, getInvalidFieldNamesInSchema, getJsonSchemaStoreByPath, getJsonValueStoreByPath, getParentForPath, getPathByStore, getValueByPath, hasPath, parsePath, pluginRefs, replaceForeignKeyValue, resolveRefs, saveSharedFields, setValueByPath, traverseStore, traverseValue, validateJsonFieldName };
826
+ //# sourceMappingURL=chunk-4BIANIHT.js.map
827
+ //# sourceMappingURL=chunk-4BIANIHT.js.map