@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.
- package/LICENSE +21 -0
- package/README.md +184 -0
- package/dist/consts/index.cjs +26 -0
- package/dist/consts/index.cjs.map +1 -0
- package/dist/consts/index.d.cts +17 -0
- package/dist/consts/index.d.ts +17 -0
- package/dist/consts/index.js +23 -0
- package/dist/consts/index.js.map +1 -0
- package/dist/index.cjs +1874 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1787 -0
- package/dist/index.js.map +1 -0
- package/dist/json-patch.types-DiJBqfxV.d.ts +28 -0
- package/dist/json-patch.types-lByaF-OL.d.cts +28 -0
- package/dist/json-string.store-O1J0j-a1.d.cts +228 -0
- package/dist/json-string.store-S9FXT39Q.d.ts +228 -0
- package/dist/json.types-46Cq-WxU.d.cts +8 -0
- package/dist/json.types-46Cq-WxU.d.ts +8 -0
- package/dist/lib/index.cjs +1346 -0
- package/dist/lib/index.cjs.map +1 -0
- package/dist/lib/index.d.cts +91 -0
- package/dist/lib/index.d.ts +91 -0
- package/dist/lib/index.js +1315 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/mocks/index.cjs +92 -0
- package/dist/mocks/index.cjs.map +1 -0
- package/dist/mocks/index.d.cts +26 -0
- package/dist/mocks/index.d.ts +26 -0
- package/dist/mocks/index.js +81 -0
- package/dist/mocks/index.js.map +1 -0
- package/dist/model/index.cjs +718 -0
- package/dist/model/index.cjs.map +1 -0
- package/dist/model/index.d.cts +19 -0
- package/dist/model/index.d.ts +19 -0
- package/dist/model/index.js +697 -0
- package/dist/model/index.js.map +1 -0
- package/dist/plugins/index.cjs +122 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.cts +21 -0
- package/dist/plugins/index.d.ts +21 -0
- package/dist/plugins/index.js +112 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/schema.types-Q3MYTp8z.d.cts +68 -0
- package/dist/schema.types-Q3MYTp8z.d.ts +68 -0
- package/dist/types/index.cjs +15 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +56 -0
- package/dist/types/index.d.ts +56 -0
- package/dist/types/index.js +13 -0
- package/dist/types/index.js.map +1 -0
- package/dist/validation-schemas/index.cjs +380 -0
- package/dist/validation-schemas/index.cjs.map +1 -0
- package/dist/validation-schemas/index.d.cts +32 -0
- package/dist/validation-schemas/index.d.ts +32 -0
- package/dist/validation-schemas/index.js +365 -0
- package/dist/validation-schemas/index.js.map +1 -0
- package/package.json +169 -0
|
@@ -0,0 +1,1346 @@
|
|
|
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 JsonArrayStore = class {
|
|
20
|
+
constructor(_items, nodeId = nanoid.nanoid()) {
|
|
21
|
+
this._items = _items;
|
|
22
|
+
this.nodeId = nodeId;
|
|
23
|
+
this.items.parent = this;
|
|
24
|
+
}
|
|
25
|
+
type = "array" /* Array */;
|
|
26
|
+
$ref = "";
|
|
27
|
+
name = "";
|
|
28
|
+
parent = null;
|
|
29
|
+
default = [];
|
|
30
|
+
title;
|
|
31
|
+
description;
|
|
32
|
+
deprecated;
|
|
33
|
+
valuesMap = /* @__PURE__ */ new Map();
|
|
34
|
+
get items() {
|
|
35
|
+
return this._items;
|
|
36
|
+
}
|
|
37
|
+
registerValue(value) {
|
|
38
|
+
const length = this.getOrCreateValues(value.rowId).push(value);
|
|
39
|
+
return length - 1;
|
|
40
|
+
}
|
|
41
|
+
getValue(rowId, index = 0) {
|
|
42
|
+
return this.getOrCreateValues(rowId)[index];
|
|
43
|
+
}
|
|
44
|
+
migrateItems(items) {
|
|
45
|
+
const previousItems = this._items;
|
|
46
|
+
previousItems.parent = null;
|
|
47
|
+
this._items = items;
|
|
48
|
+
this._items.parent = this;
|
|
49
|
+
const event = { items, previousItems };
|
|
50
|
+
for (const value of this.iterValues()) {
|
|
51
|
+
value.migrateItems(event);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
replaceItems(items) {
|
|
55
|
+
const previousItems = this._items;
|
|
56
|
+
previousItems.parent = null;
|
|
57
|
+
this._items = items;
|
|
58
|
+
this._items.parent = this;
|
|
59
|
+
const event = { items, previousItems };
|
|
60
|
+
for (const value of this.iterValues()) {
|
|
61
|
+
value.replaceItems(event);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
getPlainSchema(options) {
|
|
65
|
+
if (this.$ref && options?.skip$Ref !== true) {
|
|
66
|
+
return addSharedFieldsFromState({ $ref: this.$ref }, this);
|
|
67
|
+
}
|
|
68
|
+
return addSharedFieldsFromState(
|
|
69
|
+
{
|
|
70
|
+
type: this.type,
|
|
71
|
+
items: this.items.getPlainSchema(options)
|
|
72
|
+
},
|
|
73
|
+
this
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
getOrCreateValues(rowId) {
|
|
77
|
+
let values = this.valuesMap.get(rowId);
|
|
78
|
+
if (!values) {
|
|
79
|
+
values = [];
|
|
80
|
+
this.valuesMap.set(rowId, values);
|
|
81
|
+
}
|
|
82
|
+
return values;
|
|
83
|
+
}
|
|
84
|
+
*iterValues() {
|
|
85
|
+
for (const values of this.valuesMap.values()) {
|
|
86
|
+
for (const value of values) {
|
|
87
|
+
yield value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var JsonBooleanStore = class extends events.EventEmitter {
|
|
93
|
+
constructor(nodeId = nanoid.nanoid()) {
|
|
94
|
+
super();
|
|
95
|
+
this.nodeId = nodeId;
|
|
96
|
+
}
|
|
97
|
+
type = "boolean" /* Boolean */;
|
|
98
|
+
$ref = "";
|
|
99
|
+
name = "";
|
|
100
|
+
parent = null;
|
|
101
|
+
default = false;
|
|
102
|
+
readOnly;
|
|
103
|
+
title;
|
|
104
|
+
description;
|
|
105
|
+
deprecated;
|
|
106
|
+
valuesMap = /* @__PURE__ */ new Map();
|
|
107
|
+
registerValue(value) {
|
|
108
|
+
const length = this.getOrCreateValues(value.rowId).push(value);
|
|
109
|
+
return length - 1;
|
|
110
|
+
}
|
|
111
|
+
getValue(rowId, index = 0) {
|
|
112
|
+
return this.getOrCreateValues(rowId)[index];
|
|
113
|
+
}
|
|
114
|
+
getPlainSchema(options) {
|
|
115
|
+
if (this.$ref && options?.skip$Ref !== true) {
|
|
116
|
+
return addSharedFieldsFromState({ $ref: this.$ref }, this);
|
|
117
|
+
}
|
|
118
|
+
return addSharedFieldsFromState(
|
|
119
|
+
{
|
|
120
|
+
type: this.type,
|
|
121
|
+
default: this.default,
|
|
122
|
+
...this.readOnly ? { readOnly: this.readOnly } : {}
|
|
123
|
+
},
|
|
124
|
+
this
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
getOrCreateValues(rowId) {
|
|
128
|
+
let values = this.valuesMap.get(rowId);
|
|
129
|
+
if (!values) {
|
|
130
|
+
values = [];
|
|
131
|
+
this.valuesMap.set(rowId, values);
|
|
132
|
+
}
|
|
133
|
+
return values;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
var JsonNumberStore = class extends events.EventEmitter {
|
|
137
|
+
constructor(nodeId = nanoid.nanoid()) {
|
|
138
|
+
super();
|
|
139
|
+
this.nodeId = nodeId;
|
|
140
|
+
}
|
|
141
|
+
type = "number" /* Number */;
|
|
142
|
+
$ref = "";
|
|
143
|
+
name = "";
|
|
144
|
+
parent = null;
|
|
145
|
+
default = 0;
|
|
146
|
+
readOnly;
|
|
147
|
+
title;
|
|
148
|
+
description;
|
|
149
|
+
deprecated;
|
|
150
|
+
valuesMap = /* @__PURE__ */ new Map();
|
|
151
|
+
registerValue(value) {
|
|
152
|
+
const length = this.getOrCreateValues(value.rowId).push(value);
|
|
153
|
+
return length - 1;
|
|
154
|
+
}
|
|
155
|
+
getValue(rowId, index = 0) {
|
|
156
|
+
return this.getOrCreateValues(rowId)[index];
|
|
157
|
+
}
|
|
158
|
+
getPlainSchema(options) {
|
|
159
|
+
if (this.$ref && options?.skip$Ref !== true) {
|
|
160
|
+
return addSharedFieldsFromState({ $ref: this.$ref }, this);
|
|
161
|
+
}
|
|
162
|
+
return addSharedFieldsFromState(
|
|
163
|
+
{
|
|
164
|
+
type: this.type,
|
|
165
|
+
default: this.default,
|
|
166
|
+
...this.readOnly ? { readOnly: this.readOnly } : {}
|
|
167
|
+
},
|
|
168
|
+
this
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
getOrCreateValues(rowId) {
|
|
172
|
+
let values = this.valuesMap.get(rowId);
|
|
173
|
+
if (!values) {
|
|
174
|
+
values = [];
|
|
175
|
+
this.valuesMap.set(rowId, values);
|
|
176
|
+
}
|
|
177
|
+
return values;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
var JsonStringStore = class extends events.EventEmitter {
|
|
181
|
+
constructor(nodeId = nanoid.nanoid()) {
|
|
182
|
+
super();
|
|
183
|
+
this.nodeId = nodeId;
|
|
184
|
+
}
|
|
185
|
+
type = "string" /* String */;
|
|
186
|
+
$ref = "";
|
|
187
|
+
name = "";
|
|
188
|
+
parent = null;
|
|
189
|
+
default = "";
|
|
190
|
+
readOnly;
|
|
191
|
+
title;
|
|
192
|
+
description;
|
|
193
|
+
deprecated;
|
|
194
|
+
foreignKey;
|
|
195
|
+
pattern;
|
|
196
|
+
enum;
|
|
197
|
+
format;
|
|
198
|
+
contentMediaType;
|
|
199
|
+
valuesMap = /* @__PURE__ */ new Map();
|
|
200
|
+
registerValue(value) {
|
|
201
|
+
const length = this.getOrCreateValues(value.rowId).push(value);
|
|
202
|
+
return length - 1;
|
|
203
|
+
}
|
|
204
|
+
getValue(rowId, index = 0) {
|
|
205
|
+
return this.getOrCreateValues(rowId)[index];
|
|
206
|
+
}
|
|
207
|
+
getPlainSchema(options) {
|
|
208
|
+
if (this.$ref && options?.skip$Ref !== true) {
|
|
209
|
+
return addSharedFieldsFromState({ $ref: this.$ref }, this);
|
|
210
|
+
}
|
|
211
|
+
return addSharedFieldsFromState(
|
|
212
|
+
{
|
|
213
|
+
type: this.type,
|
|
214
|
+
default: this.default,
|
|
215
|
+
...this.foreignKey ? { foreignKey: this.foreignKey } : {},
|
|
216
|
+
...this.readOnly ? { readOnly: this.readOnly } : {},
|
|
217
|
+
...this.pattern ? { pattern: this.pattern } : {},
|
|
218
|
+
...this.enum ? { enum: this.enum } : {},
|
|
219
|
+
...this.format ? { format: this.format } : {},
|
|
220
|
+
...this.contentMediaType ? { contentMediaType: this.contentMediaType } : {}
|
|
221
|
+
},
|
|
222
|
+
this
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
getOrCreateValues(rowId) {
|
|
226
|
+
let values = this.valuesMap.get(rowId);
|
|
227
|
+
if (!values) {
|
|
228
|
+
values = [];
|
|
229
|
+
this.valuesMap.set(rowId, values);
|
|
230
|
+
}
|
|
231
|
+
return values;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
var JsonObjectStore = class {
|
|
235
|
+
constructor(nodeId = nanoid.nanoid()) {
|
|
236
|
+
this.nodeId = nodeId;
|
|
237
|
+
}
|
|
238
|
+
type = "object" /* Object */;
|
|
239
|
+
$ref = "";
|
|
240
|
+
name = "";
|
|
241
|
+
parent = null;
|
|
242
|
+
default = {};
|
|
243
|
+
title;
|
|
244
|
+
description;
|
|
245
|
+
deprecated;
|
|
246
|
+
additionalProperties = false;
|
|
247
|
+
required = [];
|
|
248
|
+
properties = {};
|
|
249
|
+
valuesMap = /* @__PURE__ */ new Map();
|
|
250
|
+
get empty() {
|
|
251
|
+
return Object.keys(this.properties).length === 0;
|
|
252
|
+
}
|
|
253
|
+
registerValue(value) {
|
|
254
|
+
const length = this.getOrCreateValues(value.rowId).push(value);
|
|
255
|
+
return length - 1;
|
|
256
|
+
}
|
|
257
|
+
getValue(rowId, index = 0) {
|
|
258
|
+
return this.getOrCreateValues(rowId)[index];
|
|
259
|
+
}
|
|
260
|
+
addPropertyWithStore(name, store) {
|
|
261
|
+
if (this.properties[name] || this.required.includes(name)) {
|
|
262
|
+
throw new Error("this name already exists");
|
|
263
|
+
}
|
|
264
|
+
store.parent = this;
|
|
265
|
+
store.name = name;
|
|
266
|
+
this.required.push(name);
|
|
267
|
+
this.required.sort((a, b) => a.localeCompare(b));
|
|
268
|
+
this.properties[name] = store;
|
|
269
|
+
this.default[name] = store.default;
|
|
270
|
+
const event = { name, property: store };
|
|
271
|
+
for (const value of this.iterValues()) {
|
|
272
|
+
value.addProperty(event);
|
|
273
|
+
}
|
|
274
|
+
return store;
|
|
275
|
+
}
|
|
276
|
+
migratePropertyWithStore(name, store) {
|
|
277
|
+
const item = this.properties[name];
|
|
278
|
+
if (!item) {
|
|
279
|
+
throw new Error("this name does not exist");
|
|
280
|
+
}
|
|
281
|
+
item.parent = null;
|
|
282
|
+
store.parent = this;
|
|
283
|
+
store.name = name;
|
|
284
|
+
this.properties[name] = store;
|
|
285
|
+
this.default[name] = store.default;
|
|
286
|
+
const event = {
|
|
287
|
+
name,
|
|
288
|
+
property: store,
|
|
289
|
+
previousProperty: item
|
|
290
|
+
};
|
|
291
|
+
for (const value of this.iterValues()) {
|
|
292
|
+
value.migrateProperty(event);
|
|
293
|
+
}
|
|
294
|
+
return store;
|
|
295
|
+
}
|
|
296
|
+
changeName(fromName, toName) {
|
|
297
|
+
const item = this.properties[fromName];
|
|
298
|
+
if (!item) {
|
|
299
|
+
throw new Error("this fromName does not exist");
|
|
300
|
+
}
|
|
301
|
+
delete this.properties[fromName];
|
|
302
|
+
delete this.default[fromName];
|
|
303
|
+
const foundRequiredIndex = this.required.findIndex(
|
|
304
|
+
(required) => required === fromName
|
|
305
|
+
);
|
|
306
|
+
if (foundRequiredIndex !== -1) {
|
|
307
|
+
this.required.splice(foundRequiredIndex, 1);
|
|
308
|
+
}
|
|
309
|
+
if (!this.required.includes(toName)) {
|
|
310
|
+
this.required.push(toName);
|
|
311
|
+
this.required.sort((a, b) => a.localeCompare(b));
|
|
312
|
+
}
|
|
313
|
+
this.properties[toName] = item;
|
|
314
|
+
this.default[toName] = item.default;
|
|
315
|
+
const event = {
|
|
316
|
+
fromName,
|
|
317
|
+
toName,
|
|
318
|
+
property: item
|
|
319
|
+
};
|
|
320
|
+
for (const value of this.iterValues()) {
|
|
321
|
+
value.changeName(event);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
removeProperty(name) {
|
|
325
|
+
const item = this.properties[name];
|
|
326
|
+
if (!item) {
|
|
327
|
+
throw new Error("this name does not exist");
|
|
328
|
+
}
|
|
329
|
+
item.parent = null;
|
|
330
|
+
item.name = "";
|
|
331
|
+
delete this.properties[name];
|
|
332
|
+
delete this.default[name];
|
|
333
|
+
const foundRequiredIndex = this.required.findIndex(
|
|
334
|
+
(required) => required === name
|
|
335
|
+
);
|
|
336
|
+
if (foundRequiredIndex !== -1) {
|
|
337
|
+
this.required.splice(foundRequiredIndex, 1);
|
|
338
|
+
}
|
|
339
|
+
const event = {
|
|
340
|
+
name,
|
|
341
|
+
property: item
|
|
342
|
+
};
|
|
343
|
+
for (const value of this.iterValues()) {
|
|
344
|
+
value.removeProperty(event);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
getProperty(name) {
|
|
348
|
+
return this.properties[name];
|
|
349
|
+
}
|
|
350
|
+
getPlainSchema(options) {
|
|
351
|
+
if (this.$ref && options?.skip$Ref !== true) {
|
|
352
|
+
return addSharedFieldsFromState({ $ref: this.$ref }, this);
|
|
353
|
+
}
|
|
354
|
+
return addSharedFieldsFromState(
|
|
355
|
+
{
|
|
356
|
+
type: this.type,
|
|
357
|
+
additionalProperties: this.additionalProperties,
|
|
358
|
+
required: this.required,
|
|
359
|
+
properties: Object.entries(this.properties).reduce((result, [name, store]) => {
|
|
360
|
+
result[name] = store.getPlainSchema(options);
|
|
361
|
+
return result;
|
|
362
|
+
}, {})
|
|
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/lib/createJsonSchemaStore.ts
|
|
385
|
+
var createJsonSchemaStore = (schema, refs = {}) => {
|
|
386
|
+
if ("$ref" in schema) {
|
|
387
|
+
const refSchema = refs[schema.$ref];
|
|
388
|
+
if (!refSchema) {
|
|
389
|
+
throw new Error(`Not found schema for $ref="${schema.$ref}"`);
|
|
390
|
+
}
|
|
391
|
+
const refStore = createJsonSchemaStore(refSchema, refs);
|
|
392
|
+
saveSharedFields(refStore, schema);
|
|
393
|
+
refStore.$ref = schema.$ref;
|
|
394
|
+
return refStore;
|
|
395
|
+
} else if (schema.type === "object" /* Object */) {
|
|
396
|
+
const objectStore = createJsonObjectSchemaStore(schema, refs);
|
|
397
|
+
saveSharedFields(objectStore, schema);
|
|
398
|
+
return objectStore;
|
|
399
|
+
} else if (schema.type === "array" /* Array */) {
|
|
400
|
+
const itemsStore = createJsonSchemaStore(schema.items, refs);
|
|
401
|
+
const arrayStore = new JsonArrayStore(itemsStore);
|
|
402
|
+
saveSharedFields(arrayStore, schema);
|
|
403
|
+
return arrayStore;
|
|
404
|
+
} else {
|
|
405
|
+
const primitivesStore = createPrimitiveStoreBySchema(schema);
|
|
406
|
+
saveSharedFields(primitivesStore, schema);
|
|
407
|
+
primitivesStore.readOnly = schema.readOnly;
|
|
408
|
+
return primitivesStore;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
var createJsonObjectSchemaStore = (value, refs) => {
|
|
412
|
+
const store = new JsonObjectStore();
|
|
413
|
+
for (const requiredField of value.required) {
|
|
414
|
+
if (!value.properties[requiredField]) {
|
|
415
|
+
throw new Error(
|
|
416
|
+
`Not found required field "${requiredField}" in "properties"`
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
Object.entries(value.properties).forEach(([name, item]) => {
|
|
421
|
+
store.addPropertyWithStore(name, createJsonSchemaStore(item, refs));
|
|
422
|
+
});
|
|
423
|
+
return store;
|
|
424
|
+
};
|
|
425
|
+
var createPrimitiveStoreBySchema = (schema) => {
|
|
426
|
+
if (schema.type === "string" /* String */) {
|
|
427
|
+
const stringStore = new JsonStringStore();
|
|
428
|
+
stringStore.foreignKey = schema.foreignKey;
|
|
429
|
+
stringStore.format = schema.format;
|
|
430
|
+
stringStore.enum = schema.enum;
|
|
431
|
+
stringStore.contentMediaType = schema.contentMediaType;
|
|
432
|
+
stringStore.pattern = schema.pattern;
|
|
433
|
+
return stringStore;
|
|
434
|
+
} else if (schema.type === "number" /* Number */) {
|
|
435
|
+
return new JsonNumberStore();
|
|
436
|
+
} else if (schema.type === "boolean" /* Boolean */) {
|
|
437
|
+
return new JsonBooleanStore();
|
|
438
|
+
} else {
|
|
439
|
+
throw new Error("this type is not allowed");
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
var saveSharedFields = (store, schema) => {
|
|
443
|
+
store.title = schema.title;
|
|
444
|
+
store.description = schema.description;
|
|
445
|
+
store.deprecated = schema.deprecated;
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// src/lib/getJsonSchemaStoreByPath.ts
|
|
449
|
+
var getJsonSchemaStoreByPath = (store, path) => {
|
|
450
|
+
if (path === "") {
|
|
451
|
+
return store;
|
|
452
|
+
}
|
|
453
|
+
if (path === "/") {
|
|
454
|
+
throw new Error(
|
|
455
|
+
'invalid root path, need to use path="" instead of path="/"'
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
const tokens = path.split("/");
|
|
459
|
+
tokens.shift();
|
|
460
|
+
let currentStore = store;
|
|
461
|
+
let currentToken = tokens.shift();
|
|
462
|
+
let currentPath = "";
|
|
463
|
+
while (currentToken) {
|
|
464
|
+
if (currentStore.type === "object" /* Object */) {
|
|
465
|
+
if (currentToken !== "properties") {
|
|
466
|
+
throw new Error(
|
|
467
|
+
`Expected "${currentPath}/properties/*" instead of ${currentPath}/${currentToken}/*`
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
currentPath = `${currentPath}/${currentToken}`;
|
|
471
|
+
currentToken = tokens.shift();
|
|
472
|
+
if (!currentToken) {
|
|
473
|
+
throw new Error(`Expected property name after "${currentPath}"`);
|
|
474
|
+
}
|
|
475
|
+
const foundCurrentStore = currentStore.getProperty(currentToken);
|
|
476
|
+
if (!foundCurrentStore) {
|
|
477
|
+
throw new Error(`Not found "${currentToken}" in "${currentPath}"`);
|
|
478
|
+
}
|
|
479
|
+
currentStore = foundCurrentStore;
|
|
480
|
+
currentPath = `${currentPath}/${currentToken}`;
|
|
481
|
+
currentToken = tokens.shift();
|
|
482
|
+
} else if (currentStore.type === "array" /* Array */) {
|
|
483
|
+
if (currentToken !== "items") {
|
|
484
|
+
throw new Error(
|
|
485
|
+
`Expected "${currentPath}/items/*" instead of ${currentPath}/${currentToken}/*`
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
currentPath = `${currentPath}/${currentToken}`;
|
|
489
|
+
currentStore = currentStore.items;
|
|
490
|
+
currentToken = tokens.shift();
|
|
491
|
+
} else {
|
|
492
|
+
throw new Error(`Unexpected "${currentToken}" in "${currentPath}"`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return currentStore;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/lib/getParentForPath.ts
|
|
499
|
+
var getParentForPath = (path) => {
|
|
500
|
+
if (path === "" || path === "/") {
|
|
501
|
+
throw new Error("Invalid path");
|
|
502
|
+
}
|
|
503
|
+
const tokens = path.split("/");
|
|
504
|
+
tokens.shift();
|
|
505
|
+
let currentToken = tokens.shift();
|
|
506
|
+
let parentPath = "";
|
|
507
|
+
let field = "";
|
|
508
|
+
while (currentToken) {
|
|
509
|
+
if (currentToken === "properties") {
|
|
510
|
+
currentToken = tokens.shift();
|
|
511
|
+
if (!currentToken) {
|
|
512
|
+
throw new Error("Invalid path");
|
|
513
|
+
}
|
|
514
|
+
field = currentToken;
|
|
515
|
+
currentToken = tokens.shift();
|
|
516
|
+
if (currentToken) {
|
|
517
|
+
parentPath = `${parentPath}/properties/${field}`;
|
|
518
|
+
}
|
|
519
|
+
} else if (currentToken === "items") {
|
|
520
|
+
field = currentToken;
|
|
521
|
+
currentToken = tokens.shift();
|
|
522
|
+
if (currentToken && !["items", "properties"].includes(currentToken)) {
|
|
523
|
+
throw new Error("Invalid path");
|
|
524
|
+
} else if (currentToken) {
|
|
525
|
+
parentPath = `${parentPath}/items`;
|
|
526
|
+
}
|
|
527
|
+
} else {
|
|
528
|
+
throw new Error("Invalid path");
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return {
|
|
532
|
+
parentPath,
|
|
533
|
+
field
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
// src/lib/validateJsonFieldName.ts
|
|
538
|
+
var maxLength = 64;
|
|
539
|
+
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 (_).`;
|
|
540
|
+
var validPattern = /^(?!__)[a-zA-Z_][a-zA-Z0-9-_]*$/;
|
|
541
|
+
var validateJsonFieldName = (id) => {
|
|
542
|
+
const isInvalid = id.length < 1 || id.length > maxLength || !validPattern.test(id);
|
|
543
|
+
return !isInvalid;
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// src/lib/applyPatches.ts
|
|
547
|
+
var applyReplacePatch = (store, patch, refs = {}) => {
|
|
548
|
+
const patchStore = createJsonSchemaStore(patch.value, refs);
|
|
549
|
+
const foundStore = getJsonSchemaStoreByPath(store, patch.path);
|
|
550
|
+
const parent = foundStore.parent;
|
|
551
|
+
if (!parent) {
|
|
552
|
+
return patchStore;
|
|
553
|
+
}
|
|
554
|
+
if (parent.type === "object" /* Object */) {
|
|
555
|
+
parent.migratePropertyWithStore(foundStore.name, patchStore);
|
|
556
|
+
} else if (parent.type === "array" /* Array */) {
|
|
557
|
+
parent.migrateItems(patchStore);
|
|
558
|
+
} else {
|
|
559
|
+
throw new Error("Invalid parent");
|
|
560
|
+
}
|
|
561
|
+
return store;
|
|
562
|
+
};
|
|
563
|
+
var applyRemovePatch = (rootStore, patch) => {
|
|
564
|
+
const foundStore = getJsonSchemaStoreByPath(rootStore, patch.path);
|
|
565
|
+
const parent = foundStore.parent;
|
|
566
|
+
if (!parent) {
|
|
567
|
+
throw new Error("Parent does not exist");
|
|
568
|
+
}
|
|
569
|
+
if (parent.type !== "object" /* Object */) {
|
|
570
|
+
throw new Error("Cannot remove from non-object");
|
|
571
|
+
}
|
|
572
|
+
parent.removeProperty(foundStore.name);
|
|
573
|
+
};
|
|
574
|
+
var applyAddPatch = (rootStore, patch, refs = {}) => {
|
|
575
|
+
const patchStore = createJsonSchemaStore(patch.value, refs);
|
|
576
|
+
const { parentPath, field } = getParentForPath(patch.path);
|
|
577
|
+
const foundParent = getJsonSchemaStoreByPath(rootStore, parentPath);
|
|
578
|
+
if (!foundParent) {
|
|
579
|
+
throw new Error("Parent does not exist");
|
|
580
|
+
}
|
|
581
|
+
if (foundParent.type !== "object" /* Object */) {
|
|
582
|
+
throw new Error("Cannot add to non-object");
|
|
583
|
+
}
|
|
584
|
+
if (foundParent.getProperty(field)) {
|
|
585
|
+
throw new Error(`Field "${field}" already exists in parent`);
|
|
586
|
+
}
|
|
587
|
+
foundParent.addPropertyWithStore(field, patchStore);
|
|
588
|
+
};
|
|
589
|
+
var applyMovePatch = (store, patch) => {
|
|
590
|
+
const { parentPath: fromParentPath, field: fromField } = getParentForPath(
|
|
591
|
+
patch.from
|
|
592
|
+
);
|
|
593
|
+
const { parentPath: toParentPath, field: toField } = getParentForPath(
|
|
594
|
+
patch.path
|
|
595
|
+
);
|
|
596
|
+
const foundFromParent = getJsonSchemaStoreByPath(store, fromParentPath);
|
|
597
|
+
const foundToParent = getJsonSchemaStoreByPath(store, toParentPath);
|
|
598
|
+
const isValidToField = validateJsonFieldName(toField);
|
|
599
|
+
if (!isValidToField) {
|
|
600
|
+
throw new Error(
|
|
601
|
+
`Invalid name: ${toField}. ${VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE}`
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
if (!foundFromParent || !foundToParent) {
|
|
605
|
+
throw new Error("Cannot move from or to non-existent parent");
|
|
606
|
+
}
|
|
607
|
+
if (foundFromParent.type !== "object" /* Object */) {
|
|
608
|
+
throw new Error("Cannot move from non-object parent");
|
|
609
|
+
}
|
|
610
|
+
const foundFromField = getJsonSchemaStoreByPath(store, patch.from);
|
|
611
|
+
const isMovedPropertyInSameParentPatch = foundFromParent === foundToParent && foundFromParent.type === "object" /* Object */ && foundFromParent.getProperty(fromField);
|
|
612
|
+
if (isMovedPropertyInSameParentPatch) {
|
|
613
|
+
return foundFromParent.changeName(fromField, toField);
|
|
614
|
+
}
|
|
615
|
+
if (foundToParent.type === "object" /* Object */) {
|
|
616
|
+
foundFromParent.removeProperty(fromField);
|
|
617
|
+
if (foundToParent.getProperty(toField)) {
|
|
618
|
+
foundToParent.removeProperty(toField);
|
|
619
|
+
}
|
|
620
|
+
foundToParent.addPropertyWithStore(toField, foundFromField);
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
if (foundToParent.type === "array" /* Array */) {
|
|
624
|
+
foundFromParent.removeProperty(fromField);
|
|
625
|
+
foundToParent.replaceItems(foundFromField);
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
throw new Error('Invalid type of "to" parent');
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
// src/model/value/value-transformation.ts
|
|
632
|
+
var equel = (value) => value;
|
|
633
|
+
var fromNumberToString = (value, defaultValue = "") => value.toString() || defaultValue;
|
|
634
|
+
var fromBooleanToString = (value, defaultValue = "") => value.toString() || defaultValue;
|
|
635
|
+
var fromStringToBoolean = (value, defaultValue = false) => {
|
|
636
|
+
if (!value) {
|
|
637
|
+
return defaultValue;
|
|
638
|
+
}
|
|
639
|
+
if (value.toLowerCase() === "false") {
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
return true;
|
|
643
|
+
};
|
|
644
|
+
var fromStringToNumber = (value, defaultValue = 0) => {
|
|
645
|
+
const number = Number.parseFloat(value);
|
|
646
|
+
if (Number.isNaN(number)) {
|
|
647
|
+
return defaultValue;
|
|
648
|
+
}
|
|
649
|
+
return number;
|
|
650
|
+
};
|
|
651
|
+
var fromBooleanToNumber = (value) => {
|
|
652
|
+
return Number(value);
|
|
653
|
+
};
|
|
654
|
+
var fromNumberToBoolean = (value) => {
|
|
655
|
+
return Boolean(value);
|
|
656
|
+
};
|
|
657
|
+
var toArrayTransformation = (transformation) => (value) => {
|
|
658
|
+
const result = transformation(value);
|
|
659
|
+
return [result];
|
|
660
|
+
};
|
|
661
|
+
var fromArrayTransformation = (transformation) => (value) => {
|
|
662
|
+
if (Array.isArray(value) && value.length) {
|
|
663
|
+
return transformation(value[0]);
|
|
664
|
+
}
|
|
665
|
+
return void 0;
|
|
666
|
+
};
|
|
667
|
+
var replaceTransformationsMapper = [
|
|
668
|
+
{
|
|
669
|
+
fromType: "number" /* Number */,
|
|
670
|
+
toType: "string" /* String */,
|
|
671
|
+
transformation: fromNumberToString
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
fromType: "string" /* String */,
|
|
675
|
+
toType: "number" /* Number */,
|
|
676
|
+
transformation: fromStringToNumber
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
fromType: "boolean" /* Boolean */,
|
|
680
|
+
toType: "string" /* String */,
|
|
681
|
+
transformation: fromBooleanToString
|
|
682
|
+
},
|
|
683
|
+
{
|
|
684
|
+
fromType: "string" /* String */,
|
|
685
|
+
toType: "boolean" /* Boolean */,
|
|
686
|
+
transformation: fromStringToBoolean
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
fromType: "boolean" /* Boolean */,
|
|
690
|
+
toType: "number" /* Number */,
|
|
691
|
+
transformation: fromBooleanToNumber
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
fromType: "number" /* Number */,
|
|
695
|
+
toType: "boolean" /* Boolean */,
|
|
696
|
+
transformation: fromNumberToBoolean
|
|
697
|
+
}
|
|
698
|
+
];
|
|
699
|
+
var getTransformation = (from, to) => {
|
|
700
|
+
if (to instanceof JsonArrayStore) {
|
|
701
|
+
const transformation = findTransformation(from.type, to.items.type);
|
|
702
|
+
if (!transformation) {
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
return toArrayTransformation(transformation);
|
|
706
|
+
} else if (from instanceof JsonArrayStore) {
|
|
707
|
+
const transformation = findTransformation(from.items.type, to.type);
|
|
708
|
+
if (!transformation) {
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
return fromArrayTransformation(transformation);
|
|
712
|
+
}
|
|
713
|
+
return findTransformation(from.type, to.type);
|
|
714
|
+
};
|
|
715
|
+
var findTransformation = (from, to) => {
|
|
716
|
+
if (from === to) {
|
|
717
|
+
return equel;
|
|
718
|
+
}
|
|
719
|
+
for (const item of replaceTransformationsMapper) {
|
|
720
|
+
if (item.fromType === from && item.toType === to) {
|
|
721
|
+
return item.transformation;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
return void 0;
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
// src/model/value/json-array-value.store.ts
|
|
728
|
+
var JsonArrayValueStore = class {
|
|
729
|
+
constructor(schema, rowId, value) {
|
|
730
|
+
this.schema = schema;
|
|
731
|
+
this.rowId = rowId;
|
|
732
|
+
this.value = value;
|
|
733
|
+
this.index = this.schema.registerValue(this);
|
|
734
|
+
this.init();
|
|
735
|
+
}
|
|
736
|
+
type = "array" /* Array */;
|
|
737
|
+
index;
|
|
738
|
+
parent = null;
|
|
739
|
+
getPlainValue() {
|
|
740
|
+
return this.value.map((item) => item.getPlainValue());
|
|
741
|
+
}
|
|
742
|
+
migrateItems(event) {
|
|
743
|
+
const transformation = getTransformation(event.previousItems, event.items);
|
|
744
|
+
this.value = this.value.map((valueItem) => {
|
|
745
|
+
const rawValue = transformation ? transformation(
|
|
746
|
+
valueItem.getPlainValue(),
|
|
747
|
+
event.items.default
|
|
748
|
+
) : event.items.default;
|
|
749
|
+
return createJsonValueStore(event.items, this.rowId, rawValue);
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
replaceItems(event) {
|
|
753
|
+
this.value = this.value.map(() => {
|
|
754
|
+
const rawValue = this.getReplacedValue(event);
|
|
755
|
+
return createJsonValueStore(event.items, this.rowId, rawValue);
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
getReplacedValue(event) {
|
|
759
|
+
const previousValue = event.items.getValue(this.rowId);
|
|
760
|
+
if (previousValue) {
|
|
761
|
+
return previousValue.getPlainValue();
|
|
762
|
+
}
|
|
763
|
+
return event.items.default;
|
|
764
|
+
}
|
|
765
|
+
init() {
|
|
766
|
+
for (const value of this.value) {
|
|
767
|
+
value.parent = this;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
// src/model/value/json-boolean-value.store.ts
|
|
773
|
+
var JsonBooleanValueStore = class {
|
|
774
|
+
constructor(schema, rowId, value = null) {
|
|
775
|
+
this.schema = schema;
|
|
776
|
+
this.rowId = rowId;
|
|
777
|
+
this.value = value;
|
|
778
|
+
this.index = this.schema.registerValue(this);
|
|
779
|
+
}
|
|
780
|
+
type = "boolean" /* Boolean */;
|
|
781
|
+
index;
|
|
782
|
+
parent = null;
|
|
783
|
+
getPlainValue() {
|
|
784
|
+
return this.value ?? this.schema.default;
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
// src/model/value/json-number-value.store.ts
|
|
789
|
+
var JsonNumberValueStore = class {
|
|
790
|
+
constructor(schema, rowId, value = null) {
|
|
791
|
+
this.schema = schema;
|
|
792
|
+
this.rowId = rowId;
|
|
793
|
+
this.value = value;
|
|
794
|
+
this.index = this.schema.registerValue(this);
|
|
795
|
+
}
|
|
796
|
+
type = "number" /* Number */;
|
|
797
|
+
index;
|
|
798
|
+
parent = null;
|
|
799
|
+
getPlainValue() {
|
|
800
|
+
return this.value ?? this.schema.default;
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
// src/model/value/json-object-value.store.ts
|
|
805
|
+
var JsonObjectValueStore = class {
|
|
806
|
+
constructor(schema, rowId, value) {
|
|
807
|
+
this.schema = schema;
|
|
808
|
+
this.rowId = rowId;
|
|
809
|
+
this.value = value;
|
|
810
|
+
this.index = this.schema.registerValue(this);
|
|
811
|
+
this.init();
|
|
812
|
+
}
|
|
813
|
+
type = "object" /* Object */;
|
|
814
|
+
index;
|
|
815
|
+
parent = null;
|
|
816
|
+
getPlainValue() {
|
|
817
|
+
return Object.entries(this.value).reduce(
|
|
818
|
+
(result, [name, store]) => {
|
|
819
|
+
result[name] = store.getPlainValue();
|
|
820
|
+
return result;
|
|
821
|
+
},
|
|
822
|
+
{}
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
migrateProperty(event) {
|
|
826
|
+
const rawValue = this.getMigratedValue(event);
|
|
827
|
+
this.value[event.name] = createJsonValueStore(
|
|
828
|
+
event.property,
|
|
829
|
+
this.rowId,
|
|
830
|
+
rawValue
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
addProperty(event) {
|
|
834
|
+
const rawValue = this.getAddedValue(event);
|
|
835
|
+
this.value[event.name] = createJsonValueStore(
|
|
836
|
+
event.property,
|
|
837
|
+
this.rowId,
|
|
838
|
+
rawValue
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
removeProperty(event) {
|
|
842
|
+
delete this.value[event.name];
|
|
843
|
+
}
|
|
844
|
+
changeName(event) {
|
|
845
|
+
const itemValue = this.value[event.fromName];
|
|
846
|
+
if (itemValue !== void 0) {
|
|
847
|
+
delete this.value[event.fromName];
|
|
848
|
+
this.value[event.toName] = itemValue;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
getAddedValue(event) {
|
|
852
|
+
const previousValue = event.property.getValue(this.rowId, this.index);
|
|
853
|
+
if (previousValue) {
|
|
854
|
+
return previousValue.getPlainValue();
|
|
855
|
+
}
|
|
856
|
+
return event.property.default;
|
|
857
|
+
}
|
|
858
|
+
getMigratedValue(event) {
|
|
859
|
+
const transformation = getTransformation(
|
|
860
|
+
event.previousProperty,
|
|
861
|
+
event.property
|
|
862
|
+
);
|
|
863
|
+
const valueStore = this.value[event.name];
|
|
864
|
+
if (transformation && valueStore) {
|
|
865
|
+
return transformation(
|
|
866
|
+
valueStore.getPlainValue(),
|
|
867
|
+
event.property.default
|
|
868
|
+
);
|
|
869
|
+
}
|
|
870
|
+
return event.property.default;
|
|
871
|
+
}
|
|
872
|
+
init() {
|
|
873
|
+
for (const value of Object.values(this.value)) {
|
|
874
|
+
value.parent = this;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
// src/model/value/json-string-value.store.ts
|
|
880
|
+
var JsonStringValueStore = class {
|
|
881
|
+
constructor(schema, rowId, value = null) {
|
|
882
|
+
this.schema = schema;
|
|
883
|
+
this.rowId = rowId;
|
|
884
|
+
this.value = value;
|
|
885
|
+
this.index = this.schema.registerValue(this);
|
|
886
|
+
}
|
|
887
|
+
type = "string" /* String */;
|
|
888
|
+
index;
|
|
889
|
+
parent = null;
|
|
890
|
+
get foreignKey() {
|
|
891
|
+
return this.schema.foreignKey;
|
|
892
|
+
}
|
|
893
|
+
getPlainValue() {
|
|
894
|
+
return this.value ?? this.schema.default;
|
|
895
|
+
}
|
|
896
|
+
};
|
|
897
|
+
|
|
898
|
+
// src/lib/createJsonValueStore.ts
|
|
899
|
+
var createJsonValueStore = (schema, rowId, rawValue) => {
|
|
900
|
+
if (schema.type === "object" /* Object */) {
|
|
901
|
+
return createJsonObjectValueStore(schema, rowId, rawValue);
|
|
902
|
+
} else if (schema.type === "array" /* Array */) {
|
|
903
|
+
return createJsonArrayValueStore(schema, rowId, rawValue);
|
|
904
|
+
} else {
|
|
905
|
+
return createPrimitiveValueStore(schema, rowId, rawValue);
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
var createJsonObjectValueStore = (schema, rowId, rawValue) => {
|
|
909
|
+
const value = Object.entries(rawValue).reduce(
|
|
910
|
+
(reduceValue, [key, itemValue]) => {
|
|
911
|
+
const itemSchema = schema.getProperty(key);
|
|
912
|
+
if (itemSchema === void 0 || itemValue === void 0) {
|
|
913
|
+
throw new Error("Invalid item");
|
|
914
|
+
}
|
|
915
|
+
reduceValue[key] = createJsonValueStore(itemSchema, rowId, itemValue);
|
|
916
|
+
return reduceValue;
|
|
917
|
+
},
|
|
918
|
+
{}
|
|
919
|
+
);
|
|
920
|
+
return new JsonObjectValueStore(schema, rowId, value);
|
|
921
|
+
};
|
|
922
|
+
var createJsonArrayValueStore = (schema, rowId, rawValue) => {
|
|
923
|
+
const value = rawValue.map(
|
|
924
|
+
(value2) => createJsonValueStore(schema.items, rowId, value2)
|
|
925
|
+
);
|
|
926
|
+
return new JsonArrayValueStore(schema, rowId, value);
|
|
927
|
+
};
|
|
928
|
+
var createPrimitiveValueStore = (schema, rowId, rawValue) => {
|
|
929
|
+
if (schema.type === "string" /* String */) {
|
|
930
|
+
return new JsonStringValueStore(schema, rowId, rawValue);
|
|
931
|
+
} else if (schema.type === "number" /* Number */) {
|
|
932
|
+
return new JsonNumberValueStore(schema, rowId, rawValue);
|
|
933
|
+
} else if (schema.type === "boolean" /* Boolean */) {
|
|
934
|
+
return new JsonBooleanValueStore(schema, rowId, rawValue);
|
|
935
|
+
} else {
|
|
936
|
+
throw new Error("this type is not allowed");
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
// src/lib/getDBJsonPathByJsonSchemaStore.ts
|
|
941
|
+
var getDBJsonPathByJsonSchemaStore = (store) => {
|
|
942
|
+
let node = store;
|
|
943
|
+
let path = "";
|
|
944
|
+
while (node.parent) {
|
|
945
|
+
if (node.parent.type === "object" /* Object */) {
|
|
946
|
+
path = `.${node.name}${path}`;
|
|
947
|
+
} else if (node.parent.type === "array" /* Array */) {
|
|
948
|
+
path = `[*]${path}`;
|
|
949
|
+
}
|
|
950
|
+
node = node.parent;
|
|
951
|
+
}
|
|
952
|
+
if (!path) {
|
|
953
|
+
return "$";
|
|
954
|
+
}
|
|
955
|
+
return `$${path}`;
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
// src/lib/getPathByStore.ts
|
|
959
|
+
var getPathByStore = (store) => {
|
|
960
|
+
let node = store;
|
|
961
|
+
let path = "";
|
|
962
|
+
while (node.parent) {
|
|
963
|
+
if (node.parent.type === "object" /* Object */) {
|
|
964
|
+
path = `/properties/${node.name}${path}`;
|
|
965
|
+
} else if (node.parent.type === "array" /* Array */) {
|
|
966
|
+
path = `/items${path}`;
|
|
967
|
+
}
|
|
968
|
+
node = node.parent;
|
|
969
|
+
}
|
|
970
|
+
if (!path) {
|
|
971
|
+
return "/";
|
|
972
|
+
}
|
|
973
|
+
return `${path}`;
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
// src/lib/traverseStore.ts
|
|
977
|
+
var traverseStore = (store, callback) => {
|
|
978
|
+
callback(store);
|
|
979
|
+
if (store.type === "object" /* Object */) {
|
|
980
|
+
Object.values(store.properties).forEach((item) => {
|
|
981
|
+
traverseStore(item, callback);
|
|
982
|
+
});
|
|
983
|
+
} else if (store.type === "array" /* Array */) {
|
|
984
|
+
traverseStore(store.items, callback);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
// src/lib/getForeignKeyPatchesFromSchema.ts
|
|
989
|
+
var getForeignKeyPatchesFromSchema = (store, options) => {
|
|
990
|
+
const stores = [];
|
|
991
|
+
traverseStore(store, (item) => {
|
|
992
|
+
if (item.type === "string" /* String */ && item.foreignKey === options.tableId) {
|
|
993
|
+
item.foreignKey = options.nextTableId;
|
|
994
|
+
const patch = {
|
|
995
|
+
op: "replace",
|
|
996
|
+
path: getPathByStore(item),
|
|
997
|
+
value: item.getPlainSchema()
|
|
998
|
+
};
|
|
999
|
+
stores.push(patch);
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
return stores;
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
// src/lib/getForeignKeysFromSchema.ts
|
|
1006
|
+
var getForeignKeysFromSchema = (store) => {
|
|
1007
|
+
const foreignKeys = /* @__PURE__ */ new Set();
|
|
1008
|
+
traverseStore(store, (item) => {
|
|
1009
|
+
if (item.type === "string" /* String */ && item.foreignKey) {
|
|
1010
|
+
foreignKeys.add(item.foreignKey);
|
|
1011
|
+
}
|
|
1012
|
+
});
|
|
1013
|
+
return [...foreignKeys].sort((a, b) => a.localeCompare(b));
|
|
1014
|
+
};
|
|
1015
|
+
|
|
1016
|
+
// src/lib/traverseValue.ts
|
|
1017
|
+
var traverseValue = (store, callback) => {
|
|
1018
|
+
callback(store);
|
|
1019
|
+
if (store.type === "object" /* Object */) {
|
|
1020
|
+
Object.values(store.value).forEach((item) => {
|
|
1021
|
+
traverseValue(item, callback);
|
|
1022
|
+
});
|
|
1023
|
+
} else if (store.type === "array" /* Array */) {
|
|
1024
|
+
store.value.forEach((itemValue) => {
|
|
1025
|
+
traverseValue(itemValue, callback);
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
// src/lib/getForeignKeysFromValue.ts
|
|
1031
|
+
var getForeignKeysFromValue = (value) => {
|
|
1032
|
+
const foreignKeys = /* @__PURE__ */ new Map();
|
|
1033
|
+
traverseValue(value, (item) => {
|
|
1034
|
+
if (item.type === "string" /* String */ && item.foreignKey) {
|
|
1035
|
+
let tableForeignKey = foreignKeys.get(item.foreignKey);
|
|
1036
|
+
if (!tableForeignKey) {
|
|
1037
|
+
tableForeignKey = /* @__PURE__ */ new Set();
|
|
1038
|
+
foreignKeys.set(item.foreignKey, tableForeignKey);
|
|
1039
|
+
}
|
|
1040
|
+
tableForeignKey.add(item.getPlainValue());
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
return [...foreignKeys].map(([tableId, rowIds]) => ({
|
|
1044
|
+
tableId,
|
|
1045
|
+
rowIds: [...rowIds].sort((a, b) => a.localeCompare(b))
|
|
1046
|
+
}));
|
|
1047
|
+
};
|
|
1048
|
+
|
|
1049
|
+
// src/lib/getInvalidFieldNamesInSchema.ts
|
|
1050
|
+
var getInvalidFieldNamesInSchema = (schema, refs = {}) => {
|
|
1051
|
+
const schemaStore = createJsonSchemaStore(schema, refs);
|
|
1052
|
+
const invalidFields = [];
|
|
1053
|
+
traverseStore(schemaStore, (item) => {
|
|
1054
|
+
if (item.parent?.type === "object" /* Object */) {
|
|
1055
|
+
if (!validateJsonFieldName(item.name)) {
|
|
1056
|
+
invalidFields.push(item);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
});
|
|
1060
|
+
return invalidFields;
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
// src/lib/getJsonValueByPath.ts
|
|
1064
|
+
var getJsonValueStoreByPath = (root, path) => {
|
|
1065
|
+
if (!path) {
|
|
1066
|
+
return root;
|
|
1067
|
+
}
|
|
1068
|
+
const segments = getSegments(path);
|
|
1069
|
+
let current = root;
|
|
1070
|
+
for (const seg of segments) {
|
|
1071
|
+
if (current instanceof JsonObjectValueStore) {
|
|
1072
|
+
const next = current.value[String(seg)];
|
|
1073
|
+
if (!next) {
|
|
1074
|
+
throw new Error(`Path not found at segment "${seg}"`);
|
|
1075
|
+
}
|
|
1076
|
+
current = next;
|
|
1077
|
+
} else if (current instanceof JsonArrayValueStore) {
|
|
1078
|
+
if (typeof seg !== "number") {
|
|
1079
|
+
throw new Error(`Invalid array index "${seg}"`);
|
|
1080
|
+
}
|
|
1081
|
+
const next = current.value[seg];
|
|
1082
|
+
if (!next) {
|
|
1083
|
+
throw new Error(`Path not found at segment "${seg}"`);
|
|
1084
|
+
}
|
|
1085
|
+
current = next;
|
|
1086
|
+
} else {
|
|
1087
|
+
throw new Error(`Cannot navigate into primitive at segment "${seg}"`);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
return current;
|
|
1091
|
+
};
|
|
1092
|
+
var regex = /([^.[\]]+)|\[(\d+)]/g;
|
|
1093
|
+
var getSegments = (path) => {
|
|
1094
|
+
const segments = [];
|
|
1095
|
+
let match;
|
|
1096
|
+
while (match = regex.exec(path)) {
|
|
1097
|
+
if (match[1] !== void 0) {
|
|
1098
|
+
segments.push(match[1]);
|
|
1099
|
+
} else if (match[2] !== void 0) {
|
|
1100
|
+
segments.push(Number(match[2]));
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
return segments;
|
|
1104
|
+
};
|
|
1105
|
+
|
|
1106
|
+
// src/lib/replaceForeignKeyValue.ts
|
|
1107
|
+
var replaceForeignKeyValue = (options) => {
|
|
1108
|
+
let wasUpdated = false;
|
|
1109
|
+
traverseValue(options.valueStore, (item) => {
|
|
1110
|
+
if (item.type === "string" /* String */ && item.foreignKey === options.foreignKey && item.value === options.value) {
|
|
1111
|
+
item.value = options.nextValue;
|
|
1112
|
+
wasUpdated = true;
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
return wasUpdated;
|
|
1116
|
+
};
|
|
1117
|
+
|
|
1118
|
+
// src/plugins/row-id.schema.ts
|
|
1119
|
+
var rowIdSchema = {
|
|
1120
|
+
type: "string" /* String */,
|
|
1121
|
+
default: "",
|
|
1122
|
+
readOnly: true
|
|
1123
|
+
};
|
|
1124
|
+
|
|
1125
|
+
// src/plugins/row-version-id.schema.ts
|
|
1126
|
+
var rowVersionIdSchema = {
|
|
1127
|
+
type: "string" /* String */,
|
|
1128
|
+
default: "",
|
|
1129
|
+
readOnly: true
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
// src/plugins/row-created-id.schema.ts
|
|
1133
|
+
var rowCreatedIdSchema = {
|
|
1134
|
+
type: "string" /* String */,
|
|
1135
|
+
default: "",
|
|
1136
|
+
readOnly: true
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
// src/plugins/row-created-at.schema.ts
|
|
1140
|
+
var rowCreatedAtSchema = {
|
|
1141
|
+
type: "string" /* String */,
|
|
1142
|
+
default: "",
|
|
1143
|
+
readOnly: true
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
// src/plugins/row-published-at.schema.ts
|
|
1147
|
+
var rowPublishedAtSchema = {
|
|
1148
|
+
type: "string" /* String */,
|
|
1149
|
+
default: ""
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
// src/plugins/row-updated-at.schema.ts
|
|
1153
|
+
var rowUpdatedAtSchema = {
|
|
1154
|
+
type: "string" /* String */,
|
|
1155
|
+
default: "",
|
|
1156
|
+
readOnly: true
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
// src/plugins/row-hash.schema.ts
|
|
1160
|
+
var rowHashSchema = {
|
|
1161
|
+
type: "string" /* String */,
|
|
1162
|
+
default: "",
|
|
1163
|
+
readOnly: true
|
|
1164
|
+
};
|
|
1165
|
+
|
|
1166
|
+
// src/plugins/row-schema-hash.schema.ts
|
|
1167
|
+
var rowSchemaHashSchema = {
|
|
1168
|
+
type: "string" /* String */,
|
|
1169
|
+
default: "",
|
|
1170
|
+
readOnly: true
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
// src/plugins/file-schema.ts
|
|
1174
|
+
var fileSchema = {
|
|
1175
|
+
type: "object" /* Object */,
|
|
1176
|
+
properties: {
|
|
1177
|
+
status: { type: "string" /* String */, default: "", readOnly: true },
|
|
1178
|
+
fileId: { type: "string" /* String */, default: "", readOnly: true },
|
|
1179
|
+
url: { type: "string" /* String */, default: "", readOnly: true },
|
|
1180
|
+
fileName: { type: "string" /* String */, default: "" },
|
|
1181
|
+
hash: {
|
|
1182
|
+
type: "string" /* String */,
|
|
1183
|
+
default: "",
|
|
1184
|
+
readOnly: true
|
|
1185
|
+
},
|
|
1186
|
+
extension: {
|
|
1187
|
+
type: "string" /* String */,
|
|
1188
|
+
default: "",
|
|
1189
|
+
readOnly: true
|
|
1190
|
+
},
|
|
1191
|
+
mimeType: {
|
|
1192
|
+
type: "string" /* String */,
|
|
1193
|
+
default: "",
|
|
1194
|
+
readOnly: true
|
|
1195
|
+
},
|
|
1196
|
+
size: {
|
|
1197
|
+
type: "number" /* Number */,
|
|
1198
|
+
default: 0,
|
|
1199
|
+
readOnly: true
|
|
1200
|
+
},
|
|
1201
|
+
width: {
|
|
1202
|
+
type: "number" /* Number */,
|
|
1203
|
+
default: 0,
|
|
1204
|
+
readOnly: true
|
|
1205
|
+
},
|
|
1206
|
+
height: {
|
|
1207
|
+
type: "number" /* Number */,
|
|
1208
|
+
default: 0,
|
|
1209
|
+
readOnly: true
|
|
1210
|
+
}
|
|
1211
|
+
},
|
|
1212
|
+
required: [
|
|
1213
|
+
"status",
|
|
1214
|
+
"fileId",
|
|
1215
|
+
"url",
|
|
1216
|
+
"fileName",
|
|
1217
|
+
"hash",
|
|
1218
|
+
"extension",
|
|
1219
|
+
"mimeType",
|
|
1220
|
+
"size",
|
|
1221
|
+
"width",
|
|
1222
|
+
"height"
|
|
1223
|
+
],
|
|
1224
|
+
additionalProperties: false
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
// src/lib/resolveRefs.ts
|
|
1228
|
+
var pluginRefs = {
|
|
1229
|
+
["urn:jsonschema:io:revisium:row-id-schema:1.0.0" /* RowId */]: rowIdSchema,
|
|
1230
|
+
["urn:jsonschema:io:revisium:row-version-id-schema:1.0.0" /* RowVersionId */]: rowVersionIdSchema,
|
|
1231
|
+
["urn:jsonschema:io:revisium:row-created-id-schema:1.0.0" /* RowCreatedId */]: rowCreatedIdSchema,
|
|
1232
|
+
["urn:jsonschema:io:revisium:row-created-at-schema:1.0.0" /* RowCreatedAt */]: rowCreatedAtSchema,
|
|
1233
|
+
["urn:jsonschema:io:revisium:row-published-at-schema:1.0.0" /* RowPublishedAt */]: rowPublishedAtSchema,
|
|
1234
|
+
["urn:jsonschema:io:revisium:row-updated-at-schema:1.0.0" /* RowUpdatedAt */]: rowUpdatedAtSchema,
|
|
1235
|
+
["urn:jsonschema:io:revisium:row-hash-schema:1.0.0" /* RowHash */]: rowHashSchema,
|
|
1236
|
+
["urn:jsonschema:io:revisium:row-schema-hash-schema:1.0.0" /* RowSchemaHash */]: rowSchemaHashSchema,
|
|
1237
|
+
["urn:jsonschema:io:revisium:file-schema:1.0.0" /* File */]: fileSchema
|
|
1238
|
+
};
|
|
1239
|
+
var resolveRefs = (schema) => {
|
|
1240
|
+
const store = createJsonSchemaStore(schema, pluginRefs);
|
|
1241
|
+
return store.getPlainSchema({ skip$Ref: true });
|
|
1242
|
+
};
|
|
1243
|
+
|
|
1244
|
+
// src/lib/schema-table.ts
|
|
1245
|
+
var SchemaTable = class {
|
|
1246
|
+
constructor(schema, refs = {}) {
|
|
1247
|
+
this.refs = refs;
|
|
1248
|
+
this.store = createJsonSchemaStore(schema, refs);
|
|
1249
|
+
}
|
|
1250
|
+
rows = /* @__PURE__ */ new Map();
|
|
1251
|
+
store;
|
|
1252
|
+
applyPatches(patches) {
|
|
1253
|
+
patches.forEach((patch) => {
|
|
1254
|
+
switch (patch.op) {
|
|
1255
|
+
case "replace": {
|
|
1256
|
+
const nextStore = applyReplacePatch(this.store, patch, this.refs);
|
|
1257
|
+
if (nextStore !== this.store) {
|
|
1258
|
+
this.migrateRows(nextStore);
|
|
1259
|
+
}
|
|
1260
|
+
break;
|
|
1261
|
+
}
|
|
1262
|
+
case "remove": {
|
|
1263
|
+
applyRemovePatch(this.store, patch);
|
|
1264
|
+
break;
|
|
1265
|
+
}
|
|
1266
|
+
case "add": {
|
|
1267
|
+
applyAddPatch(this.store, patch, this.refs);
|
|
1268
|
+
break;
|
|
1269
|
+
}
|
|
1270
|
+
case "move": {
|
|
1271
|
+
applyMovePatch(this.store, patch);
|
|
1272
|
+
break;
|
|
1273
|
+
}
|
|
1274
|
+
default:
|
|
1275
|
+
throw new Error(`Unsupported patch operation`);
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
getSchema() {
|
|
1280
|
+
return this.store.getPlainSchema();
|
|
1281
|
+
}
|
|
1282
|
+
addRow(rowId, data) {
|
|
1283
|
+
const row = createJsonValueStore(this.store, rowId, data);
|
|
1284
|
+
this.rows.set(rowId, row);
|
|
1285
|
+
}
|
|
1286
|
+
getRow(id) {
|
|
1287
|
+
const row = this.rows.get(id);
|
|
1288
|
+
if (!row) {
|
|
1289
|
+
throw new Error("Invalid id");
|
|
1290
|
+
}
|
|
1291
|
+
return row.getPlainValue();
|
|
1292
|
+
}
|
|
1293
|
+
getRows() {
|
|
1294
|
+
return [...this.rows].map(([id, data]) => ({
|
|
1295
|
+
id,
|
|
1296
|
+
data: data.getPlainValue()
|
|
1297
|
+
}));
|
|
1298
|
+
}
|
|
1299
|
+
migrateRows(nextStore) {
|
|
1300
|
+
const transformation = getTransformation(this.store, nextStore);
|
|
1301
|
+
if (transformation) {
|
|
1302
|
+
for (const [rowId, row] of this.rows) {
|
|
1303
|
+
const rawNextValue = transformation(
|
|
1304
|
+
row.getPlainValue(),
|
|
1305
|
+
nextStore.default
|
|
1306
|
+
);
|
|
1307
|
+
const nextRow = createJsonValueStore(nextStore, rowId, rawNextValue);
|
|
1308
|
+
this.rows.set(rowId, nextRow);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
this.store = nextStore;
|
|
1312
|
+
}
|
|
1313
|
+
};
|
|
1314
|
+
|
|
1315
|
+
exports.SchemaTable = SchemaTable;
|
|
1316
|
+
exports.VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE = VALIDATE_JSON_FIELD_NAME_ERROR_MESSAGE;
|
|
1317
|
+
exports.addSharedFieldsFromState = addSharedFieldsFromState;
|
|
1318
|
+
exports.applyAddPatch = applyAddPatch;
|
|
1319
|
+
exports.applyMovePatch = applyMovePatch;
|
|
1320
|
+
exports.applyRemovePatch = applyRemovePatch;
|
|
1321
|
+
exports.applyReplacePatch = applyReplacePatch;
|
|
1322
|
+
exports.createJsonArrayValueStore = createJsonArrayValueStore;
|
|
1323
|
+
exports.createJsonObjectSchemaStore = createJsonObjectSchemaStore;
|
|
1324
|
+
exports.createJsonObjectValueStore = createJsonObjectValueStore;
|
|
1325
|
+
exports.createJsonSchemaStore = createJsonSchemaStore;
|
|
1326
|
+
exports.createJsonValueStore = createJsonValueStore;
|
|
1327
|
+
exports.createPrimitiveStoreBySchema = createPrimitiveStoreBySchema;
|
|
1328
|
+
exports.createPrimitiveValueStore = createPrimitiveValueStore;
|
|
1329
|
+
exports.getDBJsonPathByJsonSchemaStore = getDBJsonPathByJsonSchemaStore;
|
|
1330
|
+
exports.getForeignKeyPatchesFromSchema = getForeignKeyPatchesFromSchema;
|
|
1331
|
+
exports.getForeignKeysFromSchema = getForeignKeysFromSchema;
|
|
1332
|
+
exports.getForeignKeysFromValue = getForeignKeysFromValue;
|
|
1333
|
+
exports.getInvalidFieldNamesInSchema = getInvalidFieldNamesInSchema;
|
|
1334
|
+
exports.getJsonSchemaStoreByPath = getJsonSchemaStoreByPath;
|
|
1335
|
+
exports.getJsonValueStoreByPath = getJsonValueStoreByPath;
|
|
1336
|
+
exports.getParentForPath = getParentForPath;
|
|
1337
|
+
exports.getPathByStore = getPathByStore;
|
|
1338
|
+
exports.pluginRefs = pluginRefs;
|
|
1339
|
+
exports.replaceForeignKeyValue = replaceForeignKeyValue;
|
|
1340
|
+
exports.resolveRefs = resolveRefs;
|
|
1341
|
+
exports.saveSharedFields = saveSharedFields;
|
|
1342
|
+
exports.traverseStore = traverseStore;
|
|
1343
|
+
exports.traverseValue = traverseValue;
|
|
1344
|
+
exports.validateJsonFieldName = validateJsonFieldName;
|
|
1345
|
+
//# sourceMappingURL=index.cjs.map
|
|
1346
|
+
//# sourceMappingURL=index.cjs.map
|