@statelyai/flow 0.13.4 → 0.15.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/README.md +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
import type { FlowEdge, FlowGraph, FlowNode, Viewport } from './types';
|
|
2
|
+
import { isValidSvgPath } from './routing';
|
|
3
|
+
|
|
4
|
+
export type FlowResult<T, TError = FlowGraphValidationError> =
|
|
5
|
+
| { ok: true; value: T }
|
|
6
|
+
| { ok: false; error: TError };
|
|
7
|
+
|
|
8
|
+
export type FlowValidationIssue = {
|
|
9
|
+
code: string;
|
|
10
|
+
path: string;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export class FlowGraphValidationError extends Error {
|
|
15
|
+
readonly issues: readonly FlowValidationIssue[];
|
|
16
|
+
|
|
17
|
+
constructor(issues: readonly FlowValidationIssue[]) {
|
|
18
|
+
super(issues.map((issue) => `${issue.path}: ${issue.message}`).join('\n'));
|
|
19
|
+
this.name = 'FlowGraphValidationError';
|
|
20
|
+
this.issues = issues;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StandardSchemaV1Like<TOutput = unknown> {
|
|
25
|
+
readonly '~standard': {
|
|
26
|
+
readonly validate: (
|
|
27
|
+
value: unknown,
|
|
28
|
+
) =>
|
|
29
|
+
| { value: TOutput; issues?: undefined }
|
|
30
|
+
| { value?: undefined; issues: readonly { message: string; path?: unknown }[] }
|
|
31
|
+
| Promise<unknown>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type FlowDataKind = 'node' | 'edge' | 'label';
|
|
36
|
+
|
|
37
|
+
export type FlowDataSchema<TData = unknown> = {
|
|
38
|
+
version: number;
|
|
39
|
+
schema?: StandardSchemaV1Like<TData>;
|
|
40
|
+
migrate?: (
|
|
41
|
+
data: unknown,
|
|
42
|
+
versions: { fromVersion: number; toVersion: number },
|
|
43
|
+
) => TData;
|
|
44
|
+
remap?: (
|
|
45
|
+
data: TData,
|
|
46
|
+
context: { idMap: ReadonlyMap<string, string> },
|
|
47
|
+
) => TData;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type FlowDataSchemas<
|
|
51
|
+
TNodeData = unknown,
|
|
52
|
+
TEdgeData = unknown,
|
|
53
|
+
TLabelData = unknown,
|
|
54
|
+
> = {
|
|
55
|
+
node?: FlowDataSchema<TNodeData>;
|
|
56
|
+
edge?: FlowDataSchema<TEdgeData>;
|
|
57
|
+
label?: FlowDataSchema<TLabelData>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type FlowDataVersions = Partial<Record<FlowDataKind, number>>;
|
|
61
|
+
|
|
62
|
+
const OMIT = Symbol('omit');
|
|
63
|
+
|
|
64
|
+
function issue(
|
|
65
|
+
issues: FlowValidationIssue[],
|
|
66
|
+
code: string,
|
|
67
|
+
path: string,
|
|
68
|
+
message: string,
|
|
69
|
+
): void {
|
|
70
|
+
issues.push({ code, path, message });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function cloneJsonValue(
|
|
74
|
+
value: unknown,
|
|
75
|
+
path: string,
|
|
76
|
+
issues: FlowValidationIssue[],
|
|
77
|
+
ancestors: Set<object>,
|
|
78
|
+
inArray = false,
|
|
79
|
+
): unknown | typeof OMIT {
|
|
80
|
+
if (value === undefined) {
|
|
81
|
+
if (inArray) {
|
|
82
|
+
issue(issues, 'json_undefined', path, 'undefined is not JSON-safe in an array');
|
|
83
|
+
}
|
|
84
|
+
return OMIT;
|
|
85
|
+
}
|
|
86
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
if (typeof value === 'number') {
|
|
90
|
+
if (!Number.isFinite(value)) {
|
|
91
|
+
issue(issues, 'json_non_finite', path, 'expected a finite number');
|
|
92
|
+
}
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
if (
|
|
96
|
+
typeof value === 'function' &&
|
|
97
|
+
/^\$\.graph\.edges\[\d+\]\.labelAnchor$/.test(path)
|
|
98
|
+
) {
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
if (
|
|
102
|
+
typeof value === 'function' ||
|
|
103
|
+
typeof value === 'symbol' ||
|
|
104
|
+
typeof value === 'bigint'
|
|
105
|
+
) {
|
|
106
|
+
issue(issues, 'json_unsupported', path, `${typeof value} is not JSON-safe`);
|
|
107
|
+
return OMIT;
|
|
108
|
+
}
|
|
109
|
+
if (typeof value !== 'object') {
|
|
110
|
+
issue(issues, 'json_unsupported', path, 'unsupported JSON value');
|
|
111
|
+
return OMIT;
|
|
112
|
+
}
|
|
113
|
+
if (ancestors.has(value)) {
|
|
114
|
+
issue(issues, 'json_cycle', path, 'cyclic references are not JSON-safe');
|
|
115
|
+
return OMIT;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const prototype = Object.getPrototypeOf(value);
|
|
119
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
|
|
120
|
+
issue(
|
|
121
|
+
issues,
|
|
122
|
+
'json_non_plain_object',
|
|
123
|
+
path,
|
|
124
|
+
`${value.constructor?.name ?? 'non-plain object'} is not JSON-safe`,
|
|
125
|
+
);
|
|
126
|
+
return OMIT;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
ancestors.add(value);
|
|
130
|
+
if (Array.isArray(value)) {
|
|
131
|
+
const result = value.map((entry, index) => {
|
|
132
|
+
const cloned = cloneJsonValue(entry, `${path}[${index}]`, issues, ancestors, true);
|
|
133
|
+
return cloned === OMIT ? null : cloned;
|
|
134
|
+
});
|
|
135
|
+
ancestors.delete(value);
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const result: Record<string, unknown> = {};
|
|
140
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
141
|
+
const cloned = cloneJsonValue(entry, `${path}.${key}`, issues, ancestors);
|
|
142
|
+
if (cloned !== OMIT) {
|
|
143
|
+
Object.defineProperty(result, key, {
|
|
144
|
+
value: cloned,
|
|
145
|
+
enumerable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
configurable: true,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
ancestors.delete(value);
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function cloneJsonSafe<T>(value: T, path = '$'): T {
|
|
156
|
+
const issues: FlowValidationIssue[] = [];
|
|
157
|
+
const cloned = cloneJsonValue(value, path, issues, new Set());
|
|
158
|
+
if (cloned === OMIT) {
|
|
159
|
+
issue(issues, 'json_unsupported', path, 'expected a JSON-safe value');
|
|
160
|
+
}
|
|
161
|
+
if (issues.length > 0) throw new FlowGraphValidationError(issues);
|
|
162
|
+
return cloned as T;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function validateFiniteGeometry(
|
|
166
|
+
value: Record<string, unknown>,
|
|
167
|
+
path: string,
|
|
168
|
+
issues: FlowValidationIssue[],
|
|
169
|
+
): void {
|
|
170
|
+
for (const key of ['x', 'y', 'width', 'height'] as const) {
|
|
171
|
+
if (typeof value[key] !== 'number' || !Number.isFinite(value[key])) {
|
|
172
|
+
issue(issues, 'invalid_geometry', `${path}.${key}`, 'expected a finite number');
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
for (const key of ['dx', 'dy'] as const) {
|
|
176
|
+
if (
|
|
177
|
+
value[key] !== undefined &&
|
|
178
|
+
(typeof value[key] !== 'number' || !Number.isFinite(value[key]))
|
|
179
|
+
) {
|
|
180
|
+
issue(issues, 'invalid_geometry', `${path}.${key}`, 'expected a finite number');
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function validateAutoSizeAnchor(
|
|
186
|
+
value: unknown,
|
|
187
|
+
path: string,
|
|
188
|
+
issues: FlowValidationIssue[],
|
|
189
|
+
): void {
|
|
190
|
+
if (value === undefined || value === 'top-left' || value === 'center') return;
|
|
191
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
192
|
+
issue(
|
|
193
|
+
issues,
|
|
194
|
+
'invalid_auto_size_anchor',
|
|
195
|
+
path,
|
|
196
|
+
'expected "top-left", "center", or fractional { x, y }',
|
|
197
|
+
);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const anchor = value as Record<string, unknown>;
|
|
201
|
+
for (const axis of ['x', 'y'] as const) {
|
|
202
|
+
if (
|
|
203
|
+
typeof anchor[axis] !== 'number' ||
|
|
204
|
+
!Number.isFinite(anchor[axis]) ||
|
|
205
|
+
anchor[axis] < 0 ||
|
|
206
|
+
anchor[axis] > 1
|
|
207
|
+
) {
|
|
208
|
+
issue(
|
|
209
|
+
issues,
|
|
210
|
+
'invalid_auto_size_anchor',
|
|
211
|
+
`${path}.${axis}`,
|
|
212
|
+
'expected a finite fraction from 0 through 1',
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function formatSchemaPath(path: unknown): string {
|
|
219
|
+
if (!Array.isArray(path)) return '';
|
|
220
|
+
return path
|
|
221
|
+
.map((segment) =>
|
|
222
|
+
typeof segment === 'object' && segment && 'key' in segment
|
|
223
|
+
? String((segment as { key: unknown }).key)
|
|
224
|
+
: String(segment),
|
|
225
|
+
)
|
|
226
|
+
.filter(Boolean)
|
|
227
|
+
.join('.');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function validateData<TData>(
|
|
231
|
+
value: unknown,
|
|
232
|
+
contract: FlowDataSchema<TData> | undefined,
|
|
233
|
+
path: string,
|
|
234
|
+
issues: FlowValidationIssue[],
|
|
235
|
+
): unknown {
|
|
236
|
+
if (!contract?.schema) return value;
|
|
237
|
+
const result = contract.schema['~standard'].validate(value);
|
|
238
|
+
if (result && typeof (result as { then?: unknown }).then === 'function') {
|
|
239
|
+
issue(issues, 'async_schema', path, 'Flow data validation must be synchronous');
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
const syncResult = result as Exclude<typeof result, Promise<unknown>>;
|
|
243
|
+
if (syncResult.issues) {
|
|
244
|
+
for (const schemaIssue of syncResult.issues) {
|
|
245
|
+
const suffix = formatSchemaPath(schemaIssue.path);
|
|
246
|
+
issue(
|
|
247
|
+
issues,
|
|
248
|
+
'invalid_app_data',
|
|
249
|
+
suffix ? `${path}.${suffix}` : path,
|
|
250
|
+
schemaIssue.message,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
return value;
|
|
254
|
+
}
|
|
255
|
+
return syncResult.value;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function validateNodeData<TNodeData>(
|
|
259
|
+
node: FlowNode<TNodeData>,
|
|
260
|
+
index: number,
|
|
261
|
+
schema: FlowDataSchema<TNodeData> | undefined,
|
|
262
|
+
issues: FlowValidationIssue[],
|
|
263
|
+
): void {
|
|
264
|
+
node.data = validateData(node.data, schema, `$.graph.nodes[${index}].data`, issues) as TNodeData;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function validateEdgeData<TEdgeData, TLabelData>(
|
|
268
|
+
edge: FlowEdge<TEdgeData, TLabelData>,
|
|
269
|
+
index: number,
|
|
270
|
+
schemas: Pick<FlowDataSchemas<unknown, TEdgeData, TLabelData>, 'edge' | 'label'> | undefined,
|
|
271
|
+
issues: FlowValidationIssue[],
|
|
272
|
+
): void {
|
|
273
|
+
edge.data = validateData(edge.data, schemas?.edge, `$.graph.edges[${index}].data`, issues) as TEdgeData;
|
|
274
|
+
if (edge.label != null) {
|
|
275
|
+
if (typeof edge.label !== 'object' || Array.isArray(edge.label)) {
|
|
276
|
+
issue(
|
|
277
|
+
issues,
|
|
278
|
+
'invalid_label',
|
|
279
|
+
`$.graph.edges[${index}].label`,
|
|
280
|
+
'expected a label object',
|
|
281
|
+
);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
edge.label.data = validateData(
|
|
285
|
+
edge.label.data,
|
|
286
|
+
schemas?.label,
|
|
287
|
+
`$.graph.edges[${index}].label.data`,
|
|
288
|
+
issues,
|
|
289
|
+
) as TLabelData;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function validateAppearance(value: unknown, path: string, issues: FlowValidationIssue[]): void {
|
|
294
|
+
if (value === undefined) return;
|
|
295
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) ||
|
|
296
|
+
typeof (value as { name?: unknown }).name !== 'string' || !(value as { name: string }).name.length) {
|
|
297
|
+
issue(issues, 'invalid_appearance', path, 'expected an appearance with a non-empty name');
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const options = (value as { options?: unknown }).options;
|
|
301
|
+
if (options !== undefined && (!options || typeof options !== 'object' || Array.isArray(options))) {
|
|
302
|
+
issue(issues, 'invalid_appearance', `${path}.options`, 'expected an options object');
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function validateAndCloneGraph<
|
|
307
|
+
TNodeData = unknown,
|
|
308
|
+
TEdgeData = unknown,
|
|
309
|
+
TLabelData = unknown,
|
|
310
|
+
>(
|
|
311
|
+
input: unknown,
|
|
312
|
+
schemas?: FlowDataSchemas<TNodeData, TEdgeData, TLabelData>,
|
|
313
|
+
): FlowGraph<TNodeData, TEdgeData, TLabelData> {
|
|
314
|
+
const issues: FlowValidationIssue[] = [];
|
|
315
|
+
const cloned = cloneJsonValue(input, '$.graph', issues, new Set());
|
|
316
|
+
if (!cloned || cloned === OMIT || typeof cloned !== 'object' || Array.isArray(cloned)) {
|
|
317
|
+
issue(issues, 'invalid_graph', '$.graph', 'expected a graph object');
|
|
318
|
+
throw new FlowGraphValidationError(issues);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const graph = cloned as FlowGraph<TNodeData, TEdgeData, TLabelData>;
|
|
322
|
+
if (!Array.isArray(graph.nodes)) {
|
|
323
|
+
issue(issues, 'invalid_nodes', '$.graph.nodes', 'expected an array');
|
|
324
|
+
}
|
|
325
|
+
if (!Array.isArray(graph.edges)) {
|
|
326
|
+
issue(issues, 'invalid_edges', '$.graph.edges', 'expected an array');
|
|
327
|
+
}
|
|
328
|
+
if (issues.length > 0) throw new FlowGraphValidationError(issues);
|
|
329
|
+
|
|
330
|
+
const ids = new Map<string, string>();
|
|
331
|
+
const nodesById = new Map<string, FlowNode<TNodeData>>();
|
|
332
|
+
graph.nodes.forEach((node, index) => {
|
|
333
|
+
const path = `$.graph.nodes[${index}]`;
|
|
334
|
+
if (!node || typeof node !== 'object' || Array.isArray(node)) {
|
|
335
|
+
issue(issues, 'invalid_node', path, 'expected a node object');
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (typeof node.id !== 'string' || node.id.length === 0) {
|
|
339
|
+
issue(issues, 'invalid_id', `${path}.id`, 'expected a non-empty string');
|
|
340
|
+
} else if (ids.has(node.id)) {
|
|
341
|
+
issue(issues, 'duplicate_id', `${path}.id`, `duplicate global entity id "${node.id}"`);
|
|
342
|
+
} else {
|
|
343
|
+
ids.set(node.id, path);
|
|
344
|
+
nodesById.set(node.id, node);
|
|
345
|
+
}
|
|
346
|
+
validateFiniteGeometry(node as unknown as Record<string, unknown>, path, issues);
|
|
347
|
+
validateAppearance(node.appearance, `${path}.appearance`, issues);
|
|
348
|
+
validateAutoSizeAnchor(node.autoSizeAnchor, `${path}.autoSizeAnchor`, issues);
|
|
349
|
+
validateNodeData(node, index, schemas?.node, issues);
|
|
350
|
+
if (node.ports !== undefined) {
|
|
351
|
+
if (!Array.isArray(node.ports)) {
|
|
352
|
+
issue(issues, 'invalid_ports', `${path}.ports`, 'expected an array');
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const portNames = new Set<string>();
|
|
356
|
+
node.ports.forEach((port, portIndex) => {
|
|
357
|
+
const portPath = `${path}.ports[${portIndex}]`;
|
|
358
|
+
if (!port || typeof port !== 'object' || Array.isArray(port)) {
|
|
359
|
+
issue(issues, 'invalid_port', portPath, 'expected a port object');
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (typeof port.name !== 'string' || port.name.length === 0) {
|
|
363
|
+
issue(issues, 'invalid_port', `${portPath}.name`, 'expected a non-empty string');
|
|
364
|
+
} else if (portNames.has(port.name)) {
|
|
365
|
+
issue(issues, 'duplicate_port', `${portPath}.name`, `duplicate port "${port.name}"`);
|
|
366
|
+
} else {
|
|
367
|
+
portNames.add(port.name);
|
|
368
|
+
}
|
|
369
|
+
validateFiniteGeometry(port as unknown as Record<string, unknown>, portPath, issues);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
graph.edges.forEach((edge, index) => {
|
|
375
|
+
const path = `$.graph.edges[${index}]`;
|
|
376
|
+
if (!edge || typeof edge !== 'object' || Array.isArray(edge)) {
|
|
377
|
+
issue(issues, 'invalid_edge', path, 'expected an edge object');
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (typeof edge.id !== 'string' || edge.id.length === 0) {
|
|
381
|
+
issue(issues, 'invalid_id', `${path}.id`, 'expected a non-empty string');
|
|
382
|
+
} else if (ids.has(edge.id)) {
|
|
383
|
+
issue(issues, 'duplicate_id', `${path}.id`, `duplicate global entity id "${edge.id}"`);
|
|
384
|
+
} else {
|
|
385
|
+
ids.set(edge.id, path);
|
|
386
|
+
}
|
|
387
|
+
validateFiniteGeometry(edge as unknown as Record<string, unknown>, path, issues);
|
|
388
|
+
validateEdgeData(edge, index, schemas, issues);
|
|
389
|
+
for (const [key, nodeId] of [
|
|
390
|
+
['sourceId', edge.sourceId],
|
|
391
|
+
['targetId', edge.targetId],
|
|
392
|
+
] as const) {
|
|
393
|
+
if (typeof nodeId !== 'string' || !nodesById.has(nodeId)) {
|
|
394
|
+
issue(issues, 'missing_endpoint', `${path}.${key}`, `node "${String(nodeId)}" does not exist`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
for (const [key, nodeId, portName] of [
|
|
398
|
+
['sourcePort', edge.sourceId, edge.sourcePort],
|
|
399
|
+
['targetPort', edge.targetId, edge.targetPort],
|
|
400
|
+
] as const) {
|
|
401
|
+
if (portName == null || portName === '*') continue;
|
|
402
|
+
const node = nodesById.get(nodeId);
|
|
403
|
+
if (
|
|
404
|
+
!Array.isArray(node?.ports) ||
|
|
405
|
+
!node.ports.some(
|
|
406
|
+
(port) =>
|
|
407
|
+
!!port &&
|
|
408
|
+
typeof port === 'object' &&
|
|
409
|
+
!Array.isArray(port) &&
|
|
410
|
+
port.name === portName,
|
|
411
|
+
)
|
|
412
|
+
) {
|
|
413
|
+
issue(issues, 'missing_port', `${path}.${key}`, `port "${portName}" does not exist on node "${nodeId}"`);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (edge.label && typeof edge.label === "object") validateAppearance(edge.label.appearance, `${path}.label.appearance`, issues);
|
|
417
|
+
if (edge.label && typeof edge.label === 'object' && !Array.isArray(edge.label)) {
|
|
418
|
+
validateFiniteGeometry(
|
|
419
|
+
edge.label as unknown as Record<string, unknown>,
|
|
420
|
+
`${path}.label`,
|
|
421
|
+
issues,
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
if (edge.waypoints !== undefined) {
|
|
425
|
+
if (!Array.isArray(edge.waypoints)) {
|
|
426
|
+
issue(issues, 'invalid_waypoints', `${path}.waypoints`, 'expected an array');
|
|
427
|
+
} else {
|
|
428
|
+
edge.waypoints.forEach((point, pointIndex) => {
|
|
429
|
+
if (
|
|
430
|
+
!point ||
|
|
431
|
+
typeof point !== 'object' ||
|
|
432
|
+
Array.isArray(point) ||
|
|
433
|
+
!Number.isFinite(point.x) ||
|
|
434
|
+
!Number.isFinite(point.y)
|
|
435
|
+
) {
|
|
436
|
+
issue(
|
|
437
|
+
issues,
|
|
438
|
+
'invalid_geometry',
|
|
439
|
+
`${path}.waypoints[${pointIndex}]`,
|
|
440
|
+
'expected finite x/y',
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (edge.points !== undefined) {
|
|
447
|
+
if (!Array.isArray(edge.points)) {
|
|
448
|
+
issue(
|
|
449
|
+
issues,
|
|
450
|
+
'invalid_points',
|
|
451
|
+
`${path}.points`,
|
|
452
|
+
'expected an array of points',
|
|
453
|
+
);
|
|
454
|
+
} else {
|
|
455
|
+
edge.points.forEach((point, pointIndex) => {
|
|
456
|
+
if (
|
|
457
|
+
!point ||
|
|
458
|
+
typeof point !== 'object' ||
|
|
459
|
+
Array.isArray(point) ||
|
|
460
|
+
!Number.isFinite(point.x) ||
|
|
461
|
+
!Number.isFinite(point.y)
|
|
462
|
+
) {
|
|
463
|
+
issue(
|
|
464
|
+
issues,
|
|
465
|
+
'invalid_geometry',
|
|
466
|
+
`${path}.points[${pointIndex}]`,
|
|
467
|
+
'expected finite x/y',
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (
|
|
474
|
+
edge.routing !== undefined &&
|
|
475
|
+
edge.routing !== 'polyline' &&
|
|
476
|
+
edge.routing !== 'orthogonal' &&
|
|
477
|
+
edge.routing !== 'splines'
|
|
478
|
+
) {
|
|
479
|
+
issue(
|
|
480
|
+
issues,
|
|
481
|
+
'invalid_routing',
|
|
482
|
+
`${path}.routing`,
|
|
483
|
+
'expected polyline, orthogonal, or splines',
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
if (edge.svgPath !== undefined && !isValidSvgPath(edge.svgPath)) {
|
|
487
|
+
issue(
|
|
488
|
+
issues,
|
|
489
|
+
'invalid_svg_path',
|
|
490
|
+
`${path}.svgPath`,
|
|
491
|
+
'expected a finite SVG path beginning with M or m',
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
for (const [index, node] of graph.nodes.entries()) {
|
|
497
|
+
if (!node || typeof node !== 'object' || Array.isArray(node)) continue;
|
|
498
|
+
if (node.parentId == null) continue;
|
|
499
|
+
if (!nodesById.has(node.parentId)) {
|
|
500
|
+
issue(
|
|
501
|
+
issues,
|
|
502
|
+
'missing_parent',
|
|
503
|
+
`$.graph.nodes[${index}].parentId`,
|
|
504
|
+
`parent node "${node.parentId}" does not exist`,
|
|
505
|
+
);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
const seen = new Set([node.id]);
|
|
509
|
+
let parentId: string | null | undefined = node.parentId;
|
|
510
|
+
while (parentId != null) {
|
|
511
|
+
if (seen.has(parentId)) {
|
|
512
|
+
issue(
|
|
513
|
+
issues,
|
|
514
|
+
'parent_cycle',
|
|
515
|
+
`$.graph.nodes[${index}].parentId`,
|
|
516
|
+
`parent cycle includes "${parentId}"`,
|
|
517
|
+
);
|
|
518
|
+
break;
|
|
519
|
+
}
|
|
520
|
+
seen.add(parentId);
|
|
521
|
+
parentId = nodesById.get(parentId)?.parentId;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (issues.length > 0) throw new FlowGraphValidationError(issues);
|
|
526
|
+
return graph;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export function validateViewport(input: unknown, path = '$.viewport'): Viewport {
|
|
530
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
531
|
+
throw new FlowGraphValidationError([
|
|
532
|
+
{ code: 'invalid_viewport', path, message: 'expected a viewport object' },
|
|
533
|
+
]);
|
|
534
|
+
}
|
|
535
|
+
const viewport = input as Viewport;
|
|
536
|
+
const issues: FlowValidationIssue[] = [];
|
|
537
|
+
for (const key of ['x', 'y', 'zoom'] as const) {
|
|
538
|
+
if (typeof viewport[key] !== 'number' || !Number.isFinite(viewport[key])) {
|
|
539
|
+
issue(issues, 'invalid_viewport', `${path}.${key}`, 'expected a finite number');
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (viewport.zoom <= 0) {
|
|
543
|
+
issue(issues, 'invalid_viewport', `${path}.zoom`, 'expected a positive zoom');
|
|
544
|
+
}
|
|
545
|
+
if (issues.length > 0) throw new FlowGraphValidationError(issues);
|
|
546
|
+
return { ...viewport };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function currentDataVersions<TNodeData, TEdgeData, TLabelData>(
|
|
550
|
+
schemas: FlowDataSchemas<TNodeData, TEdgeData, TLabelData> | undefined,
|
|
551
|
+
): FlowDataVersions | undefined {
|
|
552
|
+
if (!schemas) return undefined;
|
|
553
|
+
const versions: FlowDataVersions = {};
|
|
554
|
+
for (const kind of ['node', 'edge', 'label'] as const) {
|
|
555
|
+
if (schemas[kind]) versions[kind] = schemas[kind]!.version;
|
|
556
|
+
}
|
|
557
|
+
return Object.keys(versions).length > 0 ? versions : undefined;
|
|
558
|
+
}
|