@revisium/schema-toolkit 0.12.1 → 0.14.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.
@@ -0,0 +1,589 @@
1
+ import { parseExpression, buildDependencyGraph, detectCircularDependencies, validateFormulaSyntax, inferFormulaType, parseFormula, getTopologicalOrder, evaluateWithContext } from '@revisium/formula';
2
+ export { formulaSpec } from '@revisium/formula/spec';
3
+
4
+ // src/lib/extract-schema-formulas.ts
5
+ function extractSchemaFormulas(schema) {
6
+ const formulas = [];
7
+ extractFormulasRecursive(schema, "", formulas);
8
+ return formulas;
9
+ }
10
+ function extractFormulasRecursive(schema, pathPrefix, formulas) {
11
+ if (schema.type === "array" && schema.items) {
12
+ extractFormulasRecursive(schema.items, `${pathPrefix}[]`, formulas);
13
+ return;
14
+ }
15
+ const properties = schema.properties ?? {};
16
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
17
+ const fullPath = pathPrefix ? `${pathPrefix}.${fieldName}` : fieldName;
18
+ const xFormula = fieldSchema["x-formula"];
19
+ if (xFormula) {
20
+ formulas.push({
21
+ fieldName: fullPath,
22
+ expression: xFormula.expression,
23
+ fieldType: fieldSchema.type ?? "string"
24
+ });
25
+ }
26
+ if (fieldSchema.type === "object" && fieldSchema.properties) {
27
+ extractFormulasRecursive(fieldSchema, fullPath, formulas);
28
+ }
29
+ if (fieldSchema.type === "array" && fieldSchema.items) {
30
+ extractFormulasRecursive(fieldSchema.items, `${fullPath}[]`, formulas);
31
+ }
32
+ }
33
+ }
34
+ function validateSchemaFormulas(schema) {
35
+ const errors = [];
36
+ const formulas = extractSchemaFormulas(schema);
37
+ for (const formula of formulas) {
38
+ const error = validateFormulaAgainstSchema(
39
+ formula.expression,
40
+ formula.fieldName,
41
+ schema
42
+ );
43
+ if (error) {
44
+ errors.push(error);
45
+ }
46
+ }
47
+ if (errors.length > 0) {
48
+ return { isValid: false, errors };
49
+ }
50
+ const dependencies = {};
51
+ for (const formula of formulas) {
52
+ const parseResult = parseExpression(formula.expression);
53
+ const parentPath = getParentPath(formula.fieldName);
54
+ const prefix = parentPath ? `${parentPath}.` : "";
55
+ dependencies[formula.fieldName] = parseResult.dependencies.map((dep) => {
56
+ if (dep.startsWith("/")) {
57
+ return extractFieldRoot(dep.slice(1));
58
+ }
59
+ if (dep.startsWith("../")) {
60
+ return resolveRelativePathForDependency(dep, parentPath);
61
+ }
62
+ const rootField = extractFieldRoot(dep);
63
+ return `${prefix}${rootField}`;
64
+ });
65
+ }
66
+ const graph = buildDependencyGraph(dependencies);
67
+ const circularCheck = detectCircularDependencies(graph);
68
+ const cycle = circularCheck.cycle;
69
+ if (circularCheck.hasCircular && cycle && cycle.length > 0) {
70
+ const firstField = cycle[0];
71
+ if (firstField) {
72
+ errors.push({
73
+ field: firstField,
74
+ error: `Circular dependency: ${cycle.join(" \u2192 ")}`
75
+ });
76
+ return { isValid: false, errors };
77
+ }
78
+ }
79
+ return { isValid: true, errors: [] };
80
+ }
81
+ function validateFormulaAgainstSchema(expression, fieldName, schema) {
82
+ return validateFormulaInContext(expression, fieldName, schema);
83
+ }
84
+ function validateFormulaInContext(expression, fieldPath, rootSchema) {
85
+ const syntaxResult = validateFormulaSyntax(expression);
86
+ if (!syntaxResult.isValid) {
87
+ return {
88
+ field: fieldPath,
89
+ error: syntaxResult.error,
90
+ position: syntaxResult.position
91
+ };
92
+ }
93
+ const parentPath = getParentPath(fieldPath);
94
+ const localFieldName = getFieldName(fieldPath);
95
+ const contextSchema = resolveSubSchema(rootSchema, parentPath);
96
+ if (!contextSchema) {
97
+ return {
98
+ field: fieldPath,
99
+ error: `Cannot resolve schema context for path '${parentPath}'`
100
+ };
101
+ }
102
+ const parseResult = parseExpression(expression);
103
+ const localSchemaFields = getSchemaFields(contextSchema);
104
+ const rootSchemaFields = getSchemaFields(rootSchema);
105
+ for (const dep of parseResult.dependencies) {
106
+ if (dep.startsWith("/")) {
107
+ const rootField = extractFieldRoot(dep.slice(1));
108
+ if (!rootSchemaFields.has(rootField)) {
109
+ return {
110
+ field: fieldPath,
111
+ error: `Unknown root field '${rootField}' in formula`
112
+ };
113
+ }
114
+ } else if (dep.startsWith("../")) {
115
+ const validationResult = validateRelativePath(
116
+ dep,
117
+ parentPath,
118
+ rootSchema,
119
+ fieldPath
120
+ );
121
+ if (validationResult) {
122
+ return validationResult;
123
+ }
124
+ } else {
125
+ const rootField = extractFieldRoot(dep);
126
+ if (!localSchemaFields.has(rootField)) {
127
+ return {
128
+ field: fieldPath,
129
+ error: `Unknown field '${rootField}' in formula`
130
+ };
131
+ }
132
+ }
133
+ }
134
+ if (parseResult.dependencies.some((d) => extractFieldRoot(d) === localFieldName)) {
135
+ return {
136
+ field: fieldPath,
137
+ error: `Formula cannot reference itself`
138
+ };
139
+ }
140
+ const fieldSchema = contextSchema.properties?.[localFieldName];
141
+ const expectedType = schemaTypeToInferred(fieldSchema?.type);
142
+ const fieldTypes = getSchemaFieldTypes(contextSchema);
143
+ const inferredType = inferFormulaType(expression, fieldTypes);
144
+ if (!isTypeCompatible(inferredType, expectedType)) {
145
+ return {
146
+ field: fieldPath,
147
+ error: `Type mismatch: formula returns '${inferredType}' but field expects '${expectedType}'`
148
+ };
149
+ }
150
+ return null;
151
+ }
152
+ function resolveSubSchema(schema, fieldPath) {
153
+ if (!fieldPath) {
154
+ return schema;
155
+ }
156
+ const segments = parsePathSegmentsForSchema(fieldPath);
157
+ let current = schema;
158
+ for (const segment of segments) {
159
+ if (segment === "[]") {
160
+ if (current.type === "array" && current.items) {
161
+ current = current.items;
162
+ } else {
163
+ return null;
164
+ }
165
+ } else if (current.properties?.[segment]) {
166
+ current = current.properties[segment];
167
+ } else {
168
+ return null;
169
+ }
170
+ }
171
+ return current;
172
+ }
173
+ function parsePathSegmentsForSchema(path) {
174
+ const segments = [];
175
+ let current = "";
176
+ let inBracket = false;
177
+ for (const char of path) {
178
+ if (char === "[") {
179
+ if (current) {
180
+ segments.push(current);
181
+ current = "";
182
+ }
183
+ inBracket = true;
184
+ } else if (char === "]") {
185
+ inBracket = false;
186
+ segments.push("[]");
187
+ } else if (char === "." && !inBracket) {
188
+ if (current) {
189
+ segments.push(current);
190
+ current = "";
191
+ }
192
+ } else if (!inBracket) {
193
+ current += char;
194
+ }
195
+ }
196
+ if (current) {
197
+ segments.push(current);
198
+ }
199
+ return segments;
200
+ }
201
+ function parsePathSegments(path) {
202
+ const segments = [];
203
+ let current = "";
204
+ let inBracket = false;
205
+ for (const char of path) {
206
+ if (char === "[") {
207
+ if (current) {
208
+ segments.push(current);
209
+ current = "";
210
+ }
211
+ inBracket = true;
212
+ } else if (char === "]") {
213
+ inBracket = false;
214
+ } else if (char === "." && !inBracket) {
215
+ if (current) {
216
+ segments.push(current);
217
+ current = "";
218
+ }
219
+ } else if (!inBracket) {
220
+ current += char;
221
+ }
222
+ }
223
+ if (current) {
224
+ segments.push(current);
225
+ }
226
+ return segments;
227
+ }
228
+ function getParentPath(fieldPath) {
229
+ const lastDotIndex = fieldPath.lastIndexOf(".");
230
+ const lastBracketIndex = fieldPath.lastIndexOf("[");
231
+ const splitIndex = Math.max(lastDotIndex, lastBracketIndex);
232
+ if (splitIndex <= 0) {
233
+ return "";
234
+ }
235
+ return fieldPath.substring(0, splitIndex);
236
+ }
237
+ function getFieldName(fieldPath) {
238
+ const lastDotIndex = fieldPath.lastIndexOf(".");
239
+ const lastBracketIndex = fieldPath.lastIndexOf("]");
240
+ const splitIndex = Math.max(lastDotIndex, lastBracketIndex);
241
+ if (splitIndex === -1) {
242
+ return fieldPath;
243
+ }
244
+ return fieldPath.substring(splitIndex + 1);
245
+ }
246
+ function getSchemaFields(schema) {
247
+ const fields = /* @__PURE__ */ new Set();
248
+ const properties = schema.properties ?? {};
249
+ for (const fieldName of Object.keys(properties)) {
250
+ fields.add(fieldName);
251
+ }
252
+ return fields;
253
+ }
254
+ function getSchemaFieldTypes(schema) {
255
+ const fieldTypes = {};
256
+ const properties = schema.properties ?? {};
257
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
258
+ const schemaType = fieldSchema.type;
259
+ if (schemaType === "integer") {
260
+ fieldTypes[fieldName] = "number";
261
+ } else if (schemaType === "number" || schemaType === "string" || schemaType === "boolean" || schemaType === "object" || schemaType === "array") {
262
+ fieldTypes[fieldName] = schemaType;
263
+ }
264
+ }
265
+ return fieldTypes;
266
+ }
267
+ function schemaTypeToInferred(schemaType) {
268
+ if (schemaType === "number" || schemaType === "integer") return "number";
269
+ if (schemaType === "string") return "string";
270
+ if (schemaType === "boolean") return "boolean";
271
+ return null;
272
+ }
273
+ function isTypeCompatible(inferredType, expectedType) {
274
+ if (expectedType === null) return true;
275
+ if (inferredType === "unknown") return true;
276
+ return inferredType === expectedType;
277
+ }
278
+ function extractFieldRoot(dependency) {
279
+ const root = dependency.split(".")[0]?.split("[")[0];
280
+ return root || dependency;
281
+ }
282
+ function countParentLevels(path) {
283
+ let count = 0;
284
+ let remaining = path;
285
+ while (remaining.startsWith("../")) {
286
+ count++;
287
+ remaining = remaining.slice(3);
288
+ }
289
+ return count;
290
+ }
291
+ function resolveRelativePathForDependency(relativePath, currentPath) {
292
+ const parentLevels = countParentLevels(relativePath);
293
+ const fieldAfterParents = relativePath.replace(/^(\.\.\/)+/, "");
294
+ const targetField = extractFieldRoot(fieldAfterParents);
295
+ if (!currentPath) {
296
+ return targetField;
297
+ }
298
+ const pathSegments = parsePathSegments(currentPath);
299
+ const targetLevel = pathSegments.length - parentLevels;
300
+ if (targetLevel <= 0) {
301
+ return targetField;
302
+ }
303
+ const basePath = pathSegments.slice(0, targetLevel).join(".");
304
+ return basePath ? `${basePath}.${targetField}` : targetField;
305
+ }
306
+ function validateRelativePath(relativePath, currentPath, rootSchema, fieldPath) {
307
+ const parentLevels = countParentLevels(relativePath);
308
+ const fieldAfterParents = relativePath.replace(/^(\.\.\/)+/, "");
309
+ const targetField = extractFieldRoot(fieldAfterParents);
310
+ const pathSegments = parsePathSegments(currentPath);
311
+ const targetLevel = pathSegments.length - parentLevels;
312
+ if (targetLevel <= 0) {
313
+ const rootFields = getSchemaFields(rootSchema);
314
+ if (!rootFields.has(targetField)) {
315
+ return {
316
+ field: fieldPath,
317
+ error: `Unknown root field '${targetField}' in formula`
318
+ };
319
+ }
320
+ return null;
321
+ }
322
+ const targetPath = pathSegments.slice(0, targetLevel).join(".");
323
+ const targetSchema = resolveSubSchema(rootSchema, targetPath);
324
+ if (!targetSchema) {
325
+ return {
326
+ field: fieldPath,
327
+ error: `Cannot resolve schema for relative path '${relativePath}'`
328
+ };
329
+ }
330
+ const targetFields = getSchemaFields(targetSchema);
331
+ if (!targetFields.has(targetField)) {
332
+ return {
333
+ field: fieldPath,
334
+ error: `Unknown field '${targetField}' in formula`
335
+ };
336
+ }
337
+ return null;
338
+ }
339
+ var FORMULA_TYPES = /* @__PURE__ */ new Set(["number", "string", "boolean"]);
340
+ function collectFormulaNodes(schema, data) {
341
+ const nodes = [];
342
+ traverseAndCollect(schema, data, "", nodes, { arrayLevels: [] });
343
+ return nodes;
344
+ }
345
+ function traverseAndCollect(schema, data, currentPath, nodes, ctx) {
346
+ if (schema.type === "object" && schema.properties && typeof data === "object" && data !== null) {
347
+ const record = data;
348
+ for (const [fieldName, fieldSchema] of Object.entries(schema.properties)) {
349
+ const fieldPath = currentPath ? `${currentPath}.${fieldName}` : fieldName;
350
+ const fieldValue = record[fieldName];
351
+ if (fieldSchema["x-formula"] && FORMULA_TYPES.has(fieldSchema.type ?? "")) {
352
+ const expression = fieldSchema["x-formula"].expression;
353
+ const parentPath = getParentPath2(fieldPath);
354
+ nodes.push({
355
+ path: fieldPath,
356
+ expression,
357
+ fieldType: fieldSchema.type,
358
+ currentPath: parentPath,
359
+ dependencies: parseDependencies(expression),
360
+ arrayContext: ctx.arrayLevels.length > 0 ? { levels: [...ctx.arrayLevels] } : void 0
361
+ });
362
+ }
363
+ traverseAndCollect(fieldSchema, fieldValue, fieldPath, nodes, ctx);
364
+ }
365
+ }
366
+ if (schema.type === "array" && schema.items && Array.isArray(data)) {
367
+ for (let i = 0; i < data.length; i++) {
368
+ const itemPath = `${currentPath}[${i}]`;
369
+ const arrayLevel = {
370
+ index: i,
371
+ length: data.length,
372
+ prev: i > 0 ? data[i - 1] : null,
373
+ next: i < data.length - 1 ? data[i + 1] : null
374
+ };
375
+ const newCtx = {
376
+ arrayLevels: [arrayLevel, ...ctx.arrayLevels]
377
+ };
378
+ traverseAndCollect(schema.items, data[i], itemPath, nodes, newCtx);
379
+ }
380
+ }
381
+ }
382
+ function parseDependencies(expression) {
383
+ try {
384
+ return parseFormula(expression).dependencies;
385
+ } catch {
386
+ return [];
387
+ }
388
+ }
389
+ function getParentPath2(fieldPath) {
390
+ const lastDotIndex = fieldPath.lastIndexOf(".");
391
+ if (lastDotIndex === -1) {
392
+ return "";
393
+ }
394
+ return fieldPath.substring(0, lastDotIndex);
395
+ }
396
+ function evaluateFormulas(schema, data, options = {}) {
397
+ const nodes = collectFormulaNodes(schema, data);
398
+ if (nodes.length === 0) {
399
+ return { values: {}, errors: [] };
400
+ }
401
+ const sortedNodes = orderByDependencies(nodes);
402
+ const values = {};
403
+ const errors = [];
404
+ const failedPaths = /* @__PURE__ */ new Set();
405
+ for (const node of sortedNodes) {
406
+ const hasDependencyFailure = hasFailedDependency(node, failedPaths);
407
+ if (hasDependencyFailure) {
408
+ failedPaths.add(node.path);
409
+ handleError(node, "Dependency formula failed", data, values, errors, options);
410
+ continue;
411
+ }
412
+ const result = evaluateNode(node, data);
413
+ if (!result.success) {
414
+ failedPaths.add(node.path);
415
+ handleError(node, result.error, data, values, errors, options);
416
+ continue;
417
+ }
418
+ setValueByPath(data, node.path, result.value);
419
+ values[node.path] = result.value;
420
+ }
421
+ return { values, errors };
422
+ }
423
+ function orderByDependencies(nodes) {
424
+ if (nodes.length <= 1) {
425
+ return nodes;
426
+ }
427
+ const dependencies = Object.fromEntries(
428
+ nodes.map((n) => [n.path, n.dependencies])
429
+ );
430
+ const result = getTopologicalOrder(buildDependencyGraph(dependencies));
431
+ if (!result.success) {
432
+ throw new Error(
433
+ `Cyclic dependency detected in formulas: ${result.error ?? "unknown error"}`
434
+ );
435
+ }
436
+ const nodeMap = new Map(nodes.map((n) => [n.path, n]));
437
+ return result.order.map((path) => nodeMap.get(path)).filter((n) => n !== void 0);
438
+ }
439
+ function hasFailedDependency(node, failedPaths) {
440
+ return node.dependencies.some((dep) => {
441
+ for (const failedPath of failedPaths) {
442
+ if (failedPath === dep || failedPath.endsWith(`.${dep}`)) {
443
+ return true;
444
+ }
445
+ }
446
+ return false;
447
+ });
448
+ }
449
+ function evaluateNode(node, data) {
450
+ try {
451
+ const itemData = node.currentPath ? getValueByPath(data, node.currentPath) : void 0;
452
+ const result = evaluateWithContext(node.expression, {
453
+ rootData: data,
454
+ ...itemData && { itemData, currentPath: node.currentPath },
455
+ arrayContext: node.arrayContext
456
+ });
457
+ if (result === void 0) {
458
+ return { success: false, error: "Formula returned undefined" };
459
+ }
460
+ return { success: true, value: result };
461
+ } catch (error) {
462
+ return {
463
+ success: false,
464
+ error: error instanceof Error ? error.message : String(error)
465
+ };
466
+ }
467
+ }
468
+ function handleError(node, errorMessage, data, values, errors, options) {
469
+ const defaultUsed = options.useDefaults ?? false;
470
+ if (defaultUsed) {
471
+ const defaultValue = getDefaultValue(node, options.defaults);
472
+ setValueByPath(data, node.path, defaultValue);
473
+ values[node.path] = defaultValue;
474
+ }
475
+ errors.push({
476
+ field: node.path,
477
+ expression: node.expression,
478
+ error: errorMessage,
479
+ defaultUsed
480
+ });
481
+ }
482
+ function getDefaultValue(node, defaults) {
483
+ if (defaults && node.path in defaults) {
484
+ return defaults[node.path];
485
+ }
486
+ switch (node.fieldType) {
487
+ case "number":
488
+ return 0;
489
+ case "string":
490
+ return "";
491
+ case "boolean":
492
+ return false;
493
+ }
494
+ }
495
+ function getValueByPath(obj, path) {
496
+ const segments = parsePath(path);
497
+ let current = obj;
498
+ for (const segment of segments) {
499
+ if (current === null || current === void 0) {
500
+ return void 0;
501
+ }
502
+ if (segment.type === "field") {
503
+ current = current[segment.name];
504
+ } else {
505
+ if (!Array.isArray(current)) {
506
+ return void 0;
507
+ }
508
+ current = current[segment.index];
509
+ }
510
+ }
511
+ return current;
512
+ }
513
+ function parsePath(path) {
514
+ const segments = [];
515
+ let current = "";
516
+ let position = 0;
517
+ while (position < path.length) {
518
+ const char = path[position];
519
+ if (char === ".") {
520
+ if (current) {
521
+ segments.push({ type: "field", name: current });
522
+ current = "";
523
+ }
524
+ position++;
525
+ } else if (char === "[") {
526
+ if (current) {
527
+ segments.push({ type: "field", name: current });
528
+ current = "";
529
+ }
530
+ const endBracket = path.indexOf("]", position);
531
+ if (endBracket === -1) {
532
+ position++;
533
+ } else {
534
+ const indexStr = path.slice(position + 1, endBracket);
535
+ segments.push({ type: "index", index: Number.parseInt(indexStr, 10) });
536
+ position = endBracket + 1;
537
+ }
538
+ } else {
539
+ current += char;
540
+ position++;
541
+ }
542
+ }
543
+ if (current) {
544
+ segments.push({ type: "field", name: current });
545
+ }
546
+ return segments;
547
+ }
548
+ function isSafeKey(key) {
549
+ return key !== "__proto__";
550
+ }
551
+ function setValueByPath(obj, path, value) {
552
+ const segments = parsePath(path);
553
+ let current = obj;
554
+ for (let i = 0; i < segments.length - 1; i++) {
555
+ const segment = segments[i];
556
+ if (segment.type === "field") {
557
+ if (!isSafeKey(segment.name)) {
558
+ return;
559
+ }
560
+ const record = current;
561
+ if (!(segment.name in record)) {
562
+ record[segment.name] = {};
563
+ }
564
+ current = record[segment.name];
565
+ } else {
566
+ const arr = current;
567
+ if (!arr[segment.index]) {
568
+ arr[segment.index] = {};
569
+ }
570
+ current = arr[segment.index];
571
+ }
572
+ }
573
+ const lastSegment = segments.at(-1);
574
+ if (!lastSegment) {
575
+ return;
576
+ }
577
+ if (lastSegment.type === "field") {
578
+ if (!isSafeKey(lastSegment.name)) {
579
+ return;
580
+ }
581
+ current[lastSegment.name] = value;
582
+ } else {
583
+ current[lastSegment.index] = value;
584
+ }
585
+ }
586
+
587
+ export { collectFormulaNodes, evaluateFormulas, extractSchemaFormulas, validateFormulaAgainstSchema, validateSchemaFormulas };
588
+ //# sourceMappingURL=chunk-HHS27GGA.js.map
589
+ //# sourceMappingURL=chunk-HHS27GGA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib/extract-schema-formulas.ts","../src/lib/validate-schema-formulas.ts","../src/lib/formula.ts"],"names":["getParentPath","buildDependencyGraph"],"mappings":";;;;AA0BO,SAAS,sBACd,MAAA,EACoB;AACpB,EAAA,MAAM,WAA+B,EAAC;AACtC,EAAA,wBAAA,CAAyB,MAAA,EAAQ,IAAI,QAAQ,CAAA;AAC7C,EAAA,OAAO,QAAA;AACT;AAEA,SAAS,wBAAA,CACP,MAAA,EACA,UAAA,EACA,QAAA,EACM;AACN,EAAA,IAAI,MAAA,CAAO,IAAA,KAAS,OAAA,IAAW,MAAA,CAAO,KAAA,EAAO;AAC3C,IAAA,wBAAA,CAAyB,MAAA,CAAO,KAAA,EAAO,CAAA,EAAG,UAAU,MAAM,QAAQ,CAAA;AAClE,IAAA;AAAA,EACF;AAEA,EAAA,MAAM,UAAA,GAAa,MAAA,CAAO,UAAA,IAAc,EAAC;AAEzC,EAAA,KAAA,MAAW,CAAC,SAAA,EAAW,WAAW,KAAK,MAAA,CAAO,OAAA,CAAQ,UAAU,CAAA,EAAG;AACjE,IAAA,MAAM,WAAW,UAAA,GAAa,CAAA,EAAG,UAAU,CAAA,CAAA,EAAI,SAAS,CAAA,CAAA,GAAK,SAAA;AAE7D,IAAA,MAAM,QAAA,GAAW,YAAY,WAAW,CAAA;AACxC,IAAA,IAAI,QAAA,EAAU;AACZ,MAAA,QAAA,CAAS,IAAA,CAAK;AAAA,QACZ,SAAA,EAAW,QAAA;AAAA,QACX,YAAY,QAAA,CAAS,UAAA;AAAA,QACrB,SAAA,EAAW,YAAY,IAAA,IAAQ;AAAA,OAChC,CAAA;AAAA,IACH;AAEA,IAAA,IAAI,WAAA,CAAY,IAAA,K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AA,CAAQ,SAAS,OAAA,EAAS;AAC5B,MAAA,IAAI,CAAC,SAAA,CAAU,OAAA,CAAQ,IAAI,CAAA,EAAG;AAC5B,QAAA;AAAA,MACF;AACA,MAAA,MAAM,MAAA,GAAS,OAAA;AACf,MAAA,IAAI,EAAE,OAAA,CAAQ,IAAA,IAAQ,MAAA,CAAA,EAAS;AAC7B,QAAA,MAAA,CAAO,OAAA,CAAQ,IAAI,CAAA,GAAI,EAAC;AAAA,MAC1B;AACA,MAAA,OAAA,GAAU,MAAA,CAAO,QAAQ,IAAI,CAAA;AAAA,IAC/B,CAAA,MAAO;AACL,MAAA,MAAM,GAAA,GAAM,OAAA;AACZ,MAAA,IAAI,CAAC,GAAA,CAAI,OAAA,CAAQ,KAAK,CAAA,EAAG;AACvB,QAAA,GAAA,CAAI,OAAA,CAAQ,KAAK,CAAA,GAAI,EAAC;AAAA,MACxB;AACA,MAAA,OAAA,GAAU,GAAA,CAAI,QAAQ,KAAK,CAAA;AAAA,IAC7B;AAAA,EACF;AAEA,EAAA,MAAM,WAAA,GAAc,QAAA,CAAS,EAAA,CAAG,EAAE,CAAA;AAClC,EAAA,IAAI,CAAC,WAAA,EAAa;AAChB,IAAA;AAAA,EACF;AAEA,EAAA,IAAI,WAAA,CAAY,SAAS,OAAA,EAAS;AAChC,IAAA,IAAI,CAAC,SAAA,CAAU,WAAA,CAAY,IAAI,CAAA,EAAG;AAChC,MAAA;AAAA,IACF;AACA,IAAC,OAAA,CAAoC,WAAA,CAAY,IAAI,CAAA,GAAI,KAAA;AAAA,EAC3D,CAAA,MAAO;AACL,IAAC,OAAA,CAAsB,WAAA,CAAY,KAAK,CAAA,GAAI,KAAA;AAAA,EAC9C;AACF","file":"chunk-HHS27GGA.js","sourcesContent":["interface XFormulaInput {\n version: number;\n expression: string;\n}\n\ninterface SchemaProperty {\n type?: string;\n 'x-formula'?: XFormulaInput;\n properties?: Record<string, SchemaProperty>;\n items?: SchemaProperty;\n [key: string]: unknown;\n}\n\ninterface JsonSchemaInput {\n type?: string;\n properties?: Record<string, SchemaProperty>;\n items?: SchemaProperty;\n [key: string]: unknown;\n}\n\nexport interface ExtractedFormula {\n fieldName: string;\n expression: string;\n fieldType: string;\n}\n\nexport function extractSchemaFormulas(\n schema: JsonSchemaInput,\n): ExtractedFormula[] {\n const formulas: ExtractedFormula[] = [];\n extractFormulasRecursive(schema, '', formulas);\n return formulas;\n}\n\nfunction extractFormulasRecursive(\n schema: SchemaProperty | JsonSchemaInput,\n pathPrefix: string,\n formulas: ExtractedFormula[],\n): void {\n if (schema.type === 'array' && schema.items) {\n extractFormulasRecursive(schema.items, `${pathPrefix}[]`, formulas);\n return;\n }\n\n const properties = schema.properties ?? {};\n\n for (const [fieldName, fieldSchema] of Object.entries(properties)) {\n const fullPath = pathPrefix ? `${pathPrefix}.${fieldName}` : fieldName;\n\n const xFormula = fieldSchema['x-formula'];\n if (xFormula) {\n formulas.push({\n fieldName: fullPath,\n expression: xFormula.expression,\n fieldType: fieldSchema.type ?? 'string',\n });\n }\n\n if (fieldSchema.type === 'object' && fieldSchema.properties) {\n extractFormulasRecursive(fieldSchema, fullPath, formulas);\n }\n\n if (fieldSchema.type === 'array' && fieldSchema.items) {\n extractFormulasRecursive(fieldSchema.items, `${fullPath}[]`, formulas);\n }\n }\n}\n","import {\n parseExpression,\n validateFormulaSyntax,\n buildDependencyGraph,\n detectCircularDependencies,\n inferFormulaType,\n type FieldTypes,\n type InferredType,\n} from '@revisium/formula';\nimport { extractSchemaFormulas } from './extract-schema-formulas.js';\n\ninterface SchemaProperty {\n type?: string;\n properties?: Record<string, SchemaProperty>;\n items?: SchemaProperty;\n [key: string]: unknown;\n}\n\ninterface JsonSchemaInput {\n type?: string;\n properties?: Record<string, SchemaProperty>;\n items?: SchemaProperty;\n [key: string]: unknown;\n}\n\nexport interface FormulaValidationError {\n field: string;\n error: string;\n position?: number;\n}\n\nexport interface SchemaValidationResult {\n isValid: boolean;\n errors: FormulaValidationError[];\n}\n\nexport function validateSchemaFormulas(\n schema: JsonSchemaInput,\n): SchemaValidationResult {\n const errors: FormulaValidationError[] = [];\n const formulas = extractSchemaFormulas(schema);\n\n for (const formula of formulas) {\n const error = validateFormulaAgainstSchema(\n formula.expression,\n formula.fieldName,\n schema,\n );\n if (error) {\n errors.push(error);\n }\n }\n\n if (errors.length > 0) {\n return { isValid: false, errors };\n }\n\n const dependencies: Record<string, string[]> = {};\n for (const formula of formulas) {\n const parseResult = parseExpression(formula.expression);\n const parentPath = getParentPath(formula.fieldName);\n const prefix = parentPath ? `${parentPath}.` : '';\n\n dependencies[formula.fieldName] = parseResult.dependencies.map((dep) => {\n if (dep.startsWith('/')) {\n return extractFieldRoot(dep.slice(1));\n }\n if (dep.startsWith('../')) {\n return resolveRelativePathForDependency(dep, parentPath);\n }\n const rootField = extractFieldRoot(dep);\n return `${prefix}${rootField}`;\n });\n }\n\n const graph = buildDependencyGraph(dependencies);\n const circularCheck = detectCircularDependencies(graph);\n\n const cycle = circularCheck.cycle;\n if (circularCheck.hasCircular && cycle && cycle.length > 0) {\n const firstField = cycle[0];\n if (firstField) {\n errors.push({\n field: firstField,\n error: `Circular dependency: ${cycle.join(' → ')}`,\n });\n return { isValid: false, errors };\n }\n }\n\n return { isValid: true, errors: [] };\n}\n\nexport function validateFormulaAgainstSchema(\n expression: string,\n fieldName: string,\n schema: JsonSchemaInput,\n): FormulaValidationError | null {\n return validateFormulaInContext(expression, fieldName, schema);\n}\n\nfunction validateFormulaInContext(\n expression: string,\n fieldPath: string,\n rootSchema: JsonSchemaInput,\n): FormulaValidationError | null {\n const syntaxResult = validateFormulaSyntax(expression);\n if (!syntaxResult.isValid) {\n return {\n field: fieldPath,\n error: syntaxResult.error,\n position: syntaxResult.position,\n };\n }\n\n const parentPath = getParentPath(fieldPath);\n const localFieldName = getFieldName(fieldPath);\n const contextSchema = resolveSubSchema(rootSchema, parentPath);\n\n if (!contextSchema) {\n return {\n field: fieldPath,\n error: `Cannot resolve schema context for path '${parentPath}'`,\n };\n }\n\n const parseResult = parseExpression(expression);\n const localSchemaFields = getSchemaFields(contextSchema);\n const rootSchemaFields = getSchemaFields(rootSchema);\n\n for (const dep of parseResult.dependencies) {\n if (dep.startsWith('/')) {\n const rootField = extractFieldRoot(dep.slice(1));\n if (!rootSchemaFields.has(rootField)) {\n return {\n field: fieldPath,\n error: `Unknown root field '${rootField}' in formula`,\n };\n }\n } else if (dep.startsWith('../')) {\n const validationResult = validateRelativePath(\n dep,\n parentPath,\n rootSchema,\n fieldPath,\n );\n if (validationResult) {\n return validationResult;\n }\n } else {\n const rootField = extractFieldRoot(dep);\n if (!localSchemaFields.has(rootField)) {\n return {\n field: fieldPath,\n error: `Unknown field '${rootField}' in formula`,\n };\n }\n }\n }\n\n if (\n parseResult.dependencies.some((d) => extractFieldRoot(d) === localFieldName)\n ) {\n return {\n field: fieldPath,\n error: `Formula cannot reference itself`,\n };\n }\n\n const fieldSchema = contextSchema.properties?.[localFieldName];\n const expectedType = schemaTypeToInferred(fieldSchema?.type);\n const fieldTypes = getSchemaFieldTypes(contextSchema);\n const inferredType = inferFormulaType(expression, fieldTypes);\n\n if (!isTypeCompatible(inferredType, expectedType)) {\n return {\n field: fieldPath,\n error: `Type mismatch: formula returns '${inferredType}' but field expects '${expectedType}'`,\n };\n }\n\n return null;\n}\n\nfunction resolveSubSchema(\n schema: JsonSchemaInput,\n fieldPath: string,\n): SchemaProperty | null {\n if (!fieldPath) {\n return schema;\n }\n\n const segments = parsePathSegmentsForSchema(fieldPath);\n let current: SchemaProperty | JsonSchemaInput = schema;\n\n for (const segment of segments) {\n if (segment === '[]') {\n if (current.type === 'array' && current.items) {\n current = current.items;\n } else {\n return null;\n }\n } else if (current.properties?.[segment]) {\n current = current.properties[segment];\n } else {\n return null;\n }\n }\n\n return current;\n}\n\nfunction parsePathSegmentsForSchema(path: string): string[] {\n const segments: string[] = [];\n let current = '';\n let inBracket = false;\n\n for (const char of path) {\n if (char === '[') {\n if (current) {\n segments.push(current);\n current = '';\n }\n inBracket = true;\n } else if (char === ']') {\n inBracket = false;\n segments.push('[]');\n } else if (char === '.' && !inBracket) {\n if (current) {\n segments.push(current);\n current = '';\n }\n } else if (!inBracket) {\n current += char;\n }\n }\n\n if (current) {\n segments.push(current);\n }\n\n return segments;\n}\n\nfunction parsePathSegments(path: string): string[] {\n const segments: string[] = [];\n let current = '';\n let inBracket = false;\n\n for (const char of path) {\n if (char === '[') {\n if (current) {\n segments.push(current);\n current = '';\n }\n inBracket = true;\n } else if (char === ']') {\n inBracket = false;\n } else if (char === '.' && !inBracket) {\n if (current) {\n segments.push(current);\n current = '';\n }\n } else if (!inBracket) {\n current += char;\n }\n }\n\n if (current) {\n segments.push(current);\n }\n\n return segments;\n}\n\nfunction getParentPath(fieldPath: string): string {\n const lastDotIndex = fieldPath.lastIndexOf('.');\n const lastBracketIndex = fieldPath.lastIndexOf('[');\n const splitIndex = Math.max(lastDotIndex, lastBracketIndex);\n\n if (splitIndex <= 0) {\n return '';\n }\n\n return fieldPath.substring(0, splitIndex);\n}\n\nfunction getFieldName(fieldPath: string): string {\n const lastDotIndex = fieldPath.lastIndexOf('.');\n const lastBracketIndex = fieldPath.lastIndexOf(']');\n const splitIndex = Math.max(lastDotIndex, lastBracketIndex);\n\n if (splitIndex === -1) {\n return fieldPath;\n }\n\n return fieldPath.substring(splitIndex + 1);\n}\n\nfunction getSchemaFields(schema: SchemaProperty | JsonSchemaInput): Set<string> {\n const fields = new Set<string>();\n const properties = schema.properties ?? {};\n\n for (const fieldName of Object.keys(properties)) {\n fields.add(fieldName);\n }\n\n return fields;\n}\n\nfunction getSchemaFieldTypes(schema: SchemaProperty | JsonSchemaInput): FieldTypes {\n const fieldTypes: FieldTypes = {};\n const properties = schema.properties ?? {};\n\n for (const [fieldName, fieldSchema] of Object.entries(properties)) {\n const schemaType = fieldSchema.type;\n if (schemaType === 'integer') {\n fieldTypes[fieldName] = 'number';\n } else if (\n schemaType === 'number' ||\n schemaType === 'string' ||\n schemaType === 'boolean' ||\n schemaType === 'object' ||\n schemaType === 'array'\n ) {\n fieldTypes[fieldName] = schemaType;\n }\n }\n\n return fieldTypes;\n}\n\nfunction schemaTypeToInferred(\n schemaType: string | undefined,\n): InferredType | null {\n if (schemaType === 'number' || schemaType === 'integer') return 'number';\n if (schemaType === 'string') return 'string';\n if (schemaType === 'boolean') return 'boolean';\n return null;\n}\n\nfunction isTypeCompatible(\n inferredType: InferredType,\n expectedType: InferredType | null,\n): boolean {\n if (expectedType === null) return true;\n if (inferredType === 'unknown') return true;\n return inferredType === expectedType;\n}\n\nfunction extractFieldRoot(dependency: string): string {\n const root = dependency.split('.')[0]?.split('[')[0];\n return root || dependency;\n}\n\nfunction countParentLevels(path: string): number {\n let count = 0;\n let remaining = path;\n while (remaining.startsWith('../')) {\n count++;\n remaining = remaining.slice(3);\n }\n return count;\n}\n\nfunction resolveRelativePathForDependency(\n relativePath: string,\n currentPath: string,\n): string {\n const parentLevels = countParentLevels(relativePath);\n const fieldAfterParents = relativePath.replace(/^(\\.\\.\\/)+/, '');\n const targetField = extractFieldRoot(fieldAfterParents);\n\n if (!currentPath) {\n return targetField;\n }\n\n const pathSegments = parsePathSegments(currentPath);\n const targetLevel = pathSegments.length - parentLevels;\n\n if (targetLevel <= 0) {\n return targetField;\n }\n\n const basePath = pathSegments.slice(0, targetLevel).join('.');\n return basePath ? `${basePath}.${targetField}` : targetField;\n}\n\nfunction validateRelativePath(\n relativePath: string,\n currentPath: string,\n rootSchema: JsonSchemaInput,\n fieldPath: string,\n): FormulaValidationError | null {\n const parentLevels = countParentLevels(relativePath);\n const fieldAfterParents = relativePath.replace(/^(\\.\\.\\/)+/, '');\n const targetField = extractFieldRoot(fieldAfterParents);\n\n const pathSegments = parsePathSegments(currentPath);\n const targetLevel = pathSegments.length - parentLevels;\n\n if (targetLevel <= 0) {\n const rootFields = getSchemaFields(rootSchema);\n if (!rootFields.has(targetField)) {\n return {\n field: fieldPath,\n error: `Unknown root field '${targetField}' in formula`,\n };\n }\n return null;\n }\n\n const targetPath = pathSegments.slice(0, targetLevel).join('.');\n const targetSchema = resolveSubSchema(rootSchema, targetPath);\n\n if (!targetSchema) {\n return {\n field: fieldPath,\n error: `Cannot resolve schema for relative path '${relativePath}'`,\n };\n }\n\n const targetFields = getSchemaFields(targetSchema);\n if (!targetFields.has(targetField)) {\n return {\n field: fieldPath,\n error: `Unknown field '${targetField}' in formula`,\n };\n }\n\n return null;\n}\n","import {\n parseFormula,\n buildDependencyGraph,\n getTopologicalOrder,\n evaluateWithContext,\n type ArrayContext,\n type ArrayLevelContext,\n} from '@revisium/formula';\nimport { type JsonSchema } from '../types/index.js';\n\nexport { formulaSpec } from '@revisium/formula/spec';\nexport {\n extractSchemaFormulas,\n type ExtractedFormula,\n} from './extract-schema-formulas.js';\nexport {\n validateSchemaFormulas,\n validateFormulaAgainstSchema,\n type SchemaValidationResult,\n type FormulaValidationError,\n} from './validate-schema-formulas.js';\n\nexport interface FormulaNode {\n path: string;\n expression: string;\n fieldType: 'number' | 'string' | 'boolean';\n currentPath: string;\n dependencies: string[];\n arrayContext?: ArrayContext;\n}\n\nexport interface FormulaError {\n field: string;\n expression: string;\n error: string;\n defaultUsed: boolean;\n}\n\nexport interface EvaluateFormulasResult {\n values: Record<string, unknown>;\n errors: FormulaError[];\n}\n\nexport interface EvaluateFormulasOptions {\n useDefaults?: boolean;\n defaults?: Record<string, unknown>;\n}\n\ninterface SchemaNode {\n type?: string;\n properties?: Record<string, SchemaNode>;\n items?: SchemaNode;\n 'x-formula'?: { version: number; expression: string };\n}\n\nconst FORMULA_TYPES = new Set(['number', 'string', 'boolean']);\n\ninterface TraversalContext {\n arrayLevels: ArrayLevelContext[];\n}\n\nexport function collectFormulaNodes(\n schema: JsonSchema,\n data: Record<string, unknown>,\n): FormulaNode[] {\n const nodes: FormulaNode[] = [];\n traverseAndCollect(schema as SchemaNode, data, '', nodes, { arrayLevels: [] });\n return nodes;\n}\n\nfunction traverseAndCollect(\n schema: SchemaNode,\n data: unknown,\n currentPath: string,\n nodes: FormulaNode[],\n ctx: TraversalContext,\n): void {\n if (schema.type === 'object' && schema.properties && typeof data === 'object' && data !== null) {\n const record = data as Record<string, unknown>;\n\n for (const [fieldName, fieldSchema] of Object.entries(schema.properties)) {\n const fieldPath = currentPath ? `${currentPath}.${fieldName}` : fieldName;\n const fieldValue = record[fieldName];\n\n if (fieldSchema['x-formula'] && FORMULA_TYPES.has(fieldSchema.type ?? '')) {\n const expression = fieldSchema['x-formula'].expression;\n const parentPath = getParentPath(fieldPath);\n\n nodes.push({\n path: fieldPath,\n expression,\n fieldType: fieldSchema.type as 'number' | 'string' | 'boolean',\n currentPath: parentPath,\n dependencies: parseDependencies(expression),\n arrayContext: ctx.arrayLevels.length > 0 ? { levels: [...ctx.arrayLevels] } : undefined,\n });\n }\n\n traverseAndCollect(fieldSchema, fieldValue, fieldPath, nodes, ctx);\n }\n }\n\n if (schema.type === 'array' && schema.items && Array.isArray(data)) {\n for (let i = 0; i < data.length; i++) {\n const itemPath = `${currentPath}[${i}]`;\n const arrayLevel: ArrayLevelContext = {\n index: i,\n length: data.length,\n prev: i > 0 ? data[i - 1] : null,\n next: i < data.length - 1 ? data[i + 1] : null,\n };\n const newCtx: TraversalContext = {\n arrayLevels: [arrayLevel, ...ctx.arrayLevels],\n };\n traverseAndCollect(schema.items, data[i], itemPath, nodes, newCtx);\n }\n }\n}\n\nfunction parseDependencies(expression: string): string[] {\n try {\n return parseFormula(expression).dependencies;\n } catch {\n return [];\n }\n}\n\nfunction getParentPath(fieldPath: string): string {\n const lastDotIndex = fieldPath.lastIndexOf('.');\n if (lastDotIndex === -1) {\n return '';\n }\n return fieldPath.substring(0, lastDotIndex);\n}\n\nexport function evaluateFormulas(\n schema: JsonSchema,\n data: Record<string, unknown>,\n options: EvaluateFormulasOptions = {},\n): EvaluateFormulasResult {\n const nodes = collectFormulaNodes(schema, data);\n\n if (nodes.length === 0) {\n return { values: {}, errors: [] };\n }\n\n const sortedNodes = orderByDependencies(nodes);\n const values: Record<string, unknown> = {};\n const errors: FormulaError[] = [];\n const failedPaths = new Set<string>();\n\n for (const node of sortedNodes) {\n const hasDependencyFailure = hasFailedDependency(node, failedPaths);\n\n if (hasDependencyFailure) {\n failedPaths.add(node.path);\n handleError(node, 'Dependency formula failed', data, values, errors, options);\n continue;\n }\n\n const result = evaluateNode(node, data);\n\n if (!result.success) {\n failedPaths.add(node.path);\n handleError(node, result.error, data, values, errors, options);\n continue;\n }\n\n setValueByPath(data, node.path, result.value);\n values[node.path] = result.value;\n }\n\n return { values, errors };\n}\n\nfunction orderByDependencies(nodes: FormulaNode[]): FormulaNode[] {\n if (nodes.length <= 1) {\n return nodes;\n }\n\n const dependencies = Object.fromEntries(\n nodes.map((n) => [n.path, n.dependencies]),\n );\n\n const result = getTopologicalOrder(buildDependencyGraph(dependencies));\n\n if (!result.success) {\n throw new Error(\n `Cyclic dependency detected in formulas: ${result.error ?? 'unknown error'}`,\n );\n }\n\n const nodeMap = new Map(nodes.map((n) => [n.path, n]));\n\n return result.order\n .map((path) => nodeMap.get(path))\n .filter((n): n is FormulaNode => n !== undefined);\n}\n\nfunction hasFailedDependency(node: FormulaNode, failedPaths: Set<string>): boolean {\n return node.dependencies.some((dep) => {\n for (const failedPath of failedPaths) {\n if (failedPath === dep || failedPath.endsWith(`.${dep}`)) {\n return true;\n }\n }\n return false;\n });\n}\n\nfunction evaluateNode(\n node: FormulaNode,\n data: Record<string, unknown>,\n): { success: true; value: unknown } | { success: false; error: string } {\n try {\n const itemData = node.currentPath\n ? getValueByPath(data, node.currentPath) as Record<string, unknown> | undefined\n : undefined;\n\n const result = evaluateWithContext(node.expression, {\n rootData: data,\n ...(itemData && { itemData, currentPath: node.currentPath }),\n arrayContext: node.arrayContext,\n });\n\n if (result === undefined) {\n return { success: false, error: 'Formula returned undefined' };\n }\n\n return { success: true, value: result };\n } catch (error) {\n return {\n success: false,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n}\n\nfunction handleError(\n node: FormulaNode,\n errorMessage: string,\n data: Record<string, unknown>,\n values: Record<string, unknown>,\n errors: FormulaError[],\n options: EvaluateFormulasOptions,\n): void {\n const defaultUsed = options.useDefaults ?? false;\n\n if (defaultUsed) {\n const defaultValue = getDefaultValue(node, options.defaults);\n setValueByPath(data, node.path, defaultValue);\n values[node.path] = defaultValue;\n }\n\n errors.push({\n field: node.path,\n expression: node.expression,\n error: errorMessage,\n defaultUsed,\n });\n}\n\nfunction getDefaultValue(\n node: FormulaNode,\n defaults?: Record<string, unknown>,\n): unknown {\n if (defaults && node.path in defaults) {\n return defaults[node.path];\n }\n\n switch (node.fieldType) {\n case 'number':\n return 0;\n case 'string':\n return '';\n case 'boolean':\n return false;\n }\n}\n\nfunction getValueByPath(\n obj: Record<string, unknown>,\n path: string,\n): unknown {\n const segments = parsePath(path);\n let current: unknown = obj;\n\n for (const segment of segments) {\n if (current === null || current === undefined) {\n return undefined;\n }\n\n if (segment.type === 'field') {\n current = (current as Record<string, unknown>)[segment.name];\n } else {\n if (!Array.isArray(current)) {\n return undefined;\n }\n current = current[segment.index];\n }\n }\n\n return current;\n}\n\ntype PathSegment = { type: 'field'; name: string } | { type: 'index'; index: number };\n\nfunction parsePath(path: string): PathSegment[] {\n const segments: PathSegment[] = [];\n let current = '';\n let position = 0;\n\n while (position < path.length) {\n const char = path[position];\n\n if (char === '.') {\n if (current) {\n segments.push({ type: 'field', name: current });\n current = '';\n }\n position++;\n } else if (char === '[') {\n if (current) {\n segments.push({ type: 'field', name: current });\n current = '';\n }\n const endBracket = path.indexOf(']', position);\n if (endBracket === -1) {\n position++;\n } else {\n const indexStr = path.slice(position + 1, endBracket);\n segments.push({ type: 'index', index: Number.parseInt(indexStr, 10) });\n position = endBracket + 1;\n }\n } else {\n current += char;\n position++;\n }\n }\n\n if (current) {\n segments.push({ type: 'field', name: current });\n }\n\n return segments;\n}\n\nfunction isSafeKey(key: string): boolean {\n return key !== '__proto__';\n}\n\nfunction setValueByPath(\n obj: Record<string, unknown>,\n path: string,\n value: unknown,\n): void {\n const segments = parsePath(path);\n let current: unknown = obj;\n\n for (let i = 0; i < segments.length - 1; i++) {\n const segment = segments[i]!;\n\n if (segment.type === 'field') {\n if (!isSafeKey(segment.name)) {\n return;\n }\n const record = current as Record<string, unknown>;\n if (!(segment.name in record)) {\n record[segment.name] = {};\n }\n current = record[segment.name];\n } else {\n const arr = current as unknown[];\n if (!arr[segment.index]) {\n arr[segment.index] = {};\n }\n current = arr[segment.index];\n }\n }\n\n const lastSegment = segments.at(-1);\n if (!lastSegment) {\n return;\n }\n\n if (lastSegment.type === 'field') {\n if (!isSafeKey(lastSegment.name)) {\n return;\n }\n (current as Record<string, unknown>)[lastSegment.name] = value;\n } else {\n (current as unknown[])[lastSegment.index] = value;\n }\n}\n"]}